feat: 更新目标修改逻辑,增加日志记录和日期处理优化

- 在updateGoal方法中添加日志记录,便于调试和监控目标更新过程。
- 优化目标更新时的日期处理逻辑,确保endDate字段在未提供时设置为undefined,提升数据的灵活性。
This commit is contained in:
2025-08-26 22:12:46 +08:00
parent 475f928990
commit 8aca29e2b3

View File

@@ -146,6 +146,7 @@ export class GoalsService {
*/ */
async updateGoal(userId: string, goalId: string, updateGoalDto: UpdateGoalDto): Promise<Goal> { async updateGoal(userId: string, goalId: string, updateGoalDto: UpdateGoalDto): Promise<Goal> {
try { try {
this.logger.log(`updateGoal updateGoalDto: ${JSON.stringify(updateGoalDto, null, 2)}`);
const goal = await this.goalModel.findOne({ const goal = await this.goalModel.findOne({
where: { id: goalId, userId, deleted: false }, where: { id: goalId, userId, deleted: false },
}); });
@@ -166,10 +167,11 @@ export class GoalsService {
throw new BadRequestException('已完成的目标不能修改状态'); throw new BadRequestException('已完成的目标不能修改状态');
} }
await goal.update({ await goal.update({
...updateGoalDto, ...updateGoalDto,
startDate: updateGoalDto.startDate ? new Date(updateGoalDto.startDate) : goal.startDate, endDate: updateGoalDto.endDate ? new Date(updateGoalDto.endDate) : undefined,
endDate: updateGoalDto.endDate ? new Date(updateGoalDto.endDate) : goal.endDate,
}); });
this.logger.log(`用户 ${userId} 更新了目标: ${goal.title}`); this.logger.log(`用户 ${userId} 更新了目标: ${goal.title}`);