From 6e8eccd767fe620385d32204496c344285c27285 Mon Sep 17 00:00:00 2001 From: asif Date: Sat, 9 Aug 2025 15:51:28 +0530 Subject: [PATCH] returning a valid response after creating a beneficiary also throw 409 status code if account already exists in beneficiaries table against that customer --- src/controllers/beneficiary.controller.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/controllers/beneficiary.controller.js b/src/controllers/beneficiary.controller.js index 3c3e546..fdad3e8 100644 --- a/src/controllers/beneficiary.controller.js +++ b/src/controllers/beneficiary.controller.js @@ -54,8 +54,15 @@ async function addBeneficiary(req, res) { const query = 'INSERT INTO beneficiaries (customer_no, account_no, account_type, ifsc_code, name) VALUES ($1, $2, $3, $4, $5)'; await db.query(query, [customerNo, accountNo, accountType, ifscCode, name]); + res.json({ message: 'SUCCESS' }); } catch (error) { logger.error(error, 'Error adding beneficiary'); + if ( + error.message == + 'duplicate key value violates unique constraint "beneficiaries_pkey"' + ) { + res.status(409).json({ error: 'BENEFICIARY_ALREADY_EXITS' }); + } res.status(500).json({ error: 'INTERNAL_SERVER_ERROR' }); } }