From cec2c95db1a3c33b73ca5ffd80667ed15841485b Mon Sep 17 00:00:00 2001 From: Anusuya Srivastava Date: Wed, 20 May 2026 15:32:46 +0530 Subject: [PATCH 1/2] fix: auto-promote next card as default when default card is deleted --- apps/backend/src/routes/cards.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/apps/backend/src/routes/cards.ts b/apps/backend/src/routes/cards.ts index f1af7b0..dee5182 100644 --- a/apps/backend/src/routes/cards.ts +++ b/apps/backend/src/routes/cards.ts @@ -144,7 +144,22 @@ export async function cardRoutes(app: FastifyInstance) { } await app.prisma.card.delete({ where: { id } }); - return reply.status(204).send(); + +if (existing.isDefault) { + const nextCard = await app.prisma.card.findFirst({ + where: { userId }, + orderBy: { createdAt: 'asc' }, + }); + + if (nextCard) { + await app.prisma.card.update({ + where: { id: nextCard.id }, + data: { isDefault: true }, + }); + } +} + +return reply.status(204).send(); }); // ─── Set Default Card ─── From 9d85c0a13f297afb09bc9bce564f90be1426696d Mon Sep 17 00:00:00 2001 From: Anusuya Srivastava Date: Wed, 20 May 2026 17:00:39 +0530 Subject: [PATCH 2/2] fix: add error handling for DB logic in delete card handler --- apps/backend/src/routes/cards.ts | 40 ++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/apps/backend/src/routes/cards.ts b/apps/backend/src/routes/cards.ts index dee5182..6b2579d 100644 --- a/apps/backend/src/routes/cards.ts +++ b/apps/backend/src/routes/cards.ts @@ -131,7 +131,7 @@ export async function cardRoutes(app: FastifyInstance) { // ─── Delete Card ─── - app.delete('/:id', async (request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply) => { + app.delete('/:id', async (request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply) => { const userId = (request.user as any).id; const { id } = request.params; @@ -143,23 +143,27 @@ export async function cardRoutes(app: FastifyInstance) { return reply.status(404).send({ error: 'Card not found' }); } - await app.prisma.card.delete({ where: { id } }); - -if (existing.isDefault) { - const nextCard = await app.prisma.card.findFirst({ - where: { userId }, - orderBy: { createdAt: 'asc' }, - }); - - if (nextCard) { - await app.prisma.card.update({ - where: { id: nextCard.id }, - data: { isDefault: true }, - }); - } -} - -return reply.status(204).send(); + try { + await app.prisma.card.delete({ where: { id } }); + + if (existing.isDefault) { + const nextCard = await app.prisma.card.findFirst({ + where: { userId }, + orderBy: { createdAt: 'asc' }, + }); + + if (nextCard) { + await app.prisma.card.update({ + where: { id: nextCard.id }, + data: { isDefault: true }, + }); + } + } + + return reply.status(204).send(); + } catch (error) { + return reply.status(500).send({ error: 'Failed to delete card' }); + } }); // ─── Set Default Card ───