feat(water-records): 优化创建喝水记录和获取记录列表的日期处理逻辑

This commit is contained in:
richarjiang
2025-09-02 15:27:21 +08:00
parent 730b1df35e
commit 02f21f0858
2 changed files with 479 additions and 6 deletions

View File

@@ -13,6 +13,7 @@ import {
WaterStatsResponseDto
} from './dto/water-record.dto';
import { Op } from 'sequelize';
import * as dayjs from 'dayjs';
@Injectable()
export class WaterRecordsService {
@@ -30,14 +31,15 @@ export class WaterRecordsService {
*/
async createWaterRecord(userId: string, createDto: CreateWaterRecordDto): Promise<WaterRecordResponseDto> {
try {
this.logger.log(`createWaterRecord userId: ${userId}, createDto: ${JSON.stringify(createDto, null, 2)}`);
const waterRecord = await this.userWaterHistoryModel.create({
userId,
amount: createDto.amount,
recordedAt: createDto.recordedAt || new Date(),
recordedAt: new Date(),
note: createDto.note,
});
this.logger.log(`用户 ${userId} 创建喝水记录成功记录ID: ${waterRecord.id}`);
this.logger.log(`createWaterRecord 用户 ${userId} 创建喝水记录成功记录ID: ${waterRecord.id}`);
return {
success: true,
@@ -71,12 +73,10 @@ export class WaterRecordsService {
if (startDate || endDate) {
whereCondition.recordedAt = {};
if (startDate) {
whereCondition.recordedAt[Op.gte] = new Date(startDate);
whereCondition.recordedAt[Op.gte] = dayjs(startDate).startOf('day').toDate();
}
if (endDate) {
const endDateTime = new Date(endDate);
endDateTime.setHours(23, 59, 59, 999);
whereCondition.recordedAt[Op.lte] = endDateTime;
whereCondition.recordedAt[Op.lte] = dayjs(endDate).endOf('day').toDate();
}
}