a139fd502c71edc80f1996103904f71430a04a48
[motion.git] / server / api / fax_room / fax_room.controller.js
1 'use strict';
2
3 var _ = require('lodash');
4 var FaxRoom = require('../../models').FaxRoom;
5 var FaxAccount = require('../../models').FaxAccount;
6 var FaxMessage = require('../../models').FaxMessage;
7 var Contact = require('../../models').Contact;
8 var User = require('../../models').User;
9
10 // Get list of fax_rooms
11 exports.index = function(req, res) {
12   FaxRoom
13     .findAll({
14       include: [{
15         model: FaxMessage,
16         attributes: ['id']
17       }, {
18         model: FaxAccount
19       }, {
20         model: User
21       }]
22     })
23     .then(function(fax_rooms) {
24       return res.status(200).send(fax_rooms);
25     })
26     .catch(function(err) {
27       return handleError(res, err);
28     });
29 };
30
31 // Get list of agent mailRooms
32 exports.agentIndex = function(req, res, next) {
33   FaxRoom
34     .findAll({
35       where: {
36         UserId: req.params.id
37       },
38       include: [{
39         model: FaxAccount
40       }, {
41         model: User
42       }]
43     })
44     .then(function(faxRooms) {
45       return res.status(200).send(faxRooms);
46     })
47     .catch(function(err) {
48       return next(err);
49     });
50 };
51
52 // Get a single fax_room
53 exports.show = function(req, res) {
54   FaxRoom
55     .findOne({
56       where: {
57         id: req.params.id
58       },
59       include: [{
60         model: FaxMessage,
61         include: [{
62           model: FaxRoom,
63           include: [{
64             model: FaxAccount
65           }]
66         }, {
67           model: Contact,
68           as: 'From'
69         }, {
70           model: Contact,
71           as: 'To'
72         }]
73       }, {
74         model: FaxAccount
75       }],
76       order: [
77         [FaxMessage, 'createdAt', 'DESC']
78       ]
79     })
80     .then(function(fax_room) {
81       if (!fax_room) {
82         return res.sendStatus(404);
83       }
84       return res.send(fax_room);
85     })
86     .catch(function(err) {
87       return handleError(res, err);
88     });
89 };
90
91 // Creates a new fax_room in the DB.
92 exports.create = function(req, res) {
93   FaxRoom
94     .create(req.body)
95     .then(function(fax_room) {
96       return res.status(201).send(fax_room);
97     })
98     .catch(function(err) {
99       return handleError(res, err);
100     });
101 };
102
103 // Updates an existing fax_room in the DB.
104 exports.update = function(req, res) {
105   // if (req.body.id) {
106   //   delete req.body.id;
107   // }
108   FaxRoom
109     .findById(req.params.id)
110     .then(function(fax_room) {
111       if (!fax_room) {
112         return res.sendStatus(404);
113       }
114       var updated = _.merge(fax_room, req.body);
115       updated.save()
116         .then(function() {
117           return res.status(200).send(fax_room);
118         })
119         .catch(function(err) {
120           return handleError(res, err);
121         });
122     })
123     .catch(function(err) {
124       return handleError(res, err);
125     });
126 };
127
128 // Deletes a fax_room from the DB.
129 exports.destroy = function(req, res) {
130   FaxRoom
131     .findById(req.params.id)
132     .then(function(fax_room) {
133       if (!fax_room) {
134         return res.sendStatus(404);
135       }
136       fax_room.destroy()
137         .then(function() {
138           return res.sendStatus(204);
139         })
140         .catch(function(err) {
141           return handleError(res, err);
142         });
143     })
144     .catch(function(err) {
145       return handleError(res, err);
146     });
147 };
148
149 function handleError(res, err) {
150   return res.status(500).send(err);
151 }