MaternAlert / src /appointments /appointments.service.ts
Auspicious14's picture
refactor(appointments): switch to facility ID
01d0870
Raw
History Blame Contribute Delete
2.39 kB
import {
BadRequestException,
Injectable,
NotFoundException,
} from "@nestjs/common";
import { PrismaService } from "../database/prisma.service";
import { CreateAppointmentDto } from "./dto/create-appointment.dto";
import { UpdateAppointmentDto } from "./dto/update-appointment.dto";
import { Appointment } from "@prisma/client";
@Injectable()
export class AppointmentsService {
constructor(private prisma: PrismaService) {}
async create(
facilityId: string,
dto: CreateAppointmentDto,
): Promise<Appointment> {
const patient = await this.prisma.patient.findUnique({
where: {
id: dto.patientId,
},
});
if (!patient) {
throw new NotFoundException("Patient not found");
}
if (!patient.facilityId) {
throw new BadRequestException("Patient is not linked to a facility");
}
return this.prisma.appointment.create({
data: {
patientId: patient.id,
userId: patient.userId,
facilityId: facilityId,
title: dto.title,
type: dto.type,
notes: dto.notes,
status: dto.status,
dateTime: new Date(dto.dateTime),
},
});
}
async findAll(facilityId: string): Promise<Appointment[]> {
return this.prisma.appointment.findMany({
where: {
facilityId: facilityId,
},
include: {
patient: true,
facility: true,
},
orderBy: {
dateTime: "asc",
},
});
}
async findOne(id: string, facilityId: string) {
const appointment = await this.prisma.appointment.findFirst({
where: {
id,
facilityId: facilityId,
},
include: {
patient: true,
facility: true,
},
});
if (!appointment) {
throw new NotFoundException("Appointment not found");
}
return appointment;
}
async update(
id: string,
facilityId: string,
dto: UpdateAppointmentDto,
) {
await this.findOne(id, facilityId);
return this.prisma.appointment.update({
where: {
id,
},
data: {
...dto,
...(dto.dateTime && {
dateTime: new Date(dto.dateTime),
}),
},
});
}
async remove(id: string, facilityId: string) {
await this.findOne(id, facilityId);
await this.prisma.appointment.delete({
where: {
id,
},
});
}
}