forked from SalesforceCommerceCloud/b2c-crm-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_b2cOCAPIConfigGet.js
More file actions
110 lines (81 loc) · 3.6 KB
/
_b2cOCAPIConfigGet.js
File metadata and controls
110 lines (81 loc) · 3.6 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
'use strict';
// Initialize local libraries
const dataAPIs = require('../../lib/apis/sfcc/ocapi/data');
const b2cRequestLib = require('../../lib/_common/request');
const auditOCAPIConfig = require('../../lib/cli-api/_common/_auditOCAPIConfig');
/**
* @function _b2cOCAPIConfigGet
* @description Attempts to retrieve the OCAPI Config for the configured B2C Client ID
*
* @param {Object} environmentDef Represents the already-validated environment details to use when performing the actions
* @param {String} b2cAdminAuthToken Represents the B2C Commerce authToken used to perform REST API calls
* @returns {Promise} Returns the result of the OCAPI configuration get-attempt
*/
module.exports = (environmentDef, b2cAdminAuthToken) => new Promise(async (resolve, reject) => {
// Initialize local variables
let output,
baseRequest,
ocapiConfigResults,
siteId;
// Initialize the output property
output = {
ocapiConfig: [],
outputDisplay: {
global: [],
sites: []
},
hasGlobalData: false,
hasGlobalShop: false,
hasSites: {}
};
// Initialize the base request leveraged by this process
baseRequest = b2cRequestLib.createRequestInstance(environmentDef);
try {
// Force the update to the preferenceGroup and set the customerId for the related storefront
ocapiConfigResults = await dataAPIs.ocapiConfigGet(
baseRequest, environmentDef, b2cAdminAuthToken
);
// Audit the configResults response
output.ocapiConfig = ocapiConfigResults;
// Was the siteResults update successful?
if (ocapiConfigResults.success === true) {
// Were global configuration results found?
if (ocapiConfigResults.global.length > 0) {
// Build out the global display for OCAPI shop / data API preferences
output.outputDisplay.global = ocapiConfigResults.global[0].site_configs.map(ocapiConfig => {
return [ocapiConfig.api_type, JSON.stringify(ocapiConfig, null, 4)];
});
} else {
// Build the output display and default the API contents
output.outputDisplay.global.push(
['data', '---'],
['shop', '---']
);
}
// Were site-specific configuration results found?
if (Object.prototype.hasOwnProperty.call(ocapiConfigResults, 'ocapiConfigResults')) {
// Iterate over the global results and generate the output display
output.outputDisplay.sites = ocapiConfigResults.sites.map(ocapiConfig => {
// Replace the identifiers from the site_id in the OCAPI config
siteId = ocapiConfig.site_id.replace('Sites-', '').replace('-Site', '');
// Build the site-specific output display
return [siteId, JSON.stringify(ocapiConfig, null, 4)];
});
}
// Audit the OCAPI configuration to a local file
auditOCAPIConfig(environmentDef, ocapiConfigResults);
} else {
// Build the error display
output.outputDisplay.global.push(
['status', ocapiConfigResults.status],
['errorType', ocapiConfigResults.data.fault.type],
['errorMessage', ocapiConfigResults.data.fault.message]
);
}
// Return the processed results
resolve(output);
} catch (e) {
// Reject and return the error
reject(e);
}
});