diff --git a/src/training-plans/dto/schedule-exercise.dto.ts b/src/training-plans/dto/schedule-exercise.dto.ts index b9ff154..63085db 100644 --- a/src/training-plans/dto/schedule-exercise.dto.ts +++ b/src/training-plans/dto/schedule-exercise.dto.ts @@ -51,11 +51,6 @@ export class CreateScheduleExerciseDto { @IsEnum(['exercise', 'rest', 'note']) @IsOptional() itemType?: ScheduleItemType; - - @ApiProperty({ description: '是否已完成', default: false, required: false }) - @IsBoolean() - @IsOptional() - completed?: boolean; } export class UpdateScheduleExerciseDto extends PartialType(CreateScheduleExerciseDto) { } diff --git a/src/training-plans/models/schedule-exercise.model.ts b/src/training-plans/models/schedule-exercise.model.ts index 9563ff7..6ea24f9 100644 --- a/src/training-plans/models/schedule-exercise.model.ts +++ b/src/training-plans/models/schedule-exercise.model.ts @@ -60,9 +60,6 @@ export class ScheduleExercise extends Model { }) declare itemType: ScheduleItemType; - @Column({ type: DataType.BOOLEAN, defaultValue: false, comment: '是否已完成' }) - declare completed: boolean; - @Column({ type: DataType.INTEGER, allowNull: false, comment: '排序顺序' }) declare sortOrder: number; diff --git a/src/training-plans/schedule-exercise.service.ts b/src/training-plans/schedule-exercise.service.ts index 4cf3b00..86e0dcd 100644 --- a/src/training-plans/schedule-exercise.service.ts +++ b/src/training-plans/schedule-exercise.service.ts @@ -12,7 +12,7 @@ import { ActivityLogsService } from '../activity-logs/activity-logs.service'; import { ActivityActionType, ActivityEntityType } from '../activity-logs/models/activity-log.model'; import { Logger as WinstonLogger } from 'winston'; import { WINSTON_MODULE_PROVIDER } from 'nest-winston'; -import { Op, Transaction } from 'sequelize'; +import { Op } from 'sequelize'; @Injectable() export class ScheduleExerciseService { @@ -79,7 +79,6 @@ export class ScheduleExerciseService { restSec: dto.restSec, note: dto.note || '', itemType: dto.itemType || 'exercise', - completed: dto.completed || false, sortOrder, }); @@ -182,7 +181,6 @@ export class ScheduleExerciseService { if (dto.restSec !== undefined) exercise.restSec = dto.restSec; if (dto.note !== undefined) exercise.note = dto.note || ''; if (dto.itemType !== undefined) exercise.itemType = dto.itemType; - if (dto.completed !== undefined) exercise.completed = dto.completed; await exercise.save(); diff --git a/src/workouts/workouts.service.ts b/src/workouts/workouts.service.ts index eab4e78..33d2be4 100644 --- a/src/workouts/workouts.service.ts +++ b/src/workouts/workouts.service.ts @@ -109,7 +109,11 @@ export class WorkoutsService { }); if (!activeTrainingPlan) { - throw new NotFoundException('请先激活一个训练计划'); + this.winstonLogger.info(`今日没有激活的训练计划`, { + context: 'WorkoutsService', + userId, + }); + return null; } // 创建今日训练会话 @@ -352,28 +356,44 @@ export class WorkoutsService { * 删除训练会话 */ async deleteWorkoutSession(userId: string, sessionId: string) { - const [count] = await this.workoutSessionModel.update( - { deleted: true }, - { where: { id: sessionId, userId, deleted: false } } - ); + const transaction = await this.workoutSessionModel.sequelize?.transaction(); + if (!transaction) throw new Error('Failed to start transaction'); - if (count === 0) { - throw new NotFoundException('训练会话不存在'); + try { + const [count] = await this.workoutSessionModel.update( + { deleted: true }, + { where: { id: sessionId, userId, deleted: false }, transaction } + ); + + if (count === 0) { + throw new NotFoundException('训练会话不存在'); + } + + // 同时删除关联的训练动作 + await this.workoutExerciseModel.update( + { deleted: true }, + { where: { workoutSessionId: sessionId, userId, deleted: false }, transaction } + ); + + await transaction.commit(); + + this.winstonLogger.info(`删除训练会话 ${sessionId}`, { + context: 'WorkoutsService', + userId, + sessionId, + }); + + return { success: true }; + } catch (error) { + await transaction.rollback(); + this.winstonLogger.error(`删除训练会话失败 ${sessionId}`, { + context: 'WorkoutsService', + userId, + sessionId, + error, + }); + throw error; } - - // 同时删除关联的训练动作 - await this.workoutExerciseModel.update( - { deleted: true }, - { where: { workoutSessionId: sessionId, userId, deleted: false } } - ); - - this.winstonLogger.info(`删除训练会话 ${sessionId}`, { - context: 'WorkoutsService', - userId, - sessionId, - }); - - return { success: true }; } // ==================== 训练动作管理 ====================