forked from SalesforceCommerceCloud/b2c-crm-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_b2cOOBOCustomerGet.js
More file actions
96 lines (73 loc) · 3.3 KB
/
_b2cOOBOCustomerGet.js
File metadata and controls
96 lines (73 loc) · 3.3 KB
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
'use strict';
// Initialize constants
const config = require('config');
// Include local libraries
const dataAPIs = require('../../lib/apis/sfcc/ocapi/data');
const b2cRequestLib = require('../../lib/_common/request');
// Include B2C Commerce API functions
const b2cAuthenticate = require('../apis/ci/_authenticate');
const createOOBOCustomerDisplayOutput = require('./_common/_createOOBOCustomerDisplayOutput');
/**
* @function _b2cGetOOBOCustomer
* @description Attempts to retrieve the OOBO Customer record for a give site / customerList
* combination. It will verify the existence of the OOBO Customer record.
*
* @param {Object} environmentDef Represents the already-validated environment details to use when performing the actions
* @param {Object} siteDef Represents the site that will be used to drive the OOBO Customer retrieval
* @returns {Promise} Returns the results of the OOBO customerCreation activity
*/
module.exports = (environmentDef, siteDef) => new Promise(async (resolve, reject) => {
// Initialize constants
const customerNo = config.get('b2c.ooboCustomer.customerNo');
// Initialize local variables
let b2cAdminAuthToken,
baseRequest;
// Roll-up the validation results to a single object
const output = {
apiCalls: {
authenticate: {},
sfdcAuthenticate: {},
customerGet: {}
},
outputDisplay: {
authenticate: {},
customerGet: {}
}
};
try {
// Authenticate and audit the authorization token for future rest requests
output.apiCalls.authenticate.authToken = await b2cAuthenticate(environmentDef);
output.outputDisplay.authenticate.authToken = output.apiCalls.authenticate.authToken;
} catch (e) {
// Capture the error details -- and then exit early
reject(`${config.get('errors.b2c.unableToAuthenticate')}`);
return;
}
// Initialize the base request leveraged by this process
baseRequest = b2cRequestLib.createRequestInstance(environmentDef);
b2cAdminAuthToken = output.apiCalls.authenticate.authToken;
// Attempt to verify that the current customer exists / does not exist
output.apiCalls.customerGet = await dataAPIs.customerGet(
baseRequest, b2cAdminAuthToken, siteDef.data.customerList, customerNo);
// Was a fault encountered when we attempted to retrieve the customerProfile?
if (output.apiCalls.customerGet.status !== 200) {
// Attach the profile identifiers
output.outputDisplay.customerGet = [
['customerList', siteDef.data.customerList],
['customerNo', customerNo],
['status', 'OOBO Customer profile was not found for this customerList / customerNo combination'],
['customerId', '---'],
['accountId', '---'],
['contactId', '---']
];
} else {
// Otherwise, default the profile details
output.profile = output.apiCalls.customerGet.data;
// Map the customerList to the current profile
output.profile.customer_list = siteDef.data.customerList;
// Build out the output display for the current profile
output.outputDisplay.customerGet = createOOBOCustomerDisplayOutput(output.profile);
}
// Return the generated output
resolve(output);
});