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

@@ -13,6 +13,8 @@ import {
Req, Req,
} from '@nestjs/common'; } from '@nestjs/common';
import { BillsService } from './bills.service'; import { BillsService } from './bills.service';
import { NotificationsService } from '../notifications/notifications.service';
import { NotificationType } from '../entities/notification.entity';
import { BillsExportService } from './bills-export.service'; import { BillsExportService } from './bills-export.service';
import { GenerateBillsDto, UpdateBillStatusDto } from './dto/bill.dto'; import { GenerateBillsDto, UpdateBillStatusDto } from './dto/bill.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
@@ -28,6 +30,7 @@ export class BillsController {
private service: BillsService, private service: BillsService,
private exportService: BillsExportService, private exportService: BillsExportService,
private logService: OperationLogsService, private logService: OperationLogsService,
private readonly notificationsService: NotificationsService,
) {} ) {}
@Post('generate') @Post('generate')
@@ -44,6 +47,7 @@ export class BillsController {
ipAddress, ipAddress,
userAgent, userAgent,
}); });
// TODO: Send notifications for bill_generated — studentId→userId mapping unavailable
return result; return result;
} }
@@ -88,6 +92,7 @@ export class BillsController {
ipAddress, ipAddress,
userAgent, userAgent,
}); });
// TODO: Send notification for bill_paid — bill.studentId→userId mapping unavailable
return result; return result;
} }
@@ -105,6 +110,7 @@ export class BillsController {
ipAddress, ipAddress,
userAgent, userAgent,
}); });
// TODO: Send notification for bill_paid (batch) — bill.studentId→userId mapping unavailable
return result; return result;
} }

View File

@@ -1,4 +1,5 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { NotificationsModule } from '../notifications/notifications.module';
import { TypeOrmModule } from '@nestjs/typeorm'; import { TypeOrmModule } from '@nestjs/typeorm';
import { Bill } from '../entities/bill.entity'; import { Bill } from '../entities/bill.entity';
import { BillItem } from '../entities/bill-item.entity'; import { BillItem } from '../entities/bill-item.entity';
@@ -22,6 +23,7 @@ import { BillsController } from './bills.controller';
Room, Room,
Deposit, Deposit,
]), ]),
NotificationsModule,
], ],
controllers: [BillsController], controllers: [BillsController],
providers: [BillsService, BillsExportService], providers: [BillsService, BillsExportService],

View File

@@ -24,6 +24,8 @@ import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { OperationLogsService } from '../operation-logs/operation-logs.service'; import { OperationLogsService } from '../operation-logs/operation-logs.service';
import { extractRequestInfo } from '../common/request-utils'; import { extractRequestInfo } from '../common/request-utils';
import { RequirePermission } from '../auth/decorators/permission.decorator'; import { RequirePermission } from '../auth/decorators/permission.decorator';
import { NotificationsService } from '../notifications/notifications.service';
import { NotificationType } from '../entities/notification.entity';
import * as ExcelJS from 'exceljs'; import * as ExcelJS from 'exceljs';
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@@ -32,6 +34,7 @@ export class ClassesController {
constructor( constructor(
private readonly service: ClassesService, private readonly service: ClassesService,
private readonly logService: OperationLogsService, private readonly logService: OperationLogsService,
private readonly notificationsService: NotificationsService,
) {} ) {}
@Get() @Get()
@@ -170,6 +173,17 @@ export class ClassesController {
ipAddress, ipAddress,
userAgent, userAgent,
}); });
try {
const cls = await this.service.findOne(+id);
if (cls.headTeacherId) {
void this.notificationsService.create({
recipientIds: [cls.headTeacherId],
type: NotificationType.CLASS_CHANGE,
title: '学员变动',
content: `班级新增${result.added}名学生`,
});
}
} catch {}
return result; return result;
} }
@@ -222,6 +236,14 @@ export class ClassesController {
ipAddress, ipAddress,
userAgent, userAgent,
}); });
try {
void this.notificationsService.create({
recipientIds: [dto.userId],
type: NotificationType.CLASS_CHANGE,
title: '班级分配',
content: `您已被分配到班级担任${dto.roleType}角色`,
});
} catch {}
return result; return result;
} }

View File

