3 var _ = require('lodash');
4 var util = require('util');
6 var User = require('../../models').User;
7 var Contact = require('../../models').Contact;
8 var MailRoom = require('../../models').MailRoom;
9 var MailMessage = require('../../models').MailMessage;
10 var MailAccount = require('../../models').MailAccount;
11 var MailAttachment = require('../../models').MailAttachment;
13 // Get list of mailRooms
14 exports.index = function(req, res, next) {
16 var attributes = ['subject', 'from', 'status'];
17 var per_page = req.query.per_page ? parseInt(req.query.per_page, 10) : 100;
18 var page = req.query.page ? parseInt(req.query.page, 10) : 0;
27 offset: page * per_page,
33 _.forIn(req.query, function(value, key) {
39 query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
45 attributes.forEach(function(attribute) {
48 $like: '%' + value + '%'
51 query.where.$or.push(tmp);
58 query.where[key].$like = '%' + value + '%';
64 .findAndCountAll(query)
65 .then(function(result) {
66 var total_pages = Math.ceil(result.count / per_page);
67 var next_page = total_pages > (query.offset + 1) ? util.format('%s://%s%s?page=%d', req.protocol, req.headers.host, req.baseUrl, page + 1) : null;
68 var previous_page = page > 0 ? util.format('%s://%s%s?page=%d', req.protocol, req.headers.host, req.baseUrl, page - 1) : null;
70 res.status(200).send({
72 mailRooms: result.rows,
74 previous_page: previous_page,
75 total_pages: total_pages
79 .catch(function(err) {
84 // Get list of agent mailRooms
85 exports.agentIndex = function(req, res, next) {
95 .then(function(mailRooms) {
96 return res.status(200).send(mailRooms);
98 .catch(function(err) {
103 // Get a single mailRoom
104 exports.show = function(req, res, next) {
106 .findById(req.params.id, {
116 .then(function(mailRoom) {
118 return res.sendStatus(404);
121 return res.send(mailRoom);
123 .catch(function(err) {
128 // Creates a new mailRoom in the DB.
129 exports.create = function(req, res, next) {
132 .then(function(mailRoom) {
133 return res.status(201).send(mailRoom);
135 .catch(function(err) {
140 // Updates an existing mailRoom in the DB.
141 exports.update = function(req, res, next) {
143 .findById(req.params.id)
144 .then(function(mailRoom) {
146 return res.sendStatus(404);
148 // CHECK IF MAIL IS ALREADY ASSIGNED
149 if (mailRoom.jobId) {
150 return next(new Error('Mail already assigned'));
152 var updated = _.merge(mailRoom, req.body);
156 return res.status(200).send(mailRoom);
158 .catch(function(err) {
162 .catch(function(err) {
167 // Deletes a mailRoom from the DB.
168 exports.destroy = function(req, res, next) {
170 .findById(req.params.id)
171 .then(function(mailRoom) {
173 return res.sendStatus(404);
177 return res.sendStatus(204);
179 .catch(function(err) {
183 .catch(function(err) {
188 // Deletes a agent from the DB.
189 exports.bulkDestroy = function(req, res) {
195 individualHooks: true
198 return res.sendStatus(200);
200 .catch(function(err) {
201 return handleError(res, err);
205 function handleError(res, err) {
206 return res.status(500).send(err);