feat: integrate notification creation into business modules (classes, schedules done; bills/occupancies/deposits need student→userId mapping)

This commit is contained in:
2026-07-05 23:25:34 +08:00
parent 155e3b3c96
commit 7020e86809
10 changed files with 118 additions and 30 deletions

View File

@@ -20,6 +20,9 @@ import {
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { OperationLogsService } from '../operation-logs/operation-logs.service';
import { extractRequestInfo } from '../common/request-utils';
import { ConflictException } from '@nestjs/common';
import { NotificationsService } from '../notifications/notifications.service';
import { NotificationType } from '../entities/notification.entity';
import { RequirePermission } from '../auth/decorators/permission.decorator';
@UseGuards(JwtAuthGuard)
@@ -28,6 +31,7 @@ export class SchedulesController {
constructor(
private readonly service: SchedulesService,
private readonly logService: OperationLogsService,
private readonly notificationsService: NotificationsService,
) {}
@Get()
@@ -61,19 +65,39 @@ export class SchedulesController {
@RequirePermission('schedule:create')
async create(@Body() dto: CreateScheduleDto, @Request() req: { user?: { id: number; username: string }; headers?: Record<string, string> }) {
const { ipAddress, userAgent } = extractRequestInfo(req);
const result = await this.service.create(dto);
await this.logService.log({
userId: req.user?.id,
username: req.user?.username,
module: '排课管理',
action: '创建排课',
targetId: result.id,
targetType: 'class-schedule',
detail: `${result.subject}${result.weekDay} ${result.startTime}-${result.endTime}`,
ipAddress,
userAgent,
});
return result;
try {
const result = await this.service.create(dto);
await this.logService.log({
userId: req.user?.id,
username: req.user?.username,
module: '排课管理',
action: '创建排课',
targetId: result.id,
targetType: 'class-schedule',
detail: `${result.subject}${result.weekDay} ${result.startTime}-${result.endTime}`,
ipAddress,
userAgent,
});
return result;
} catch (error) {
if (error instanceof ConflictException) {
try {
const conflicts = await this.service.checkConflict(
dto.classroomId, dto.weekDay, dto.startTime, dto.endTime, dto.startDate, dto.endDate,
);
const teacherIds = [...new Set(conflicts.map(c => c.teacherId).filter(Boolean))];
if (teacherIds.length > 0) {
void this.notificationsService.create({
recipientIds: teacherIds,
type: NotificationType.SCHEDULE_CONFLICT,
title: '排课冲突',
content: `教室${dto.classroomId}${dto.weekDay} ${dto.startTime}-${dto.endTime} 与已有排课冲突`,
});
}
} catch {}
}
throw error;
}
}
@Put(':id')
@@ -84,19 +108,39 @@ export class SchedulesController {
@Request() req: { user?: { id: number; username: string }; headers?: Record<string, string> },
) {
const { ipAddress, userAgent } = extractRequestInfo(req);
const result = await this.service.update(+id, dto);
await this.logService.log({
userId: req.user?.id,
username: req.user?.username,
module: '排课管理',
action: '编辑排课',
targetId: +id,
targetType: 'class-schedule',
detail: JSON.stringify(dto),
ipAddress,
userAgent,
});
return result;
try {
const result = await this.service.update(+id, dto);
await this.logService.log({
userId: req.user?.id,
username: req.user?.username,
module: '排课管理',
action: '编辑排课',
targetId: +id,
targetType: 'class-schedule',
detail: JSON.stringify(dto),
ipAddress,
userAgent,
});
return result;
} catch (error) {
if (error instanceof ConflictException) {
try {
const conflicts = await this.service.checkConflict(
dto.classroomId ?? 0, dto.weekDay ?? 0, dto.startTime ?? '', dto.endTime ?? '', dto.startDate ?? '', dto.endDate ?? '',
);
const teacherIds = [...new Set(conflicts.map(c => c.teacherId).filter(Boolean))];
if (teacherIds.length > 0) {
void this.notificationsService.create({
recipientIds: teacherIds,
type: NotificationType.SCHEDULE_CONFLICT,
title: '排课冲突',
content: `教室${dto.classroomId}${dto.weekDay} ${dto.startTime}-${dto.endTime} (更新) 与已有排课冲突`,
});
}
} catch {}
}
throw error;
}
}
@Delete(':id')