File size: 2,432 Bytes
56b6519
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
module.exports = function (app) {
  var Response = require('../lib/httpResponse.js');
  var Client = require('mongoose').model('Client');
  var acl = require('../lib/auth').acl;

  // Get clients list
  app.get(
    '/api/clients',
    acl.hasPermission('clients:read'),
    function (req, res) {
      Client.getAll()
        .then(msg => Response.Ok(res, msg))
        .catch(err => Response.Internal(res, err));
    },
  );

  // Create client
  app.post(
    '/api/clients',
    acl.hasPermission('clients:create'),
    function (req, res) {
      if (!req.body.email) {
        Response.BadParameters(res, 'Required parameters: email');
        return;
      }

      var client = {};
      // Required parameters
      client.email = req.body.email;

      // Optional parameters
      if (req.body.lastname) client.lastname = req.body.lastname;
      if (req.body.firstname) client.firstname = req.body.firstname;
      if (req.body.phone) client.phone = req.body.phone;
      if (req.body.cell) client.cell = req.body.cell;
      if (req.body.title) client.title = req.body.title;
      var company = null;
      if (req.body.company && req.body.company.name)
        company = req.body.company.name;

      Client.create(client, company)
        .then(msg => Response.Created(res, msg))
        .catch(err => Response.Internal(res, err));
    },
  );

  // Update client
  app.put(
    '/api/clients/:id',
    acl.hasPermission('clients:update'),
    function (req, res) {
      var client = {};
      // Optional parameters
      if (req.body.email) client.email = req.body.email;
      client.lastname = req.body.lastname || null;
      client.firstname = req.body.firstname || null;
      client.phone = req.body.phone || null;
      client.cell = req.body.cell || null;
      client.title = req.body.title || null;
      var company = null;
      if (req.body.company && req.body.company.name)
        company = req.body.company.name;

      Client.update(req.params.id, client, company)
        .then(msg => Response.Ok(res, 'Client updated successfully'))
        .catch(err => Response.Internal(res, err));
    },
  );

  // Delete client
  app.delete(
    '/api/clients/:id',
    acl.hasPermission('clients:delete'),
    function (req, res) {
      Client.delete(req.params.id)
        .then(msg => Response.Ok(res, 'Client deleted successfully'))
        .catch(err => Response.Internal(res, err));
    },
  );
};