Skip to content

Commit 01873b8

Browse files
authored
fix(event): remove all any casts — typed authenticate, user.id, and Prisma error narrowing (#595)
* fix(event): remove all �ny casts, use typed authenticate and Prisma narrowing - Replace 3× equest.server as any / (app as any).authenticate preHandler boilerplate with �pp.authenticate — already declared on FastifyInstance in src/types/fastify.d.ts, consistent with follow.ts / team.ts pattern - Replace 3× (request.user as any).id with equest.user.id — covered by the FastifyJWT augmentation (user: { id: string; username: string }) - Replace 2× catch (error: any) with catch (error: unknown) + instanceof Prisma.PrismaClientKnownRequestError narrowing before .code access (P2002 join-duplicate → 409, P2025 leave-not-found → 404) - Replace (error as Error).message with getErrorMessage(error) from the shared error utility already used in follow.ts / connect.ts - Update event.test.ts: construct Prisma error mocks with ew Prisma.PrismaClientKnownRequestError(...) so instanceof narrowing resolves correctly in tests; import Prisma alongside PrismaClient * fixed * fixed again * fix: resolve lint issues in event routes and tests --------- Signed-off-by: Srejoye Saha <sahasrejoye2005@gmail.com>
1 parent affea61 commit 01873b8

2 files changed

Lines changed: 214 additions & 211 deletions

File tree

apps/backend/src/__tests__/event.test.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import Fastify from 'fastify';
1+
import { type PrismaClient, Prisma } from '@prisma/client';
2+
import Fastify, { type FastifyInstance } from 'fastify';
23
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
34

45
import { eventRoutes } from '../routes/event';
56

6-
import type { PrismaClient } from '@prisma/client';
7-
import type { FastifyInstance,LightMyRequestResponse } from 'fastify';
87

98
// ─── Shared mock data ────────────────────────────────────────────────────────
109

@@ -107,7 +106,7 @@ async function createEvent(
107106
app: FastifyInstance,
108107
body: Record<string, unknown>,
109108
authenticated = true,
110-
): Promise<LightMyRequestResponse> {
109+
): Promise<Awaited<ReturnType<FastifyInstance['inject']>>> {
111110
return app.inject({
112111
method: 'POST',
113112
url: '/api/events',
@@ -367,9 +366,10 @@ describe('Events API', () => {
367366
it('409 — returns 409 when user already joined the event', async () => {
368367
prismaMock.event.findUnique.mockResolvedValue(MOCK_EVENT);
369368
// Prisma unique constraint error
370-
const uniqueError = Object.assign(new Error('Unique constraint'), {
371-
code: 'P2002',
372-
});
369+
const uniqueError = new Prisma.PrismaClientKnownRequestError(
370+
'Unique constraint failed',
371+
{ code: 'P2002', clientVersion: '6.0.0' },
372+
);
373373
prismaMock.eventAttendee.create.mockRejectedValue(uniqueError);
374374

375375
const res = await app.inject({
@@ -452,9 +452,10 @@ describe('Events API', () => {
452452
it('404 — returns 404 when user was never an attendee (P2025)', async () => {
453453
prismaMock.event.findUnique.mockResolvedValue(MOCK_EVENT);
454454
// Prisma record-not-found error
455-
const notFoundError = Object.assign(new Error('Record not found'), {
456-
code: 'P2025',
457-
});
455+
const notFoundError = new Prisma.PrismaClientKnownRequestError(
456+
'Record not found',
457+
{ code: 'P2025', clientVersion: '6.0.0' },
458+
);
458459
prismaMock.eventAttendee.delete.mockRejectedValue(notFoundError);
459460

460461
const res = await app.inject({
@@ -488,13 +489,13 @@ describe('Events API', () => {
488489
/** Builds a raw EventAttendee row as Prisma returns it (with nested user) */
489490
function makeAttendeeRow(
490491
profile: typeof MOCK_USER_PROFILE | typeof MOCK_OTHER_USER_PROFILE,
491-
) : {
492-
id: string;
493-
userId: string;
494-
eventId: string;
495-
joinedAt: Date;
496-
user: typeof MOCK_USER_PROFILE | typeof MOCK_OTHER_USER_PROFILE;
497-
} {
492+
): {
493+
id: string;
494+
userId: string;
495+
eventId: string;
496+
joinedAt: Date;
497+
user: typeof profile;
498+
} {
498499
return {
499500
id: `attendee-${profile.id}`,
500501
userId: profile.id,

0 commit comments

Comments
 (0)