Skip to content

Commit

Permalink
Merge branch 'leungkinghin/implement-incremental-updater' into leungk…
Browse files Browse the repository at this point in the history
…inghin/inc-update-post-deploy-scripts
  • Loading branch information
leungkinghin-ct committed Aug 25, 2023
2 parents 267ea79 + 6cf1518 commit ce0f4aa
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 48 deletions.
19 changes: 1 addition & 18 deletions incremental-updater/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions incremental-updater/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
2 changes: 0 additions & 2 deletions incremental-updater/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dotenv/config';

import express from 'express';
import bodyParser from 'body-parser';

Expand Down
66 changes: 41 additions & 25 deletions incremental-updater/src/routes/event.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,51 @@ 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');
}

// 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');
}
try {
// Check request body
if (!request.body) {
logger.error('Missing request body.');
throw new CustomError(
400,
'Bad request: No Pub/Sub message was received'
);
}

// 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:
// 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: Message queue name is not defined'
'Bad request: No message data in incoming message'
);
}

const encodedMessageBody = request.body.message.data;

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'
);
}
} catch (err) {
logger.error(err);
return response.status(err.statusCode).send(err);

Check warning

Code scanning / CodeQL

Information exposure through a stack trace Medium

This information exposed to the user depends on
stack trace information
.
}
}

Expand Down

0 comments on commit ce0f4aa

Please sign in to comment.