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;
23 offset: page * per_page,
29 _.forIn(req.query, function(value, key) {
35 query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
41 attributes.forEach(function(attribute) {
44 $like: '%' + value + '%'
47 query.where.$or.push(tmp);
54 query.where[key].$like = '%' + value + '%';
60 .findAndCountAll(query)
61 .then(function(result) {
62 var total_pages = Math.ceil(result.count / per_page);
63 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;
64 var previous_page = page > 0 ? util.format('%s://%s%s?page=%d', req.protocol, req.headers.host, req.baseUrl, page - 1) : null;
66 res.status(200).send({
68 mailRooms: result.rows,
70 previous_page: previous_page,
71 total_pages: total_pages
75 .catch(function(err) {
80 // Get list of agent mailRooms
81 exports.agentIndex = function(req, res, next) {
91 .then(function(mailRooms) {
92 return res.status(200).send(mailRooms);
94 .catch(function(err) {
99 // Get a single mailRoom
100 exports.show = function(req, res, next) {
102 .findById(req.params.id, {
112 .then(function(mailRoom) {
114 return res.sendStatus(404);
117 return res.send(mailRoom);
119 .catch(function(err) {
124 // Creates a new mailRoom in the DB.
125 exports.create = function(req, res, next) {
128 .then(function(mailRoom) {
129 return res.status(201).send(mailRoom);
131 .catch(function(err) {
136 // Updates an existing mailRoom in the DB.
137 exports.update = function(req, res, next) {
139 .findById(req.params.id)
140 .then(function(mailRoom) {
142 return res.sendStatus(404);
145 // CHECK IF MAIL IS ALREADY ASSIGNED
146 if (mailRoom.jobId) {
147 return next(new Error('Mail already assigned'));
150 var updated = _.merge(mailRoom, req.body);
154 return res.status(200).send(mailRoom);
156 .catch(function(err) {
160 .catch(function(err) {
165 // Deletes a mailRoom from the DB.
166 exports.destroy = function(req, res, next) {
168 .findById(req.params.id)
169 .then(function(mailRoom) {
171 return res.sendStatus(404);
175 return res.sendStatus(204);
177 .catch(function(err) {
181 .catch(function(err) {
186 // Deletes a agent from the DB.
187 exports.bulkDestroy = function(req, res) {
193 individualHooks: true
196 return res.sendStatus(200);
198 .catch(function(err) {
199 return handleError(res, err);
203 function handleError(res, err) {
204 return res.status(500).send(err);