2f7a2893cfed8651151081ddc8678a1149f205df
[motion.git] / server / api / fax_business_automation / fax_business_automation.controller.js
1 'use strict';
2
3 var _ = require('lodash');
4 var util = require('util');
5
6 var FaxBusinessAutomation = require('../../models').FaxBusinessAutomation;
7 var BusinessCondition = require('../../models').BusinessCondition;
8 var BusinessAction = require('../../models').BusinessAction;
9 var sequelize = require('../../models').sequelize;
10
11 // Get list of fax_business_automations
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   FaxBusinessAutomation
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         rows: 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
77 // Get a single fax_business_automation
78 exports.show = function(req, res) {
79   FaxBusinessAutomation
80     .findById(req.params.id, {
81       include: [{
82         all: true
83       }]
84     })
85     .then(function(fax_business_automation) {
86       if (!fax_business_automation) {
87         return res.sendStatus(404);
88       }
89       return res.send(fax_business_automation);
90     })
91     .catch(function(err) {
92       return handleError(res, err);
93     });
94 };
95
96 // Creates a new fax_business_automation in the DB.
97 exports.create = function(req, res, next) {
98   return sequelize.transaction(function(t) {
99       return FaxBusinessAutomation
100         .create(req.body, {
101           transaction: t
102         })
103         .then(function(faxBusinessAutomation) {
104           var conditions = [];
105           if (req.body.or) {
106             req.body.or.forEach(function(elm) {
107               elm.FaxBusinessAutomationId = faxBusinessAutomation.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.FaxBusinessAutomationId = faxBusinessAutomation.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.FaxBusinessAutomationId = faxBusinessAutomation.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 fax_business_automation in the DB.
149 exports.update = function(req, res) {
150   if (req.body.id) {
151     delete req.body.id;
152   }
153   FaxBusinessAutomation
154     .find({
155       where: {
156         id: req.params.id
157       }
158     })
159     .then(function(fax_business_automation) {
160       if (!fax_business_automation) {
161         return res.sendStatus(404);
162       }
163       var updated = _.merge(fax_business_automation, req.body);
164       updated.save()
165         .then(function() {
166           return res.status(200).send(fax_business_automation);
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 fax_business_automation from the DB.
178 exports.destroy = function(req, res) {
179   FaxBusinessAutomation
180     .find({
181       where: {
182         id: req.params.id
183       }
184     })
185     .then(function(fax_business_automation) {
186       if (!fax_business_automation) {
187         return res.sendStatus(404);
188       }
189       fax_business_automation.destroy()
190         .then(function() {
191           return res.sendStatus(204);
192         })
193         .catch(function(err) {
194           return handleError(res, err);
195         });
196     })
197     .catch(function(err) {
198       return handleError(res, err);
199     });
200 };
201
202 function handleError(res, err) {
203   return res.status(500).send(err);
204 }