|
1 | | -import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'; |
2 | | -import { ZodError } from 'zod'; |
3 | | -import { createSchema } from 'zod-openapi'; |
| 1 | +import type { FastifyInstance } from 'fastify'; |
4 | 2 | import { TeamService } from '../../services/teamService'; |
5 | 3 | import { requirePermission } from '../../middleware/roleMiddleware'; |
6 | | -import { CreateTeamSchema, TeamResponseSchema, ErrorResponseSchema, type CreateTeamInput } from './schemas'; |
| 4 | +import { |
| 5 | + CREATE_TEAM_REQUEST_SCHEMA, |
| 6 | + TEAM_SUCCESS_RESPONSE_SCHEMA, |
| 7 | + ERROR_RESPONSE_SCHEMA, |
| 8 | + type CreateTeamRequest, |
| 9 | + type TeamSuccessResponse, |
| 10 | + type ErrorResponse |
| 11 | +} from './schemas'; |
7 | 12 |
|
8 | | -export default async function createTeamRoute(fastify: FastifyInstance) { |
| 13 | +export default async function createTeamRoute(server: FastifyInstance) { |
9 | 14 | // POST /teams - Create a new team |
10 | | - fastify.post<{ Body: CreateTeamInput }>('/teams', { |
| 15 | + server.post('/teams', { |
| 16 | + preValidation: requirePermission('teams.create'), |
11 | 17 | schema: { |
12 | 18 | tags: ['Teams'], |
13 | 19 | summary: 'Create new team', |
14 | | - description: 'Creates a new team with the specified name and description. Users can create up to 3 teams maximum. The slug is automatically generated from the team name and made unique.', |
| 20 | + description: 'Creates a new team with the specified name and description. Users can create up to 3 teams maximum. The slug is automatically generated from the team name and made unique. Requires Content-Type: application/json header when sending request body.', |
15 | 21 | security: [{ cookieAuth: [] }], |
16 | | - body: createSchema(CreateTeamSchema), |
| 22 | + |
| 23 | + // Fastify validation schema |
| 24 | + body: CREATE_TEAM_REQUEST_SCHEMA, |
| 25 | + |
| 26 | + // OpenAPI documentation (same schema, reused) |
| 27 | + requestBody: { |
| 28 | + required: true, |
| 29 | + content: { |
| 30 | + 'application/json': { |
| 31 | + schema: CREATE_TEAM_REQUEST_SCHEMA |
| 32 | + } |
| 33 | + } |
| 34 | + }, |
| 35 | + |
17 | 36 | response: { |
18 | | - 201: createSchema(TeamResponseSchema.describe('Team created successfully')), |
19 | | - 400: createSchema(ErrorResponseSchema.describe('Bad Request - Validation error or team limit reached')), |
20 | | - 401: createSchema(ErrorResponseSchema.describe('Unauthorized - Authentication required')), |
21 | | - 403: createSchema(ErrorResponseSchema.describe('Forbidden - Insufficient permissions')), |
22 | | - 500: createSchema(ErrorResponseSchema.describe('Internal Server Error')) |
| 37 | + 201: { |
| 38 | + ...TEAM_SUCCESS_RESPONSE_SCHEMA, |
| 39 | + description: 'Team created successfully' |
| 40 | + }, |
| 41 | + 400: { |
| 42 | + ...ERROR_RESPONSE_SCHEMA, |
| 43 | + description: 'Bad Request - Validation error or team limit reached' |
| 44 | + }, |
| 45 | + 401: { |
| 46 | + ...ERROR_RESPONSE_SCHEMA, |
| 47 | + description: 'Unauthorized - Authentication required' |
| 48 | + }, |
| 49 | + 403: { |
| 50 | + ...ERROR_RESPONSE_SCHEMA, |
| 51 | + description: 'Forbidden - Insufficient permissions' |
| 52 | + }, |
| 53 | + 500: { |
| 54 | + ...ERROR_RESPONSE_SCHEMA, |
| 55 | + description: 'Internal Server Error' |
| 56 | + } |
23 | 57 | } |
24 | 58 | }, |
25 | | - preValidation: requirePermission('teams.create'), |
26 | | - }, async (request: FastifyRequest<{ Body: CreateTeamInput }>, reply: FastifyReply) => { |
| 59 | + }, async (request, reply) => { |
27 | 60 | try { |
28 | 61 | if (!request.user) { |
29 | | - return reply.status(401).send({ |
| 62 | + const errorResponse: ErrorResponse = { |
30 | 63 | success: false, |
31 | | - error: 'Authentication required', |
32 | | - }); |
| 64 | + error: 'Authentication required' |
| 65 | + }; |
| 66 | + const jsonString = JSON.stringify(errorResponse); |
| 67 | + return reply.status(401).type('application/json').send(jsonString); |
33 | 68 | } |
34 | 69 |
|
35 | | - // Validate request body |
36 | | - const validatedData = CreateTeamSchema.parse(request.body); |
| 70 | + // TypeScript type assertion (Fastify has already validated) |
| 71 | + const { name, description } = request.body as CreateTeamRequest; |
37 | 72 |
|
38 | 73 | // Check if user can create more teams (3 team limit) |
39 | 74 | const canCreate = await TeamService.canUserCreateTeam(request.user.id); |
40 | 75 | if (!canCreate) { |
41 | | - return reply.status(400).send({ |
| 76 | + const errorResponse: ErrorResponse = { |
42 | 77 | success: false, |
43 | | - error: 'You have reached the maximum limit of 3 teams', |
44 | | - }); |
| 78 | + error: 'You have reached the maximum limit of 3 teams' |
| 79 | + }; |
| 80 | + const jsonString = JSON.stringify(errorResponse); |
| 81 | + return reply.status(400).type('application/json').send(jsonString); |
45 | 82 | } |
46 | 83 |
|
47 | 84 | // Create the team |
48 | 85 | const team = await TeamService.createTeam({ |
49 | | - name: validatedData.name, |
50 | | - description: validatedData.description, |
| 86 | + name, |
| 87 | + description, |
51 | 88 | owner_id: request.user.id, |
52 | 89 | }); |
53 | 90 |
|
54 | | - return reply.status(201).send({ |
| 91 | + const successResponse: TeamSuccessResponse = { |
55 | 92 | success: true, |
56 | 93 | data: team, |
57 | | - message: 'Team created successfully', |
58 | | - }); |
| 94 | + message: 'Team created successfully' |
| 95 | + }; |
| 96 | + const jsonString = JSON.stringify(successResponse); |
| 97 | + return reply.status(201).type('application/json').send(jsonString); |
59 | 98 | } catch (error) { |
60 | | - if (error instanceof ZodError) { |
61 | | - return reply.status(400).send({ |
62 | | - success: false, |
63 | | - error: 'Validation error', |
64 | | - details: error.issues, |
65 | | - }); |
66 | | - } |
67 | | - |
68 | 99 | if (error instanceof Error) { |
69 | 100 | // Handle specific team creation errors |
70 | 101 | if (error.message.includes('slug')) { |
71 | | - return reply.status(400).send({ |
| 102 | + const errorResponse: ErrorResponse = { |
72 | 103 | success: false, |
73 | | - error: 'Team name conflicts with existing team', |
74 | | - }); |
| 104 | + error: 'Team name conflicts with existing team' |
| 105 | + }; |
| 106 | + const jsonString = JSON.stringify(errorResponse); |
| 107 | + return reply.status(400).type('application/json').send(jsonString); |
75 | 108 | } |
76 | 109 | } |
77 | 110 |
|
78 | | - fastify.log.error(error, 'Error creating team'); |
79 | | - return reply.status(500).send({ |
| 111 | + server.log.error(error, 'Error creating team'); |
| 112 | + const errorResponse: ErrorResponse = { |
80 | 113 | success: false, |
81 | | - error: 'Failed to create team', |
82 | | - }); |
| 114 | + error: 'Failed to create team' |
| 115 | + }; |
| 116 | + const jsonString = JSON.stringify(errorResponse); |
| 117 | + return reply.status(500).type('application/json').send(jsonString); |
83 | 118 | } |
84 | 119 | }); |
85 | 120 | } |
0 commit comments