From 69c05d73f9007f97a3778e00e1080c21f1904de7 Mon Sep 17 00:00:00 2001 From: Dmytro Svyrydenko Date: Tue, 17 Sep 2024 23:05:15 +0200 Subject: [PATCH] fix: tx creation route validation --- .../create-transaction.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/controllers/transactions.controller/create-transaction.ts b/src/controllers/transactions.controller/create-transaction.ts index 693e66a8..c5d902d8 100644 --- a/src/controllers/transactions.controller/create-transaction.ts +++ b/src/controllers/transactions.controller/create-transaction.ts @@ -88,7 +88,7 @@ const bodyZodSchema = z .optional(), destinationAccountId: recordId().optional(), destinationTransactionId: recordId().optional(), - categoryId: recordId(), + categoryId: z.union([recordId(), z.undefined()]), transferNature: z.nativeEnum(TRANSACTION_TRANSFER_NATURE), refundsTxId: recordId().optional(), }) @@ -145,6 +145,21 @@ const bodyZodSchema = z message: `For ${TRANSACTION_TRANSFER_NATURE.common_transfer} without "destinationTransactionId" - "destinationAccountId", and "destinationAmount" must be provided`, path: ['destinationAccountId', 'destinationAmount', 'destinationTransactionId'], }, + ) + .refine( + (data) => { + if ( + data.transferNature === TRANSACTION_TRANSFER_NATURE.not_transfer && + data.categoryId === undefined + ) { + return false; + } + return true; + }, + { + message: "'categoryId' is required for non-transfer transactions.", + path: ['categoryId', 'transferNature'], + }, ); export const createTransactionSchema = z.object({