Skip to content

Commit 73661e8

Browse files
committed
refactor(appointments/referrals): fill data automatically when request user is specialist
1 parent 44ea805 commit 73661e8

12 files changed

Lines changed: 83 additions & 24 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { MigrationInterface, QueryRunner } from "typeorm";
2+
3+
export class AddRelations1771606365676 implements MigrationInterface {
4+
name = 'AddRelations1771606365676'
5+
6+
public async up(queryRunner: QueryRunner): Promise<void> {
7+
await queryRunner.query(`ALTER TABLE \`referrals\` ADD \`user_id\` varchar(255) NULL`);
8+
await queryRunner.query(`ALTER TABLE \`appointments\` ADD \`user_id\` varchar(255) NULL`);
9+
}
10+
11+
public async down(queryRunner: QueryRunner): Promise<void> {
12+
await queryRunner.query(`ALTER TABLE \`appointments\` DROP COLUMN \`user_id\``);
13+
await queryRunner.query(`ALTER TABLE \`referrals\` DROP COLUMN \`user_id\``);
14+
}
15+
16+
}

src/app/http/appointments/appointments.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
33

44
import { Appointment } from '@/domain/entities/appointment';
55
import { Patient } from '@/domain/entities/patient';
6+
import { User } from '@/domain/entities/user';
67

78
import { AppointmentsController } from './appointments.controller';
89
import { CancelAppointmentUseCase } from './use-cases/cancel-appointment.use-case';
@@ -11,7 +12,7 @@ import { GetAppointmentsUseCase } from './use-cases/get-appointments.use-case';
1112
import { UpdateAppointmentUseCase } from './use-cases/update-appointment.use-case';
1213

1314
@Module({
14-
imports: [TypeOrmModule.forFeature([Appointment, Patient])],
15+
imports: [TypeOrmModule.forFeature([Appointment, Patient, User])],
1516
controllers: [AppointmentsController],
1617
providers: [
1718
GetAppointmentsUseCase,

src/app/http/appointments/use-cases/create-appointment.use-case.ts

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import { BadRequestException, Injectable, Logger } from '@nestjs/common';
1+
import { Injectable, Logger, NotFoundException } from '@nestjs/common';
22
import { InjectRepository } from '@nestjs/typeorm';
33
import type { Repository } from 'typeorm';
44

55
import { Appointment } from '@/domain/entities/appointment';
66
import { Patient } from '@/domain/entities/patient';
7+
import { User } from '@/domain/entities/user';
78

89
import type { AuthUserDto } from '../../auth/auth.dtos';
910
import type { CreateAppointmentDto } from '../appointments.dtos';
1011

1112
interface 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(

src/app/http/patients/use-cases/get-patient-options.use-case.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export class GetPatientOptionsUseCase {
2626

2727
const patients = await this.patientsRepository.find({
2828
select: { id: true, name: true, cpf: true },
29+
order: { name: 'ASC' },
2930
where,
3031
});
3132

src/app/http/referrals/referrals.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
33

44
import { Patient } from '@/domain/entities/patient';
55
import { Referral } from '@/domain/entities/referral';
6+
import { User } from '@/domain/entities/user';
67

78
import { ReferralsController } from './referrals.controller';
89
import { CancelReferralUseCase } from './use-cases/cancel-referral.use-case';
@@ -11,7 +12,7 @@ import { GetReferralsUseCase } from './use-cases/get-referrals.use-case';
1112
import { UpdateReferralUseCase } from './use-cases/update-referral.use-case';
1213

1314
@Module({
14-
imports: [TypeOrmModule.forFeature([Patient, Referral])],
15+
imports: [TypeOrmModule.forFeature([Referral, Patient, User])],
1516
controllers: [ReferralsController],
1617
providers: [
1718
GetReferralsUseCase,

src/app/http/referrals/use-cases/create-referrals.use-case.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { type Repository } from 'typeorm';
44

55
import { Patient } from '@/domain/entities/patient';
66
import { Referral } from '@/domain/entities/referral';
7+
import { User } from '@/domain/entities/user';
78

89
import type { AuthUserDto } from '../../auth/auth.dtos';
910
import { CreateReferralDto } from '../referrals.dtos';
@@ -20,9 +21,12 @@ export class CreateReferralUseCase {
2021
constructor(
2122
@InjectRepository(Patient)
2223
private readonly patientsRepository: Repository<Patient>,
24+
@InjectRepository(User)
25+
private readonly usersRepository: Repository<User>,
2326
@InjectRepository(Referral)
2427
private readonly referralsRepository: Repository<Referral>,
2528
) {}
29+
2630
async execute({
2731
user,
2832
createReferralDto,
@@ -38,11 +42,30 @@ export class CreateReferralUseCase {
3842
throw new NotFoundException('Paciente não encontrado.');
3943
}
4044

41-
const referral = await this.referralsRepository.save({
45+
const referralPayload: Partial<Referral> = {
4246
...createReferralDto,
47+
patient_id: patientId,
4348
status: 'scheduled',
4449
created_by: user.id,
45-
});
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+
referralPayload.user_id = specialist.id;
63+
referralPayload.professional_name = specialist.name;
64+
referralPayload.category = specialist.specialty;
65+
}
66+
67+
const referral = this.referralsRepository.create(referralPayload);
68+
await this.referralsRepository.save(referral);
4669

4770
this.logger.log(
4871
{

src/domain/entities/appointment.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ export class Appointment implements AppointmentSchema {
4343
@Column({ type: 'varchar', length: 64, nullable: true })
4444
professional_name: string | null;
4545

46+
@Column({ type: 'uuid', nullable: true })
47+
user_id: string | null;
48+
4649
@Column('uuid')
4750
created_by: string;
4851

src/domain/entities/referral.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ export class Referral implements ReferralSchema {
4040
@Column({ type: 'varchar', length: 64, nullable: true })
4141
professional_name: string | null;
4242

43+
@Column({ type: 'uuid', nullable: true })
44+
user_id: string | null;
45+
4346
@Column('uuid')
4447
created_by: string;
4548

src/domain/schemas/appointments/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const appointmentSchema = z
1414
condition: patientConditionSchema,
1515
annotation: z.string().max(500).nullable(),
1616
professional_name: nameSchema.nullable(),
17+
user_id: z.string().uuid().nullable(),
1718
created_by: z.string().uuid(),
1819
created_at: z.coerce.date(),
1920
updated_at: z.coerce.date(),

src/domain/schemas/appointments/responses.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const getAppointmentsResponseSchema = baseResponseSchema.extend({
1717
condition: true,
1818
annotation: true,
1919
professional_name: true,
20+
user_id: true,
2021
created_at: true,
2122
updated_at: true,
2223
})

0 commit comments

Comments
 (0)