Skip to content

Commit

Permalink
Merge branch 'leungkinghin/implement-full-ingestion' into leungkinghi…
Browse files Browse the repository at this point in the history
…n/full-ingestion-test-case
  • Loading branch information
leungkinghin-ct committed Aug 14, 2023
2 parents f2a9361 + 4765ad9 commit a856f21
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 65 deletions.
54 changes: 0 additions & 54 deletions full-ingestion/src/controllers/event.controller.js

This file was deleted.

76 changes: 76 additions & 0 deletions full-ingestion/src/controllers/sync.controller.js
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();
};
4 changes: 2 additions & 2 deletions full-ingestion/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import express from 'express';
import bodyParser from 'body-parser';

// Import routes
import EventRoutes from './routes/event.route.js';
import SyncRoutes from './routes/sync.route.js';
import { logger } from './utils/logger.utils.js';

const PORT = 8080;
Expand All @@ -17,7 +17,7 @@ app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// Define routes
app.use('/', EventRoutes);
app.use('/', SyncRoutes);

// Listen the application
const server = app.listen(PORT, () => {
Expand Down
9 changes: 0 additions & 9 deletions full-ingestion/src/routes/event.route.js

This file was deleted.

9 changes: 9 additions & 0 deletions full-ingestion/src/routes/sync.route.js
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;

0 comments on commit a856f21

Please sign in to comment.