-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (68 loc) · 2.3 KB
/
index.js
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
'use strict';
var soajs = require('soajs');
var config = require('./config.js');
var BLModule = require("./lib/index");
var service = new soajs.server.service(config);
service.init(function () {
/**
*
* jamil id: 575a7d4c349e672f123990d5
* getCart: the getCart api returns the content of the cart for the current logged in user
*/
service.get("/cart/getCart", function (req, res) {
initBLModel(req, res, function (BL) {
BL.getCart(config, req.soajs, function (error, response) {
return res.json(req.soajs.buildResponse(error, response));
});
});
});
/**
* setCart: the setCart api will add items to the cart; it updates an existing cart or creates a new one
*/
service.post("/cart/setCart", function (req, res) {
initBLModel(req, res, function (BL) {
BL.setCart(config, req.soajs, function (error, response) {
return res.json(req.soajs.buildResponse(error, response));
});
});
});
/**
* emptyCart: the emptyCart api will delete all items from the cart
*/
service.delete("/cart/emptyCart", function (req, res) {
initBLModel(req, res, function (BL) {
BL.emptyCart(config, req.soajs, function (error, response) {
return res.json(req.soajs.buildResponse(error, response));
});
});
});
/**
* getCarts: this api returns all the carts of all the tenant’s users; it takes a limit and sorts the entries by most recent
*/
service.get("/cart/getCarts", function (req, res) {
initBLModel(req, res, function (BL) {
BL.getCarts(config, req.soajs, function (error, response) {
return res.json(req.soajs.buildResponse(error, response));
});
});
});
service.start();
});
/**
* Mongo connection
* @param req
* @param res
* @param cb
*/
function initBLModel(req, res, cb) {
var modelName = req.soajs.inputmaskData.model;
BLModule.init(modelName, function (error, BL) {
if (error) {
req.soajs.log.error(error);
return res.json(req.soajs.buildResponse({"code": 407, "msg": config.errors[407]}));
}
else {
return cb(BL);
}
});
}