@@ -4,9 +4,10 @@ import { Class, ClassStudent, ClassTeacher } from '../entities';
import { ClassesService } from './classes.service'; import { ClassesService } from './classes.service';
import { ClassesController } from './classes.controller'; import { ClassesController } from './classes.controller';
import { OperationLogsModule } from '../operation-logs/operation-logs.module'; import { OperationLogsModule } from '../operation-logs/operation-logs.module';
import { NotificationsModule } from '../notifications/notifications.module';
@Module({ @Module({
imports: [TypeOrmModule.forFeature([Class, ClassStudent, ClassTeacher]), OperationLogsModule], imports: [TypeOrmModule.forFeature([Class, ClassStudent, ClassTeacher]), OperationLogsModule, NotificationsModule],
controllers: [ClassesController], controllers: [ClassesController],
providers: [ClassesService], providers: [ClassesService],
exports: [ClassesService], exports: [ClassesService],

View File

@@ -11,6 +11,8 @@ import {
Request, Request,
} from '@nestjs/common'; } from '@nestjs/common';
import { DepositsService } from './deposits.service'; import { DepositsService } from './deposits.service';
import { NotificationsService } from '../notifications/notifications.service';
import { NotificationType } from '../entities/notification.entity';
import { CreateDepositDto, RefundDepositDto, CreateDepositWithInstallmentsDto } from './dto/deposit.dto'; import { CreateDepositDto, RefundDepositDto, CreateDepositWithInstallmentsDto } from './dto/deposit.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { OperationLogsService } from '../operation-logs/operation-logs.service'; import { OperationLogsService } from '../operation-logs/operation-logs.service';
@@ -23,6 +25,7 @@ export class DepositsController {
constructor( constructor(
private service: DepositsService, private service: DepositsService,
private logService: OperationLogsService, private logService: OperationLogsService,
private readonly notificationsService: NotificationsService,
) {} ) {}
@Get() @Get()
@@ -68,6 +71,7 @@ export class DepositsController {
ipAddress, ipAddress,
userAgent, userAgent,
}); });
// TODO: Send notification for deposit_due — studentId→userId mapping unavailable
return result; return result;
} }
@@ -111,6 +115,7 @@ export class DepositsController {
ipAddress, ipAddress,
userAgent, userAgent,
}); });
// TODO: Send notification for deposit_refunded — studentId→userId mapping unavailable
return result; return result;
} }

View File

@@ -5,9 +5,10 @@ import { DepositInstallment } from '../entities/deposit-installment.entity';
import { DepositsService } from './deposits.service'; import { DepositsService } from './deposits.service';
import { DepositsController } from './deposits.controller'; import { DepositsController } from './deposits.controller';
import { OperationLogsModule } from '../operation-logs/operation-logs.module'; import { OperationLogsModule } from '../operation-logs/operation-logs.module';
import { NotificationsModule } from '../notifications/notifications.module';
@Module({ @Module({
imports: [TypeOrmModule.forFeature([Deposit, DepositInstallment]), OperationLogsModule], imports: [TypeOrmModule.forFeature([Deposit, DepositInstallment]), OperationLogsModule, NotificationsModule],
controllers: [DepositsController], controllers: [DepositsController],
providers: [DepositsService], providers: [DepositsService],
exports: [DepositsService], exports: [DepositsService],

View File

@@ -16,6 +16,8 @@ import {
import { FileInterceptor } from '@nestjs/platform-express'; import { FileInterceptor } from '@nestjs/platform-express';
import type { Response } from 'express'; import type { Response } from 'express';
import { OccupanciesService } from './occupancies.service'; import { OccupanciesService } from './occupancies.service';
import { NotificationsService } from '../notifications/notifications.service';
import { NotificationType } from '../entities/notification.entity';
import { CheckInDto, CheckOutDto, TransferRoomDto, BatchCheckOutDto } from './dto/occupancy.dto'; import { CheckInDto, CheckOutDto, TransferRoomDto, BatchCheckOutDto } from './dto/occupancy.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { OperationLogsService } from '../operation-logs/operation-logs.service'; import { OperationLogsService } from '../operation-logs/operation-logs.service';
@@ -29,6 +31,7 @@ export class OccupanciesController {
constructor( constructor(
private service: OccupanciesService, private service: OccupanciesService,
private logService: OperationLogsService, private logService: OperationLogsService,
private readonly notificationsService: NotificationsService,
) {} ) {}
@Get() @Get()
@@ -78,6 +81,7 @@ export class OccupanciesController {
ipAddress, ipAddress,
userAgent, userAgent,
}); });
// TODO: Send notification for check_in — studentId→userId mapping unavailable
return result; return result;
} }
@@ -96,6 +100,7 @@ export class OccupanciesController {
ipAddress, ipAddress,
userAgent, userAgent,
}); });
// TODO: Send notification for check_out — studentId→userId mapping unavailable
return result; return result;
} }

View File

