diff --git a/src/core/middlewares/validator.middleware.ts b/src/core/middlewares/validator.middleware.ts index 68ae46e..29d27f4 100644 --- a/src/core/middlewares/validator.middleware.ts +++ b/src/core/middlewares/validator.middleware.ts @@ -7,6 +7,38 @@ import { BadRequestError } from "../app/app.errors"; @injectable() export class ValidationMiddleware { + static validateInputWithLogging(DTO: ClassConstructor, logPrefix: string = "Request Body"): RequestHandler { + return async (req: Request, _res: Response, next: NextFunction): Promise => { + try { + // Log the raw request body before validation + console.log(`${logPrefix}:`, JSON.stringify(req.body, null, 2)); + + // Convert the request body to an instance of the DTO class + const validationClass = plainToInstance(DTO, req.body, { + excludeExtraneousValues: true, + }); + + // Validate the DTO instance, including nested objects + const errors: ValidationError[] = await validate(validationClass, { + whitelist: true, + // forbidNonWhitelisted: true, + // transform: true, + }); + if (errors.length > 0) { + const errorMsg = errors.map((error) => ValidationMiddleware.flattenValidationErrors(error)).flat(); + + next(new BadRequestError(errorMsg)); + // throw new BadRequestError("Bad Request", errorMsg); + } else { + req.body = validationClass; + next(); + } + } catch (error) { + next(error); + } + }; + } + static validateInput(DTO: ClassConstructor): RequestHandler { return async (req: Request, _res: Response, next: NextFunction): Promise => { try { diff --git a/src/modules/IPG/gateways/sep.ts b/src/modules/IPG/gateways/sep.ts index 5bce058..7199846 100644 --- a/src/modules/IPG/gateways/sep.ts +++ b/src/modules/IPG/gateways/sep.ts @@ -51,7 +51,7 @@ class SepGateway implements IGateway { RedirectUrl: `${this.callBackURL}${callBackPath}`, ResNum: Date.now().toString(), CellNumber: _mobile, - GetMethod:true + // GetMethod:true }; const response = await axios.post(requesturl, purchaseData, { headers: this.requestHeader }); diff --git a/src/modules/payment/payment.controller.ts b/src/modules/payment/payment.controller.ts index 01af650..2ce243a 100644 --- a/src/modules/payment/payment.controller.ts +++ b/src/modules/payment/payment.controller.ts @@ -111,7 +111,7 @@ class PaymentController extends BaseController { @ApiOperation("verify a payment for current session user for cartCheckout (method post for SEP)") @ApiResponse("successful", HttpStatus.Ok) @ApiParam("gateway", "the name of api gateway", true) - @httpPost("/verify/:gateway/cart", ValidationMiddleware.validateInput(VerifyOnlinePaymentDTO)) + @httpPost("/verify/:gateway/cart", ValidationMiddleware.validateInputWithLogging(VerifyOnlinePaymentDTO, "Verify Cart Checkout")) public async VerifyCartCheckoutPost( @requestParam("gateway") gateway: string, @response() res: Response,