From 816fd7987bd437526c230f5c9e5ea5b6c817bd55 Mon Sep 17 00:00:00 2001 From: MarcosPierasNL <pieras.marcos@gmail.com> Date: Thu, 20 Mar 2025 11:19:33 +0100 Subject: [PATCH] test: handles message invalid insights --- src/readers/insightProcessor.ts | 27 +++++++++++++++++++++++---- src/utils/insights/validateInsight.ts | 16 ++++++++++++++-- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/readers/insightProcessor.ts b/src/readers/insightProcessor.ts index e937452..1fb0637 100644 --- a/src/readers/insightProcessor.ts +++ b/src/readers/insightProcessor.ts @@ -57,10 +57,31 @@ export const insightProcessor = async (frontendPublisher: RabbitMqBroker) => { let insight = message.insight; if (!validateInsight(insight, message.force)) { + if (ENV == 'develop') { + const message: WsMessageBackend2Frontend = { + type: wsReturnKey.error, + callID: headers.callID, + status: 'success', + value: 'Insight no valid', + }; + + frontendPublisher.publishMessageToFrontend(message, headers.routingKey, headers); + } return; } if (!validateTypeInsight(insight, message.force)) { + if (ENV == 'develop') { + const message: WsMessageBackend2Frontend = { + type: wsReturnKey.error, + callID: headers.callID, + status: 'success', + value: 'Insight Type no valid', + }; + + frontendPublisher.publishMessageToFrontend(message, headers.routingKey, headers); + } + return; } @@ -102,16 +123,14 @@ export const insightProcessor = async (frontendPublisher: RabbitMqBroker) => { insight = statCheck(insight, result); } - log.info('Insight save: ', insight); - await ums.updateInsight(insight.userId as number, insight.id, insight); if (ENV == 'develop') { const message: WsMessageBackend2Frontend = { - type: wsReturnKey.insightResults, + type: wsReturnKey.insightResult, callID: headers.callID, - insight, status: 'success', + value: insight, }; frontendPublisher.publishMessageToFrontend(message, headers.routingKey, headers); diff --git a/src/utils/insights/validateInsight.ts b/src/utils/insights/validateInsight.ts index e8efc8d..f0827ab 100644 --- a/src/utils/insights/validateInsight.ts +++ b/src/utils/insights/validateInsight.ts @@ -1,4 +1,4 @@ -import { type InsightModel } from 'ts-common'; +import { type InsightModel, isValidEmail, isValidCronExpression } from 'ts-common'; import { log } from '../../logger'; export function validateInsight(insight: InsightModel, force: boolean) { @@ -8,7 +8,19 @@ export function validateInsight(insight: InsightModel, force: boolean) { } if (!insight.recipients || insight.recipients.length === 0) { - log.debug('No recipients found in the insight, skipping'); + log.error('No recipients found in the insight, skipping'); + return false; + } + + for (const recipient of insight.recipients) { + if (!isValidEmail(recipient)) { + log.error(`Invalid email for recipient: ${recipient}`); + return false; + } + } + + if (!insight.frequency || !isValidCronExpression(insight.frequency)) { + log.error('No frequency found in the insight, skipping'); return false; } -- GitLab