Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import useNetwork from '@hooks/useNetwork';
import useOnyx from '@hooks/useOnyx';
import usePaginatedReportActions from '@hooks/usePaginatedReportActions';
import useParticipantsInvoiceReport from '@hooks/useParticipantsInvoiceReport';
import usePayChatReportActions from '@hooks/usePayChatReportActions';
import usePaymentOptions from '@hooks/usePaymentOptions';
import usePermissions from '@hooks/usePermissions';
import usePolicy from '@hooks/usePolicy';
Expand Down Expand Up @@ -142,6 +143,7 @@ function MoneyReportHeaderSecondaryActionsContent({reportID, primaryAction, isRe
const isInvoiceReport = isInvoiceReportUtil(moneyRequestReport);
const isAnyTransactionOnHold = hasHeldExpensesReportUtils(allTransactions);
const existingB2BInvoiceReport = useParticipantsInvoiceReport(activePolicyID, CONST.REPORT.INVOICE_RECEIVER_TYPE.BUSINESS, chatReport?.policyID);
const getChatReportActions = usePayChatReportActions(chatReport, existingB2BInvoiceReport);

const confirmPayment = ({paymentType: type, payAsBusiness, methodID, paymentMethod}: PaymentActionParams) => {
if (!type || !chatReport) {
Expand Down Expand Up @@ -186,6 +188,7 @@ function MoneyReportHeaderSecondaryActionsContent({reportID, primaryAction, isRe
betas,
isSelfTourViewed,
defaultWorkspaceName: generateDefaultWorkspaceName(email ?? '', lastWorkspaceNumber, translate),
chatReportActions: getChatReportActions(payAsBusiness),
});
} else {
startAnimation();
Expand All @@ -209,6 +212,7 @@ function MoneyReportHeaderSecondaryActionsContent({reportID, primaryAction, isRe
onPaid: () => {
startAnimation();
},
chatReportActions: getChatReportActions(false),
});
if (currentSearchQueryJSON && !isOffline) {
search({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useOnyx from '@hooks/useOnyx';
import useParticipantsInvoiceReport from '@hooks/useParticipantsInvoiceReport';
import usePayChatReportActions from '@hooks/usePayChatReportActions';
import usePaymentOptions from '@hooks/usePaymentOptions';
import usePermissions from '@hooks/usePermissions';
import usePolicy from '@hooks/usePolicy';
Expand Down Expand Up @@ -128,6 +129,7 @@ function MoneyReportHeaderSelectionDropdown({reportID, primaryAction, isReportIn
const activePolicy = usePolicy(activePolicyID);
const chatReportPolicy = usePolicy(chatReport?.policyID);
const existingB2BInvoiceReport = useParticipantsInvoiceReport(activePolicyID, CONST.REPORT.INVOICE_RECEIVER_TYPE.BUSINESS, chatReport?.policyID);
const getChatReportActions = usePayChatReportActions(chatReport, existingB2BInvoiceReport);
const isInvoiceReport = isInvoiceReportUtil(moneyRequestReport);

const {transactionThreadReportID, reportActions} = useTransactionThreadReport(reportID);
Expand Down Expand Up @@ -286,6 +288,7 @@ function MoneyReportHeaderSelectionDropdown({reportID, primaryAction, isReportIn
betas,
isSelfTourViewed,
defaultWorkspaceName: generateDefaultWorkspaceName(email ?? '', lastWorkspaceNumber, translate),
chatReportActions: getChatReportActions(payAsBusiness),
});
} else {
payMoneyRequest({
Expand All @@ -308,6 +311,7 @@ function MoneyReportHeaderSelectionDropdown({reportID, primaryAction, isReportIn
onPaid: () => {
startAnimation();
},
chatReportActions: getChatReportActions(false),
});
if (currentSearchQueryJSON && !isOffline) {
search({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useOnyx from '@hooks/useOnyx';
import useParticipantsInvoiceReport from '@hooks/useParticipantsInvoiceReport';
import usePayChatReportActions from '@hooks/usePayChatReportActions';
import usePolicy from '@hooks/usePolicy';
import useSearchShouldCalculateTotals from '@hooks/useSearchShouldCalculateTotals';
import useTransactionsAndViolationsForReport from '@hooks/useTransactionsAndViolationsForReport';
Expand Down Expand Up @@ -64,6 +65,7 @@ function PayPrimaryAction({reportID, chatReportID}: PayPrimaryActionProps) {
const invoiceReceiverPolicyID = chatReport?.invoiceReceiver && 'policyID' in chatReport.invoiceReceiver ? chatReport.invoiceReceiver.policyID : undefined;
const [invoiceReceiverPolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${invoiceReceiverPolicyID}`);
const existingB2BInvoiceReport = useParticipantsInvoiceReport(activePolicyID, CONST.REPORT.INVOICE_RECEIVER_TYPE.BUSINESS, chatReport?.policyID);
const getChatReportActions = usePayChatReportActions(chatReport, existingB2BInvoiceReport);
const {convertToDisplayString} = useCurrencyListActions();

const isInvoiceReport = isInvoiceReportUtil(moneyRequestReport);
Expand Down Expand Up @@ -146,6 +148,7 @@ function PayPrimaryAction({reportID, chatReportID}: PayPrimaryActionProps) {
betas,
isSelfTourViewed,
defaultWorkspaceName: generateDefaultWorkspaceName(email ?? '', lastWorkspaceNumber, translate),
chatReportActions: getChatReportActions(payAsBusiness),
});
} else {
startAnimation();
Expand All @@ -167,6 +170,7 @@ function PayPrimaryAction({reportID, chatReportID}: PayPrimaryActionProps) {
ownerBillingGracePeriodEnd,
methodID: type === CONST.IOU.PAYMENT_TYPE.VBBA ? methodID : undefined,
onPaid: startAnimation,
chatReportActions: getChatReportActions(false),
});
if (currentSearchQueryJSON && !isOffline) {
search({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useOnyx from '@hooks/useOnyx';
import useParticipantsInvoiceReport from '@hooks/useParticipantsInvoiceReport';
import usePayChatReportActions from '@hooks/usePayChatReportActions';
import usePermissions from '@hooks/usePermissions';
import usePolicy from '@hooks/usePolicy';
import useReportTransactionsCollection from '@hooks/useReportTransactionsCollection';
Expand Down Expand Up @@ -99,6 +100,7 @@ function PayActionButton({

const isASAPSubmitBetaEnabled = isBetaEnabled(CONST.BETAS.ASAP_SUBMIT);
const existingB2BInvoiceReport = useParticipantsInvoiceReport(activePolicyID, CONST.REPORT.INVOICE_RECEIVER_TYPE.BUSINESS, chatReport?.policyID);
const getChatReportActions = usePayChatReportActions(chatReport, existingB2BInvoiceReport);
const canAllowSettlement = hasUpdatedTotal(iouReport, policy);
const hasViolations = hasViolationsReportUtils(iouReport?.reportID, transactionViolations, currentUserAccountID, currentUserEmail);

Expand Down Expand Up @@ -188,6 +190,7 @@ function PayActionButton({
betas,
isSelfTourViewed,
defaultWorkspaceName: generateDefaultWorkspaceName(currentUserEmail, lastWorkspaceNumber, translate),
chatReportActions: getChatReportActions(payAsBusiness),
});
} else {
payMoneyRequest({
Expand All @@ -207,6 +210,7 @@ function PayActionButton({
amountOwed,
ownerBillingGracePeriodEnd,
onPaid: startAnimation,
chatReportActions: getChatReportActions(false),
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import useThemeStyles from '@hooks/useThemeStyles';
import {payInvoice, payMoneyRequest} from '@libs/actions/IOU/PayMoneyRequest';
import {canIOUBePaid} from '@libs/actions/IOU/ReportWorkflow';
import {getSearchPayOnyxData} from '@libs/actions/Search';
import {isInvoiceReport} from '@libs/ReportUtils';
import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID';
import {isIndividualInvoiceRoom, isInvoiceReport} from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
Expand All @@ -42,6 +43,7 @@ function PayActionCell({isLoading, policyID, reportID, hash, amount, extraSmall,
const policy = usePolicy(policyID);
const [bankAccountList] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST);
const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT);
const [allReportActions] = useOnyx(ONYXKEYS.COLLECTION.REPORT_ACTIONS);
const [reportNameValuePairs] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS);
const invoiceReceiverPolicyID = chatReport?.invoiceReceiver && 'policyID' in chatReport.invoiceReceiver ? chatReport.invoiceReceiver.policyID : undefined;
const invoiceReceiverPolicy = usePolicy(invoiceReceiverPolicyID);
Expand Down Expand Up @@ -93,6 +95,13 @@ function PayActionCell({isLoading, policyID, reportID, hash, amount, extraSmall,
invoiceReceiverPolicyID ?? chatReport?.policyID,
);

// getPayMoneyRequestParams resolves the chat report from `chatReport` but swaps to `existingB2BInvoiceReport`
// when paying an individual invoice room as a business. `payAsBusiness` is only known at click time, so pick
// the right report's actions here in the function scope.
const shouldUseB2BInvoiceReport = !!payAsBusiness && !!existingB2BInvoiceReport && isIndividualInvoiceRoom(chatReport);
const chatReportActions =
allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${getNonEmptyStringOnyxID(shouldUseB2BInvoiceReport ? existingB2BInvoiceReport?.reportID : chatReport?.reportID)}`];

payInvoice({
paymentMethodType: type,
chatReport,
Expand All @@ -111,6 +120,7 @@ function PayActionCell({isLoading, policyID, reportID, hash, amount, extraSmall,
isSelfTourViewed,
defaultWorkspaceName,
additionalOnyxData,
chatReportActions,
});
return;
}
Expand All @@ -133,6 +143,7 @@ function PayActionCell({isLoading, policyID, reportID, hash, amount, extraSmall,
ownerBillingGracePeriodEnd,
methodID: type === CONST.IOU.PAYMENT_TYPE.VBBA ? methodID : undefined,
additionalOnyxData,
chatReportActions: allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${getNonEmptyStringOnyxID(chatReport?.reportID)}`],
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ function ExpenseReportListItemInner<TItem extends ListItem>({
}, [searchData, snapshotReport?.chatReportID, reportItem.parentReportID]);

const chatReport = parentChatReport ?? snapshotChatReport;
const [chatReportActions] = originalUseOnyx(
`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${getNonEmptyStringOnyxID(chatReport?.reportID ?? snapshotReport?.chatReportID ?? snapshotReport.parentReportID)}`,
);

const snapshotPolicy = useMemo(() => {
return (searchData?.[`${ONYXKEYS.COLLECTION.POLICY}${reportItem.policyID}`] ?? {}) as Policy;
Expand Down Expand Up @@ -277,6 +280,7 @@ function ExpenseReportListItemInner<TItem extends ListItem>({
chatReportPolicy,
iouReportCurrentNextStepDeprecated: nextStep,
searchData,
chatReportActions,
});
}, [
currentSearchHash,
Expand Down Expand Up @@ -312,6 +316,7 @@ function ExpenseReportListItemInner<TItem extends ListItem>({
activePolicy,
chatReportPolicy,
nextStep,
chatReportActions,
]);

const handleSelectionButtonPress = useCallback(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ function ReportListItemHeaderInner<TItem extends ListItem>({
const [parentPolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${getNonEmptyStringOnyxID(snapshotReport?.policyID ?? reportItem.policyID)}`);
const [parentChatReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${getNonEmptyStringOnyxID(snapshotReport?.chatReportID ?? reportItem.parentReportID)}`);
const chatReport = parentChatReport ?? snapshotChatReport;
const [chatReportActions] = useOnyx(
`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${getNonEmptyStringOnyxID(chatReport?.reportID ?? snapshotReport?.chatReportID ?? snapshotReport.parentReportID)}`,
);
const {currentUserAccountID, currentUserLogin, introSelected, betas, isSelfTourViewed, activePolicy, nextStep, chatReportPolicy, amountOwed} = useReportPaymentContext({
reportID: reportItem.reportID,
chatReportPolicyID: chatReport?.policyID,
Expand Down Expand Up @@ -308,6 +311,7 @@ function ReportListItemHeaderInner<TItem extends ListItem>({
chatReportPolicy,
iouReportCurrentNextStepDeprecated: nextStep,
searchData: snapshot?.data,
chatReportActions,
});
};
return !isLargeScreenWidth ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ function TransactionListItemInner<TItem extends ListItem>({
]);
const currentUserDetails = useCurrentUserPersonalDetails();
const [parentChatReport] = originalUseOnyx(`${ONYXKEYS.COLLECTION.REPORT}${getNonEmptyStringOnyxID(snapshotReport?.chatReportID)}`);
const [chatReportActions] = originalUseOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${getNonEmptyStringOnyxID(snapshotReport?.chatReportID ?? snapshotReport?.parentReportID)}`);
const {amountOwed, currentUserAccountID, currentUserLogin, introSelected, betas, isSelfTourViewed, activePolicy, nextStep, chatReportPolicy} = useReportPaymentContext({
reportID: transactionItem.reportID,
chatReportPolicyID: parentChatReport?.policyID,
Expand Down Expand Up @@ -214,6 +215,7 @@ function TransactionListItemInner<TItem extends ListItem>({
chatReportPolicy,
iouReportCurrentNextStepDeprecated: nextStep,
searchData: currentSearchResults?.data,
chatReportActions,
});
};

Expand Down
3 changes: 3 additions & 0 deletions src/hooks/useHoldMenuSubmit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {PaymentMethodType} from '@src/types/onyx/OriginalMessage';
import type DeepValueOf from '@src/types/utils/DeepValueOf';
import useCurrentUserPersonalDetails from './useCurrentUserPersonalDetails';
import useOnyx from './useOnyx';
import usePayChatReportActions from './usePayChatReportActions';
import usePermissions from './usePermissions';
import usePolicy from './usePolicy';

Expand All @@ -35,6 +36,7 @@ function useHoldMenuSubmit({moneyRequestReport, chatReport, requestType, payment
const activePolicy = usePolicy(activePolicyID);
const policy = usePolicy(moneyRequestReport?.policyID);
const chatReportPolicy = usePolicy(chatReport?.policyID);
const getChatReportActions = usePayChatReportActions(chatReport, undefined);
const [introSelected] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED);
const [betas] = useOnyx(ONYXKEYS.BETAS);
const [delegateEmail] = useOnyx(ONYXKEYS.ACCOUNT, {selector: delegateEmailSelector});
Expand Down Expand Up @@ -97,6 +99,7 @@ function useHoldMenuSubmit({moneyRequestReport, chatReport, requestType, payment
ownerBillingGracePeriodEnd,
methodID,
onPaid: animationCallback,
chatReportActions: getChatReportActions(false),
});
}
onClose();
Expand Down
5 changes: 3 additions & 2 deletions src/hooks/useLifecycleActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ function useLifecycleActions({reportID, startApprovedAnimation, startAnimation,
const [moneyRequestReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`);
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${getNonEmptyStringOnyxID(moneyRequestReport?.policyID)}`);
const [chatReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${getNonEmptyStringOnyxID(moneyRequestReport?.chatReportID)}`);
const [chatReportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${getNonEmptyStringOnyxID(moneyRequestReport?.chatReportID)}`);
const [nextStep] = useOnyx(`${ONYXKEYS.COLLECTION.NEXT_STEP}${moneyRequestReport?.reportID}`);
const [allTransactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS);
const [betas] = useOnyx(ONYXKEYS.BETAS);
Expand Down Expand Up @@ -318,15 +319,15 @@ function useLifecycleActions({reportID, startApprovedAnimation, startAnimation,
CONST.IOU.REPORT_ACTION_TYPE.PAY,
() => {
startAnimation();
markReportPaymentReceived(chatReport, moneyRequestReport, nextStep, accountID, email ?? '');
markReportPaymentReceived(chatReport, moneyRequestReport, nextStep, accountID, email ?? '', chatReportActions);
},
CONST.IOU.PAYMENT_TYPE.ELSEWHERE,
);
return;
}

startAnimation();
markReportPaymentReceived(chatReport, moneyRequestReport, nextStep, accountID, email ?? '');
markReportPaymentReceived(chatReport, moneyRequestReport, nextStep, accountID, email ?? '', chatReportActions);
},
},
[CONST.REPORT.SECONDARY_ACTIONS.UNAPPROVE]: {
Expand Down
26 changes: 26 additions & 0 deletions src/hooks/usePayChatReportActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type {OnyxEntry} from 'react-native-onyx';
import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID';
import {isIndividualInvoiceRoom} from '@libs/ReportUtils';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Report, ReportActions} from '@src/types/onyx';
import useOnyx from './useOnyx';

/**
* Returns a resolver for the chat report's actions that getPayMoneyRequestParams will use internally.
*
* getPayMoneyRequestParams resolves the chat report from `initialChatReport` but swaps to
* `existingB2BInvoiceReport` when paying an individual invoice room as a business. `payAsBusiness`
* is chosen at click time, so the hook subscribes to both candidates and exposes a callback that
* picks the right slice once the user has chosen.
*/
function usePayChatReportActions(initialChatReport: OnyxEntry<Report>, existingB2BInvoiceReport: OnyxEntry<Report>): (payAsBusiness: boolean | undefined) => OnyxEntry<ReportActions> {
const [initialChatReportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${getNonEmptyStringOnyxID(initialChatReport?.reportID)}`);
const [b2bInvoiceReportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${getNonEmptyStringOnyxID(existingB2BInvoiceReport?.reportID)}`);

return (payAsBusiness: boolean | undefined) => {
const shouldUseB2BInvoiceReport = !!payAsBusiness && !!existingB2BInvoiceReport && isIndividualInvoiceRoom(initialChatReport);
return shouldUseB2BInvoiceReport ? b2bInvoiceReportActions : initialChatReportActions;
};
}

export default usePayChatReportActions;
Loading
Loading