增强自定义训练会话创建逻辑,添加详细日志记录以便于调试和错误追踪;调整训练会话查询排序方式,改为按创建时间排序。

This commit is contained in:
2025-08-16 14:19:50 +08:00
parent 477f5b4b79
commit e719c959aa

View File

@@ -125,11 +125,17 @@ export class WorkoutsService {
const transaction = await this.workoutSessionModel.sequelize?.transaction();
if (!transaction) throw new Error('Failed to start transaction');
this.winstonLogger.info(`创建自定义训练会话`, {
context: 'WorkoutsService',
userId,
dto,
});
try {
// 1. 创建训练会话
const workoutSession = await this.workoutSessionModel.create({
userId,
trainingPlanId: null, // 自定义训练不关联训练计划
name: dto.name,
scheduledDate: dto.scheduledDate ? new Date(dto.scheduledDate) : new Date(),
status: 'planned',
@@ -163,17 +169,23 @@ export class WorkoutsService {
}
}
await transaction.commit();
this.winstonLogger.info(`创建自定义训练会话 ${workoutSession.id}`, {
context: 'WorkoutsService',
userId,
workoutSessionId: workoutSession.id,
exerciseCount: dto.customExercises!.length,
exerciseCount: dto.customExercises?.length,
});
await transaction.commit();
return workoutSession.toJSON();
} catch (error) {
this.winstonLogger.error(`创建自定义训练会话失败`, {
context: 'WorkoutsService',
userId,
dto,
error,
});
await transaction.rollback();
throw error;
}
@@ -309,7 +321,7 @@ export class WorkoutsService {
attributes: ['id', 'name', 'goal']
}
],
order: [['scheduledDate', 'DESC']],
order: [['createdAt', 'DESC']],
limit,
offset,
});