diff --git a/src/modules/appointments/schemas/index.ts b/src/modules/appointments/schemas/index.ts index 9b7e66d..f51bddf 100644 --- a/src/modules/appointments/schemas/index.ts +++ b/src/modules/appointments/schemas/index.ts @@ -1,8 +1,6 @@ -import { AppointmentType } from '@prisma/client'; +import { AppointmentStatus, AppointmentType } from '@prisma/client'; import z from 'zod'; -import { AppError } from '@app/errors/app-client'; - export const appointmentParamId = z.object({ patientId: z.string().uuid(), }); @@ -12,58 +10,25 @@ export const appointmentParamsSchema = z.object({ appointmentId: z.coerce.number().int(), }); -export const appointmentQuerySchema = z - .object({ - name: z.string().optional(), - page: z.coerce.number().default(1), - items_per_page: z.coerce.number().max(500).default(10), - appointment_type: z - .union([z.nativeEnum(AppointmentType), z.literal('')]) - .transform(val => (val === '' ? undefined : val)) - .optional(), - start_date: z.preprocess( - val => { - if (typeof val === 'string' && val.trim() === '') return undefined; - - if (typeof val === 'string' && !/^\d{4}-\d{2}-\d{2}$/.test(val)) { - throw new AppError( - 'Data de início inválida. Use o formato YYYY-MM-DD', - ); - } - return typeof val === 'string' ? new Date(val) : val; - }, - z - .date({ - invalid_type_error: - 'Data de início inválida. Use o formato YYYY-MM-DD', - }) - .optional(), - ), - end_date: z.preprocess( - val => { - if (typeof val === 'string' && val.trim() === '') return undefined; - - if (typeof val === 'string' && !/^\d{4}-\d{2}-\d{2}$/.test(val)) { - throw new AppError('Data de fim inválida. Use o formato YYYY-MM-DD'); - } - return typeof val === 'string' ? new Date(val) : val; - }, - z - .date({ - invalid_type_error: 'Data de fim inválida. Use o formato YYYY-MM-DD', - }) - .optional(), - ), - }) - .refine( - data => { - if (data.start_date && data.end_date) { - return data.start_date <= data.end_date; - } - return true; - }, - { - message: 'A data de início não pode ser maior que a data de fim', - path: ['start_date'], - }, - ); +export const appointmentQuerySchema = z.object({ + name: z.string().optional(), + appointment_type: z + .union([z.nativeEnum(AppointmentType), z.literal('')]) + .transform(val => (val === '' ? undefined : val)) + .optional(), + status: z + .union([z.nativeEnum(AppointmentStatus), z.literal('')]) + .transform(val => (val === '' ? undefined : val)) + .optional(), + scheduled_date: z.preprocess(val => { + if (typeof val === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(val)) { + return new Date(`${val}T00:00:00.000Z`); + } + if (val === '' || val === null || val === undefined) { + return undefined; + } + throw new Error('Data inválida. Use o formato YYYY-MM-DD.'); + }, z.date().optional()), + page: z.coerce.number().default(1), + items_per_page: z.coerce.number().max(500).default(10), +}); diff --git a/src/modules/appointments/useCases/get-all-appointments/get-all-appointments-service.ts b/src/modules/appointments/useCases/get-all-appointments/get-all-appointments-service.ts index 6bffb0b..1e76931 100644 --- a/src/modules/appointments/useCases/get-all-appointments/get-all-appointments-service.ts +++ b/src/modules/appointments/useCases/get-all-appointments/get-all-appointments-service.ts @@ -1,4 +1,4 @@ -import { AppointmentType } from '@prisma/client'; +import { AppointmentStatus, AppointmentType } from '@prisma/client'; import { IAppointment, IPaginateRequest } from '@shared/entities'; import { AppointmentRepository } from '@shared/repositories/implementations/appointment-repository'; @@ -6,9 +6,9 @@ import { validatePaginationParams } from '@shared/utils'; import { FindAndCountAll } from '@shared/utils/format-paginate'; type IGetAllAppointmentsParams = IPaginateRequest & { - end_date?: Date; - start_date?: Date; + scheduled_date?: Date; appointment_type?: AppointmentType; + status?: AppointmentStatus; }; export class GetAllAppointmentsService { diff --git a/src/modules/appointments/useCases/get-appointment/get-appointment-service.ts b/src/modules/appointments/useCases/get-appointment/get-appointment-service.ts index 27a8eb2..5f95c78 100644 --- a/src/modules/appointments/useCases/get-appointment/get-appointment-service.ts +++ b/src/modules/appointments/useCases/get-appointment/get-appointment-service.ts @@ -17,14 +17,15 @@ export class GetAppointmentService { this.patientRepository = patientRepository; } - async execute(queryId: number, patientId: string) { + async execute(appointmentId: number, patientId: string) { const patient = await this.patientRepository.findById(patientId); if (!patient) { throw new AppError(PATIENT_NOT_FOUND, 404); } - const appointment = await this.appointmentRepository.findById(queryId); + const appointment = + await this.appointmentRepository.findById(appointmentId); if (patientId !== appointment?.patient_id) { throw new AppError(APPOINTMENT_NOT_FOUND, 404); diff --git a/src/modules/appointments/useCases/get-appointments-by-patient/get-appointments-by-patient-service.ts b/src/modules/appointments/useCases/get-appointments-by-patient/get-appointments-by-patient-service.ts index 551425b..580a9e6 100644 --- a/src/modules/appointments/useCases/get-appointments-by-patient/get-appointments-by-patient-service.ts +++ b/src/modules/appointments/useCases/get-appointments-by-patient/get-appointments-by-patient-service.ts @@ -1,4 +1,4 @@ -import { AppointmentType } from '@prisma/client'; +import { AppointmentStatus, AppointmentType } from '@prisma/client'; import { AppError } from '@app/errors/app-client'; import { PATIENT_NOT_FOUND } from '@shared/constants/messages'; @@ -12,9 +12,9 @@ import { FindAndCountAll } from '@shared/utils/format-paginate'; type IGetAppointmentsByPatientParams = Omit & { patient_id: string; - end_date?: Date; - start_date?: Date; + scheduled_date?: Date; appointment_type?: AppointmentType; + status?: AppointmentStatus; }; export class GetAppointmentsByPatientService { diff --git a/src/shared/entities/entities.ts b/src/shared/entities/entities.ts index ed789c1..2da7764 100644 --- a/src/shared/entities/entities.ts +++ b/src/shared/entities/entities.ts @@ -1,4 +1,4 @@ -import { AppointmentType, Status } from '@prisma/client'; +import { AppointmentStatus, AppointmentType, Status } from '@prisma/client'; export interface FindEntitiesAndCountParams { name?: string; @@ -14,16 +14,16 @@ export interface FindEntitiesAndCountResult { export interface FindAppointmentsAndCountParams extends Omit { patient_id: string; + scheduled_date?: Date; appointment_type?: AppointmentType; - start_date?: Date; - end_date?: Date; + status?: AppointmentStatus; } export interface FindAllAppointmentsAndCountParams extends FindEntitiesAndCountParams { + scheduled_date?: Date; appointment_type?: AppointmentType; - start_date?: Date; - end_date?: Date; + status?: AppointmentStatus; } export interface FindAllPatientsAndCountParams diff --git a/src/shared/repositories/implementations/appointment-repository.ts b/src/shared/repositories/implementations/appointment-repository.ts index ec459c9..831410a 100644 --- a/src/shared/repositories/implementations/appointment-repository.ts +++ b/src/shared/repositories/implementations/appointment-repository.ts @@ -15,14 +15,18 @@ export class AppointmentRepository implements IAppointmentRepository { async findAllAppointments( params: FindAllAppointmentsAndCountParams, ): Promise> { - const { name, skip, take, appointment_type, start_date, end_date } = params; + const { name, skip, take, appointment_type, scheduled_date, status } = + params; const where: Prisma.AppointmentWhereInput = { ...(appointment_type && { appointment_type }), - scheduled_date: { - ...(start_date && { gte: start_date }), - ...(end_date && { lte: end_date }), - }, + ...(scheduled_date && { + scheduled_date: { + gte: scheduled_date, + lt: new Date(scheduled_date.getTime() + 24 * 60 * 60 * 1000), // Adiciona 1 dia + }, + }), + ...(status && { status }), patient: { ...(name && { name: { contains: name } }), }, @@ -51,16 +55,19 @@ export class AppointmentRepository implements IAppointmentRepository { async findAndCountAll( params: FindAppointmentsAndCountParams, ): Promise> { - const { patient_id, skip, take, appointment_type, start_date, end_date } = + const { patient_id, skip, take, appointment_type, scheduled_date, status } = params; const where: Prisma.AppointmentWhereInput = { patient_id: patient_id, ...(appointment_type && { appointment_type }), - scheduled_date: { - ...(start_date && { gte: start_date }), - ...(end_date && { lte: end_date }), - }, + ...(scheduled_date && { + scheduled_date: { + gte: scheduled_date, + lt: new Date(scheduled_date.getTime() + 24 * 60 * 60 * 1000), // Adiciona 1 dia + }, + }), + ...(status && { status }), }; const count = await prisma.appointment.count({