-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproductDetail.js
99 lines (79 loc) · 3.39 KB
/
productDetail.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
const axios = require('axios');
const parseString = require('xml2js');
const CONSTANTS = require('../constants');
module.exports = function(app, debugLogger) {
/*
* GET (Read)
*/
app.get('/api/productDetail', async function(req, res, next) {
const { DEBUG, WEB_STORE, SF_USERNAME, ORG_ID, SF_SITE_URL, SF_PASSWORD, SF_LOGIN_URL, SF_API_VERSION } = process.env;
const response = {...CONSTANTS.RESPONSE_OBJECT};
try {
const { sku,effectiveAccountId } = req.query;
if (!sku && !effectiveAccountId) {
const error = new Error();
error.message = 'Required fields not found.';
error.status = 206;
throw(error);
}
// Make SOAP auth call for accessToken
const soapAuthUrl = `${SF_SITE_URL}/services/Soap/u/${SF_API_VERSION}`;
const soapAuthBody = `<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:partner.soap.sforce.com">
<SOAP-ENV:Header>
<ns1:LoginScopeHeader>
<ns1:organizationId>${ORG_ID}</ns1:organizationId>
</ns1:LoginScopeHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:login>
<ns1:username>${SF_USERNAME}</ns1:username>
<ns1:password>${SF_PASSWORD}</ns1:password>
</ns1:login>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>`;
const soapConfig = {
headers: {
'Content-Type' : 'text/xml',
'SOAPAction': 'login',
'charset': 'UTF-8'
}
}
const soapAuth = await axios.post(soapAuthUrl, soapAuthBody, soapConfig);
const auth = await parseString.parseStringPromise(soapAuth.data, { mergeAttrs: true })
const accessToken = auth['soapenv:Envelope']['soapenv:Body'][0].loginResponse[0].result[0].sessionId[0];
const url = `${SF_LOGIN_URL}/services/data/v${SF_API_VERSION}/commerce/webstores/${WEB_STORE}/products`;
let config = {
headers: {
'Content-Type' : 'application/json',
'Authorization': 'Bearer '+accessToken
},
params: {
effectiveAccountId: effectiveAccountId,
skus: sku
},
}
console.log('##DEBUG config: '+JSON.stringify(config));
const productsRes = await axios.get(url, config);
console.log('##DEBUG productsRes: '+productsRes);
if (DEBUG === 'true') debugLogger.info('/api/productDetail', 'GET', id, 'Get product details.', productsRes);
if (!productsRes) {
const error = new Error();
error.message = 'Product not found.';
error.status = 404;
throw(error);
}
response.data = productsRes.data;
response.success = true;
res.status(200).send(response);
} catch (error) {
const { id } = req.query;
response.error = {...CONSTANTS.RESPONSE_ERROR_OBJECT};
response.error.message = error.message || 'Internal Server Error';
response.error.status = error.status || 500;
response.success = false;
if (DEBUG === 'true') debugLogger.info('/api/productDetail', 'GET', id, 'Exception', response);
res.status(error.status || 500).send(response);
}
});
};