-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy path_b2cOOBOCustomerCreate.js
More file actions
226 lines (175 loc) · 9.49 KB
/
_b2cOOBOCustomerCreate.js
File metadata and controls
226 lines (175 loc) · 9.49 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
'use strict';
// Initialize constants
const config = require('config');
const randomEmail = require('random-email');
const randomPassword = require('generate-password');
// Include local libraries
const dataAPIs = require('../../lib/apis/sfcc/ocapi/data');
const shopAPIs = require('../../lib/apis/sfcc/ocapi/shop');
const b2cRequestLib = require('../../lib/_common/request');
const sObjectAPIs = require('../../lib/apis/sfdc/sObject');
const sfAuthUserCredentials = require('./_sfAuthUserCredentials');
// Include B2C Commerce API functions
const b2cAuthenticate = require('../apis/ci/_authenticate');
const createOOBOCustomerDisplayOutput = require('./_common/_createOOBOCustomerDisplayOutput');
/**
* @function _b2cCreateOOBOCustomer
* @description Attempts to create or retrieve the OOBO Customer record for a give site / customerList
* combination. It will either verify the existence of the OOBO Customer record -- or create one.
*
* @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 creation
* @returns {Promise} Returns the result of the OOBO customerCreate request
*/
module.exports = async (environmentDef, siteDef) =>
new Promise(async (resolve, reject) => {
// Initialize constants
const customerNo = config.get('b2c.ooboCustomer.customerNo');
const customerEmail = randomEmail({domain: config.get('b2c.ooboCustomer.emailDomain')});
// Initialize local variables
let customerProfile,
b2cAdminAuthToken,
sfdcAuthResults,
baseRequest,
b2cGuestAuth;
// Retrieve the template customer profile
customerProfile = config.util.toObject(config.get('b2c.ooboCustomer.profile'));
// Overwrite the email / login for the current customer
customerProfile.customer.email = 'oobo-' + customerEmail;
customerProfile.customer.login = customerProfile.customer.email;
customerProfile.password = randomPassword.generate({length: 10, numbers: true, symbols: true, strict: true});
// Roll-up the validation results to a single object
const output = {
unableToVerifySFDC: false,
apiCalls: {
authenticate: {},
sfdcAuthenticate: {},
customerGet: {},
customerCreate: {}
},
outputDisplay: {
authenticate: {},
customerGet: {},
customerCreate: {}
}
};
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;
}
try {
// Attempt to authenticate against the SFDC environment
sfdcAuthResults = await sfAuthUserCredentials(environmentDef);
// Audit the authResults service-call
output.apiCalls.sfdcAuthenticate = sfdcAuthResults.apiCalls.sfAuthenticate.authResults;
} catch (e) {
// Capture the error details -- and then exit early
reject(`${config.get('errors.sf.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) {
// Create the output results for the retrieval process
output.outputDisplay.customerGet = [
output.apiCalls.customerGet.data.fault.type,
output.apiCalls.customerGet.data.fault.message
];
// Retrieve the guestAuthorization token from B2C Commerce
b2cGuestAuth = await shopAPIs.authAsGuest(environmentDef, siteDef.siteId, environmentDef.b2cClientId);
// Register the B2C Commerce customer profile
output.apiCalls.customerCreate = await shopAPIs.customerPost(
environmentDef, siteDef.siteId, environmentDef.b2cClientId, b2cGuestAuth.authToken, customerProfile);
// Was the registration successful?
if (output.apiCalls.customerCreate.success === true) {
// If so, then let's update the customerNo for this customer
output.apiCalls.customerGet = await dataAPIs.customerPatch(
baseRequest,
b2cAdminAuthToken,
siteDef.data.customerList,
output.apiCalls.customerCreate.data.customer_no,
{
customer_no: customerNo
});
// Map the customerList to the current profile
output.apiCalls.customerGet.data.password = customerProfile.password;
// Before updating the customerNumber for the SFDC Contact -- validate that the
// contactId exists on the B2C Customer Profile
if (Object.prototype.hasOwnProperty.call(output.apiCalls.customerGet.data, 'c_b2ccrm_contactId') &&
output.apiCalls.customerGet.data.c_b2ccrm_contactId !== undefined) {
// Update the customerNo for the specified contactId
output.apiCalls.sfdcCustomerNoUpdate = await sObjectAPIs.update(
output.apiCalls.sfdcAuthenticate.conn,
'Contact',
{
Id: output.apiCalls.customerGet.data.c_b2ccrm_contactId,
B2C_Customer_No__c: customerNo
});
}
}
// Attach the profile identifiers
output.profile = {
exists: false,
customerListId: siteDef.data.customerList,
siteId: siteDef.siteId,
customerNo: customerNo,
customerId: output.apiCalls.customerGet.data.customer_id,
accountId: output.apiCalls.customerGet.data.c_b2ccrm_accountId,
contactId: output.apiCalls.customerGet.data.c_b2ccrm_contactId
};
// Before updating the customerNumber for the SFDC Contact -- validate
// that the contactId exists on the B2C Customer Profile
} else {
// Attach the profile identifiers
output.profile = {
exists: true,
customerListId: siteDef.data.customerList,
siteId: siteDef.siteId,
customerNo: output.apiCalls.customerGet.data.customer_no,
customerId: output.apiCalls.customerGet.data.customer_id,
accountId: output.apiCalls.customerGet.data.c_b2ccrm_accountId,
contactId: output.apiCalls.customerGet.data.c_b2ccrm_contactId
};
if (Object.prototype.hasOwnProperty.call(output.apiCalls.customerGet.data, 'c_b2ccrm_contactId') &&
output.apiCalls.customerGet.data.c_b2ccrm_contactId !== undefined) {
try {
// Retrieve the SFDC customerDetails for this contact
output.apiCalls.sfdcCustomerGet = await sObjectAPIs.retrieve(
output.apiCalls.sfdcAuthenticate.conn,
'Contact',
output.apiCalls.customerGet.data.c_b2ccrm_contactId);
// Was the corresponding contact record found in the SFDC org? And do the customerNo's match across records?
if (output.apiCalls.sfdcCustomerGet.success === true &&
output.apiCalls.sfdcCustomerGet.B2C_Customer_No__c !== customerNo) {
// Update the customerNo for the specified contactId -- if it's not in-sync
output.apiCalls.sfdcCustomerNoUpdate = await sObjectAPIs.update(
output.apiCalls.sfdcAuthenticate.conn,
'Contact',
{
Id: output.apiCalls.customerGet.data.c_b2ccrm_contactId,
B2C_Customer_No__c: customerNo
});
}
} catch (e) {
// Set the flag describing that we couldn't verify the referenced records in SFDC
output.unableToVerifySFDC = true;
}
}
}
// Append the customerList to the customerProfile output
output.apiCalls.customerGet.data.customer_list = siteDef.data.customerList;
// Create the output display for the customerGet (display the details of the OOBO customer)
output.outputDisplay.customerGet = createOOBOCustomerDisplayOutput(output.apiCalls.customerGet.data);
// Exit and render the processing results
resolve(output);
});