Skip to content

fix: auto-promote next card as default when default card is deleted#191

Open
anusuyasri wants to merge 2 commits into
Dev-Card:mainfrom
anusuyasri:fix/delete-default-card-auto-promote
Open

fix: auto-promote next card as default when default card is deleted#191
anusuyasri wants to merge 2 commits into
Dev-Card:mainfrom
anusuyasri:fix/delete-default-card-auto-promote

Conversation

@anusuyasri
Copy link
Copy Markdown

Summary

Fixes the DELETE /:id card endpoint which had no logic to handle deletion of the default card. When a user deleted their default card, no other card was promoted — leaving them with no default card and breaking QR code sharing. This PR adds auto-promotion logic so the next oldest card is automatically set as default.

Closes #170


Type of Change

  • Bug fix

What Changed

  • apps/backend/src/routes/cards.ts — Added a check in DELETE /:id handler: if the deleted card was the default, the next oldest card belonging to the user is automatically promoted as the new default
  • Handles edge case gracefully where the deleted card is the user's last card (no promotion needed)

How to Test

1.Create two cards for a user — set one as default
2.Send DELETE /cards/:id for the default card
3.Verify the remaining card is now automatically marked as isDefault: true
4.Repeat with only one card — verify deletion succeeds with no errors


Checklist

  • My code follows the project's coding style (pnpm -r run lint passes).
  • TypeScript compiles without errors (pnpm -r run typecheck).
  • I have added or updated tests for the changes I made.
  • All tests pass locally (pnpm -r run test).
  • I have updated documentation where necessary.
  • No new console.log or debug statements left in the code.
  • Breaking changes are documented in this PR description.

Screenshots / Recordings

N/A backend change logic


Additional Context

The fix uses the same Prisma patterns already established in the codebase. The existing variable (already fetched before deletion for the 404 check) is reused to check isDefault, so no extra DB query is added before the delete.

@anusuyasri
Copy link
Copy Markdown
Author

Also please add the gssoc labels after merge,thank you!

@Harxhit Harxhit added the gssoc:approved Required label for every approved PR. Gives the base +50 points and enables contribution tracking. label May 20, 2026
@Harxhit
Copy link
Copy Markdown
Collaborator

Harxhit commented May 20, 2026

Also please add the gssoc labels after merge,thank you!

Please check our Discord channel linked in the README for a better understanding of how GSSoC labels are assigned.

Comment thread apps/backend/src/routes/cards.ts Outdated
}
}

return reply.status(204).send();
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.

Error handling for DB logic can be better.

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.

Thanks for the feedback, I've wrapped the DB logic in a try/catch block and return a 500 with a descriptive error message if anything fails.

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

@Harxhit
Copy link
Copy Markdown
Collaborator

Harxhit commented May 20, 2026

@anusuyasri We wrap db logic in try catch so we can do better error handling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gssoc:approved Required label for every approved PR. Gives the base +50 points and enables contribution tracking.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: deleting the default card leaves user with no default card — next card should be auto-promoted

2 participants