Skip to content

Commit d4c2635

Browse files
authored
fix(backend): automatically handle default card reassignment on card deletion (#285)
* fix(backend): automatically handle default card reassignment on card deletion * feat(backend): add limit to card list query * refactor(backend): add try-catch and typed responses per review * feat(backend): add Fastify request schemas for card routes * feat(backend): use Fastify typed request schema generics for card routes * feat(backend): remove manual JSON request schemas, keeping Fastify generic typing
1 parent ab4e523 commit d4c2635

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

apps/backend/src/routes/cards.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ import type { Card } from '@devcard/shared';
44
import { createCardSchema, updateCardSchema } from '../utils/validators.js';
55
import { handleDbError } from '../utils/error.util.js';
66

7+
interface CreateCardBody {
8+
title: string;
9+
linkIds: string[];
10+
}
11+
12+
interface UpdateCardBody {
13+
title?: string;
14+
linkIds?: string[];
15+
}
16+
17+
interface CardParams {
18+
id: string;
19+
}
20+
721
export async function cardRoutes(app: FastifyInstance): Promise<void> {
822
app.addHook('preHandler', app.authenticate);
923

@@ -38,7 +52,7 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
3852

3953
// ─── Create Card ───
4054

41-
app.post('/', async (request: FastifyRequest, reply: FastifyReply): Promise<Card | void> => {
55+
app.post('/', async (request: FastifyRequest<{ Body: CreateCardBody }>, reply: FastifyReply): Promise<Card | void> => {
4256
const userId = (request.user as { id: string }).id;
4357
const parsed = createCardSchema.safeParse(request.body);
4458

@@ -82,7 +96,7 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
8296

8397
// ─── Update Card ───
8498

85-
app.put('/:id', async (request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply): Promise<Card | void> => {
99+
app.put('/:id', async (request: FastifyRequest<{ Params: CardParams; Body: UpdateCardBody }>, reply: FastifyReply): Promise<Card | void> => {
86100
const userId = (request.user as { id: string }).id;
87101
const { id } = request.params;
88102

@@ -157,7 +171,7 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
157171

158172
// ─── Delete Card ───
159173

160-
app.delete('/:id', async (request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply): Promise<void> => {
174+
app.delete('/:id', async (request: FastifyRequest<{ Params: CardParams }>, reply: FastifyReply): Promise<void> => {
161175
const userId = (request.user as { id: string }).id;
162176
const { id } = request.params;
163177

@@ -200,7 +214,7 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
200214

201215
// ─── Set Default Card ───
202216

203-
app.put('/:id/default', async (request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply): Promise<object | void> => {
217+
app.put('/:id/default', async (request: FastifyRequest<{ Params: CardParams }>, reply: FastifyReply): Promise<object | void> => {
204218
const userId = (request.user as { id: string }).id;
205219
const { id } = request.params;
206220

0 commit comments

Comments
 (0)