支持主任/组长/医生删除设备并新增删除范围权限校验

This commit is contained in:
EL 2026-04-03 09:41:31 +08:00
parent d77627e44b
commit d2d87701de
2 changed files with 76 additions and 2 deletions

View File

@ -160,7 +160,13 @@ export class BDevicesController {
*
*/
@Delete(':id')
@Roles(Role.SYSTEM_ADMIN, Role.HOSPITAL_ADMIN)
@Roles(
Role.SYSTEM_ADMIN,
Role.HOSPITAL_ADMIN,
Role.DIRECTOR,
Role.LEADER,
Role.DOCTOR,
)
@ApiOperation({ summary: '删除设备' })
@ApiParam({ name: 'id', description: '设备 ID' })
remove(

View File

@ -177,7 +177,7 @@ export class DevicesService {
* 409
*/
async remove(actor: ActorContext, id: number) {
const current = await this.findOne(actor, id);
const current = await this.findRemovableDevice(actor, id);
try {
return await this.prisma.device.delete({
@ -538,6 +538,74 @@ export class DevicesService {
/**
*
*/
private async findRemovableDevice(actor: ActorContext, id: number) {
const deviceId = this.toInt(id, 'id');
const device = await this.prisma.device.findUnique({
where: { id: deviceId },
select: {
id: true,
patient: {
select: {
hospitalId: true,
doctorId: true,
doctor: {
select: {
departmentId: true,
groupId: true,
},
},
},
},
},
});
if (!device) {
throw new NotFoundException(MESSAGES.DEVICE.NOT_FOUND);
}
this.assertDeviceRemovableScope(actor, device.patient);
return device;
}
private assertDeviceRemovableScope(
actor: ActorContext,
patient: {
hospitalId: number;
doctorId: number;
doctor: { departmentId: number | null; groupId: number | null };
},
) {
switch (actor.role) {
case Role.SYSTEM_ADMIN:
return;
case Role.HOSPITAL_ADMIN:
if (patient.hospitalId !== this.requireActorHospitalId(actor)) {
throw new ForbiddenException(MESSAGES.DEFAULT_FORBIDDEN);
}
return;
case Role.DIRECTOR:
if (
!actor.departmentId ||
patient.doctor.departmentId !== actor.departmentId
) {
throw new ForbiddenException(MESSAGES.DEFAULT_FORBIDDEN);
}
return;
case Role.LEADER:
if (!actor.groupId || patient.doctor.groupId !== actor.groupId) {
throw new ForbiddenException(MESSAGES.DEFAULT_FORBIDDEN);
}
return;
case Role.DOCTOR:
if (patient.doctorId !== actor.id) {
throw new ForbiddenException(MESSAGES.DEFAULT_FORBIDDEN);
}
return;
default:
throw new ForbiddenException(MESSAGES.DEFAULT_FORBIDDEN);
}
}
private assertAdmin(actor: ActorContext) {
if (
actor.role !== Role.SYSTEM_ADMIN &&