This repository has been 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 pathcart.js
126 lines (102 loc) · 4.67 KB
/
cart.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
'use strict';
var uglycart = require('./lib/UGLYCART.js');
module.exports = function(RED) {
var YaaS = require('yaas.js');
function YaasAddToCart(config) {
RED.nodes.createNode(this, config);
var yaas = new YaaS();
this.yaasCustomerCredentials = RED.nodes.getNode(config.yaasCustomerCredentials);
this.yaasCredentials = RED.nodes.getNode(config.yaasCredentials);
this.status({fill:'yellow',shape:'dot',text:'idle'});
this.tenant_id = this.yaasCredentials.application_id.split('.')[0];
this.on('input', msg => {
var storedCustomer = this.context().flow.get('storedCustomer');
this.yaasCustomerCredentials = storedCustomer || this.yaasCustomerCredentials;
if (!msg.payload.body) {
this.error('error in addToCart: no body object in msg.payload');
this.status({fill:'red',shape:'dot', text: 'no body object in payload'});
return;
}
var product = (msg.payload.body.constructor === Array) ? msg.payload.body[0] : msg.payload.body;
product.images = product.media;
// FIXME find correct language values
if (product.name.en) {
product.name = product.name.en;
}
if (product.description.en) {
product.description = product.description.en;
}
var quantity = Math.round(config.quantity);
var productname = product.name || 'item';
this.status({fill:'yellow', shape:'dot', text: 'adding ' +quantity + 'x ' + productname + ' to cart'});
yaas.init(this.yaasCredentials.client_id,
this.yaasCredentials.client_secret,
'hybris.customer_read hybris.cart_manage',
this.tenant_id)
.then(() => uglycart.getCartByCustomerEmail(yaas,
this.yaasCustomerCredentials.email,
config.siteCode,
config.currency))
.then(response => {
this.cartId = response.cartId;
return yaas.price.getPricesForProducts([product.id], 'USD')
})
.then(p_response => {
product.price = p_response.body[0];
return yaas.cart.addProduct(this.cartId, product, quantity, product.price)
})
.then(cart => {
this.send({payload:cart.body});
this.status({fill:'green',shape:'dot',text:quantity + 'x ' + productname+ ' added'});
})
.catch(e => {
console.error('addToCart', e);
if (e.body && e.body.details) console.log('...', e.body.details[0]);
this.error('error in addToCart');
this.status({fill:'red',shape:'dot', text: 'error in addToCart'});
});
});
}
RED.nodes.registerType('add to cart', YaasAddToCart);
function YaasApplyDiscount(config) {
RED.nodes.createNode(this, config);
var node = this;
node.yaasCustomerCredentials = RED.nodes.getNode(config.yaasCustomerCredentials);
node.yaasCredentials = RED.nodes.getNode(config.yaasCredentials);
node.status({fill:'yellow',shape:'dot',text:'idle'});
node.tenant_id = node.yaasCredentials.application_id.split('.')[0];
var yaas = new YaaS();
node.on('input', function(msg) {
yaas.init(node.yaasCredentials.client_id,
node.yaasCredentials.client_secret,
'hybris.customer_read hybris.cart_manage',
node.tenant_id)
.then(function() {
return uglycart.getCartByCustomerEmail(yaas,
node.yaasCustomerCredentials.email,
config.siteCode,
config.currency);
})
.then(function(response) {
var coupon = msg.payload;
// Fixing glitches in YaaS API, adding missing fields
if(coupon.discountType === 'PERCENT') {
coupon.discountRate = coupon.discountPercentage;
} else {
coupon.amount = coupon.discountAbsolute.amount;
}
coupon.currency = config.currency;
node.status({fill:'green',shape:'dot',text: coupon.code});
return yaas.cart.addDiscount(response.cartId, coupon);
})
.then(function(response) {
node.status({fill:'yellow',shape:'dot',text: 'discountId: ' + response.body.discountId});
node.send({payload: response.body});
})
.catch(function(error) {
console.error(JSON.stringify(error));
});
});
}
RED.nodes.registerType('apply discount', YaasApplyDiscount);
};