feat(water-records): 优化创建喝水记录和获取记录列表的日期处理逻辑
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user