Skip to content

Commit

Permalink
fix(web&server): fix billing query time boundary problem (#1745)
Browse files Browse the repository at this point in the history
* fix(web&server): Fix billing inquiry time boundary problem

* chore
  • Loading branch information
HUAHUAI23 authored Dec 20, 2023
1 parent f1fd3ba commit d9cfd9f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
36 changes: 24 additions & 12 deletions server/src/billing/billing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand Down Expand Up @@ -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 }
}
Expand Down Expand Up @@ -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 }
}
Expand Down
28 changes: 20 additions & 8 deletions web/src/pages/app/setting/UserSetting/Usage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Date | null>(() => {
const [endTime, setEndTime] = React.useState<Date>(() => {
const today = new Date();
today.setHours(23, 59, 59, 999);
return today;
});
const [startTime, setStartTime] = React.useState<Date | null>(() => {
const [startTime, setStartTime] = React.useState<Date>(() => {
const today = new Date();
today.setTime(today.getTime() - DATA_DURATION);
today.setHours(0, 0, 0, 0);
Expand All @@ -47,29 +47,41 @@ 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(),
});
},
);

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(),
});
},
);

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(),
});
},
);
Expand Down

0 comments on commit d9cfd9f

Please sign in to comment.