-
Notifications
You must be signed in to change notification settings - Fork 1
/
routes.js
57 lines (51 loc) · 1.7 KB
/
routes.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
/**
* Module Dependencies
*/
const errors = require('restify-errors');
const paymentController = require('./controllers/payment.controller.js');
const savings = require('./services/savings');
const Savings = require('./models/savings');
module.exports = function(server) {
/**
* POST
* Create new savings subscription by
* updating SC and setting a payment cronjob.
*/
server.post({url: '/savings', validation: {
content : {
address: { isRequired: true },
endTime: { isRequired: true },
interval: { isRequired: true, isNatural: true },
amount: { isRequired: true },
name: { isRequired: true },
}
}}, (req, res, next) => {
if (!req.is('application/json')) {
return next(
new errors.InvalidContentError("Expects 'application/json'")
);
}
const data = req.body;
const savings = new Savings(data.address, data.endTime, data.interval, data.amount, data.name);
paymentController.startPaymentCronjob(savings);
});
/**
* End the savings and make a withdrawal.
*/
server.post({url: '/requestwithdrawal', validation: {
content : {
address: { isRequired: true },
name: { isRequired: true },
}
}}, (req, res, next) => {
if (!req.is('application/json')) {
return next(
new errors.InvalidContentError("Expects 'application/json'")
);
}
const data = req.body;
const state = savings.getSavingsState(data.name);
savings.makeSavingsWithdrawal(data.name, state);
savings.closeSavings(data.name);
});
};