/opt/mawid/apps/api/src/staff
Edit: /opt/mawid/apps/api/src/staff/staff.service.ts (2330B)
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
import type { CreateStaffInput, UpdateStaffInput } from '@mawid/shared';
import { PrismaService } from '../prisma/prisma.service';
const STAFF_INCLUDE = { services: { select: { id: true, name: true } } } as const;
@Injectable()
export class StaffService {
constructor(private readonly prisma: PrismaService) {}
list(clinicId: string) {
return this.prisma.staff.findMany({
where: { clinicId },
include: STAFF_INCLUDE,
orderBy: { createdAt: 'asc' },
});
}
async get(clinicId: string, id: string) {
const staff = await this.prisma.staff.findFirst({
where: { id, clinicId },
include: STAFF_INCLUDE,
});
if (!staff) throw new NotFoundException('Staff member not found');
return staff;
}
async create(clinicId: string, input: CreateStaffInput) {
await this.assertServicesBelongToClinic(clinicId, input.serviceIds);
return this.prisma.staff.create({
data: {
clinicId,
name: input.name,
role: input.role,
workingHours: input.workingHours,
active: input.active,
services: { connect: input.serviceIds.map((id) => ({ id })) },
},
include: STAFF_INCLUDE,
});
}
async update(clinicId: string, id: string, input: UpdateStaffInput) {
await this.get(clinicId, id);
if (input.serviceIds) await this.assertServicesBelongToClinic(clinicId, input.serviceIds);
const { serviceIds, ...fields } = input;
return this.prisma.staff.update({
where: { id },
data: {
...fields,
...(serviceIds ? { services: { set: serviceIds.map((sid) => ({ id: sid })) } } : {}),
},
include: STAFF_INCLUDE,
});
}
async remove(clinicId: string, id: string) {
await this.get(clinicId, id);
await this.prisma.staff.delete({ where: { id } });
return { deleted: true };
}
private async assertServicesBelongToClinic(clinicId: string, serviceIds: string[]) {
if (serviceIds.length === 0) return;
const count = await this.prisma.service.count({
where: { id: { in: serviceIds }, clinicId },
});
if (count !== new Set(serviceIds).size) {
throw new BadRequestException('One or more serviceIds do not belong to this clinic');
}
}
}