Built motion from commit 5e31ea4.|0.0.32
[motion.git] / server / api / automation / automation.controller.js
1 'use strict';
2
3 var _ = require('lodash');
4 var util = require('util');
5
6 var Automation = require('../../models').Automation;
7
8 // Get list of agents
9 exports.index = function(req, res, next) {
10
11   var attributes = ['fullname', 'name', 'email'];
12   var per_page = req.query.per_page ? parseInt(req.query.per_page, 10) : 100;
13   var page = req.query.page ? parseInt(req.query.page, 10) : 0;
14
15   var query = {
16     where: {},
17     limit: per_page,
18     offset: page * per_page
19   };
20
21   _.forIn(req.query, function(value, key) {
22     switch (key) {
23       case 'per_page':
24       case 'page':
25         break;
26       case 'sort_by':
27         query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
28         break;
29       case 'sort_order':
30         break;
31       case '$':
32         query.where.$or = [];
33         attributes.forEach(function(attribute) {
34           var tmp = {};
35           tmp[attribute] = {
36             $like: '%' + value + '%'
37           };
38
39           query.where.$or.push(tmp);
40         });
41         break;
42       default:
43         query.where[key] = {
44           $like: {}
45         };
46         query.where[key].$like = '%' + value + '%';
47     }
48   });
49
50   Automation
51     .findAndCountAll(query)
52     .then(function(result) {
53
54       var total_pages = Math.ceil(result.count / per_page);
55       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;
56       var previous_page = page > 0 ? util.format('%s://%s%s?page=%d', req.protocol, req.headers.host, req.baseUrl, page - 1) : null;
57
58       res.status(200).send({
59         count: result.count,
60         rows: result.rows,
61         next_page: next_page,
62         previous_page: previous_page,
63         total_pages: total_pages
64       });
65
66     })
67     .catch(function(err) {
68       res.status(500).send({
69         error: 'Something blew up!'
70       });
71     });
72 };
73
74 // Get a single Automation
75 exports.show = function(req, res) {
76   Automation
77     .findById(req.params.id)
78     .then(function(Automation) {
79       if (!Automation) {
80         return res.sendStatus(404);
81       }
82       return res.send(Automation);
83     })
84     .catch(function(err) {
85       return handleError(res, err);
86     });
87 };
88
89 // Creates a new Automation in the DB.
90 exports.create = function(req, res, next) {
91   Automation
92     .create(req.body)
93     .then(function() {
94       return res.sendStatus(201);
95     })
96     .catch(function(err) {
97       return handleError(res, err);
98     });
99 };
100
101 // Updates an existing Automation in the DB.
102 exports.update = function(req, res, next) {
103   if (req.body.id) {
104     delete req.body.id;
105   }
106   Automation
107     .findById(req.params.id)
108     .then(function(Automation) {
109       if (!Automation) {
110         return res.sendStatus(404);
111       }
112       var updated = _.merge(Automation, req.body);
113       updated
114         .save()
115         .then(function() {
116           return res.status(200).send(Automation);
117         })
118         .catch(function(err) {
119           // return handleError(res, err);
120           return next(err);
121
122         });
123     })
124     .catch(function(err) {
125       return next(err);
126     });
127 };
128
129 // Deletes a Automation from the DB.
130 exports.destroy = function(req, res) {
131   Automation
132     .findById(req.params.id)
133     .then(function(Automation) {
134       if (!Automation) {
135         return res.sendStatus(404);
136       }
137       Automation.destroy()
138         .then(function() {
139           return res.sendStatus(204);
140         })
141         .catch(function(err) {
142           return handleError(res, err);
143         });
144     })
145     .catch(function(err) {
146       return handleError(res, err);
147     });
148 }
149
150 exports.bulkDestroy = function(req, res) {
151   Automation
152     .destroy({
153       where: {
154         id: req.query.id
155       },
156       individualHooks: true
157     })
158     .then(function() {
159       return res.sendStatus(204);
160     })
161     .catch(function(err) {
162       return handleError(res, err);
163     });
164 };
165
166 function handleError(res, err) {
167   return res.status(500).send(err);
168 }