1- import { BadRequestException , Injectable , Logger } from '@nestjs/common' ;
1+ import { Injectable , Logger , NotFoundException } from '@nestjs/common' ;
22import { InjectRepository } from '@nestjs/typeorm' ;
33import type { Repository } from 'typeorm' ;
44
55import { Appointment } from '@/domain/entities/appointment' ;
66import { Patient } from '@/domain/entities/patient' ;
7+ import { User } from '@/domain/entities/user' ;
78
89import type { AuthUserDto } from '../../auth/auth.dtos' ;
910import type { CreateAppointmentDto } from '../appointments.dtos' ;
1011
1112interface CreateAppointmentUseCaseInput {
12- createAppointmentDto : CreateAppointmentDto ;
1313 user : AuthUserDto ;
14+ createAppointmentDto : CreateAppointmentDto ;
1415}
1516
1617@Injectable ( )
@@ -22,42 +23,48 @@ export class CreateAppointmentUseCase {
2223 private readonly appointmentsRepository : Repository < Appointment > ,
2324 @InjectRepository ( Patient )
2425 private readonly patientsRepository : Repository < Patient > ,
26+ @InjectRepository ( User )
27+ private readonly usersRepository : Repository < User > ,
2528 ) { }
2629
2730 async execute ( {
2831 createAppointmentDto,
2932 user,
3033 } : CreateAppointmentUseCaseInput ) : Promise < void > {
31- const { patient_id : patientId , date } = createAppointmentDto ;
32-
33- const MAX_APPOINTMENT_MONTHS_LIMIT = 3 ;
34- const appointmentDate = new Date ( date ) ;
35- const maxAppointmentDate = new Date ( ) ;
36-
37- maxAppointmentDate . setMonth (
38- maxAppointmentDate . getMonth ( ) + MAX_APPOINTMENT_MONTHS_LIMIT ,
39- ) ;
40-
41- if ( appointmentDate > maxAppointmentDate ) {
42- throw new BadRequestException (
43- 'A data de atendimento deve estar dentro dos próximos 3 meses.' ,
44- ) ;
45- }
34+ const { patient_id : patientId } = createAppointmentDto ;
4635
4736 const patient = await this . patientsRepository . findOne ( {
4837 where : { id : patientId } ,
4938 select : { id : true } ,
5039 } ) ;
5140
5241 if ( ! patient ) {
53- throw new BadRequestException ( 'Paciente não encontrado.' ) ;
42+ throw new NotFoundException ( 'Paciente não encontrado.' ) ;
5443 }
5544
56- const appointment = this . appointmentsRepository . create ( {
45+ const appointmentPayload : Partial < Appointment > = {
5746 ...createAppointmentDto ,
47+ patient_id : patientId ,
48+ status : 'scheduled' ,
5849 created_by : user . id ,
59- } ) ;
50+ } ;
51+
52+ if ( user . role === 'specialist' ) {
53+ const specialist = await this . usersRepository . findOne ( {
54+ select : { id : true , name : true , specialty : true } ,
55+ where : { id : user . id } ,
56+ } ) ;
57+
58+ if ( ! specialist || ! specialist . specialty ) {
59+ throw new NotFoundException ( 'Especialista não encontrado.' ) ;
60+ }
61+
62+ appointmentPayload . user_id = specialist . id ;
63+ appointmentPayload . professional_name = specialist . name ;
64+ appointmentPayload . category = specialist . specialty ;
65+ }
6066
67+ const appointment = this . appointmentsRepository . create ( appointmentPayload ) ;
6168 await this . appointmentsRepository . save ( appointment ) ;
6269
6370 this . logger . log (
0 commit comments