@@ -7,9 +7,10 @@ import { Deposit } from '../entities/deposit.entity';
import { OccupanciesService } from './occupancies.service'; import { OccupanciesService } from './occupancies.service';
import { OccupanciesController } from './occupancies.controller'; import { OccupanciesController } from './occupancies.controller';
import { OperationLogsModule } from '../operation-logs/operation-logs.module'; import { OperationLogsModule } from '../operation-logs/operation-logs.module';
import { NotificationsModule } from '../notifications/notifications.module';
@Module({ @Module({
imports: [TypeOrmModule.forFeature([Occupancy, Room, Student, Deposit]), OperationLogsModule], imports: [TypeOrmModule.forFeature([Occupancy, Room, Student, Deposit]), OperationLogsModule, NotificationsModule],
controllers: [OccupanciesController], controllers: [OccupanciesController],
providers: [OccupanciesService], providers: [OccupanciesService],
exports: [OccupanciesService], exports: [OccupanciesService],

View File

@@ -20,6 +20,9 @@ import {
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { OperationLogsService } from '../operation-logs/operation-logs.service'; import { OperationLogsService } from '../operation-logs/operation-logs.service';
import { extractRequestInfo } from '../common/request-utils'; 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'; import { RequirePermission } from '../auth/decorators/permission.decorator';
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@@ -28,6 +31,7 @@ export class SchedulesController {
constructor( constructor(
private readonly service: SchedulesService, private readonly service: SchedulesService,
private readonly logService: OperationLogsService, private readonly logService: OperationLogsService,
private readonly notificationsService: NotificationsService,
) {} ) {}
@Get() @Get()
@@ -61,19 +65,39 @@ export class SchedulesController {
@RequirePermission('schedule:create') @RequirePermission('schedule:create')
async create(@Body() dto: CreateScheduleDto, @Request() req: { user?: { id: number; username: string }; headers?: Record<string, string> }) { async create(@Body() dto: CreateScheduleDto, @Request() req: { user?: { id: number; username: string }; headers?: Record<string, string> }) {
const { ipAddress, userAgent } = extractRequestInfo(req); const { ipAddress, userAgent } = extractRequestInfo(req);
const result = await this.service.create(dto); try {
await this.logService.log({ const result = await this.service.create(dto);
userId: req.user?.id, await this.logService.log({
username: req.user?.username, userId: req.user?.id,
module: '排课管理', username: req.user?.username,
action: '创建排课', module: '排课管理',
targetId: result.id, action: '创建排课',
targetType: 'class-schedule', targetId: result.id,
detail: `${result.subject}${result.weekDay} ${result.startTime}-${result.endTime}`, targetType: 'class-schedule',
ipAddress, detail: `${result.subject}${result.weekDay} ${result.startTime}-${result.endTime}`,
userAgent, ipAddress,
}); userAgent,
return result; });
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') @Put(':id')
@@ -84,19 +108,39 @@ export class SchedulesController {
@Request() req: { user?: { id: number; username: string }; headers?: Record<string, string> }, @Request() req: { user?: { id: number; username: string }; headers?: Record<string, string> },
) { ) {
const { ipAddress, userAgent } = extractRequestInfo(req); const { ipAddress, userAgent } = extractRequestInfo(req);
const result = await this.service.update(+id, dto); try {
await this.logService.log({ const result = await this.service.update(+id, dto);
userId: req.user?.id, await this.logService.log({
username: req.user?.username, userId: req.user?.id,
module: '排课管理', username: req.user?.username,
action: '编辑排课', module: '排课管理',
targetId: +id, action: '编辑排课',
targetType: 'class-schedule', targetId: +id,
detail: JSON.stringify(dto), targetType: 'class-schedule',
ipAddress, detail: JSON.stringify(dto),
userAgent, ipAddress,
}); userAgent,
return result; });
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') @Delete(':id')

View File

@@ -4,9 +4,10 @@ import { ClassSchedule } from '../entities';
import { SchedulesService } from './schedules.service'; import { SchedulesService } from './schedules.service';
import { SchedulesController } from './schedules.controller'; import { SchedulesController } from './schedules.controller';
import { OperationLogsModule } from '../operation-logs/operation-logs.module'; import { OperationLogsModule } from '../operation-logs/operation-logs.module';
import { NotificationsModule } from '../notifications/notifications.module';
@Module({ @Module({
imports: [TypeOrmModule.forFeature([ClassSchedule]), OperationLogsModule], imports: [TypeOrmModule.forFeature([ClassSchedule]), OperationLogsModule, NotificationsModule],
controllers: [SchedulesController], controllers: [SchedulesController],
providers: [SchedulesService], providers: [SchedulesService],
exports: [SchedulesService], exports: [SchedulesService],