From d9cfd9f933cb260ec43a658db8139e2a471067b0 Mon Sep 17 00:00:00 2001 From: limbo <43649186+HUAHUAI23@users.noreply.github.com> Date: Tue, 19 Dec 2023 22:01:34 -0600 Subject: [PATCH] fix(web&server): fix billing query time boundary problem (#1745) * fix(web&server): Fix billing inquiry time boundary problem * chore --- server/src/billing/billing.service.ts | 36 ++++++++++++------- .../app/setting/UserSetting/Usage/index.tsx | 28 ++++++++++----- 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/server/src/billing/billing.service.ts b/server/src/billing/billing.service.ts index 377066fb49..363cc9e10f 100644 --- a/server/src/billing/billing.service.ts +++ b/server/src/billing/billing.service.ts @@ -24,14 +24,18 @@ export class BillingService { async query(userId: ObjectId, condition?: BillingQuery) { const query = { createdBy: userId } - if (condition.endTime) { - query['endAt'] = { $lte: condition.endTime } - } - if (condition.startTime) { query['startAt'] = { $gte: condition.startTime } } + if (condition.endTime) { + if (condition.startTime) { + query['startAt']['$lte'] = condition.endTime + } else { + query['startAt'] = { $lte: condition.endTime } + } + } + if (condition.appid) { query['appid'] = { $in: condition.appid } } @@ -75,14 +79,18 @@ export class BillingService { async getExpenseByDay(userId: ObjectId, condition?: BillingQuery) { const query = { createdBy: userId } - if (condition.endTime) { - query['endAt'] = { $lte: condition.endTime } - } - if (condition.startTime) { query['startAt'] = { $gte: condition.startTime } } + if (condition.endTime) { + if (condition.startTime) { + query['startAt']['$lte'] = condition.endTime + } else { + query['startAt'] = { $lte: condition.endTime } + } + } + if (condition.appid) { query['appid'] = { $in: condition.appid } } @@ -135,14 +143,18 @@ export class BillingService { async getExpense(userId: ObjectId, condition?: BillingQuery) { const query = { createdBy: userId } - if (condition.endTime) { - query['endAt'] = { $lte: condition.endTime } - } - if (condition.startTime) { query['startAt'] = { $gte: condition.startTime } } + if (condition.endTime) { + if (condition.startTime) { + query['startAt']['$lte'] = condition.endTime + } else { + query['startAt'] = { $lte: condition.endTime } + } + } + if (condition.appid) { query['appid'] = { $in: condition.appid } } diff --git a/web/src/pages/app/setting/UserSetting/Usage/index.tsx b/web/src/pages/app/setting/UserSetting/Usage/index.tsx index 6076b0e37b..6a5055143e 100644 --- a/web/src/pages/app/setting/UserSetting/Usage/index.tsx +++ b/web/src/pages/app/setting/UserSetting/Usage/index.tsx @@ -29,12 +29,12 @@ const DATA_DURATION = 6 * 24 * 60 * 60 * 1000; export default function Usage() { const { t } = useTranslation(); const darkMode = useColorMode().colorMode === "dark"; - const [endTime, setEndTime] = React.useState(() => { + const [endTime, setEndTime] = React.useState(() => { const today = new Date(); today.setHours(23, 59, 59, 999); return today; }); - const [startTime, setStartTime] = React.useState(() => { + const [startTime, setStartTime] = React.useState(() => { const today = new Date(); today.setTime(today.getTime() - DATA_DURATION); today.setHours(0, 0, 0, 0); @@ -47,9 +47,13 @@ export default function Usage() { const { data: billingAmountRes, isLoading: billLoading } = useQuery( ["billing", startTime, endTime], async () => { + const startOfDay = new Date(startTime); + startOfDay.setHours(0, 0, 0, 0); + const endOfDay = new Date(endTime); + endOfDay.setHours(23, 59, 59, 999); return BillingControllerGetExpense({ - startTime: startTime?.getTime(), - endTime: endTime?.getTime(), + startTime: startOfDay?.getTime(), + endTime: endOfDay?.getTime(), }); }, ); @@ -57,9 +61,13 @@ export default function Usage() { const { data: chargeOrderAmountRes, isLoading: chargeLoading } = useQuery( ["chargeOrderAmount", startTime, endTime], async () => { + const startOfDay = new Date(startTime); + startOfDay.setHours(0, 0, 0, 0); + const endOfDay = new Date(endTime); + endOfDay.setHours(23, 59, 59, 999); return AccountControllerGetChargeOrderAmount({ - startTime: startTime?.getTime(), - endTime: endTime?.getTime(), + startTime: startOfDay?.getTime(), + endTime: endOfDay?.getTime(), }); }, ); @@ -67,9 +75,13 @@ export default function Usage() { const { data: billingAmountByDayRes, isLoading: billingLoading } = useQuery( ["billingByDay", startTime, endTime], async () => { + const startOfDay = new Date(startTime); + startOfDay.setHours(0, 0, 0, 0); + const endOfDay = new Date(endTime); + endOfDay.setHours(23, 59, 59, 999); return BillingControllerGetExpenseByDay({ - startTime: startTime?.getTime(), - endTime: endTime?.getTime(), + startTime: startOfDay?.getTime(), + endTime: endOfDay?.getTime(), }); }, );