Skip to content

Commit 90acd30

Browse files
authored
fix(console): fix overdue invoices notification (#7922)
fix(console): fix overdue payment notification fix overdue payment notification
1 parent 04b0766 commit 90acd30

File tree

4 files changed

+36
-30
lines changed

4 files changed

+36
-30
lines changed

packages/console/src/components/PaymentOverdueModal/index.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ import styles from './index.module.scss';
1919
function PaymentOverdueModal() {
2020
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
2121
const { currentTenant, currentTenantId } = useContext(TenantsContext);
22-
const { openInvoices = [] } = currentTenant ?? {};
2322

2423
const { visitManagePaymentPage } = useSubscribe();
2524
const [isActionLoading, setIsActionLoading] = useState(false);
2625
const [hasClosed, setHasClosed] = useState(false);
2726

28-
const { hasOverdueInvoices } = useOverdueInvoices();
27+
const { hasOverdueInvoices, overdueInvoices } = useOverdueInvoices(currentTenant);
2928

3029
const handleCloseModal = () => {
3130
setHasClosed(true);
@@ -73,7 +72,7 @@ function PaymentOverdueModal() {
7372
)}
7473
<FormField title="upsell.payment_overdue_modal.unpaid_bills">
7574
<BillInfo
76-
cost={openInvoices.reduce(
75+
cost={overdueInvoices.reduce(
7776
(total, currentInvoice) => total + currentInvoice.amountDue,
7877
0
7978
)}

packages/console/src/components/Topbar/TenantSelector/TenantDropdownItem/TenantStatusTag.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function TenantStatusTag({ tenantData, className }: Props) {
1717
subscription: { planId, isEnterprisePlan },
1818
} = tenantData;
1919

20-
const { hasOverdueInvoices } = useOverdueInvoices();
20+
const { hasOverdueInvoices } = useOverdueInvoices(tenantData);
2121

2222
/**
2323
* Tenant status priority:

packages/console/src/components/Topbar/TenantSelector/TenantDropdownItem/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function TenantDropdownItem({ tenantData, isSelected, onClick }: Props) {
2222
name,
2323
tag,
2424
regionName,
25-
subscription: { planId, isEnterprisePlan },
25+
subscription: { planId },
2626
} = tenantData;
2727
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
2828

packages/console/src/hooks/use-overdue-invoices.ts

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
1-
import { useContext, useMemo } from 'react';
1+
import dayjs from 'dayjs';
2+
import { useMemo } from 'react';
23

4+
import { type TenantResponse } from '@/cloud/types/router';
35
import { isCloud } from '@/consts/env';
4-
import { TenantsContext } from '@/contexts/TenantsProvider';
56

6-
function useOverdueInvoices() {
7-
const { currentTenant } = useContext(TenantsContext);
8-
const { openInvoices = [] } = currentTenant ?? {};
9-
const isEnterprise = currentTenant?.subscription.isEnterprisePlan;
7+
function useOverdueInvoices(tenantData: TenantResponse | undefined) {
8+
const { openInvoices = [] } = tenantData ?? {};
9+
const isEnterprise = tenantData?.subscription.isEnterprisePlan;
10+
11+
const overdueInvoices = useMemo(
12+
() =>
13+
openInvoices.filter(({ dueDate, collectionMethod }) => {
14+
switch (collectionMethod) {
15+
// For automatic collection method, consider open invoices always overdue
16+
case 'charge_automatically': {
17+
return true;
18+
}
19+
// For manual collection method, consider invoices overdue if past due date
20+
case 'send_invoice': {
21+
return dueDate && dayjs().isAfter(dayjs(dueDate));
22+
}
23+
default: {
24+
// For legacy invoices without collection method, consider them overdue
25+
return true;
26+
}
27+
}
28+
}),
29+
[openInvoices]
30+
);
1031

1132
const hasOverdueInvoices = useMemo(() => {
12-
if (!isCloud || openInvoices.length === 0) {
33+
if (!isCloud) {
1334
return false;
1435
}
1536

@@ -21,30 +42,16 @@ function useOverdueInvoices() {
2142
return false;
2243
}
2344

24-
return openInvoices.some(({ dueDate, collectionMethod }) => {
25-
switch (collectionMethod) {
26-
// For automatic collection method, consider open invoices always overdue
27-
case 'charge_automatically': {
28-
return true;
29-
}
30-
// For manual collection method, consider invoices overdue if past due date
31-
case 'send_invoice': {
32-
return dueDate && dueDate < new Date();
33-
}
34-
default: {
35-
// For legacy invoices without collection method, consider them overdue
36-
return true;
37-
}
38-
}
39-
});
40-
}, [isEnterprise, openInvoices]);
45+
return overdueInvoices.length > 0;
46+
}, [isEnterprise, overdueInvoices]);
4147

4248
return useMemo(
4349
() => ({
4450
hasOverdueInvoices,
51+
overdueInvoices,
4552
openInvoices,
4653
}),
47-
[hasOverdueInvoices, openInvoices]
54+
[hasOverdueInvoices, openInvoices, overdueInvoices]
4855
);
4956
}
5057

0 commit comments

Comments
 (0)