feat(ai-coach,checkins): 实现软删除功能

在AI教练和打卡模块中添加deleted字段,将物理删除改为软删除:
- 在AiConversation和AiMessage模型中添加deleted布尔字段
- 在Checkin模型中添加deleted字段
- 更新所有查询条件添加deleted: false过滤
- 修改删除操作为标记deleted: true而非物理删除
- 在打卡服务中添加重复记录检查逻辑
This commit is contained in:
richarjiang
2025-08-14 19:16:57 +08:00
parent 9ad65b19fd
commit 812ac5c21e
5 changed files with 58 additions and 10 deletions

View File

@@ -19,6 +19,21 @@ export class CheckinsService {
) { }
async create(dto: CreateCheckinDto): Promise<CheckinResponseDto> {
// 检查是否已存在未删除的记录
const existingRecord = await this.checkinModel.findOne({
where: {
userId: dto.userId,
workoutId: dto.workoutId || null,
planId: dto.planId || null,
checkinDate: dto.checkinDate || null,
deleted: false,
},
});
if (existingRecord) {
return { code: ResponseCode.SUCCESS, message: 'success', data: existingRecord.toJSON() };
}
const record = await this.checkinModel.create({
userId: dto.userId,
workoutId: dto.workoutId || null,
@@ -43,7 +58,12 @@ export class CheckinsService {
}
async update(dto: UpdateCheckinDto, userId: string): Promise<CheckinResponseDto> {
const record = await this.checkinModel.findByPk(dto.id);
const record = await this.checkinModel.findOne({
where: {
id: dto.id,
deleted: false,
},
});
if (!record) {
throw new NotFoundException('打卡记录不存在');
}
@@ -75,7 +95,12 @@ export class CheckinsService {
}
async complete(dto: CompleteCheckinDto, userId: string): Promise<CheckinResponseDto> {
const record = await this.checkinModel.findByPk(dto.id);
const record = await this.checkinModel.findOne({
where: {
id: dto.id,
deleted: false,
},
});
if (!record) {
throw new NotFoundException('打卡记录不存在');
}
@@ -114,7 +139,8 @@ export class CheckinsService {
if (record.userId !== userId) {
throw new ForbiddenException('无权操作该打卡记录');
}
await record.destroy();
record.deleted = true;
await record.save();
await this.activityLogsService.record({
userId: record.userId,
entityType: ActivityEntityType.CHECKIN,
@@ -139,6 +165,7 @@ export class CheckinsService {
const rows = await this.checkinModel.findAll({
where: {
userId,
deleted: false,
[Op.or]: [
{ checkinDate: target.format('YYYY-MM-DD') as any },
{
@@ -170,6 +197,7 @@ export class CheckinsService {
const rows = await this.checkinModel.findAll({
where: {
userId,
deleted: false,
[Op.or]: [
{
checkinDate: {