Skip to content

Commit 6e92cc8

Browse files
committed
update code
1 parent 1fce133 commit 6e92cc8

2 files changed

Lines changed: 39 additions & 5 deletions

File tree

src/libs/actions/SplitExpenses.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import isSearchTopmostFullScreenRoute from '@libs/Navigation/helpers/isSearchTop
66
import Navigation from '@libs/Navigation/Navigation';
77
import {rand64} from '@libs/NumberUtils';
88
import {getTransactionDetails, isOpenReport} from '@libs/ReportUtils';
9+
import {shouldRestrictUserBillableActions} from '@libs/SubscriptionUtils';
910
import {buildOptimisticTransaction, getChildTransactions, getOriginalTransactionWithSplitInfo, isDistanceRequest} from '@libs/TransactionUtils';
1011
import CONST from '@src/CONST';
1112
import ONYXKEYS from '@src/ONYXKEYS';
1213
import ROUTES from '@src/ROUTES';
13-
import type {Policy, Report, Transaction} from '@src/types/onyx';
14+
import type {BillingGraceEndPeriod, Policy, Report, Transaction} from '@src/types/onyx';
1415
import type {Attendee} from '@src/types/onyx/IOU';
1516
import type {TransactionCustomUnit} from '@src/types/onyx/Transaction';
1617
import {initSplitExpenseItemData, updateSplitExpenseDistanceFromAmount} from './IOU/SplitExpenseItems';
@@ -35,14 +36,47 @@ Onyx.connectWithoutView({
3536
callback: (value) => (allReports = value),
3637
});
3738

39+
let ownerBillingGracePeriodEnd: OnyxEntry<number>;
40+
// We use connectWithoutView because `initSplitExpense` doesn't affect the UI rendering and
41+
// this avoids unnecessary re-rendering for components when owner billing grace period changes. This data should ONLY
42+
// be used for `initSplitExpense`
43+
Onyx.connectWithoutView({
44+
key: ONYXKEYS.NVP_PRIVATE_OWNER_BILLING_GRACE_PERIOD_END,
45+
callback: (value) => (ownerBillingGracePeriodEnd = value),
46+
});
47+
48+
let userBillingGracePeriodEnds: OnyxCollection<BillingGraceEndPeriod>;
49+
// We use connectWithoutView because `initSplitExpense` doesn't affect the UI rendering and
50+
// this avoids unnecessary re-rendering for components when user billing grace periods change. This data should ONLY
51+
// be used for `initSplitExpense`
52+
Onyx.connectWithoutView({
53+
key: ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_USER_BILLING_GRACE_PERIOD_END,
54+
waitForCollectionCallback: true,
55+
callback: (value) => (userBillingGracePeriodEnds = value),
56+
});
57+
58+
let amountOwed: OnyxEntry<number>;
59+
// We use connectWithoutView because `initSplitExpense` doesn't affect the UI rendering and
60+
// this avoids unnecessary re-rendering for components when amount owed changes. This data should ONLY
61+
// be used for `initSplitExpense`
62+
Onyx.connectWithoutView({
63+
key: ONYXKEYS.NVP_PRIVATE_AMOUNT_OWED,
64+
callback: (value) => (amountOwed = value),
65+
});
66+
3867
/**
3968
* Create a draft transaction to set up split expense details for the split expense flow
4069
*/
41-
function initSplitExpense(transaction: OnyxEntry<Transaction>, policy?: OnyxEntry<Policy>): void {
70+
function initSplitExpense(transaction: OnyxEntry<Transaction>, policy: OnyxEntry<Policy>): void {
4271
if (!transaction) {
4372
return;
4473
}
4574

75+
if (!!policy && shouldRestrictUserBillableActions(policy, ownerBillingGracePeriodEnd, userBillingGracePeriodEnds, amountOwed)) {
76+
Navigation.navigate(ROUTES.RESTRICTED_ACTION.getRoute(policy.id));
77+
return;
78+
}
79+
4680
const reportID = transaction.reportID ?? String(CONST.DEFAULT_NUMBER_ID);
4781
const originalTransactionID = transaction?.comment?.originalTransactionID;
4882
const originalTransaction = allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${originalTransactionID}`];

tests/actions/IOUTest/SplitTest.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4670,7 +4670,7 @@ describe('initSplitExpense', () => {
46704670
reportID: '456',
46714671
};
46724672

4673-
initSplitExpense(transaction);
4673+
initSplitExpense(transaction, undefined);
46744674
await waitForBatchedUpdates();
46754675

46764676
const draftTransaction = await getOnyxValue(`${ONYXKEYS.COLLECTION.SPLIT_TRANSACTION_DRAFT}${transaction.transactionID}`);
@@ -4695,7 +4695,7 @@ describe('initSplitExpense', () => {
46954695
});
46964696
it('should not initialize split expense for null transaction', async () => {
46974697
const transaction: Transaction | undefined = undefined;
4698-
initSplitExpense(transaction);
4698+
initSplitExpense(transaction, undefined);
46994699
await waitForBatchedUpdates();
47004700

47014701
expect(transaction).toBeFalsy();
@@ -4719,7 +4719,7 @@ describe('initSplitExpense', () => {
47194719
reportID: '456',
47204720
};
47214721

4722-
initSplitExpense(transaction);
4722+
initSplitExpense(transaction, undefined);
47234723
await waitForBatchedUpdates();
47244724

47254725
const draftTransaction = await getOnyxValue(`${ONYXKEYS.COLLECTION.SPLIT_TRANSACTION_DRAFT}${transaction.transactionID}`);

0 commit comments

Comments
 (0)