3 var _ = require('lodash');
4 var util = require('util');
6 var FaxBusinessAutomation = require('../../models').FaxBusinessAutomation;
7 var BusinessCondition = require('../../models').BusinessCondition;
8 var BusinessAction = require('../../models').BusinessAction;
9 var sequelize = require('../../models').sequelize;
11 // Get list of fax_business_automations
12 exports.index = function(req, res, next) {
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;
21 offset: page * per_page
24 _.forIn(req.query, function(value, key) {
30 query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
36 attributes.forEach(function(attribute) {
39 $like: '%' + value + '%'
42 query.where.$or.push(tmp);
49 query.where[key].$like = '%' + value + '%';
54 .findAndCountAll(query)
55 .then(function(result) {
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;
61 res.status(200).send({
65 previous_page: previous_page,
66 total_pages: total_pages
70 .catch(function(err) {
71 res.status(500).send({
72 error: 'Something blew up!'
77 // Get a single fax_business_automation
78 exports.show = function(req, res) {
80 .findById(req.params.id, {
85 .then(function(fax_business_automation) {
86 if (!fax_business_automation) {
87 return res.sendStatus(404);
89 return res.send(fax_business_automation);
91 .catch(function(err) {
92 return handleError(res, err);
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
103 .then(function(faxBusinessAutomation) {
106 req.body.or.forEach(function(elm) {
107 elm.FaxBusinessAutomationId = faxBusinessAutomation.id;
109 conditions = conditions.concat(req.body.or);
113 req.body.and.forEach(function(elm) {
114 elm.FaxBusinessAutomationId = faxBusinessAutomation.id;
116 conditions = conditions.concat(req.body.and);
119 return BusinessCondition
120 .bulkCreate(conditions, {
123 .then(function(businessCondtions) {
124 if (req.body.actions) {
125 req.body.actions.forEach(function(elm) {
126 elm.FaxBusinessAutomationId = faxBusinessAutomation.id;
130 return BusinessAction
131 .bulkCreate(req.body.actions, {
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
148 // Updates an existing fax_business_automation in the DB.
149 exports.update = function(req, res) {
153 FaxBusinessAutomation
159 .then(function(fax_business_automation) {
160 if (!fax_business_automation) {
161 return res.sendStatus(404);
163 var updated = _.merge(fax_business_automation, req.body);
166 return res.status(200).send(fax_business_automation);
168 .catch(function(err) {
169 return handleError(res, err);
172 .catch(function(err) {
173 return handleError(res, err);
177 // Deletes a fax_business_automation from the DB.
178 exports.destroy = function(req, res) {
179 FaxBusinessAutomation
185 .then(function(fax_business_automation) {
186 if (!fax_business_automation) {
187 return res.sendStatus(404);
189 fax_business_automation.destroy()
191 return res.sendStatus(204);
193 .catch(function(err) {
194 return handleError(res, err);
197 .catch(function(err) {
198 return handleError(res, err);
202 function handleError(res, err) {
203 return res.status(500).send(err);