Skip to content
Merged
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
39 changes: 17 additions & 22 deletions apps/backend/src/routes/cards.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';

import { createCardSchema, updateCardSchema } from '../utils/validators.js';

export async function cardRoutes(app: FastifyInstance) {
export async function cardRoutes(app: FastifyInstance): Promise<void> {
app.addHook('preHandler', app.authenticate);

// ─── List Cards ───

app.get('/', async (request: FastifyRequest, reply: FastifyReply) => {
const userId = (request.user as any).id;
app.get('/', async (request: FastifyRequest): Promise<object> => {
const userId = (request.user as { id: string }).id;

const cards = await app.prisma.card.findMany({
where: { userId },
Expand All @@ -30,15 +31,14 @@ export async function cardRoutes(app: FastifyInstance) {

// ─── Create Card ───

app.post('/', async (request: FastifyRequest, reply: FastifyReply) => {
const userId = (request.user as any).id;
app.post('/', async (request: FastifyRequest, reply: FastifyReply): Promise<object> => {
const userId = (request.user as { id: string }).id;
const parsed = createCardSchema.safeParse(request.body);

if (!parsed.success) {
return reply.status(400).send({ error: 'Validation failed', details: parsed.error.flatten() });
}

// Check if user's first card → make it default
const cardCount = await app.prisma.card.count({ where: { userId } });

const card = await app.prisma.card.create({
Expand Down Expand Up @@ -71,8 +71,8 @@ export async function cardRoutes(app: FastifyInstance) {

// ─── Update Card ───

app.put('/:id', async (request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply) => {
const userId = (request.user as any).id;
app.put('/:id', async (request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply): Promise<object> => {
const userId = (request.user as { id: string }).id;
const { id } = request.params;

const existing = await app.prisma.card.findFirst({
Expand All @@ -88,17 +88,14 @@ export async function cardRoutes(app: FastifyInstance) {
return reply.status(400).send({ error: 'Validation failed', details: parsed.error.flatten() });
}

// Update card title
if (parsed.data.title) {
await app.prisma.card.update({
where: { id },
data: { title: parsed.data.title },
});
}

// Update card links if provided
if (parsed.data.linkIds) {
// Remove existing links
await app.prisma.cardLink.deleteMany({ where: { cardId: id } });


Expand All @@ -112,7 +109,6 @@ export async function cardRoutes(app: FastifyInstance) {
});
}

// Fetch updated card
const updated = await app.prisma.card.findUnique({
where: { id },
include: {
Expand All @@ -133,26 +129,27 @@ export async function cardRoutes(app: FastifyInstance) {

// ─── Delete Card ───

app.delete('/:id', async (request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply) => {
const userId = (request.user as any).id;
app.delete('/:id', async (request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply): Promise<void> => {
const userId = (request.user as { id: string }).id;
const { id } = request.params;

const existing = await app.prisma.card.findFirst({
where: { id, userId },
});

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

await app.prisma.card.delete({ where: { id } });
return reply.status(204).send();
reply.status(204).send();
});

// ─── Set Default Card ───

app.put('/:id/default', async (request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply) => {
const userId = (request.user as any).id;
app.put('/:id/default', async (request: FastifyRequest<{ Params: { id: string } }>, reply: FastifyReply): Promise<object> => {
const userId = (request.user as { id: string }).id;
const { id } = request.params;

const existing = await app.prisma.card.findFirst({
Expand All @@ -163,18 +160,16 @@ export async function cardRoutes(app: FastifyInstance) {
return reply.status(404).send({ error: 'Card not found' });
}

// Unset all other defaults
await app.prisma.card.updateMany({
where: { userId },
data: { isDefault: false },
});

// Set this one
await app.prisma.card.update({
where: { id },
data: { isDefault: true },
});

return { message: 'Default card updated' };
});
}
}