Built motion from commit 4143fce.|0.0.15
[motion.git] / server / api / freshdesk_configuration / freshdesk_configuration.controller.js
1 /**
2  * Using Rails-like standard naming convention for endpoints.
3  * GET     /api/freshdesk/configurations              ->  index
4  * POST    /api/freshdesk/configurations              ->  create
5  * GET     /api/freshdesk/configurations/:id          ->  show
6  * PUT     /api/freshdesk/configurations/:id          ->  update
7  * DELETE  /api/freshdesk/configurations/:id          ->  destroy
8  */
9
10 'use strict';
11
12
13 var _ = require('lodash');
14
15 var FreshdeskConfiguration = require('../../models').FreshdeskConfiguration;
16 var FreshdeskField = require('../../models').FreshdeskField;
17
18 function handleError(res, statusCode) {
19   statusCode = statusCode || 500;
20   return function(err) {
21     res.status(statusCode).send(err);
22   };
23 }
24
25 function responseWithResult(res, statusCode) {
26   statusCode = statusCode || 200;
27   return function(entity) {
28     if (entity) {
29       res.status(statusCode).json(entity);
30     }
31   };
32 }
33
34 function handleEntityNotFound(res) {
35   return function(entity) {
36     if (!entity) {
37       res.status(404).end();
38       return null;
39     }
40     return entity;
41   };
42 }
43
44 function saveUpdates(updates) {
45   return function(entity) {
46     return entity.updateAttributes(updates)
47       .then(updated => {
48         return updated;
49       });
50   };
51 }
52
53 function removeEntity(res) {
54   return function(entity) {
55     if (entity) {
56       return entity.destroy()
57         .then(() => {
58           res.status(204).end();
59         });
60     }
61   };
62 }
63
64 // Gets a list of FreshdeskConfigurations
65 exports.index = function(req, res) {
66   FreshdeskConfiguration.findAll({
67       where: req.query
68     })
69     .then(responseWithResult(res))
70     .catch(handleError(res));
71 }
72
73 // Gets a single FreshdeskConfiguration from the DB
74 exports.show = function(req, res) {
75   FreshdeskConfiguration.find({
76       where: {
77         id: req.params.id
78       },
79       include: [{
80         model: FreshdeskField,
81         as: 'Subject'
82       }, {
83         model: FreshdeskField,
84         as: 'Description'
85       }, {
86         model: FreshdeskField,
87         as: 'Field'
88       }]
89     })
90     .then(handleEntityNotFound(res))
91     .then(responseWithResult(res))
92     .catch(handleError(res));
93 }
94
95 // Creates a new FreshdeskConfiguration in the DB
96 exports.create = function(req, res) {
97   FreshdeskConfiguration.create(req.body)
98     .then(responseWithResult(res, 201))
99     .catch(handleError(res));
100 }
101
102 // Updates an existing FreshdeskConfiguration in the DB
103 exports.update = function(req, res) {
104   if (req.body.id) {
105     delete req.body.id;
106   }
107   FreshdeskConfiguration.findById(req.params.id)
108     .then(handleEntityNotFound(res))
109     .then(saveUpdates(req.body))
110     .then(responseWithResult(res))
111     .catch(handleError(res));
112 }
113
114 // Deletes a FreshdeskConfiguration from the DB
115 exports.destroy = function(req, res) {
116   FreshdeskConfiguration.findById(req.params.id)
117     .then(handleEntityNotFound(res))
118     .then(removeEntity(res))
119     .catch(handleError(res));
120 }
121
122 exports.addConfigurationField = function(req, res, next) {
123   // console.log(req.params);
124   var freshdeskConfiguration;
125   FreshdeskConfiguration
126     .findById(req.params.id)
127     .then(handleEntityNotFound(res))
128     .then(function(salesforce_configuration) {
129       freshdeskConfiguration = salesforce_configuration;
130       return FreshdeskField.create(req.body)
131     })
132     .then(function(freshdeskField) {
133       switch (req.params.type) {
134         case 'subject':
135           return [freshdeskConfiguration.addSubject(freshdeskField), freshdeskField];
136         case 'description':
137           return [freshdeskConfiguration.addDescription(freshdeskField), freshdeskField];
138         case 'field':
139           return [freshdeskConfiguration.addField(freshdeskField), freshdeskField];
140       }
141     })
142     .spread(function(data, freshdeskField) {
143       return res.status(201).json(freshdeskField);
144     })
145     .catch(handleError(res));
146 };