This repository was archived by the owner on Feb 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcoupon.js
100 lines (87 loc) · 3.43 KB
/
coupon.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
'use strict';
module.exports = function (RED) {
var YaaS = require('yaas.js');
var helper = require('./lib/helper');
function YaasCouponCreateNode(config) {
RED.nodes.createNode(this, config);
var node = this;
node.yaasCredentials = RED.nodes.getNode(config.yaasCredentials);
node.tenant_id = config.tenantId || helper.tenantId(node.yaasCredentials);
node.couponType = config.couponType;
node.currency = config.currency;
console.log(config.couponType);
node.status({ fill: 'red', shape: 'ring', text: 'disconnected' });
var yaas = new YaaS();
yaas.init(node.yaasCredentials.client_id,
node.yaasCredentials.client_secret,
'hybris.coupon_manage',
node.tenant_id)
.then(function () {
node.status({ fill: 'yellow', shape: 'ring', text: 'idle' });
node.on('input', function (msg) {
var amount = msg.payload + '';
var coupon = {
// TO BE IMPLEMENTED: 'code':'???',
'name': config.name,
'description': config.name,
'discountType': config.couponType,
'restrictions': { 'validFrom': new Date().toISOString() },
'allowAnonymous': true
};
if (config.couponType === 'PERCENT') {
coupon.discountPercentage = amount;
} else {
coupon.discountAbsolute = {
'amount': amount,
'currency': config.currency
};
}
console.log(coupon);
yaas.coupon.post(coupon)
.then(function (response) {
var info;
if (config.couponType === 'PERCENT') {
info = coupon.discountPercentage + '%';
} else {
info = coupon.discountAbsolute.currency + ' ' + coupon.discountAbsolute.amount;
}
node.status({ fill: 'green', shape: 'dot', text: response.body.id + ' (' + info + ')' });
node.send({ payload: response.body });
}, function (error) {
console.error(JSON.stringify(error));
});
});
});
node.on('close', function () { });
}
RED.nodes.registerType('create coupon', YaasCouponCreateNode);
function YaasCouponGetNode(config) {
RED.nodes.createNode(this, config);
var node = this;
node.yaasCredentials = RED.nodes.getNode(config.yaasCredentials);
node.tenant_id = config.tenantId || helper.tenantId(node.yaasCredentials);
node.status({ fill: 'red', shape: 'ring', text: 'disconnected' });
var yaas = new YaaS();
yaas.init(node.yaasCredentials.client_id,
node.yaasCredentials.client_secret,
'hybris.coupon_redeem',
node.tenant_id)
.then(function () {
node.status({ fill: 'yellow', shape: 'ring', text: 'idle' });
node.on('input', function (msg) {
var couponId = msg.payload.id || msg.payload;
node.status({ fill: 'green', shape: 'dot', text: couponId });
console.log(couponId);
yaas.coupon.get(couponId)
.then(function (response) {
console.log(response);
var statusText = response.body.code + ' (' + response.body.status + ')';
node.status({ fill: 'green', shape: 'dot', text: statusText });
node.send({ payload: response.body });
}, console.error);
});
});
node.on('close', function () { });
}
RED.nodes.registerType('get', YaasCouponGetNode);
};