Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions apps/backend/src/routes/cards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -143,8 +143,27 @@ export async function cardRoutes(app: FastifyInstance) {
return reply.status(404).send({ error: 'Card not found' });
}

await app.prisma.card.delete({ where: { id } });
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) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a db logic too?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

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 ───
Expand Down