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
/
productdetails.js
79 lines (66 loc) · 2.64 KB
/
productdetails.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
'use strict';
module.exports = function(RED) {
var YaaS = require('yaas.js');
function YaasProductDetailsByIDNode(config) {
RED.nodes.createNode(this, config);
var node = this;
node.yaasCredentials = RED.nodes.getNode(config.yaasCredentials);
node.status({fill:'yellow',shape:'dot',text:'idle'});
node.on('input',function(msg) {
node.status({fill:'green',shape:'dot',text:'retrieve product'});
var yaas = new YaaS();
yaas.init(
node.yaasCredentials.client_id, // theClientId
node.yaasCredentials.client_secret, // theClientSecret
'hybris.pcm_read', // theScope
node.yaasCredentials.application_id // theProjectId
)
.then(function() {
return yaas.product.getProduct(msg.payload);
})
.then(function(result){
msg.payload = result
node.send(msg);
var name = result.body.name;
if (name && name.en) {
name = name.en;
}
node.status({fill:'yellow',shape:'dot',text: name || 'found'});
})
.catch( function(e) {
console.error(e);
node.status({fill:'red',shape:'dot',text:'error'});
});
});
}
function YaasProductDetailsByQueryNode(config) {
RED.nodes.createNode(this, config);
var node = this;
node.yaasCredentials = RED.nodes.getNode(config.yaasCredentials);
node.on('input',function(msg) {
var yaas = new YaaS();
yaas.init(
node.yaasCredentials.client_id, // theClientId
node.yaasCredentials.client_secret, // theClientSecret
'hybris.pcm_read', // theScope
node.yaasCredentials.application_id // theProjectId
)
.then(function() {
var query = {
"q" : msg.payload
};
return yaas.product.getProducts(query);
})
.then(function(result){
node.send({payload:result});
node.status({fill:'yellow',shape:'dot',text:'found ' + result.body.length + ' items.'});
})
.catch( function(e) {
console.error(e);
node.status({fill:'red',shape:'dot',text:'error'});
});
});
}
RED.nodes.registerType('get product details by ID', YaasProductDetailsByIDNode);
RED.nodes.registerType('get product details by query', YaasProductDetailsByQueryNode);
};