feat:新增活动日志模块,包括控制器、服务、模型及数据传输对象,更新应用模块以引入新模块,并在打卡和训练计划模块中集成活动日志记录功能。

This commit is contained in:
richarjiang
2025-08-14 15:43:29 +08:00
parent 24924e5d81
commit bc8a52852d
15 changed files with 373 additions and 20 deletions

View File

@@ -5,6 +5,8 @@ import { CreateCheckinDto, UpdateCheckinDto, CompleteCheckinDto, RemoveCheckinDt
import { ResponseCode } from '../base.dto';
import * as dayjs from 'dayjs';
import { Op } from 'sequelize';
import { ActivityLogsService } from '../activity-logs/activity-logs.service';
import { ActivityActionType, ActivityEntityType } from '../activity-logs/models/activity-log.model';
@Injectable()
export class CheckinsService {
@@ -13,6 +15,7 @@ export class CheckinsService {
constructor(
@InjectModel(Checkin)
private readonly checkinModel: typeof Checkin,
private readonly activityLogsService: ActivityLogsService,
) { }
async create(dto: CreateCheckinDto): Promise<CheckinResponseDto> {
@@ -28,6 +31,14 @@ export class CheckinsService {
status: CheckinStatus.PENDING,
});
await this.activityLogsService.record({
userId: record.userId,
entityType: ActivityEntityType.CHECKIN,
action: ActivityActionType.CREATE,
entityId: record.id,
changes: record.toJSON(),
});
return { code: ResponseCode.SUCCESS, message: 'success', data: record.toJSON() };
}
@@ -40,18 +51,26 @@ export class CheckinsService {
throw new ForbiddenException('无权操作该打卡记录');
}
if (dto.workoutId !== undefined) record.workoutId = dto.workoutId;
if (dto.planId !== undefined) record.planId = dto.planId;
if (dto.title !== undefined) record.title = dto.title;
if (dto.checkinDate !== undefined) record.checkinDate = dto.checkinDate as any;
if (dto.startedAt !== undefined) record.startedAt = dto.startedAt ? new Date(dto.startedAt) : null;
if (dto.notes !== undefined) record.notes = dto.notes;
if (dto.metrics !== undefined) record.metrics = dto.metrics as any;
if (dto.status !== undefined) record.status = dto.status;
if (dto.completedAt !== undefined) record.completedAt = dto.completedAt ? new Date(dto.completedAt) : null;
if (dto.durationSeconds !== undefined) record.durationSeconds = dto.durationSeconds;
const changes: Record<string, any> = {};
if (dto.workoutId !== undefined) { record.workoutId = dto.workoutId; changes.workoutId = dto.workoutId; }
if (dto.planId !== undefined) { record.planId = dto.planId; changes.planId = dto.planId; }
if (dto.title !== undefined) { record.title = dto.title; changes.title = dto.title; }
if (dto.checkinDate !== undefined) { record.checkinDate = dto.checkinDate as any; changes.checkinDate = dto.checkinDate; }
if (dto.startedAt !== undefined) { record.startedAt = dto.startedAt ? new Date(dto.startedAt) : null; changes.startedAt = dto.startedAt; }
if (dto.notes !== undefined) { record.notes = dto.notes; changes.notes = dto.notes; }
if (dto.metrics !== undefined) { record.metrics = dto.metrics as any; changes.metrics = dto.metrics; }
if (dto.status !== undefined) { record.status = dto.status; changes.status = dto.status; }
if (dto.completedAt !== undefined) { record.completedAt = dto.completedAt ? new Date(dto.completedAt) : null; changes.completedAt = dto.completedAt; }
if (dto.durationSeconds !== undefined) { record.durationSeconds = dto.durationSeconds; changes.durationSeconds = dto.durationSeconds; }
await record.save();
await this.activityLogsService.record({
userId: record.userId,
entityType: ActivityEntityType.CHECKIN,
action: ActivityActionType.UPDATE,
entityId: record.id,
changes,
});
return { code: ResponseCode.SUCCESS, message: 'success', data: record.toJSON() };
}
@@ -71,6 +90,19 @@ export class CheckinsService {
if (dto.metrics !== undefined) record.metrics = dto.metrics as any;
await record.save();
await this.activityLogsService.record({
userId: record.userId,
entityType: ActivityEntityType.CHECKIN,
action: ActivityActionType.UPDATE,
entityId: record.id,
changes: {
status: record.status,
completedAt: record.completedAt,
durationSeconds: record.durationSeconds,
notes: record.notes,
metrics: record.metrics,
},
});
return { code: ResponseCode.SUCCESS, message: 'success', data: record.toJSON() };
}
@@ -83,6 +115,13 @@ export class CheckinsService {
throw new ForbiddenException('无权操作该打卡记录');
}
await record.destroy();
await this.activityLogsService.record({
userId: record.userId,
entityType: ActivityEntityType.CHECKIN,
action: ActivityActionType.DELETE,
entityId: record.id,
changes: null,
});
return { code: ResponseCode.SUCCESS, message: 'success', data: { id: dto.id } };
}