3 var _ = require('lodash');
4 var util = require('util');
5 var async = require('async');
7 var Trunk = require('../../models').Trunk;
8 var FaxAccount = require('../../models').FaxAccount;
9 var FaxApplication = require('../../models').FaxApplication;
10 var VoiceExtension = require('../../models').VoiceExtension;
11 var sequelize = require('../../models').sequelize;
14 exports.index = function(req, res, next) {
16 var attributes = ['description', 'name', 'phone'];
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;
23 offset: page * per_page
26 _.forIn(req.query, function(value, key) {
32 query.order = util.format('%s %s', req.query.sort_by, req.query.sort_order || 'ASC') || null;
38 attributes.forEach(function(attribute) {
41 $like: '%' + value + '%'
44 query.where.$or.push(tmp);
51 query.where[key].$like = '%' + value + '%';
56 .findAndCountAll(query)
57 .then(function(result) {
59 var total_pages = Math.ceil(result.count / per_page);
60 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;
61 var previous_page = page > 0 ? util.format('%s://%s%s?page=%d', req.protocol, req.headers.host, req.baseUrl, page - 1) : null;
63 res.status(200).send({
67 previous_page: previous_page,
68 total_pages: total_pages
72 .catch(function(err) {
73 res.status(500).send({
74 error: 'Something blew up!'
79 // Get a single faxAccount
80 exports.show = function(req, res, next) {
82 .findById(req.params.id)
83 .then(function(faxAccount) {
85 return res.sendStatus(404);
87 return res.send(faxAccount);
89 .catch(function(err) {
94 // Creates a new faxAccount in the DB.
95 exports.create = function(req, res, next) {
96 // console.log(req.body);
106 .then(function(faxAccount) {
107 callback(null, faxAccount);
109 .catch(function(err) {
113 function(faxAccount, callback) {
115 .findById(req.body.TrunkId, {
118 .then(function(faxTrunk) {
119 callback(null, faxAccount, faxTrunk);
121 .catch(function(err) {
125 function(faxAccount, faxTrunk, callback) {
126 return VoiceExtension
128 context: faxTrunk.context,
129 exten: req.body.phone,
132 appdata: 'inbound-fax,s,1',
138 .then(function(voiceExtension) {
139 callback(null, faxAccount);
141 .catch(function(err) {
145 ], function(err, faxAccount) {
149 return res.status(404).send(err);
153 return res.status(201).send(faxAccount);
159 // Updates an existing faxAccount in the DB.
160 exports.update = function(req, res, next) {
161 // console.log(req.body);
162 // console.log(req.params);
184 .then(function(faxAccount) {
185 callback(null, faxAccount);
187 .catch(function(err) {
191 function(faxAccount, callback) {
193 .findById(req.body.TrunkId, {
196 .then(function(trunk) {
203 .then(function(data) {
204 callback(null, faxAccount, trunk);
206 .catch(function(err) {
210 .catch(function(err) {
214 function(faxAccount, trunk, callback) {
215 return VoiceExtension
219 exten: faxAccount.phone,
220 context: faxAccount.Trunk.context
225 .then(function(voiceExtension) {
226 if (!voiceExtension) {
227 callback(null, faxAccount);
229 return voiceExtension
231 exten: req.body.phone,
232 context: trunk.context,
233 TrunkId: req.body.UserId
238 console.log('voiceExtension edited');
239 callback(null, faxAccount);
241 .catch(function(err) {
245 .catch(function(err) {
249 function(faxAccount, callback) {
250 var updated = _.merge(faxAccount, req.body);
251 return updated.save({
255 console.log('faxAccount saved');
256 callback(null, faxAccount);
258 .catch(function(err) {
262 ], function(err, faxAccount) {
266 return res.status(404).send(err);
270 return res.status(201).send(faxAccount);
276 // Deletes a faxAccount from the DB.
277 exports.destroy = function(req, res, next) {
284 .then(function(faxAccount) {
286 return res.sendStatus(404);
290 return res.sendStatus(204);
292 .catch(function(err) {
296 .catch(function(err) {
301 exports.addApplications = function(req, res, next) {
306 id: req.body.applications
309 .then(function(faxApplications) {
310 var tmpFaxApplications = faxApplications;
312 return sequelize.transaction(function(t) {
313 return FaxApplication.destroy({
315 id: req.body.applications
321 var sortedApplications = [];
323 for (var i = 0; i < req.body.applications.length; i++) {
325 var tmpFaxApplication = _.find(tmpFaxApplications, {
326 'id': req.body.applications[i]
329 if (tmpFaxApplication) {
330 tmpFaxApplication.priority = i + 1;
331 sortedApplications.push(tmpFaxApplication.dataValues);
335 return FaxApplication.bulkCreate(sortedApplications, {
337 }).then(function(data) {
341 }).then(function(result) {
342 return res.status(200).send(result);
343 // Transaction has been committed
344 // result is whatever the result of the promise chain returned to the transaction callback
345 }).catch(function(err) {
346 return handleError(res, err);
347 // Transaction has been rolled back
348 // err is whatever rejected the promise chain returned to the transaction callback
351 .catch(function(err) {
356 exports.bulkDestroy = function(req, res) {
362 individualHooks: true
365 return res.sendStatus(204);
367 .catch(function(err) {
368 return handleError(res, err);
372 function handleError(res, err) {
373 return res.status(500).send(err);