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, {
115 model: MailAttachment
119 .then(function (mailRoom) {
121 return res.sendStatus(404);
124 return res.send(mailRoom);
126 .catch(function (err) {
131 // Creates a new mailRoom in the DB.
132 exports.create = function (req, res, next) {
135 .then(function (mailRoom) {
136 return res.status(201).send(mailRoom);
138 .catch(function (err) {
139 return handleError(res, err);
143 // Updates an existing mailRoom in the DB.
144 exports.update = function (req, res, next) {
146 .findById(req.params.id)
147 .then(function (mailRoom) {
149 return res.sendStatus(404);
151 // CHECK IF MAIL IS ALREADY ASSIGNED
152 if (mailRoom.jobId) {
153 throw new Error('Mail already assigned');
155 var updated = _.merge(mailRoom, req.body);
160 .then(function (mailRoom) {
161 return res.status(200).send(mailRoom);
163 .catch(function (err) {
164 return handleError(res, err);
168 // Deletes a mailRoom from the DB.
169 exports.destroy = function (req, res, next) {
171 .findById(req.params.id)
172 .then(function (mailRoom) {
174 return res.sendStatus(404);
176 return mailRoom.destroy();
179 return res.sendStatus(204);
181 .catch(function (err) {
182 return handleError(res, 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);