Skip to content

feat: DELETE /cards/:id returns 204 No Content on success but has no cascade behavior defined β€” deleting a card that is someone's default leaves them with no default card and no recovery pathΒ #663

Description

@divyanshim27

πŸ› Problem Statement

The README's API error table documents:

DELETE /:id β†’ 404 { error: 'Card not found' } when card ID doesn't exist

But there is no documented behavior for deleting the user's default card. The Prisma schema has a isDefault boolean on each card, and the README states: "first card is auto-set as default."

If a user has 3 cards (Card A = default, Card B, Card C) and deletes Card A:

  • No new default is automatically assigned
  • The user's QR code (which presumably resolves to their default card) now points to null
  • The PUT /:id/default endpoint can be used to fix this, but the user has no indication anything went wrong
  • A receiver who scans the QR before the user notices sees a broken profile

Proposed Fix

In apps/backend/src/routes/cards.ts, add a post-delete cascade that automatically promotes the oldest remaining card to default when the deleted card was the default:

// apps/backend/src/routes/cards.ts β€” inside DELETE /:id handler

const deletedCard = await prisma.card.findUnique({
  where: { id: params.id, userId: request.user.id }
});

if (!deletedCard) {
  return reply.status(404).send({ error: 'Card not found' });
}

await prisma.card.delete({ where: { id: params.id } });

// If the deleted card was the default, auto-promote the next oldest card
if (deletedCard.isDefault) {
  const nextCard = await prisma.card.findFirst({
    where: { userId: request.user.id },
    orderBy: { createdAt: 'asc' },
  });

  if (nextCard) {
    await prisma.card.update({
      where: { id: nextCard.id },
      data: { isDefault: true },
    });
  }
}

return reply.status(204).send();

Also update the API response for this case to return 200 with a body describing the auto-promotion (rather than 204 No Content) so clients can update their state:

{
  "message": "Card deleted. Card 'My Professional Card' has been set as your new default."
}

Update the error table in README.md to document this behavior.

Files to Modify

File Change
apps/backend/src/routes/cards.ts Add default card cascade logic in DELETE handler
README.md Document auto-promotion behavior in error/response table

Suggested labels: bug, backend, level: intermediate

I would like to work on this. Could you please assign it to me?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status
    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions