Skip to content

Commit 3475b45

Browse files
authored
Merge pull request #96 from shamoo53/Department-and-Ward-Management
implementation of Department-and-Ward-Management
2 parents f5567d5 + c80aeec commit 3475b45

16 files changed

Lines changed: 659 additions & 4 deletions
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import {
2+
Controller,
3+
Get,
4+
Post,
5+
Body,
6+
Param,
7+
Put,
8+
Delete,
9+
Patch,
10+
} from '@nestjs/common';
11+
import { BedService } from './bed.service';
12+
import { BedStatus } from './entities/bed.entity';
13+
14+
@Controller('beds')
15+
export class BedController {
16+
constructor(private readonly bedService: BedService) {}
17+
18+
@Post()
19+
create(@Body() data: any) {
20+
return this.bedService.create(data);
21+
}
22+
23+
@Get()
24+
findAll() {
25+
return this.bedService.findAll();
26+
}
27+
28+
@Get(':id')
29+
findOne(@Param('id') id: string) {
30+
return this.bedService.findOne(id);
31+
}
32+
33+
@Put(':id')
34+
update(@Param('id') id: string, @Body() data: any) {
35+
return this.bedService.update(id, data);
36+
}
37+
38+
@Delete(':id')
39+
remove(@Param('id') id: string) {
40+
return this.bedService.remove(id);
41+
}
42+
43+
@Patch(':id/status')
44+
updateStatus(@Param('id') id: string, @Body('status') status: BedStatus) {
45+
return this.bedService.updateStatus(id, status);
46+
}
47+
48+
@Patch(':id/assign')
49+
assignToPatient(
50+
@Param('id') id: string,
51+
@Body('patientId') patientId: string,
52+
) {
53+
return this.bedService.assignToPatient(id, patientId);
54+
}
55+
56+
@Patch(':id/release')
57+
releaseBed(@Param('id') id: string) {
58+
return this.bedService.releaseBed(id);
59+
}
60+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { InjectRepository } from '@nestjs/typeorm';
3+
import { Repository } from 'typeorm';
4+
import { Bed, BedStatus } from './entities/bed.entity';
5+
6+
@Injectable()
7+
export class BedService {
8+
constructor(
9+
@InjectRepository(Bed)
10+
private readonly bedRepository: Repository<Bed>,
11+
) {}
12+
13+
async create(data: Partial<Bed>): Promise<Bed> {
14+
return this.bedRepository.save(data);
15+
}
16+
17+
async findAll(): Promise<Bed[]> {
18+
return this.bedRepository.find({ relations: ['room'] });
19+
}
20+
21+
async findOne(id: string): Promise<Bed | null> {
22+
return this.bedRepository.findOne({ where: { id }, relations: ['room'] });
23+
}
24+
25+
async update(id: string, data: Partial<Bed>): Promise<Bed | null> {
26+
await this.bedRepository.update(id, data);
27+
return this.findOne(id);
28+
}
29+
30+
async remove(id: string): Promise<void> {
31+
await this.bedRepository.delete(id);
32+
}
33+
34+
async updateStatus(id: string, status: BedStatus): Promise<Bed | null> {
35+
await this.bedRepository.update(id, { status });
36+
return this.findOne(id);
37+
}
38+
39+
async assignToPatient(id: string, patientId: string): Promise<Bed | null> {
40+
await this.bedRepository.update(id, {
41+
status: BedStatus.OCCUPIED,
42+
assignedPatientId: patientId,
43+
});
44+
return this.findOne(id);
45+
}
46+
47+
async releaseBed(id: string): Promise<Bed | null> {
48+
await this.bedRepository.update(id, {
49+
status: BedStatus.AVAILABLE,
50+
assignedPatientId: null,
51+
});
52+
return this.findOne(id);
53+
}
54+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Controller, Post, Body } from '@nestjs/common';
2+
import { DepartmentWorkflowService } from './department-workflow.service';
3+
4+
@Controller('department-workflow')
5+
export class DepartmentWorkflowController {
6+
constructor(private readonly workflowService: DepartmentWorkflowService) {}
7+
8+
@Post('admit')
9+
admitPatientToBed(@Body() body: { bedId: string; patientId: string }) {
10+
return this.workflowService.admitPatientToBed(body.bedId, body.patientId);
11+
}
12+
13+
@Post('discharge')
14+
dischargePatientFromBed(@Body() body: { bedId: string }) {
15+
return this.workflowService.dischargePatientFromBed(body.bedId);
16+
}
17+
18+
@Post('transfer')
19+
transferPatient(
20+
@Body() body: { bedIdFrom: string; bedIdTo: string; patientId: string },
21+
) {
22+
return this.workflowService.transferPatient(
23+
body.bedIdFrom,
24+
body.bedIdTo,
25+
body.patientId,
26+
);
27+
}
28+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}

‎src/modules/departments/departments.module.ts‎

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,50 @@ import { TypeOrmModule } from '@nestjs/typeorm';
33
import { DepartmentsService } from './departments.service';
44
import { DepartmentsController } from './departments.controller';
55
import { Department } from './entities/department.entity';
6+
import { Ward } from './entities/ward.entity';
7+
import { Room } from './entities/room.entity';
8+
import { Bed } from './entities/bed.entity';
9+
import { WardService } from './ward.service';
10+
import { WardController } from './ward.controller';
11+
import { RoomService } from './room.service';
12+
import { RoomController } from './room.controller';
13+
import { BedService } from './bed.service';
14+
import { BedController } from './bed.controller';
15+
import { DepartmentWorkflowService } from './department-workflow.service';
16+
import { DepartmentWorkflowController } from './department-workflow.controller';
17+
import { ResourceAllocationService } from './resource-allocation.service';
18+
import { ResourceAllocationController } from './resource-allocation.controller';
19+
import { MetricsService } from './metrics.service';
20+
import { MetricsController } from './metrics.controller';
621

722
@Module({
8-
imports: [TypeOrmModule.forFeature([Department])],
9-
controllers: [DepartmentsController],
10-
providers: [DepartmentsService],
11-
exports: [DepartmentsService],
23+
imports: [TypeOrmModule.forFeature([Department, Ward, Room, Bed])],
24+
controllers: [
25+
DepartmentsController,
26+
WardController,
27+
RoomController,
28+
BedController,
29+
DepartmentWorkflowController,
30+
ResourceAllocationController,
31+
MetricsController,
32+
],
33+
providers: [
34+
DepartmentsService,
35+
WardService,
36+
RoomService,
37+
BedService,
38+
DepartmentWorkflowService,
39+
ResourceAllocationService,
40+
MetricsService,
41+
],
42+
exports: [
43+
DepartmentsService,
44+
WardService,
45+
RoomService,
46+
BedService,
47+
DepartmentWorkflowService,
48+
ResourceAllocationService,
49+
MetricsService,
50+
],
1251
})
1352
export class DepartmentsModule {}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {
2+
Entity,
3+
PrimaryGeneratedColumn,
4+
Column,
5+
ManyToOne,
6+
CreateDateColumn,
7+
UpdateDateColumn,
8+
} from 'typeorm';
9+
import { Room } from './room.entity';
10+
11+
export enum BedStatus {
12+
AVAILABLE = 'available',
13+
OCCUPIED = 'occupied',
14+
MAINTENANCE = 'maintenance',
15+
}
16+
17+
@Entity('beds')
18+
export class Bed {
19+
@PrimaryGeneratedColumn('uuid')
20+
id: string;
21+
22+
@Column()
23+
number: string;
24+
25+
@ManyToOne(() => Room, (room) => room.beds, { onDelete: 'CASCADE' })
26+
room: Room;
27+
28+
@Column({ type: 'enum', enum: BedStatus, default: BedStatus.AVAILABLE })
29+
status: BedStatus;
30+
31+
@Column({ nullable: true })
32+
assignedPatientId: string | null;
33+
34+
@CreateDateColumn()
35+
createdAt: Date;
36+
37+
@UpdateDateColumn()
38+
updatedAt: Date;
39+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {
2+
Entity,
3+
PrimaryGeneratedColumn,
4+
Column,
5+
ManyToOne,
6+
OneToMany,
7+
CreateDateColumn,
8+
UpdateDateColumn,
9+
} from 'typeorm';
10+
import { Ward } from './ward.entity';
11+
import { Bed } from './bed.entity';
12+
13+
@Entity('rooms')
14+
export class Room {
15+
@PrimaryGeneratedColumn('uuid')
16+
id: string;
17+
18+
@Column()
19+
number: string;
20+
21+
@ManyToOne(() => Ward, (ward) => ward.rooms, { onDelete: 'CASCADE' })
22+
ward: Ward;
23+
24+
@OneToMany(() => Bed, (bed) => bed.room)
25+
beds: Bed[];
26+
27+
@CreateDateColumn()
28+
createdAt: Date;
29+
30+
@UpdateDateColumn()
31+
updatedAt: Date;
32+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, OneToMany, CreateDateColumn, UpdateDateColumn } from 'typeorm';
2+
import { Department } from './department.entity';
3+
import { Room } from './room.entity';
4+
5+
@Entity('wards')
6+
export class Ward {
7+
@PrimaryGeneratedColumn('uuid')
8+
id: string;
9+
10+
@Column()
11+
name: string;
12+
13+
@ManyToOne(() => Department, (department) => department.id, { onDelete: 'CASCADE' })
14+
department: Department;
15+
16+
@OneToMany(() => Room, (room) => room.ward)
17+
rooms: Room[];
18+
19+
@CreateDateColumn()
20+
createdAt: Date;
21+
22+
@UpdateDateColumn()
23+
updatedAt: Date;
24+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Controller, Get, Param } from '@nestjs/common';
2+
import { MetricsService } from './metrics.service';
3+
4+
@Controller('metrics')
5+
export class MetricsController {
6+
constructor(private readonly metricsService: MetricsService) {}
7+
8+
@Get('department/:id')
9+
getDepartmentMetrics(@Param('id') id: string) {
10+
return this.metricsService.getDepartmentMetrics(id);
11+
}
12+
}

0 commit comments

Comments
 (0)