Skip to content

fix(backend): automatically handle default card reassignment on card deletion #251

@mramansayyad

Description

@mramansayyad

Summary

Prevents database inconsistencies and profile breakage by restricting or gracefully handling the deletion of a user's active default profile card.

Contexts

Currently, the DELETE /cards/:id endpoint allows a user to delete any card belonging to them. If they delete their active default card (isDefault: true), the user profile is left without a default fallback state. This breaks core external entry points, such as primary QR code resolution and the public-facing web backup route (apps/web), which depend on a valid default card.

Tasks

  • Intercept DELETE /cards/:id requests to check if the target card has isDefault: true.
  • Implement fallback logic: if other cards exist, automatically promote the oldest remaining card to isDefault: true.
  • Prevent deletion or return a descriptive error if it is the user's last remaining card (or auto-create a baseline fallback).
  • Add automated unit and integration tests covering the new deletion behavior.

Acceptance Criteria

  • Attempting to delete a default card gracefully reassigns the default flag to an existing sibling card.
  • Deleting a non-default card leaves the default card unaffected.
  • Back-end unit tests added and pass cleanly (pnpm test:backend).

Area

backend

Difficulty

Medium

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions