Skip to content

Commit 5b06033

Browse files
committed
fix: improve type safety in team.ts, remove any usages (closes #554)
1 parent f6e043e commit 5b06033

1 file changed

Lines changed: 23 additions & 33 deletions

File tree

‎apps/backend/src/routes/team.ts‎

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {generateUniqueSlug} from '../utils/slug'
55
import { createTeamScehma,inviteMembers,updateTeam } from '../validations/team.validation';
66

77
import type {PlatformLink, PublicProfile} from '@devcard/shared'
8-
import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
8+
import type { FastifyInstance } from 'fastify';
99

1010
type TeamMember = PublicProfile & {
1111
teamRole: TeamRole
@@ -24,16 +24,11 @@ type TeamProfile = {
2424
members: TeamMember[];
2525
}
2626

27-
export async function teamRoutes(app:FastifyInstance){
28-
app.post('/', { preHandler: [async (request, reply) => {
29-
const server = request.server as any;
30-
if (typeof server?.authenticate === 'function') { await server.authenticate(request, reply); return }
31-
if (typeof (app as any).authenticate === 'function') { await (app as any).authenticate(request, reply); return }
32-
try { const payload = await request.jwtVerify(); if (payload) (request as any).user = payload; } catch (e) { reply.status(401).send({ error: 'Unauthorized' }) }
33-
}] }, async(request:FastifyRequest<{
34-
Body: {name: string, description? : string, avatarUrl?: string }
35-
}>, reply: FastifyReply) => {
36-
const userId = (request.user as any).id;
27+
export async function teamRoutes(app: FastifyInstance): Promise<void> {
28+
app.post<{
29+
Body: {name: string, description? : string, avatarUrl?: string }
30+
}>('/',{ preHandler: [app.authenticate.bind(app)] }, async (request, reply): Promise<void> => {
31+
const userId = request.user.id;
3732
const parsed = createTeamScehma.safeParse(request.body);
3833
if(!parsed.success){
3934
return reply.status(400).send({error: 'Bad request'})
@@ -48,7 +43,7 @@ export async function teamRoutes(app:FastifyInstance){
4843

4944
try {
5045
const team = await app.prisma.$transaction(async (tx) => {
51-
const team = await tx.team.create({
46+
const createdTeam = await tx.team.create({
5247
data: {
5348
name,
5449
slug: finalSlug,
@@ -60,13 +55,13 @@ export async function teamRoutes(app:FastifyInstance){
6055

6156
await tx.teamMember.create({
6257
data: {
63-
teamId : team.id,
58+
teamId : createdTeam.id,
6459
userId,
6560
role: TeamRole.OWNER,
6661
joinedAt: new Date(),
6762
}
6863
})
69-
return team
64+
return createdTeam
7065
})
7166
return reply.status(201).send(team)
7267

@@ -91,7 +86,7 @@ export async function teamRoutes(app:FastifyInstance){
9186
}
9287
})
9388

94-
app.get('/:slug', async(request: FastifyRequest<{Params: {slug: string}}>, reply: FastifyReply) => {
89+
app.get<{Params: {slug: string}}>('/:slug', async (request, reply): Promise<void> => {
9590
const paramsSlug = request.params.slug;
9691

9792
try {
@@ -149,22 +144,17 @@ export async function teamRoutes(app:FastifyInstance){
149144
members
150145
}
151146

152-
return response;
147+
return reply.status(200).send(response);
153148
} catch (error) {
154149
app.log.error(error);
155150
return reply.status(500).send('Database query failed')
156151
}
157152

158153
})
159154

160-
app.post('/:slug/members', { preHandler: [async (request, reply) => {
161-
const server = request.server as any;
162-
if (typeof server?.authenticate === 'function') { await server.authenticate(request, reply); return }
163-
if (typeof (app as any).authenticate === 'function') { await (app as any).authenticate(request, reply); return }
164-
try { const payload = await request.jwtVerify(); if (payload) (request as any).user = payload; } catch (e) { reply.status(401).send({ error: 'Unauthorized' }) }
165-
}] }, async(request: FastifyRequest<{Params: {slug:string}, Body:{username:string}}>, reply: FastifyReply) => {
155+
app.post<{Params: {slug:string}, Body:{username:string}}>('/:slug/members', { preHandler: [app.authenticate.bind(app)] }, async (request, reply): Promise<void> => {
166156
const paramsSlug = request.params.slug;
167-
const userId = (request.user as any).id;
157+
const userId = request.user.id;
168158
const parsed = inviteMembers.safeParse(request.body);
169159
if(!parsed.success){
170160
return reply.status(400).send({error: 'Bad request'})
@@ -224,10 +214,10 @@ export async function teamRoutes(app:FastifyInstance){
224214
}
225215
})
226216

227-
app.delete('/:slug/members/:userId', { preHandler: [async (request, reply) => { 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' }) } }] }, async(request: FastifyRequest<{Params: {slug: string, userId: string}}>, reply: FastifyReply) => {
217+
app.delete<{Params: {slug: string, userId: string}}>('/:slug/members/:userId',{ preHandler: [app.authenticate.bind(app)] }, async (request, reply): Promise<void> => {
228218
const paramsSlug = request.params.slug
229219
const paramsUserId = request.params.userId
230-
const userID = (request.user as any).id;
220+
const userId = request.user.id;
231221
const teamDetails = await app.prisma.team.findUnique(
232222
{where: {slug: paramsSlug},
233223
include: {
@@ -251,8 +241,8 @@ export async function teamRoutes(app:FastifyInstance){
251241
});
252242
}
253243

254-
const isOwner = teamDetails.ownerId === userID;
255-
const isSelfRemove = paramsUserId === userID;
244+
const isOwner = teamDetails.ownerId === userId;
245+
const isSelfRemove = paramsUserId === userId;
256246

257247
if (!isOwner && !isSelfRemove) {
258248
return reply.status(403).send({
@@ -286,8 +276,8 @@ export async function teamRoutes(app:FastifyInstance){
286276
}
287277
})
288278

289-
app.patch('/:slug',{ preHandler: [async (request, reply) => { 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' }) } }] }, async(request: FastifyRequest<{Params: {slug: string},Body: {description?:string, name?:string, avatarUrl?:string}}>, reply: FastifyReply) => {
290-
const userId = (request.user as any).id;
279+
app.patch<{Params: {slug: string},Body: {description?:string, name?:string, avatarUrl?:string}}>('/:slug',{ preHandler: [app.authenticate.bind(app)] }, async (request, reply): Promise<void> => {
280+
const userId = request.user.id;
291281
const paramsSlug = request.params.slug;
292282
const parsed = updateTeam.safeParse(request.body);
293283
if(!parsed.success){
@@ -328,8 +318,8 @@ export async function teamRoutes(app:FastifyInstance){
328318

329319
})
330320

331-
app.delete('/:slug',{ preHandler: [async (request, reply) => { 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' }) } }] }, async(request:FastifyRequest<{Params:{slug: string}}>, reply:FastifyReply) => {
332-
const userId = (request.user as any).id;
321+
app.delete<{Params:{slug: string}}>('/:slug',{ preHandler: [app.authenticate.bind(app)] }, async (request, reply): Promise<void> => {
322+
const userId = request.user.id;
333323
const paramsSlug = request.params.slug;
334324

335325

@@ -364,7 +354,7 @@ export async function teamRoutes(app:FastifyInstance){
364354
}
365355
})
366356

367-
app.get('/:slug/qr',async(request:FastifyRequest<{Params:{slug:string}}>, reply: FastifyReply) => {
357+
app.get<{Params:{slug:string}}>('/:slug/qr', async (request, reply): Promise<void> => {
368358
const paramsSlug = request.params.slug;
369359
try {
370360
const teamDetails = await app.prisma.team.findUnique({
@@ -386,4 +376,4 @@ export async function teamRoutes(app:FastifyInstance){
386376
}
387377

388378
})
389-
}
379+
}

0 commit comments

Comments
 (0)