|
1 | | -import type { NextRequest } from 'next/server'; |
2 | | -import { NextResponse } from 'next/server'; |
3 | | -import prisma from '@lib/prisma'; |
4 | | -import { hash } from 'bcrypt'; |
| 1 | +import 'reflect-metadata'; |
| 2 | +import type { NextRequest, NextResponse } from 'next/server'; |
| 3 | +import type { HttpResponseDto } from '@shared/dto/output/responses/abstract/http.response.dto'; |
| 4 | +import type { RegisterDataDto } from '@shared/dto/output/data/register.data.dto'; |
| 5 | +import type { RegisterPayloadDto } from '@shared/dto/input/payloads/register.payload.dto'; |
| 6 | +import { container } from 'tsyringe'; |
| 7 | +import { handleApiRequest } from '@api/helpers/api/handle-api-request'; |
5 | 8 | import { StatusCodes } from 'http-status-codes'; |
6 | | -import type { User } from '@prisma/generated'; |
7 | | -/** |
8 | | - * @swagger |
9 | | - * tags: |
10 | | - * - name: Auth |
11 | | - * description: Authentication and user management |
12 | | - */ |
| 9 | +import type { UserModelDto } from '@shared/dto/models/user.model.dto'; |
| 10 | +import { RegisterUseCase } from '@api/usecases/auth/register.usecase'; |
13 | 11 |
|
14 | 12 | /** |
15 | 13 | * @swagger |
16 | 14 | * /api/auth/register: |
17 | 15 | * post: |
18 | | - * summary: Register a new user |
19 | 16 | * tags: [Auth] |
| 17 | + * summary: Register a new user |
20 | 18 | * requestBody: |
21 | | - * description: JSON object containing email and password |
22 | 19 | * required: true |
23 | 20 | * content: |
24 | 21 | * application/json: |
25 | 22 | * schema: |
26 | | - * type: object |
27 | | - * required: |
28 | | - * - email |
29 | | - * - password |
30 | | - * properties: |
31 | | - * email: |
32 | | - * type: string |
33 | | - * format: email |
34 | | - * example: user@example.com |
35 | | - * password: |
36 | | - * type: string |
37 | | - * format: password |
38 | | - * example: securePassword123 |
| 23 | + * $ref: '#/components/schemas/RegisterPayloadDto' |
39 | 24 | * responses: |
40 | 25 | * 201: |
41 | 26 | * description: User created successfully |
42 | 27 | * content: |
43 | 28 | * application/json: |
44 | 29 | * schema: |
45 | | - * $ref: '#/components/schemas/User' |
46 | | - * 400: |
47 | | - * description: Bad request (missing email or password) |
| 30 | + * $ref: '#/components/schemas/RegisterDataDto' |
48 | 31 | * 409: |
49 | | - * description: Conflict user already exists |
| 32 | + * description: User already exists |
| 33 | + * content: |
| 34 | + * application/json: |
| 35 | + * schema: |
| 36 | + * $ref: '#/components/schemas/BusinessErrorDto' |
50 | 37 | * 500: |
51 | 38 | * description: Internal server error |
52 | | - */ |
53 | | - |
54 | | -/** |
55 | | - * @swagger |
56 | | - * components: |
57 | | - * schemas: |
58 | | - * User: |
59 | | - * type: object |
60 | | - * properties: |
61 | | - * id: |
62 | | - * type: string |
63 | | - * description: Unique identifier generated by the database |
64 | | - * example: 'cl0x1a2b3c4d5e6f7g8h9' |
65 | | - * email: |
66 | | - * type: string |
67 | | - * format: email |
68 | | - * description: User's email address |
69 | | - * example: user@example.com |
| 39 | + * content: |
| 40 | + * application/json: |
| 41 | + * schema: |
| 42 | + * $ref: '#/components/schemas/HttpErrorDto' |
70 | 43 | */ |
71 | 44 | export async function POST( |
72 | | - req: NextRequest |
73 | | -): Promise< |
74 | | - | NextResponse<{ message: string }> |
75 | | - | NextResponse<{ id: string; email: string }> |
76 | | -> { |
77 | | - try { |
78 | | - const { email, password } = await req.json(); |
79 | | - if (!email || !password) { |
80 | | - return NextResponse.json( |
81 | | - { message: 'Email and password are required' }, |
82 | | - { status: StatusCodes.BAD_REQUEST } |
83 | | - ); |
84 | | - } |
85 | | - const exists: User | null = await prisma.user.findUnique({ |
86 | | - where: { email }, |
87 | | - }); |
88 | | - if (exists) { |
89 | | - return NextResponse.json( |
90 | | - { message: 'User already exists' }, |
91 | | - { status: StatusCodes.CONFLICT } |
92 | | - ); |
93 | | - } |
94 | | - const salt: number = parseInt(process.env.BCRYPT_SALT_ROUNDS || '10', 10); |
95 | | - const hashed: string = await hash(password, salt); |
96 | | - const user: User = await prisma.user.create({ |
97 | | - data: { email, password: hashed }, |
98 | | - }); |
99 | | - return NextResponse.json( |
100 | | - { id: user.id, email: user.email }, |
101 | | - { status: StatusCodes.CREATED } |
102 | | - ); |
103 | | - } catch { |
104 | | - return NextResponse.json( |
105 | | - { message: 'Internal server error' }, |
106 | | - { status: StatusCodes.INTERNAL_SERVER_ERROR } |
107 | | - ); |
108 | | - } |
| 45 | + request: NextRequest |
| 46 | +): Promise<NextResponse<HttpResponseDto<RegisterDataDto>>> { |
| 47 | + const payload: RegisterPayloadDto = await request.json(); |
| 48 | + const registerUseCase: RegisterUseCase = container.resolve(RegisterUseCase); |
| 49 | + return await handleApiRequest<RegisterDataDto>(async () => { |
| 50 | + const userCreated: UserModelDto = await registerUseCase.handle(payload); |
| 51 | + const response: RegisterDataDto = { userCreated }; |
| 52 | + return response; |
| 53 | + }, StatusCodes.CREATED); |
109 | 54 | } |
0 commit comments