Skip to content
Merged
Show file tree
Hide file tree
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
59 changes: 3 additions & 56 deletions apps/backend/src/__tests__/cards.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Fastify, { type FastifyInstance, type FastifyRequest } from 'fastify';
import Fastify, { type FastifyInstance } from 'fastify';
import { describe, it, expect, beforeEach, vi } from 'vitest';

import { cardRoutes } from '../routes/cards.js';
Expand Down Expand Up @@ -48,14 +48,10 @@ const mockPrisma = {
// against the same mock client, preserving existing per-operation mocks.
function wireTransaction(): void {
mockPrisma.$transaction.mockImplementation(
async (callback: (tx: typeof mockPrisma) => Promise<unknown>, _options?: unknown) => callback(mockPrisma),
async (callback: (tx: typeof mockPrisma) => Promise<unknown>) => callback(mockPrisma),
);
}

async function buildApp(): Promise<FastifyInstance> {
const app = Fastify({ logger: false });
app.decorate('prisma', mockPrisma);
app.decorate('authenticate', async (request: FastifyRequest & { user?: { id: string } }) => {
async function buildApp():Promise<FastifyInstance> {
const app = Fastify({ logger: false });
app.decorate('prisma', mockPrisma as unknown as PrismaClient);
Expand Down Expand Up @@ -186,55 +182,6 @@ describe('POST /api/cards — link ownership validation', () => {

expect(res.statusCode).toBe(500);
});

it('wraps creation in a Serializable transaction to prevent race conditions', async () => {
mockPrisma.platformLink.findMany.mockResolvedValue([{ id: OWNED_LINK_ID }]);
mockPrisma.card.count.mockResolvedValue(0);
mockPrisma.card.create.mockResolvedValue({ ...mockCard, cardLinks: [] });

const app = await buildApp();
const res = await app.inject({
method: 'POST',
url: '/api/cards',
payload: { title: 'Test Card', linkIds: [OWNED_LINK_ID] },
});

expect(res.statusCode).toBe(201);
expect(mockPrisma.$transaction).toHaveBeenCalledWith(
expect.any(Function),
{ isolationLevel: 'Serializable' }
);
});

it('retries the transaction on P2034 serialization failure', async () => {
mockPrisma.platformLink.findMany.mockResolvedValue([]);

// First attempt fails with P2034 (serialization conflict)
// Second attempt succeeds
const error = new Error('Serialization failure') as Error & { code: string };
error.code = 'P2034';

// We mock $transaction to fail once, then succeed
mockPrisma.$transaction
.mockRejectedValueOnce(error)
.mockImplementationOnce(
async (callback: (tx: typeof mockPrisma) => Promise<unknown>) => callback(mockPrisma)
);

mockPrisma.card.count.mockResolvedValue(1); // second attempt sees count > 0
mockPrisma.card.create.mockResolvedValue({ ...mockCard, isDefault: false, cardLinks: [] });

const app = await buildApp();
const res = await app.inject({
method: 'POST',
url: '/api/cards',
payload: { title: 'Test Card', linkIds: [] },
});

expect(res.statusCode).toBe(201);
expect(res.json().isDefault).toBe(false);
expect(mockPrisma.$transaction).toHaveBeenCalledTimes(2);
});
});

// ─────────────────────────────────────────────────────────────────────────────
Expand Down Expand Up @@ -493,4 +440,4 @@ describe('PUT /api/cards/:id/default', () => {
expect(mockPrisma.card.updateMany).toHaveBeenCalled();
expect(mockPrisma.card.update).toHaveBeenCalled();
});
});
});
38 changes: 22 additions & 16 deletions apps/backend/src/routes/cards.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import * as cardService from '../services/cardService'
import { handleDbError } from '../utils/error.util.js';
import { createCardSchema, updateCardSchema } from '../utils/validators.js';
import * as cardService from '../services/cardService'

import type { CardResponse } from '../services/cardService';
import type { Card } from '@devcard/shared';
import type { Prisma } from '@prisma/client';
import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';


