refactor: resolve remaining field audit issues
This commit is contained in:
@@ -114,7 +114,13 @@ export class RoomsService {
|
||||
}
|
||||
|
||||
async update(id: number, dto: UpdateRoomDto) {
|
||||
await this.findOne(id);
|
||||
const room = await this.findOne(id);
|
||||
if (Object.prototype.hasOwnProperty.call(dto, 'gender') && dto.gender !== room.gender) {
|
||||
const activeCount = await this.occRepo.count({
|
||||
where: { roomId: id, checkOutDate: IsNull() },
|
||||
});
|
||||
if (activeCount > 0) throw new BadRequestException('该宿舍有在住人员,无法修改宿舍性别');
|
||||
}
|
||||
await this.repo.update(id, dto);
|
||||
return this.repo.findOne({ where: { id } });
|
||||
}
|
||||
@@ -202,10 +208,7 @@ export class RoomsService {
|
||||
for (const occ of occupancies) {
|
||||
if (!occMap.has(occ.roomId)) occMap.set(occ.roomId, []);
|
||||
const checkIn = new Date(occ.checkInDate);
|
||||
const days = Math.max(
|
||||
1,
|
||||
Math.ceil((refTime - checkIn.getTime()) / (1000 * 60 * 60 * 24)),
|
||||
);
|
||||
const days = Math.max(1, Math.ceil((refTime - checkIn.getTime()) / (1000 * 60 * 60 * 24)));
|
||||
occMap.get(occ.roomId)!.push({
|
||||
studentId: occ.studentId,
|
||||
studentName: occ.student?.name || '未知',
|
||||
@@ -250,8 +253,11 @@ export class RoomsService {
|
||||
const allSameOrg = occ.every((o: any) => o.organization && o.organization === orgs[0]);
|
||||
orgLabel = allSameOrg ? `均为${orgs[0]}人员` : `存在${orgs.join('、')}人员`;
|
||||
}
|
||||
const organizationColors = [...new Set(occ.map((o: any) => o.organizationColor).filter(Boolean))];
|
||||
const organizationColor: string | null = organizationColors.length === 1 ? organizationColors[0] : null;
|
||||
const organizationColors = [
|
||||
...new Set(occ.map((o: any) => o.organizationColor).filter(Boolean)),
|
||||
];
|
||||
const organizationColor: string | null =
|
||||
organizationColors.length === 1 ? organizationColors[0] : null;
|
||||
const organizationIds = [...new Set(occ.map((o: any) => o.organizationId).filter(Boolean))];
|
||||
return {
|
||||
id: room.id,
|
||||
@@ -274,7 +280,14 @@ export class RoomsService {
|
||||
...new Map(
|
||||
occupancies
|
||||
.filter((o) => o.responsibleOrganizationId && o.responsibleOrganization)
|
||||
.map((o) => [o.responsibleOrganizationId, { id: o.responsibleOrganizationId, name: o.responsibleOrganization.name, color: o.responsibleOrganization.color || null }]),
|
||||
.map((o) => [
|
||||
o.responsibleOrganizationId,
|
||||
{
|
||||
id: o.responsibleOrganizationId,
|
||||
name: o.responsibleOrganization.name,
|
||||
color: o.responsibleOrganization.color || null,
|
||||
},
|
||||
]),
|
||||
).values(),
|
||||
].sort((a, b) => a.name.localeCompare(b.name)),
|
||||
};
|
||||
@@ -289,6 +302,7 @@ export class RoomsService {
|
||||
roomType?: string;
|
||||
rentalCategory?: string;
|
||||
monthlyRate?: number;
|
||||
gender?: '男' | '女';
|
||||
}[],
|
||||
) {
|
||||
let imported = 0;
|
||||
@@ -314,6 +328,7 @@ export class RoomsService {
|
||||
roomType: row.roomType || parsed.roomType || undefined,
|
||||
rentalCategory: row.rentalCategory || undefined,
|
||||
monthlyRate: row.monthlyRate ?? undefined,
|
||||
gender: row.gender ?? undefined,
|
||||
}),
|
||||
);
|
||||
imported++;
|
||||
@@ -336,7 +351,10 @@ export class RoomsService {
|
||||
async getRoomAvailableBeds(roomId: number): Promise<Bed[]> {
|
||||
const room = await this.repo.findOne({ where: { id: roomId } });
|
||||
if (!room) throw new NotFoundException('宿舍不存在');
|
||||
return this.bedRepo.find({ where: { roomId, status: 'available' }, order: { bedNumber: 'ASC' } });
|
||||
return this.bedRepo.find({
|
||||
where: { roomId, status: 'available' },
|
||||
order: { bedNumber: 'ASC' },
|
||||
});
|
||||
}
|
||||
|
||||
async createBed(roomId: number, dto: CreateBedDto): Promise<Bed> {
|
||||
@@ -377,7 +395,7 @@ export class RoomsService {
|
||||
if (!room) throw new NotFoundException('宿舍不存在');
|
||||
if (room.status === 'archived') throw new BadRequestException('已归档宿舍不能添加床位');
|
||||
const existing = await this.bedRepo.find({ where: { roomId }, order: { bedNumber: 'ASC' } });
|
||||
const numbers = existing.map(b => {
|
||||
const numbers = existing.map((b) => {
|
||||
const match = b.bedNumber.match(/^\d+/);
|
||||
return match ? parseInt(match[0]) : 0;
|
||||
});
|
||||
@@ -400,14 +418,19 @@ export class RoomsService {
|
||||
async getRoomAvailableLockers(roomId: number): Promise<Locker[]> {
|
||||
const room = await this.repo.findOne({ where: { id: roomId } });
|
||||
if (!room) throw new NotFoundException('宿舍不存在');
|
||||
return this.lockerRepo.find({ where: { roomId, status: 'available' }, order: { lockerNumber: 'ASC' } });
|
||||
return this.lockerRepo.find({
|
||||
where: { roomId, status: 'available' },
|
||||
order: { lockerNumber: 'ASC' },
|
||||
});
|
||||
}
|
||||
|
||||
async createLocker(roomId: number, dto: CreateLockerDto): Promise<Locker> {
|
||||
const room = await this.repo.findOne({ where: { id: roomId } });
|
||||
if (!room) throw new NotFoundException('宿舍不存在');
|
||||
if (room.status === 'archived') throw new BadRequestException('已归档宿舍不能添加柜子');
|
||||
const existing = await this.lockerRepo.findOne({ where: { roomId, lockerNumber: dto.lockerNumber } });
|
||||
const existing = await this.lockerRepo.findOne({
|
||||
where: { roomId, lockerNumber: dto.lockerNumber },
|
||||
});
|
||||
if (existing) throw new BadRequestException('该柜子编号已存在');
|
||||
const locker = this.lockerRepo.create({ ...dto, roomId });
|
||||
return this.lockerRepo.save(locker);
|
||||
@@ -420,7 +443,9 @@ export class RoomsService {
|
||||
throw new BadRequestException('该柜子有人占用,请先释放');
|
||||
}
|
||||
if (dto.lockerNumber && dto.lockerNumber !== locker.lockerNumber) {
|
||||
const dup = await this.lockerRepo.findOne({ where: { roomId, lockerNumber: dto.lockerNumber } });
|
||||
const dup = await this.lockerRepo.findOne({
|
||||
where: { roomId, lockerNumber: dto.lockerNumber },
|
||||
});
|
||||
if (dup) throw new BadRequestException('该柜子编号已存在');
|
||||
}
|
||||
Object.assign(locker, dto);
|
||||
@@ -438,8 +463,11 @@ export class RoomsService {
|
||||
const room = await this.repo.findOne({ where: { id: roomId } });
|
||||
if (!room) throw new NotFoundException('宿舍不存在');
|
||||
if (room.status === 'archived') throw new BadRequestException('已归档宿舍不能添加柜子');
|
||||
const existing = await this.lockerRepo.find({ where: { roomId }, order: { lockerNumber: 'ASC' } });
|
||||
const numbers = existing.map(b => {
|
||||
const existing = await this.lockerRepo.find({
|
||||
where: { roomId },
|
||||
order: { lockerNumber: 'ASC' },
|
||||
});
|
||||
const numbers = existing.map((b) => {
|
||||
const match = b.lockerNumber.match(/^\d+/);
|
||||
return match ? parseInt(match[0]) : 0;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user