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) {
15 var attributes = ['subject', 'from', 'status'];
16 var per_page = req.query.per_page ? parseInt(req.query.per_page, 10) : 100;
17 var page = req.query.page ? parseInt(req.query.page, 10) : 0;
29 attributes: ['id', 'name', 'fullname', 'email', 'role', 'internal']
34 offset: page * per_page
37 if (req.user.role === 'agent') {
38 query.include[0].where = {
43 _.forIn(req.query, function (value, key) {
49 query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
55 attributes.forEach(function (attribute) {
58 $like: '%' + value + '%'
61 query.where.$or.push(tmp);
68 query.where[key].$like = '%' + value + '%';
72 console.log('query', query);
76 .findAndCountAll(query)
77 .then(function (result) {
79 var total_pages = Math.ceil(result.count / per_page);
80 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;
81 var previous_page = page > 0 ? util.format('%s://%s%s?page=%d', req.protocol, req.headers.host, req.baseUrl, page - 1) : null;
83 res.status(200).send({
85 mailRooms: result.rows,
87 previous_page: previous_page,
88 total_pages: total_pages
92 .catch(function (err) {
93 return handleError(res, err);
97 // Get list of agent mailRooms
98 exports.agentIndex = function (req, res, next) {
102 UserId: req.params.id
108 .then(function (mailRooms) {
109 return res.status(200).send(mailRooms);
111 .catch(function (err) {
112 return handleError(res, err);
116 // Get a single mailRoom
117 exports.show = function (req, res, next) {
119 .findById(req.params.id, {
123 model: MailAttachment
129 .then(function (mailRoom) {
131 return res.sendStatus(404);
134 return res.send(mailRoom);
136 .catch(function (err) {
137 return handleError(res, err);
141 // Get a single mailRoom
142 exports.showAgents = function (req, res, next) {
144 .findById(req.params.id)
145 .then(function (mailRoom) {
147 return res.sendStatus(404);
152 attributes: ['id', 'name', 'fullname', 'email', 'role', 'internal']
155 .then(function (users) {
156 return res.send(users);
158 .catch(function (err) {
159 return handleError(res, err);
163 // Creates a new mailRoom in the DB.
164 exports.create = function (req, res, next) {
167 .then(function (mailRoom) {
168 return res.status(201).send(mailRoom);
170 .catch(function (err) {
171 return handleError(res, err);
175 // Updates an existing mailRoom in the DB.
176 exports.update = function (req, res, next) {
178 .findById(req.params.id)
179 .then(function (mailRoom) {
181 return res.sendStatus(404);
183 // CHECK IF MAIL IS ALREADY ASSIGNED
184 if (mailRoom.jobId) {
185 throw new Error('Mail already assigned');
187 var updated = _.merge(mailRoom, req.body);
192 .then(function (mailRoom) {
193 return res.status(200).send(mailRoom);
195 .catch(function (err) {
196 return handleError(res, err);
200 // Get a single mailRoom
201 exports.updateAgents = function (req, res, next) {
203 .findById(req.params.id)
204 .then(function (mailRoom) {
206 return res.sendStatus(404);
210 .setUsers(req.body.agents);
213 return res.sendStatus(200);
215 .catch(function (err) {
216 return handleError(res, err);
220 // Deletes a mailRoom from the DB.
221 exports.destroy = function (req, res, next) {
223 .findById(req.params.id)
224 .then(function (mailRoom) {
226 return res.sendStatus(404);
228 return mailRoom.destroy();
231 return res.sendStatus(204);
233 .catch(function (err) {
234 return handleError(res, err);
238 // Deletes a agent from the DB.
239 exports.bulkDestroy = function (req, res) {
245 individualHooks: true
248 return res.sendStatus(200);
250 .catch(function (err) {
251 return handleError(res, err);
255 function handleError(res, err) {
256 return res.status(500).send(err);