From 34024629f31393b1108260db8da1b65d70464b2f Mon Sep 17 00:00:00 2001 From: King-Hin Leung Date: Wed, 16 Aug 2023 18:19:37 +0200 Subject: [PATCH 1/2] Implement routing for changes in different resource type --- incremental-updater/package-lock.json | 19 +-------- incremental-updater/package.json | 5 +-- incremental-updater/src/index.js | 2 - incremental-updater/src/routes/event.route.js | 42 +++++++++++-------- 4 files changed, 28 insertions(+), 40 deletions(-) diff --git a/incremental-updater/package-lock.json b/incremental-updater/package-lock.json index 3ec547f..47fb31b 100644 --- a/incremental-updater/package-lock.json +++ b/incremental-updater/package-lock.json @@ -13,8 +13,7 @@ "@commercetools/platform-sdk": "^4.1.0", "@commercetools/sdk-client-v2": "^2.0.1", "body-parser": "^1.20.1", - "dotenv": "^16.0.3", - "express": "4.18.2", + "express": "^4.18.2", "validator": "^13.7.0" }, "devDependencies": { @@ -3793,17 +3792,6 @@ "node": ">=6.0.0" } }, - "node_modules/dotenv": { - "version": "16.3.1", - "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz", - "integrity": "sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==", - "engines": { - "node": ">=12" - }, - "funding": { - "url": "https://github.com/motdotla/dotenv?sponsor=1" - } - }, "node_modules/ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -11213,11 +11201,6 @@ "esutils": "^2.0.2" } }, - "dotenv": { - "version": "16.3.1", - "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz", - "integrity": "sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==" - }, "ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", diff --git a/incremental-updater/package.json b/incremental-updater/package.json index 637370c..e72efd1 100644 --- a/incremental-updater/package.json +++ b/incremental-updater/package.json @@ -29,8 +29,7 @@ "@commercetools/platform-sdk": "^4.1.0", "@commercetools/sdk-client-v2": "^2.0.1", "body-parser": "^1.20.1", - "dotenv": "^16.0.3", - "express": "4.18.2", + "express": "^4.18.2", "validator": "^13.7.0" } -} \ No newline at end of file +} diff --git a/incremental-updater/src/index.js b/incremental-updater/src/index.js index 8a04059..31179d8 100644 --- a/incremental-updater/src/index.js +++ b/incremental-updater/src/index.js @@ -1,5 +1,3 @@ -import 'dotenv/config'; - import express from 'express'; import bodyParser from 'body-parser'; diff --git a/incremental-updater/src/routes/event.route.js b/incremental-updater/src/routes/event.route.js index d36e260..6d3c908 100644 --- a/incremental-updater/src/routes/event.route.js +++ b/incremental-updater/src/routes/event.route.js @@ -21,23 +21,31 @@ async function eventHandler(request, response) { throw new CustomError(400, 'Bad request: Wrong No Pub/Sub message format'); } - // TODO : Investigate how to determine the source subscription of the message (store? product-selection? product?) - const message = request.body.message; - switch (message?.source) { - case 'store': - await storeEventHandler(request, response); - break; - case 'product-selection': - await productSelectionEventHandler(request, response); - break; - case 'product': - await productEventHandler(request, response); - break; - default: - throw new CustomError( - 400, - 'Bad request: Message queue name is not defined' - ); + const encodedMessageBody = request.body?.message?.data; + if (encodedMessageBody) { + const buff = new Buffer(encodedMessageBody, 'base64'); + const messageBody = JSON.parse(buff.toString('ascii')); + + const resourceType = messageBody?.resource?.typeId; + + switch (resourceType) { + case 'store': + await storeEventHandler(request, response); + break; + case 'product-selection': + await productSelectionEventHandler(request, response); + break; + case 'product': + await productEventHandler(request, response); + break; + default: + throw new CustomError( + 400, + 'Bad request: Resource type is not defined in incoming message data' + ); + } + } else { + throw new CustomError(400, 'Bad request: message data is not defined'); } } From 6cf1518df22c7054808710dc613e7eef84594240 Mon Sep 17 00:00:00 2001 From: King-Hin Leung Date: Wed, 16 Aug 2023 18:29:17 +0200 Subject: [PATCH 2/2] Improve error handling in event.route.js --- incremental-updater/src/routes/event.route.js | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/incremental-updater/src/routes/event.route.js b/incremental-updater/src/routes/event.route.js index 6d3c908..16c9c55 100644 --- a/incremental-updater/src/routes/event.route.js +++ b/incremental-updater/src/routes/event.route.js @@ -9,20 +9,27 @@ import { logger } from '../utils/logger.utils.js'; const eventRouter = Router(); async function eventHandler(request, response) { - // Check request body - if (!request.body) { - logger.error('Missing request body.'); - throw new CustomError(400, 'Bad request: No Pub/Sub message was received'); - } + try { + // Check request body + if (!request.body) { + logger.error('Missing request body.'); + throw new CustomError( + 400, + 'Bad request: No Pub/Sub message was received' + ); + } - // Check if the body comes in a message - if (!request.body.message) { - logger.error('Missing body message'); - throw new CustomError(400, 'Bad request: Wrong No Pub/Sub message format'); - } + // Check if the body comes in a message + if (!request.body.message || !request.body.message.data) { + logger.error('Missing message data in incoming message'); + throw new CustomError( + 400, + 'Bad request: No message data in incoming message' + ); + } + + const encodedMessageBody = request.body.message.data; - const encodedMessageBody = request.body?.message?.data; - if (encodedMessageBody) { const buff = new Buffer(encodedMessageBody, 'base64'); const messageBody = JSON.parse(buff.toString('ascii')); @@ -44,8 +51,9 @@ async function eventHandler(request, response) { 'Bad request: Resource type is not defined in incoming message data' ); } - } else { - throw new CustomError(400, 'Bad request: message data is not defined'); + } catch (err) { + logger.error(err); + return response.status(err.statusCode).send(err); } }