ba9c227527f602fbd423c32fd2f1c859799e4d9c
[motion.git] / server / api / fax_room / fax_room.kue.js
1 /**
2  * Broadcast updates to client when the model changes
3  */
4
5 'use strict';
6
7 var User = require('../../models').User;
8 var FaxRoom = require('../../models').FaxRoom;
9 var FaxQueue = require('../../models').FaxQueue;
10 var FaxAccount = require('../../models').FaxAccount;
11 var FaxApplication = require('../../models').FaxApplication;
12
13 exports.register = function(queue) {
14   FaxRoom.afterCreate(function(doc) {
15     onSave(queue, doc);
16   });
17 }
18
19 function onSave(queue, doc, cb) {
20   if (doc.status === 'NEW') {
21     FaxAccount
22       .findById(doc.FaxAccountId)
23       .then(function(faxAccount) {
24         faxAccount
25           .getFaxApplications({
26             include: [{
27               model: User,
28               attributes: ['id']
29             }, {
30               model: FaxQueue,
31               attributes: ['id', 'strategy', 'timeout'],
32               include: {
33                 model: User,
34                 attributes: ['id']
35               }
36             }],
37             order: [
38               ['priority']
39             ]
40           })
41           .then(function(faxApplications) {
42
43             var data = {
44               roomId: doc.id,
45               accountId: doc.FaxAccountId,
46               channel: 'FAX',
47               msg: {
48                 from: doc.from
49               },
50               applications: faxApplications
51             };
52
53             // Create queue process
54             queue.create('fax', data).save();
55           })
56           .catch(function(err) {
57             console.error(err);
58           });
59       })
60       .catch(function(err) {
61         console.error(err);
62       });
63   }
64 }