-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'leungkinghin/implement-full-ingestion' into leungkinghi…
…n/full-ingestion-test-case
- Loading branch information
Showing
5 changed files
with
87 additions
and
65 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { logger } from '../utils/logger.utils.js'; | ||
import { createApiRoot } from '../clients/create.client.js'; | ||
import CustomError from '../errors/custom.error.js'; | ||
import { default as saveProducts } from '../extensions/algolia-example/clients/client.js'; | ||
|
||
const CHUNK_SIZE = 100; | ||
|
||
async function syncProducts(storeKey) { | ||
const products = await getProductsByStore(storeKey); | ||
|
||
await saveProducts(products).catch((error) => { | ||
throw new CustomError(400, `Bad request: ${error.message}`, error); | ||
}); | ||
} | ||
|
||
async function getProductsByStore(storeKey) { | ||
let lastProductId = undefined; | ||
let hasNextQuery = true; | ||
let allProducts = []; | ||
|
||
while (hasNextQuery) { | ||
let queryArgs = { | ||
limit: CHUNK_SIZE, | ||
withTotal: false, | ||
sort: 'product.id asc', | ||
expand: [ | ||
'product', | ||
'product.productType', | ||
'product.taxCategory', | ||
'product.masterData.current.categories[*]', | ||
], | ||
}; | ||
if (lastProductId) { | ||
queryArgs.where = `product(id>"${lastProductId}")`; | ||
} | ||
|
||
let productChunk = await createApiRoot() | ||
.inStoreKeyWithStoreKeyValue({ | ||
storeKey: Buffer.from(storeKey).toString(), | ||
}) | ||
.productSelectionAssignments() | ||
.get({ queryArgs }) | ||
.execute() | ||
.then((response) => response.body.results) | ||
.then((results) => results.map((result) => result.product)) | ||
.catch((error) => { | ||
throw new CustomError(400, `Bad request: ${error.message}`, error); | ||
}); | ||
hasNextQuery = productChunk.length == CHUNK_SIZE; | ||
if (productChunk.length > 0) { | ||
lastProductId = productChunk[productChunk.length - 1].id; | ||
allProducts = allProducts.concat(productChunk); | ||
} | ||
} | ||
return allProducts; | ||
} | ||
|
||
export const syncHandler = async (request, response) => { | ||
try { | ||
const storeKey = request.params.storeKey; | ||
if (!storeKey) { | ||
logger.error('Missing store key in query parameter.'); | ||
throw new CustomError( | ||
400, | ||
'Bad request: No store key is defined in query parameter' | ||
); | ||
} | ||
await syncProducts(storeKey); | ||
} catch (err) { | ||
logger.error(err); | ||
return response.status(err.statusCode).send(err); | ||
} | ||
|
||
// Return the response for the client | ||
return response.status(204).send(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Router } from 'express'; | ||
|
||
import { syncHandler } from '../controllers/sync.controller.js'; | ||
|
||
const syncRouter = Router(); | ||
|
||
syncRouter.post('/fullSync/:storeKey', syncHandler); | ||
|
||
export default syncRouter; |