-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy path_b2cSitesVerify.js
More file actions
160 lines (141 loc) · 5.88 KB
/
_b2cSitesVerify.js
File metadata and controls
160 lines (141 loc) · 5.88 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
'use strict';
// Initialize constants
const config = require('config');
// Include local libraries
const validators = require('../../lib/cli-api/validators');
// Include B2C Commerce API functions
const b2cAuthenticate = require('../apis/ci/_authenticate');
const ciSitesAPI = require('../../lib/apis/ci/sites');
/**
* @typedef {Object} siteDetails
* @description Represents a object describing a subset of the B2C Commerce Data API site properties
*
* @property {String} cartridges Describes the contents of the cartridge path
* @property {String} display_name Describes the customer-facing name for the storefront
* @property {String} storefront_status Describes the display-status of the storefront
* @property {Object} customer_list_link Object describing the customer list to which a site is associated
* @property {String} customer_list_link.customer_list_id Describes the primary key of the associated customer list
*/
/**
* @private
* @function createValidationSummary
* @description Helper function to roll-up the site / storefront details retrieved into
* a simple object to consume. Abstracts the verbose results into a more consumable format.
*
* @param {Object} siteResults Represents the verbose site detail results
* @return {*} Returns an array representing the collection of sites that were validated
*/
function createValidationSummary(siteResults) {
// Initialize the output variable
const summaries = {
success: [],
error: []
};
Object.keys(siteResults).forEach(siteKey => {
const siteSummary = siteResults[siteKey];
const siteDetails = siteSummary.siteDetails;
// were the siteDetails summary found?
if (siteSummary.isValid === true) {
// Add the key / value pair to the summary variable
summaries.success.push({
siteId: siteKey,
url: siteDetails.url,
version: config.get('b2c.ocapiVersion'),
status: siteDetails.status,
statusText: siteDetails.data.storefront_status,
data: {
cartridges: siteDetails.data.cartridges,
displayName: siteDetails.data.display_name,
customerList: siteDetails.data.customer_list_link.customer_list_id
},
outputDisplay: [
siteKey,
siteDetails.status,
siteDetails.data.storefront_status,
siteDetails.url
]
});
return;
}
// Add the key / value pair to the summary variable
summaries.error.push({
siteId: siteKey,
url: siteDetails?.url,
version: config.get('b2c.ocapiVersion'),
status: siteDetails?.status,
statusText: 'invalid',
data: siteDetails?.data,
outputDisplay: [
siteKey,
siteSummary.error
]
});
});
return summaries;
}
/**
* @function _b2cSitesVerify
* @description Attempts to validate that the specified B2C Commerce storefront sites
* are valid, well-formed, and available via the B2C Commerce instance specified
*
* @param {Object} environmentDef Represents the already-validated environment details to use
* @returns {Promise} Returns the site validationSummary
*/
module.exports = environmentDef => new Promise(async (resolve, reject) => {
// Roll-up the validation results to a single object
const output = {
apiCalls: {
authenticate: {},
verifySites: {}
},
siteResults: {},
outputDisplay: {
authenticate: {},
verifySites: {}
}
};
const b2cSiteProperties = validators.validateB2CSiteIDs(environmentDef.b2cSiteIds);
let invalidSiteCount = 0;
// Authenticate first
try {
// Audit the authorization token for future rest requests
output.apiCalls.authenticate.authToken = await b2cAuthenticate(environmentDef);
output.outputDisplay.authenticate = output.apiCalls.authenticate;
} catch (e) {
reject(`${config.get('errors.b2c.unableToAuthenticate')}`);
return;
}
for (let siteId of b2cSiteProperties.value) {
try {
// Connect with the B2C Commerce instance and attempt to retrieve the details for the current storefront
const siteDetailResults = await ciSitesAPI.getDetail(environmentDef, output.apiCalls.authenticate.authToken, siteId);
// Initialize the object used to track service results
output.apiCalls.verifySites[siteId] = {
isValid: siteDetailResults.hasOwnProperty('status') && siteDetailResults.status === 200,
siteDetails: siteDetailResults
};
} catch (e) {
output.apiCalls.verifySites[siteId] = {
isValid: false,
error: e,
siteDetails: undefined
};
} finally {
// Increment the invalid site-count -- if the site is invalid
if (output.apiCalls.verifySites[siteId].isValid === false) {
invalidSiteCount = invalidSiteCount + 1;
}
}
}
// Build a validation summary and attach that to the top-level object
output.siteResults = createValidationSummary(output.apiCalls.verifySites);
output.outputDisplay.verifySites.success = output.siteResults.success.map(siteData => siteData.outputDisplay);
output.outputDisplay.verifySites.error = output.siteResults.error.map(siteData => siteData.outputDisplay);
// If all the sites are invalid -- then throw an exception
if (invalidSiteCount === b2cSiteProperties.value.length) {
reject('All sites failed validation; please check your environment definition');
return;
}
// Invoke the callback and continue processing
resolve(output);
});