From 4f53cc22f2ed086fa9fa472282049a77cd6486fa Mon Sep 17 00:00:00 2001 From: Pankaj Sha Date: Fri, 3 Jan 2025 21:43:00 +0530 Subject: [PATCH] fix: add condition to check the request type and refactor order of condition for early validation --- controllers/onboardingExtension.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/controllers/onboardingExtension.ts b/controllers/onboardingExtension.ts index 0789f35ce..b8534ef76 100644 --- a/controllers/onboardingExtension.ts +++ b/controllers/onboardingExtension.ts @@ -1,7 +1,7 @@ import firestore from "../utils/firestore"; import { CustomResponse } from "../types/global"; import { UpdateOnboardingExtensionRequest, UpdateOnboardingExtensionRequestBody } from "../types/onboardingExtension"; -import { ERROR_WHILE_UPDATING_REQUEST, LOG_ACTION, REQUEST_DOES_NOT_EXIST, REQUEST_LOG_TYPE, REQUEST_STATE } from "../constants/requests"; +import { ERROR_WHILE_UPDATING_REQUEST, LOG_ACTION, REQUEST_DOES_NOT_EXIST, REQUEST_LOG_TYPE, REQUEST_STATE, REQUEST_TYPE } from "../constants/requests"; import { addLog } from "../models/logs"; const requestModel = firestore.collection("requests"); @@ -22,14 +22,18 @@ export const updateOnboardingExtensionRequestController = async (req: UpdateOnbo const extensionRequest = extensionRequestDoc.data(); - if(extensionRequest.oldEndsOn > body.newEndsOn) { - return res.boom.badRequest("Request new deadline must be greater than old deadline."); + if(extensionRequest.type !== REQUEST_TYPE.ONBOARDING) { + return res.boom.badRequest("Invalid request type") } - + if(extensionRequest.state != REQUEST_STATE.PENDING){ return res.boom.badRequest("Request state is not pending"); } - + + if(extensionRequest.oldEndsOn > body.newEndsOn) { + return res.boom.badRequest("Request new deadline must be greater than old deadline."); + } + const requestBody = { ...body, lastModifiedBy,