|
| 1 | +import { applyDecorators } from '@nestjs/common'; |
| 2 | +import { |
| 3 | + ApiBadRequestResponse, |
| 4 | + ApiConflictResponse, |
| 5 | + ApiCreatedResponse, |
| 6 | + ApiForbiddenResponse, |
| 7 | + ApiNotFoundResponse, |
| 8 | + ApiOkResponse, |
| 9 | + ApiUnauthorizedResponse, |
| 10 | +} from '@nestjs/swagger'; |
| 11 | +import { Prisma } from '@prisma/client'; |
| 12 | + |
| 13 | +export type SwaggerParamsType = { |
| 14 | + description: string; |
| 15 | + example: Record<string, any>; |
| 16 | +}; |
| 17 | + |
| 18 | +export function CreatedResponseDecorator( |
| 19 | + description: string, |
| 20 | + example: Record<string, any>, |
| 21 | +) { |
| 22 | + return applyDecorators( |
| 23 | + ApiCreatedResponse({ |
| 24 | + description, |
| 25 | + example, |
| 26 | + }), |
| 27 | + ); |
| 28 | +} |
| 29 | + |
| 30 | +export function OkResponseDecorator( |
| 31 | + description: string, |
| 32 | + example: Record<string, any>, |
| 33 | +) { |
| 34 | + return applyDecorators( |
| 35 | + ApiOkResponse({ |
| 36 | + description, |
| 37 | + example, |
| 38 | + }), |
| 39 | + ); |
| 40 | +} |
| 41 | + |
| 42 | +export function UsuarioUnauthorizedResponseDecorator() { |
| 43 | + return applyDecorators( |
| 44 | + ApiUnauthorizedResponse({ |
| 45 | + description: 'Usuário não autenticado', |
| 46 | + example: { |
| 47 | + statusCode: 401, |
| 48 | + message: `Unauthorized`, |
| 49 | + }, |
| 50 | + }), |
| 51 | + ); |
| 52 | +} |
| 53 | + |
| 54 | +export function ValidationBadRequestResponseDecorator() { |
| 55 | + return applyDecorators( |
| 56 | + ApiBadRequestResponse({ |
| 57 | + example: { |
| 58 | + statusCode: 400, |
| 59 | + message: ['dto message error'], |
| 60 | + error: 'Bad Request', |
| 61 | + }, |
| 62 | + }), |
| 63 | + ); |
| 64 | +} |
| 65 | + |
| 66 | +export function ConflictResponseDecorator(text: string) { |
| 67 | + return applyDecorators( |
| 68 | + ApiConflictResponse({ |
| 69 | + description: text, |
| 70 | + example: { |
| 71 | + statusCode: 409, |
| 72 | + message: text, |
| 73 | + }, |
| 74 | + }), |
| 75 | + ); |
| 76 | +} |
| 77 | + |
| 78 | +export function NotFoundEntityResponseDecorator( |
| 79 | + entity: string, |
| 80 | + field: string = 'field_example', |
| 81 | +) { |
| 82 | + return applyDecorators( |
| 83 | + ApiNotFoundResponse({ |
| 84 | + description: `${entity} não encontrado`, |
| 85 | + example: { |
| 86 | + statusCode: 404, |
| 87 | + message: `Nenhum ${entity} encontrado com os atributos: [${field}: ${field}]`, |
| 88 | + }, |
| 89 | + }), |
| 90 | + ); |
| 91 | +} |
| 92 | + |
| 93 | +export function ForbiddenResponseDecorator(text: string) { |
| 94 | + return applyDecorators( |
| 95 | + ApiForbiddenResponse({ |
| 96 | + description: text, |
| 97 | + example: { statusCode: 403, message: text }, |
| 98 | + }), |
| 99 | + ); |
| 100 | +} |
| 101 | + |
| 102 | +export const userResponseExample = { |
| 103 | + id: 'user-123', |
| 104 | + username: 'joao', |
| 105 | + created_at: '2025-01-01T00:00:00.000Z', |
| 106 | + updated_at: '2025-01-01T00:00:00.000Z', |
| 107 | + deleted_at: null, |
| 108 | +} as const; |
| 109 | + |
| 110 | +export const collaboratorResponseExample = { |
| 111 | + id: 'collab-123', |
| 112 | + name: 'Maria Silva', |
| 113 | + user_id: 'user-123', |
| 114 | + created_at: '2025-01-01T00:00:00.000Z', |
| 115 | + updated_at: '2025-01-01T00:00:00.000Z', |
| 116 | + deleted_at: null, |
| 117 | + user: userResponseExample, |
| 118 | +} as const; |
| 119 | + |
| 120 | +export const projectResponseExample = { |
| 121 | + id: 'proj-123', |
| 122 | + name: 'Projeto XPTO', |
| 123 | + created_at: '2025-01-01T00:00:00.000Z', |
| 124 | + updated_at: '2025-01-01T00:00:00.000Z', |
| 125 | + deleted_at: null, |
| 126 | + tasks: [], |
| 127 | +} as const; |
| 128 | + |
| 129 | +export const taskResponseExample = { |
| 130 | + id: 'task-123', |
| 131 | + name: 'Implementar autenticação', |
| 132 | + description: 'Criar sistema de login com JWT', |
| 133 | + project_id: 'proj-123', |
| 134 | + created_at: '2025-01-01T00:00:00.000Z', |
| 135 | + updated_at: '2025-01-01T00:00:00.000Z', |
| 136 | + deleted_at: null, |
| 137 | + project: { |
| 138 | + id: 'proj-123', |
| 139 | + name: 'Projeto XPTO', |
| 140 | + }, |
| 141 | +} as const; |
| 142 | + |
| 143 | +export const timeTrackerResponseExample = { |
| 144 | + id: 'tt-123', |
| 145 | + start_date: '2025-01-15T08:00:00.000Z', |
| 146 | + end_date: '2025-01-15T17:00:00.000Z', |
| 147 | + timezone_id: 'America/Sao_Paulo', |
| 148 | + task_id: 'task-123', |
| 149 | + collaborator_id: 'collab-123', |
| 150 | + created_at: '2025-01-01T00:00:00.000Z', |
| 151 | + updated_at: '2025-01-01T00:00:00.000Z', |
| 152 | + deleted_at: null, |
| 153 | + tasks: { |
| 154 | + id: 'task-123', |
| 155 | + name: 'Implementar autenticação', |
| 156 | + }, |
| 157 | + collaborators: { |
| 158 | + id: 'collab-123', |
| 159 | + name: 'Maria Silva', |
| 160 | + }, |
| 161 | +} as const; |
0 commit comments