增强自定义训练会话创建逻辑,添加详细日志记录以便于调试和错误追踪;调整训练会话查询排序方式,改为按创建时间排序。
This commit is contained in:
@@ -125,11 +125,17 @@ export class WorkoutsService {
|
|||||||
const transaction = await this.workoutSessionModel.sequelize?.transaction();
|
const transaction = await this.workoutSessionModel.sequelize?.transaction();
|
||||||
if (!transaction) throw new Error('Failed to start transaction');
|
if (!transaction) throw new Error('Failed to start transaction');
|
||||||
|
|
||||||
|
this.winstonLogger.info(`创建自定义训练会话`, {
|
||||||
|
context: 'WorkoutsService',
|
||||||
|
userId,
|
||||||
|
dto,
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 1. 创建训练会话
|
// 1. 创建训练会话
|
||||||
const workoutSession = await this.workoutSessionModel.create({
|
const workoutSession = await this.workoutSessionModel.create({
|
||||||
userId,
|
userId,
|
||||||
trainingPlanId: null, // 自定义训练不关联训练计划
|
|
||||||
name: dto.name,
|
name: dto.name,
|
||||||
scheduledDate: dto.scheduledDate ? new Date(dto.scheduledDate) : new Date(),
|
scheduledDate: dto.scheduledDate ? new Date(dto.scheduledDate) : new Date(),
|
||||||
status: 'planned',
|
status: 'planned',
|
||||||
@@ -163,17 +169,23 @@ export class WorkoutsService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await transaction.commit();
|
|
||||||
|
|
||||||
this.winstonLogger.info(`创建自定义训练会话 ${workoutSession.id}`, {
|
this.winstonLogger.info(`创建自定义训练会话 ${workoutSession.id}`, {
|
||||||
context: 'WorkoutsService',
|
context: 'WorkoutsService',
|
||||||
userId,
|
userId,
|
||||||
workoutSessionId: workoutSession.id,
|
workoutSessionId: workoutSession.id,
|
||||||
exerciseCount: dto.customExercises!.length,
|
exerciseCount: dto.customExercises?.length,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await transaction.commit();
|
||||||
|
|
||||||
return workoutSession.toJSON();
|
return workoutSession.toJSON();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
this.winstonLogger.error(`创建自定义训练会话失败`, {
|
||||||
|
context: 'WorkoutsService',
|
||||||
|
userId,
|
||||||
|
dto,
|
||||||
|
error,
|
||||||
|
});
|
||||||
await transaction.rollback();
|
await transaction.rollback();
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
@@ -309,7 +321,7 @@ export class WorkoutsService {
|
|||||||
attributes: ['id', 'name', 'goal']
|
attributes: ['id', 'name', 'goal']
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
order: [['scheduledDate', 'DESC']],
|
order: [['createdAt', 'DESC']],
|
||||||
limit,
|
limit,
|
||||||
offset,
|
offset,
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user