|
| 1 | +import { Injectable } from '@nestjs/common'; |
| 2 | +import { InjectRepository } from '@nestjs/typeorm'; |
| 3 | +import { Repository } from 'typeorm'; |
| 4 | +import { Department } from './entities/department.entity'; |
| 5 | +import { Ward } from './entities/ward.entity'; |
| 6 | +import { Room } from './entities/room.entity'; |
| 7 | +import { Bed, BedStatus } from './entities/bed.entity'; |
| 8 | + |
| 9 | +@Injectable() |
| 10 | +export class DepartmentWorkflowService { |
| 11 | + constructor( |
| 12 | + @InjectRepository(Department) |
| 13 | + private readonly departmentRepository: Repository<Department>, |
| 14 | + @InjectRepository(Ward) |
| 15 | + private readonly wardRepository: Repository<Ward>, |
| 16 | + @InjectRepository(Room) |
| 17 | + private readonly roomRepository: Repository<Room>, |
| 18 | + @InjectRepository(Bed) |
| 19 | + private readonly bedRepository: Repository<Bed>, |
| 20 | + ) {} |
| 21 | + |
| 22 | + async admitPatientToBed( |
| 23 | + bedId: string, |
| 24 | + patientId: string, |
| 25 | + ): Promise<Bed | null> { |
| 26 | + await this.bedRepository.update(bedId, { |
| 27 | + status: BedStatus.OCCUPIED, |
| 28 | + assignedPatientId: patientId, |
| 29 | + }); |
| 30 | + return this.bedRepository.findOne({ where: { id: bedId } }); |
| 31 | + } |
| 32 | + |
| 33 | + async dischargePatientFromBed(bedId: string): Promise<Bed | null> { |
| 34 | + await this.bedRepository.update(bedId, { |
| 35 | + status: BedStatus.AVAILABLE, |
| 36 | + assignedPatientId: null, |
| 37 | + }); |
| 38 | + return this.bedRepository.findOne({ where: { id: bedId } }); |
| 39 | + } |
| 40 | + |
| 41 | + async transferPatient( |
| 42 | + bedIdFrom: string, |
| 43 | + bedIdTo: string, |
| 44 | + patientId: string, |
| 45 | + ): Promise<{ from: Bed | null; to: Bed | null }> { |
| 46 | + await this.bedRepository.update(bedIdFrom, { |
| 47 | + status: BedStatus.AVAILABLE, |
| 48 | + assignedPatientId: null, |
| 49 | + }); |
| 50 | + await this.bedRepository.update(bedIdTo, { |
| 51 | + status: BedStatus.OCCUPIED, |
| 52 | + assignedPatientId: patientId, |
| 53 | + }); |
| 54 | + const from = await this.bedRepository.findOne({ where: { id: bedIdFrom } }); |
| 55 | + const to = await this.bedRepository.findOne({ where: { id: bedIdTo } }); |
| 56 | + return { from, to }; |
| 57 | + } |
| 58 | +} |
0 commit comments