@@ -12,18 +12,18 @@ governing permissions and limitations under the License.
12
12
13
13
const { Timings, aggregate } = require ( './lib/benchmark' ) ;
14
14
const { AdminAPI } = require ( './lib/aem' ) ;
15
- const { requestSaaS, requestSpreadsheet, isValidUrl } = require ( '../utils' ) ;
15
+ const { requestSaaS, requestSpreadsheet, isValidUrl, getProductUrl , mapLocale } = require ( '../utils' ) ;
16
16
const { GetAllSkusQuery, GetLastModifiedQuery } = require ( '../queries' ) ;
17
17
const { Core } = require ( '@adobe/aio-sdk' ) ;
18
18
19
19
const BATCH_SIZE = 50 ;
20
20
21
- async function loadState ( storeCode , stateLib ) {
22
- const stateKey = storeCode ? `${ storeCode } ` : 'default' ;
21
+ async function loadState ( locale , stateLib ) {
22
+ const stateKey = locale ? `${ locale } ` : 'default' ;
23
23
const stateData = await stateLib . get ( stateKey ) ;
24
24
if ( ! stateData ?. value ) {
25
25
return {
26
- storeCode ,
26
+ locale ,
27
27
skusLastQueriedAt : new Date ( 0 ) ,
28
28
skus : { } ,
29
29
} ;
@@ -34,7 +34,7 @@ async function loadState(storeCode, stateLib) {
34
34
// folloed by a pair of SKUs and timestamps which are the last preview times per SKU
35
35
const [ catalogQueryTimestamp , ...skus ] = stateData && stateData . value ? stateData . value . split ( ',' ) : [ 0 ] ;
36
36
return {
37
- storeCode ,
37
+ locale ,
38
38
skusLastQueriedAt : new Date ( parseInt ( catalogQueryTimestamp ) ) ,
39
39
skus : Object . fromEntries ( skus
40
40
. map ( ( sku , i , arr ) => ( i % 2 === 0 ? [ sku , new Date ( parseInt ( arr [ i + 1 ] ) ) ] : null ) )
@@ -43,11 +43,11 @@ async function loadState(storeCode, stateLib) {
43
43
}
44
44
45
45
async function saveState ( state , stateLib ) {
46
- let { storeCode } = state ;
47
- if ( ! storeCode ) {
48
- storeCode = 'default' ;
46
+ let { locale } = state ;
47
+ if ( ! locale ) {
48
+ locale = 'default' ;
49
49
}
50
- const stateKey = `${ storeCode } ` ;
50
+ const stateKey = `${ locale } ` ;
51
51
const stateData = [
52
52
state . skusLastQueriedAt . getTime ( ) ,
53
53
...Object . entries ( state . skus ) . flatMap ( ( [ sku , lastPreviewedAt ] ) => [ sku , lastPreviewedAt . getTime ( ) ] ) ,
@@ -61,28 +61,28 @@ async function saveState(state, stateLib) {
61
61
* state accordingly.
62
62
*
63
63
* @param {Object } params - The parameters object.
64
- * @param {string } params.siteName - The name of the site (repo or repoless).
65
- * @param {string } params.PDPURIPrefix - The URI prefix for Product Detail Pages .
64
+ * @param {string } params.HLX_SITE_NAME - The name of the site (repo or repoless).
65
+ * @param {string } params.HLX_PATH_FORMAT - The URL format for product detail pages .
66
66
* @param {string } params.PLPURIPrefix - The URI prefix for Product List Pages.
67
- * @param {string } params.orgName - The name of the organization.
68
- * @param {string } params.configName - The name of the configuration json/xlsx.
67
+ * @param {string } params.HLX_ORG_NAME - The name of the organization.
68
+ * @param {string } params.HLX_CONFIG_NAME - The name of the configuration json/xlsx.
69
69
* @param {number } [params.requestPerSecond=5] - The number of requests per second allowed by the throttling logic.
70
70
* @param {string } params.authToken - The authentication token.
71
71
* @param {number } [params.skusRefreshInterval=600000] - The interval for refreshing SKUs in milliseconds.
72
- * @param {string } [params.storeUrl ] - The store's base URL.
73
- * @param {string } [params.storeCodes ] - Comma-separated list of store codes .
72
+ * @param {string } [params.HLX_STORE_URL ] - The store's base URL.
73
+ * @param {string } [params.HLX_LOCALES ] - Comma-separated list of allowed locales .
74
74
* @param {string } [params.LOG_LEVEL] - The log level.
75
75
* @param {Object } stateLib - The state provider object.
76
76
* @returns {Promise<Object> } The result of the polling action.
77
77
*/
78
78
function checkParams ( params ) {
79
- const requiredParams = [ 'siteName ' , 'PDPURIPrefix ' , 'PLPURIPrefix' , 'orgName ' , 'configName ' , 'authToken' ] ;
79
+ const requiredParams = [ 'HLX_SITE_NAME ' , 'HLX_PATH_FORMAT ' , 'PLPURIPrefix' , 'HLX_ORG_NAME ' , 'HLX_CONFIG_NAME ' , 'authToken' ] ;
80
80
const missingParams = requiredParams . filter ( param => ! params [ param ] ) ;
81
81
if ( missingParams . length > 0 ) {
82
82
throw new Error ( `Missing required parameters: ${ missingParams . join ( ', ' ) } ` ) ;
83
83
}
84
84
85
- if ( params . storeUrl && ! isValidUrl ( params . storeUrl ) ) {
85
+ if ( params . HLX_STORE_URL && ! isValidUrl ( params . HLX_STORE_URL ) ) {
86
86
throw new Error ( 'Invalid storeUrl' ) ;
87
87
}
88
88
}
@@ -110,43 +110,47 @@ function shouldProcessProduct(product) {
110
110
async function poll ( params , stateLib ) {
111
111
checkParams ( params ) ;
112
112
113
- const log = Core . Logger ( 'main' , { level : params . LOG_LEVEL || 'info' } ) ;
113
+ const logger = Core . Logger ( 'main' , { level : params . LOG_LEVEL || 'info' } ) ;
114
114
const {
115
- siteName,
116
- PDPURIPrefix ,
117
- orgName,
118
- configName,
115
+ HLX_SITE_NAME : siteName ,
116
+ HLX_PATH_FORMAT : pathFormat ,
117
+ HLX_ORG_NAME : orgName ,
118
+ HLX_CONFIG_NAME : configName ,
119
119
requestPerSecond = 5 ,
120
120
authToken,
121
121
skusRefreshInterval = 600000 ,
122
122
} = params ;
123
- const storeUrl = params . storeUrl ? params . storeUrl : `https://main--${ siteName } --${ orgName } .aem.live` ;
124
- const storeCodes = params . storeCodes ? params . storeCodes . split ( ',' ) : [ null ] ;
123
+ const storeUrl = params . HLX_STORE_URL ? params . HLX_STORE_URL : `https://main--${ siteName } --${ orgName } .aem.live` ;
124
+ const locales = params . HLX_LOCALES ? params . HLX_LOCALES . split ( ',' ) : [ null ] ;
125
125
126
126
const counts = {
127
127
published : 0 , unpublished : 0 , ignored : 0 , failed : 0 ,
128
128
} ;
129
129
const sharedContext = {
130
- storeUrl, configName, log , counts,
130
+ storeUrl, configName, logger , counts, pathFormat ,
131
131
} ;
132
132
const timings = new Timings ( ) ;
133
133
const adminApi = new AdminAPI ( {
134
134
org : orgName ,
135
135
site : siteName ,
136
136
} , sharedContext , { requestPerSecond, authToken } ) ;
137
137
138
- log . info ( `Starting poll from ${ storeUrl } for store codes ${ storeCodes } ` ) ;
138
+ logger . info ( `Starting poll from ${ storeUrl } for locales ${ locales } ` ) ;
139
139
140
140
try {
141
141
// start processing preview and publish queues
142
142
await adminApi . startProcessing ( ) ;
143
143
144
- const results = await Promise . all ( storeCodes . map ( async ( storeCode ) => {
144
+ const results = await Promise . all ( locales . map ( async ( locale ) => {
145
145
const timings = new Timings ( ) ;
146
146
// load state
147
- const state = await loadState ( storeCode , stateLib ) ;
147
+ const state = await loadState ( locale , stateLib ) ;
148
148
timings . sample ( 'loadedState' ) ;
149
- const context = { ...sharedContext , storeCode } ;
149
+
150
+ let context = { ...sharedContext } ;
151
+ if ( locale ) {
152
+ context = { ...context , ...mapLocale ( locale , context ) } ;
153
+ }
150
154
151
155
// setup preview / publish queues
152
156
@@ -173,7 +177,7 @@ async function poll(params, stateLib) {
173
177
const skus = Object . keys ( state . skus ) ;
174
178
const lastModifiedResp = await requestSaaS ( GetLastModifiedQuery , 'getLastModified' , { skus } , context ) ;
175
179
timings . sample ( 'fetchedLastModifiedDates' ) ;
176
- log . info ( `Fetched last modified date for ${ lastModifiedResp . data . products . length } skus, total ${ skus . length } ` ) ;
180
+ logger . info ( `Fetched last modified date for ${ lastModifiedResp . data . products . length } skus, total ${ skus . length } ` ) ;
177
181
178
182
// group preview in batches of 50
179
183
let products = lastModifiedResp . data . products
@@ -198,7 +202,7 @@ async function poll(params, stateLib) {
198
202
const batches = products . filter ( shouldProcessProduct )
199
203
. reduce ( ( acc , product , i , arr ) => {
200
204
const { sku, urlKey } = product ;
201
- const path = ( storeCode ? `/ ${ storeCode } ${ PDPURIPrefix } / ${ urlKey } / ${ sku } ` : ` ${ PDPURIPrefix } / ${ urlKey } / ${ sku } ` ) . toLowerCase ( ) ;
205
+ const path = getProductUrl ( { urlKey, sku } , context , false ) . toLowerCase ( ) ;
202
206
const req = adminApi . previewAndPublish ( { path, sku } ) ;
203
207
acc . push ( req ) ;
204
208
if ( acc . length === BATCH_SIZE || i === arr . length - 1 ) {
@@ -251,7 +255,7 @@ async function poll(params, stateLib) {
251
255
}
252
256
} catch ( e ) {
253
257
// in case the index doesn't yet exist or any other error
254
- log . error ( e ) ;
258
+ logger . error ( e ) ;
255
259
}
256
260
257
261
timings . sample ( 'unpublishedPaths' ) ;
@@ -277,14 +281,14 @@ async function poll(params, stateLib) {
277
281
}
278
282
timings . measures . previewDuration = aggregate ( adminApi . previewDurations ) ;
279
283
} catch ( e ) {
280
- log . error ( e ) ;
284
+ logger . error ( e ) ;
281
285
// wait for queues to finish, even in error case
282
286
await adminApi . stopProcessing ( ) ;
283
287
}
284
288
285
289
const elapsed = new Date ( ) - timings . now ;
286
290
287
- log . info ( `Finished polling, elapsed: ${ elapsed } ms` ) ;
291
+ logger . info ( `Finished polling, elapsed: ${ elapsed } ms` ) ;
288
292
289
293
return {
290
294
state : 'completed' ,
0 commit comments