c82e68dddbb31af82d6fbc6fdd240c5d14c5feb6
[motion.git] / server / api / mail_room / mail_room.controller.js
1 'use strict';
2
3 var _ = require('lodash');
4 var util = require('util');
5
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;
12
13 // Get list of mailRooms
14 exports.index = function (req, res, next) {
15
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;
19
20   var query = {
21     where: {
22       status: {
23         $or: ['NEW', 'OPEN']
24       }
25     },
26     limit: per_page,
27     offset: page * per_page,
28     include: [{
29       all: true
30     }]
31   };
32
33   _.forIn(req.query, function (value, key) {
34     switch (key) {
35     case 'per_page':
36     case 'page':
37       break;
38     case 'sort_by':
39       query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
40       break;
41     case 'sort_order':
42       break;
43     case '$':
44       query.where.$or = [];
45       attributes.forEach(function (attribute) {
46         var tmp = {};
47         tmp[attribute] = {
48           $like: '%' + value + '%'
49         };
50
51         query.where.$or.push(tmp);
52       });
53       break;
54     default:
55       query.where[key] = {
56         $like: {}
57       };
58       query.where[key].$like = '%' + value + '%';
59     }
60   });
61
62
63   MailRoom
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;
69
70       res.status(200).send({
71         count: result.count,
72         mailRooms: result.rows,
73         next_page: next_page,
74         previous_page: previous_page,
75         total_pages: total_pages
76       });
77
78     })
79     .catch(function (err) {
80       return next(err);
81     });
82 };
83
84 // Get list of agent mailRooms
85 exports.agentIndex = function (req, res, next) {
86   MailRoom
87     .findAll({
88       where: {
89         UserId: req.params.id
90       },
91       include: [{
92         all: true
93       }]
94     })
95     .then(function (mailRooms) {
96       return res.status(200).send(mailRooms);
97     })
98     .catch(function (err) {
99       return next(err);
100     });
101 };
102
103 // Get a single mailRoom
104 exports.show = function (req, res, next) {
105   return MailRoom
106     .findById(req.params.id, {
107       include: [{
108         model: MailMessage,
109         include: [{
110           model: MailRoom,
111           include: [{
112             model: MailAccount
113           }]
114         }, {
115           model: MailAttachment
116         }]
117       }]
118     })
119     .then(function (mailRoom) {
120       if (!mailRoom) {
121         return res.sendStatus(404);
122       }
123
124       return res.send(mailRoom);
125     })
126     .catch(function (err) {
127       return next(err);
128     });
129 };
130
131 // Creates a new mailRoom in the DB.
132 exports.create = function (req, res, next) {
133   return MailRoom
134     .create(req.body)
135     .then(function (mailRoom) {
136       return res.status(201).send(mailRoom);
137     })
138     .catch(function (err) {
139       return handleError(res, err);
140     });
141 };
142
143 // Updates an existing mailRoom in the DB.
144 exports.update = function (req, res, next) {
145   return MailRoom
146     .findById(req.params.id)
147     .then(function (mailRoom) {
148       if (!mailRoom) {
149         return res.sendStatus(404);
150       }
151       // CHECK IF MAIL IS ALREADY ASSIGNED
152       if (mailRoom.jobId) {
153         throw new Error('Mail already assigned');
154       }
155       var updated = _.merge(mailRoom, req.body);
156
157       return updated
158         .save();
159     })
160     .then(function (mailRoom) {
161       return res.status(200).send(mailRoom);
162     })
163     .catch(function (err) {
164       return handleError(res, err);
165     });
166 };
167
168 // Deletes a mailRoom from the DB.
169 exports.destroy = function (req, res, next) {
170   return MailRoom
171     .findById(req.params.id)
172     .then(function (mailRoom) {
173       if (!mailRoom) {
174         return res.sendStatus(404);
175       }
176       return mailRoom.destroy();
177     })
178     .then(function () {
179       return res.sendStatus(204);
180     })
181     .catch(function (err) {
182       return handleError(res, err);
183     });
184 };
185
186 // Deletes a agent from the DB.
187 exports.bulkDestroy = function (req, res) {
188   return MailRoom
189     .destroy({
190       where: {
191         id: req.query.id
192       },
193       individualHooks: true
194     })
195     .then(function () {
196       return res.sendStatus(200);
197     })
198     .catch(function (err) {
199       return handleError(res, err);
200     });
201 };
202
203 function handleError(res, err) {
204   return res.status(500).send(err);
205 }