5c49502615aa2181dc54eb3ce26b5eb16cc3dc38
[motion.git] / server / api / mail_business_automation / mail_business_automation.controller.js
1 'use strict';
2
3 var _ = require('lodash');
4 var util = require('util');
5
6 var MailBusinessAutomation = require('../../models').MailBusinessAutomation;
7 var BusinessCondition = require('../../models').BusinessCondition;
8 var BusinessAction = require('../../models').BusinessAction;
9 var sequelize = require('../../models').sequelize;
10
11 // Get list of agents
12 exports.index = function(req, res, next) {
13
14   var attributes = ['name', 'description'];
15   var per_page = req.query.per_page ? parseInt(req.query.per_page, 10) : 100;
16   var page = req.query.page ? parseInt(req.query.page, 10) : 0;
17
18   var query = {
19     where: {},
20     limit: per_page,
21     offset: page * per_page
22   };
23
24   _.forIn(req.query, function(value, key) {
25     switch (key) {
26       case 'per_page':
27       case 'page':
28         break;
29       case 'sort_by':
30         query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
31         break;
32       case 'sort_order':
33         break;
34       case '$':
35         query.where.$or = [];
36         attributes.forEach(function(attribute) {
37           var tmp = {};
38           tmp[attribute] = {
39             $like: '%' + value + '%'
40           };
41
42           query.where.$or.push(tmp);
43         });
44         break;
45       default:
46         query.where[key] = {
47           $like: {}
48         };
49         query.where[key].$like = '%' + value + '%';
50     }
51   });
52
53   MailBusinessAutomation
54     .findAndCountAll(query)
55     .then(function(result) {
56
57       var total_pages = Math.ceil(result.count / per_page);
58       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;
59       var previous_page = page > 0 ? util.format('%s://%s%s?page=%d', req.protocol, req.headers.host, req.baseUrl, page - 1) : null;
60
61       res.status(200).send({
62         count: result.count,
63         mailAutomations: result.rows,
64         next_page: next_page,
65         previous_page: previous_page,
66         total_pages: total_pages
67       });
68
69     })
70     .catch(function(err) {
71       res.status(500).send({
72         error: 'Something blew up!'
73       });
74     });
75 };
76 // Get a single mailBusinessAutomation
77 exports.show = function(req, res) {
78   MailBusinessAutomation
79     .findById(req.params.id, {
80       include: [{
81         all: true
82       }]
83     })
84     .then(function(mailBusinessAutomation) {
85       if (!mailBusinessAutomation) {
86         return res.sendStatus(404);
87       }
88       return res.send(mailBusinessAutomation);
89     })
90     .catch(function(err) {
91       return handleError(res, err);
92     });
93 };
94
95 // Creates a new mailBusinessAutomation in the DB.
96 exports.create = function(req, res, next) {
97
98   return sequelize.transaction(function(t) {
99       return MailBusinessAutomation
100         .create(req.body, {
101           transaction: t
102         })
103         .then(function(mailBusinessAutomation) {
104           var conditions = [];
105           if (req.body.or) {
106             req.body.or.forEach(function(elm) {
107               elm.MailBusinessAutomationId = mailBusinessAutomation.id;
108             });
109             conditions = conditions.concat(req.body.or);
110           }
111
112           if (req.body.and) {
113             req.body.and.forEach(function(elm) {
114               elm.MailBusinessAutomationId = mailBusinessAutomation.id;
115             });
116             conditions = conditions.concat(req.body.and);
117           }
118
119           return BusinessCondition
120             .bulkCreate(conditions, {
121               transaction: t
122             })
123             .then(function(businessCondtions) {
124               if (req.body.actions) {
125                 req.body.actions.forEach(function(elm) {
126                   elm.MailBusinessAutomationId = mailBusinessAutomation.id;
127                 });
128               }
129
130               return BusinessAction
131                 .bulkCreate(req.body.actions, {
132                   transaction: t
133                 });
134             });
135         })
136     })
137     .then(function() {
138       // Transaction has been committed
139       // result is whatever the result of the promise chain returned to the transaction callback
140       return res.sendStatus(201);
141     }).catch(function(err) {
142       // Transaction has been rolled back
143       // err is whatever rejected the promise chain returned to the transaction callback
144       return next(err);
145     });
146 };
147
148 // Updates an existing mailBusinessAutomation in the DB.
149 exports.update = function(req, res) {
150   if (req.body.id) {
151     delete req.body.id;
152   }
153   MailBusinessAutomation
154     .find({
155       where: {
156         id: req.params.id
157       }
158     })
159     .then(function(mailBusinessAutomation) {
160       if (!mailBusinessAutomation) {
161         return res.sendStatus(404);
162       }
163       var updated = _.merge(mailBusinessAutomation, req.body);
164       updated.save()
165         .then(function() {
166           return res.status(200).send(mailBusinessAutomation);
167         })
168         .catch(function(err) {
169           return handleError(res, err);
170         });
171     })
172     .catch(function(err) {
173       return handleError(res, err);
174     });
175 };
176
177 // Deletes a mailBusinessAutomation from the DB.
178 exports.destroy = function(req, res) {
179   MailBusinessAutomation
180     .findById(req.params.id)
181     .then(function(mailBusinessAutomation) {
182       if (!mailBusinessAutomation) {
183         return res.sendStatus(404);
184       }
185       mailBusinessAutomation
186         .destroy()
187         .then(function() {
188           return res.sendStatus(204);
189         })
190         .catch(function(err) {
191           return handleError(res, err);
192         });
193     })
194     .catch(function(err) {
195       return handleError(res, err);
196     });
197 };
198
199 // Deletes a agent from the DB.
200 exports.bulkDestroy = function(req, res) {
201   MailBusinessAutomation
202     .destroy({
203       where: {
204         id: req.query.id
205       },
206       individualHooks: true
207     })
208     .then(function() {
209       return res.sendStatus(204);
210     })
211     .catch(function(err) {
212       return handleError(res, err);
213     });
214 };
215
216 function handleError(res, err) {
217   return res.status(500).send(err);
218 }