@@ -14,10 +14,12 @@ const { Core, Files } = require('@adobe/aio-sdk');
1414const { ObservabilityClient } = require ( '../lib/observability' ) ;
1515const { GetUrlKeyQuery } = require ( '../queries' ) ;
1616const { getRuntimeConfig } = require ( '../lib/runtimeConfig' ) ;
17+ const { AdminAPI } = require ( '../lib/aem' ) ;
1718const {
1819 requestSaaS,
19- requestSpreadsheet ,
20+ requestPublishedProductsIndex ,
2021 PDP_FILE_EXT ,
22+ createBatches,
2123} = require ( '../utils' ) ;
2224
2325/**
@@ -43,13 +45,13 @@ function urlkeymatch(publishedProduct, queriedProducts){
4345 * @param {Object } context - The context object with logger and other utilities
4446 * @param {Object } filesLib - The files library instance from '@adobe/aio-sdk'
4547 */
46- async function markUpCleanUP ( context , filesLib , logger ) {
48+ async function markUpCleanUP ( context , filesLib , logger , adminApi ) {
4749
4850 try {
49- const publishedProducts = await requestSpreadsheet ( 'published-products-index' , null , context ) ;
51+ const publishedProducts = await requestPublishedProductsIndex ( context ) ;
5052 const publishedSkus = publishedProducts . data . map ( ( product ) => product . sku ) ;
5153 let queryResult = await requestSaaS ( GetUrlKeyQuery , 'getUrlKey' , { skus : publishedSkus } , context ) ;
52- queryResult = queryResult . data . products
54+ queryResult = queryResult . data . products ;
5355
5456 const redundantpublishedProducts = publishedProducts . data . filter ( ( product ) => ! urlkeymatch ( product , queryResult ) )
5557 context . counts . detected = redundantpublishedProducts . length ;
@@ -68,6 +70,20 @@ async function markUpCleanUP(context, filesLib, logger) {
6870 }
6971 }
7072 } ;
73+ const pendingJobs = [ ] ;
74+ const unpublishJobsBatches = createBatches ( redundantpublishedProducts ) ;
75+ for ( let batchNumber = 0 ; batchNumber < unpublishJobsBatches . length ; batchNumber ++ ) {
76+ const batch = unpublishJobsBatches [ batchNumber ] . map ( ( product ) => {
77+ return {
78+ sku : product . sku ,
79+ path : product . path ,
80+ } ;
81+ } ) ;
82+ const pendingJob = adminApi . unpublishAndDelete ( batch , context . locale , batchNumber ) ;
83+ context . counts . unpublished += batch . length ;
84+ pendingJobs . push ( pendingJob ) ;
85+ }
86+ await Promise . all ( pendingJobs ) ;
7187 } catch ( e ) {
7288 logger . error ( 'Error while cleanning up markup storage' , e ) ;
7389 }
@@ -99,7 +115,7 @@ async function main(params) {
99115 logIngestorEndpoint,
100116 } = cfg ;
101117
102- const counts = { detected : 0 , deleted : 0 } ;
118+ const counts = { detected : 0 , deleted : 0 , unpublished : 0 } ;
103119
104120 const sharedContext = {
105121 storeUrl,
@@ -113,22 +129,30 @@ async function main(params) {
113129 logLevel,
114130 logIngestorEndpoint,
115131 }
132+ const adminApi = new AdminAPI ( { org : params . ORG , site : params . SITE } , sharedContext , { authToken : params . AEM_ADMIN_AUTH_TOKEN } ) ;
116133
117134 let activationResult = { status : { } } ;
118135
119- for ( const locale of locales ) {
120- const context = { ...sharedContext , startTime : new Date ( ) } ;
121- let tempLocale = 'default' ;
122- if ( locale ) {
123- context . locale = locale ;
124- tempLocale = locale ;
125- }
136+ try {
137+ adminApi . startProcessing ( ) ;
126138
127- activationResult . status [ tempLocale ] = await markUpCleanUP ( context , filesLib , logger ) ;
128- }
139+ for ( const locale of locales ) {
140+ const context = { ...sharedContext , startTime : new Date ( ) } ;
141+ let tempLocale = 'default' ;
142+ if ( locale ) {
143+ context . locale = locale ;
144+ tempLocale = locale ;
145+ }
146+ activationResult . status [ tempLocale ] = await markUpCleanUP ( context , filesLib , logger ) ;
147+ }
129148
130- await observabilityClient . sendActivationResult ( activationResult ) ;
131- return activationResult ;
149+ await observabilityClient . sendActivationResult ( activationResult ) ;
150+ return activationResult ;
151+ } catch ( e ) {
152+ logger . error ( e ) ;
153+ } finally {
154+ await adminApi . stopProcessing ( ) ;
155+ }
132156}
133157
134158exports . main = main
0 commit comments