interface CreateCardBody {
title: string;
linkIds: string[];
Expand Down Expand Up @@ -39,7 +38,7 @@ interface CardLinkWithPlatform {
platformLink: PlatformLink;
}

interface CardWithLinks {
interface _CardWithLinks {
id: string;
userId: string;
title: string;
Expand All @@ -54,12 +53,12 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
const server = request.server as any;
if (typeof server?.authenticate === 'function') { await server.authenticate(request, reply); return }
if (typeof (app as any).authenticate === 'function') { await (app as any).authenticate(request, reply); return }
try { await request.jwtVerify() } catch (e) { reply.status(401).send({ error: 'Unauthorized' }) }
try { await request.jwtVerify() } catch (_e) { reply.status(401).send({ error: 'Unauthorized' }) }
});

// ─── List Cards ───

app.get('/', async (request: FastifyRequest, reply: FastifyReply): Promise<Card[] | void> => {
app.get('/', async (request: FastifyRequest, reply: FastifyReply): Promise<CardResponse[] | void> => {
const userId = (request.user as { id: string }).id;
try {
return await cardService.listCards(app, userId)
Expand All @@ -82,25 +81,25 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
const card = await cardService.createCard(app, userId, parsed.data)
return reply.status(201).send(card)
} catch (error: any) {
if (error?.code === 'OWNERSHIP') return reply.status(403).send({ error: 'One or more links do not belong to your account' })
if (error?.code === 'OWNERSHIP') {return reply.status(403).send({ error: 'One or more links do not belong to your account' })}
return handleDbError(error, request, reply)
}
});

// ─── Update Card ───

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

try {
const parsed = updateCardSchema.safeParse(request.body)
if (!parsed.success) return reply.status(400).send({ error: 'Validation failed', details: parsed.error.flatten() })
if (!parsed.success) {return reply.status(400).send({ error: 'Validation failed', details: parsed.error.flatten() })}
const updated = await cardService.updateCard(app, userId, id, parsed.data)
if (!updated) return reply.status(404).send({ error: 'Card not found' })
if (!updated) {return reply.status(404).send({ error: 'Card not found' })}
return updated
} catch (error: any) {
if (error?.code === 'OWNERSHIP') return reply.status(403).send({ error: 'One or more links do not belong to your account' })
if (error?.code === 'OWNERSHIP') {return reply.status(403).send({ error: 'One or more links do not belong to your account' })}
return handleDbError(error, request, reply)
}
});
Expand All @@ -112,11 +111,18 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {
const { id } = request.params;

try {
const res = await cardService.deleteCard(app, userId, id)
if (res && (res as any).code === 'NOT_FOUND') return reply.status(404).send({ error: 'Card not found' })
if (res && (res as any).code === 'LAST_CARD') return reply.status(400).send({ error: 'Cannot delete the last remaining card. A user must have at least one card.' })
await cardService.deleteCard(app, userId, id)
return reply.status(204).send()
} catch (error) {
} catch (error:any) {
if (error?.code === 'NOT_FOUND') {
return reply.status(404).send({ error: 'Card not found' });
}

if (error?.code === 'LAST_CARD') {
return reply.status(400).send({
error: 'Cannot delete the last remaining card. A user must have at least one card.',
});
}
return handleDbError(error, request, reply)
}
});
Expand All @@ -129,7 +135,7 @@ export async function cardRoutes(app: FastifyInstance): Promise<void> {

try {
const resp = await cardService.setDefaultCard(app, userId, id)
if (!resp) return reply.status(404).send({ error: 'Card not found' })
if (!resp) {return reply.status(404).send({ error: 'Card not found' })}
return resp
} catch (error) {
return handleDbError(error, request, reply)
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/services/cardService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { FastifyInstance } from 'fastify';

type CardLinkResponse = { platformLink: unknown };
type RawCard = { id: string; title: string; isDefault: boolean; cardLinks: CardLinkResponse[] };
type CardResponse = { id: string; title: string; isDefault: boolean; links: unknown[] };
export type CardResponse = { id: string; title: string; isDefault: boolean; links: unknown[] };

function mapCard(card: RawCard): CardResponse {
return {
Expand Down