Skip to content

[CP Staging] Fixed scan expense flow when participants are missing #58390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2025
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
15 changes: 12 additions & 3 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9244,7 +9244,7 @@ function replaceReceipt(transactionID: string, file: File, source: string) {
* @param transactionID of the transaction to set the participants of
* @param report attached to the transaction
*/
function setMoneyRequestParticipantsFromReport(transactionID: string, report: OnyxEntry<OnyxTypes.Report>): Participant[] {
function getMoneyRequestParticipantsFromReport(report: OnyxEntry<OnyxTypes.Report>): Participant[] {
// If the report is iou or expense report, we should get the chat report to set participant for request money
const chatReport = isMoneyRequestReportReportUtils(report) ? getReportOrDraftReport(report?.chatReportID) : report;
const currentUserAccountID = currentUserPersonalDetails?.accountID;
Expand All @@ -9269,11 +9269,19 @@ function setMoneyRequestParticipantsFromReport(transactionID: string, report: On
participants = chatReportOtherParticipants.map((accountID) => ({accountID, selected: true}));
}

Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`, {participants, participantsAutoAssigned: true});

return participants;
}

/**
* Sets the participants for an IOU based on the attached report
* @param transactionID of the transaction to set the participants of
* @param report attached to the transaction
*/
function setMoneyRequestParticipantsFromReport(transactionID: string, report: OnyxEntry<OnyxTypes.Report>) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a return type of promise

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not required since it will be implicitly typed to a Promise.

const participants = getMoneyRequestParticipantsFromReport(report);
return Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`, {participants, participantsAutoAssigned: true});
}

function setMoneyRequestTaxRate(transactionID: string, taxCode: string | null) {
Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`, {taxCode});
}
Expand Down Expand Up @@ -10114,6 +10122,7 @@ export {
setMoneyRequestMerchant,
setMoneyRequestParticipants,
setMoneyRequestParticipantsFromReport,
getMoneyRequestParticipantsFromReport,
setMoneyRequestPendingFields,
setMoneyRequestReceipt,
setMoneyRequestTag,
Expand Down
27 changes: 15 additions & 12 deletions src/pages/iou/request/step/IOURequestStepAmount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {shouldRestrictUserBillableActions} from '@libs/SubscriptionUtils';
import {calculateTaxAmount, getAmount, getCurrency, getDefaultTaxCode, getRequestType, getTaxValue} from '@libs/TransactionUtils';
import MoneyRequestAmountForm from '@pages/iou/MoneyRequestAmountForm';
import {
getMoneyRequestParticipantsFromReport,
requestMoney,
resetSplitShares,
sendMoneyElsewhere,
Expand Down Expand Up @@ -186,7 +187,7 @@ function IOURequestStepAmount({
// to the confirm step.
// If the user is started this flow using the Create expense option (combined submit/track flow), they should be redirected to the participants page.
if (report?.reportID && !isArchivedReport(reportNameValuePairs) && iouType !== CONST.IOU.TYPE.CREATE) {
const selectedParticipants = setMoneyRequestParticipantsFromReport(transactionID, report);
const selectedParticipants = getMoneyRequestParticipantsFromReport(report);
const participants = selectedParticipants.map((participant) => {
const participantAccountID = participant?.accountID ?? CONST.DEFAULT_NUMBER_ID;
return participantAccountID ? getParticipantsOption(participant, personalDetails) : getReportOption(participant);
Expand Down Expand Up @@ -242,28 +243,30 @@ function IOURequestStepAmount({
return;
}
}
setMoneyRequestParticipantsFromReport(transactionID, report);
if (isSplitBill && !report.isOwnPolicyExpenseChat && report.participants) {
const participantAccountIDs = Object.keys(report.participants).map((accountID) => Number(accountID));
setSplitShares(transaction, amountInSmallestCurrencyUnits, currency || CONST.CURRENCY.USD, participantAccountIDs);
}
navigateToConfirmationPage();
setMoneyRequestParticipantsFromReport(transactionID, report).then(() => {
navigateToConfirmationPage();
});
return;
}

// If there was no reportID, then that means the user started this flow from the global + menu
// and an optimistic reportID was generated. In that case, the next step is to select the participants for this expense.
if (iouType === CONST.IOU.TYPE.CREATE && isPaidGroupPolicy(activePolicy) && activePolicy?.isPolicyExpenseChatEnabled && !shouldRestrictUserBillableActions(activePolicy.id)) {
const activePolicyExpenseChat = getPolicyExpenseChat(currentUserPersonalDetails.accountID, activePolicy?.id);
setMoneyRequestParticipantsFromReport(transactionID, activePolicyExpenseChat);
Navigation.navigate(
ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(
CONST.IOU.ACTION.CREATE,
iouType === CONST.IOU.TYPE.CREATE ? CONST.IOU.TYPE.SUBMIT : iouType,
transactionID,
activePolicyExpenseChat?.reportID,
),
);
setMoneyRequestParticipantsFromReport(transactionID, activePolicyExpenseChat).then(() => {
Navigation.navigate(
ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(
CONST.IOU.ACTION.CREATE,
iouType === CONST.IOU.TYPE.CREATE ? CONST.IOU.TYPE.SUBMIT : iouType,
transactionID,
activePolicyExpenseChat?.reportID,
),
);
});
} else {
navigateToParticipantPage();
}
Expand Down
27 changes: 15 additions & 12 deletions src/pages/iou/request/step/IOURequestStepDistance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import useThemeStyles from '@hooks/useThemeStyles';
import {
createDistanceRequest,
getIOURequestPolicyID,
getMoneyRequestParticipantsFromReport,
resetSplitShares,
setCustomUnitRateID,
setMoneyRequestAmount,
Expand Down Expand Up @@ -312,7 +313,7 @@ function IOURequestStepDistance({
// to the confirm step.
// If the user started this flow using the Create expense option (combined submit/track flow), they should be redirected to the participants page.
if (report?.reportID && !isArchivedReport(reportNameValuePairs) && iouType !== CONST.IOU.TYPE.CREATE) {
const selectedParticipants = setMoneyRequestParticipantsFromReport(transactionID, report);
const selectedParticipants = getMoneyRequestParticipantsFromReport(report);
const participants = selectedParticipants.map((participant) => {
const participantAccountID = participant?.accountID ?? CONST.DEFAULT_NUMBER_ID;
return participantAccountID ? getParticipantsOption(participant, personalDetails) : getReportOption(participant);
Expand Down Expand Up @@ -371,26 +372,28 @@ function IOURequestStepDistance({
});
return;
}
setMoneyRequestParticipantsFromReport(transactionID, report);
navigateToConfirmationPage();
setMoneyRequestParticipantsFromReport(transactionID, report).then(() => {
navigateToConfirmationPage();
});
return;
}

// If there was no reportID, then that means the user started this flow from the global menu
// and an optimistic reportID was generated. In that case, the next step is to select the participants for this expense.
if (iouType === CONST.IOU.TYPE.CREATE && isPaidGroupPolicy(activePolicy) && activePolicy?.isPolicyExpenseChatEnabled && !shouldRestrictUserBillableActions(activePolicy.id)) {
const activePolicyExpenseChat = getPolicyExpenseChat(currentUserPersonalDetails.accountID, activePolicy?.id);
setMoneyRequestParticipantsFromReport(transactionID, activePolicyExpenseChat);
const rateID = DistanceRequestUtils.getCustomUnitRateID(activePolicyExpenseChat?.reportID);
setCustomUnitRateID(transactionID, rateID);
Navigation.navigate(
ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(
CONST.IOU.ACTION.CREATE,
iouType === CONST.IOU.TYPE.CREATE ? CONST.IOU.TYPE.SUBMIT : iouType,
transactionID,
activePolicyExpenseChat?.reportID,
),
);
setMoneyRequestParticipantsFromReport(transactionID, activePolicyExpenseChat).then(() => {
Navigation.navigate(
ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(
CONST.IOU.ACTION.CREATE,
iouType === CONST.IOU.TYPE.CREATE ? CONST.IOU.TYPE.SUBMIT : iouType,
transactionID,
activePolicyExpenseChat?.reportID,
),
);
});
} else {
navigateToParticipantPage();
}
Expand Down
26 changes: 15 additions & 11 deletions src/pages/iou/request/step/IOURequestStepScan/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import StepScreenWrapper from '@pages/iou/request/step/StepScreenWrapper';
import withFullTransactionOrNotFound from '@pages/iou/request/step/withFullTransactionOrNotFound';
import withWritableReportOrNotFound from '@pages/iou/request/step/withWritableReportOrNotFound';
import {
getMoneyRequestParticipantsFromReport,
replaceReceipt,
requestMoney,
setMoneyRequestParticipantsFromReport,
Expand Down Expand Up @@ -304,15 +305,16 @@ function IOURequestStepScan({
if ((transaction?.isFromGlobalCreate && iouType !== CONST.IOU.TYPE.TRACK && !report?.reportID) || iouType === CONST.IOU.TYPE.CREATE) {
if (activePolicy && isPaidGroupPolicy(activePolicy) && !shouldRestrictUserBillableActions(activePolicy.id)) {
const activePolicyExpenseChat = getPolicyExpenseChat(currentUserPersonalDetails.accountID, activePolicy?.id);
setMoneyRequestParticipantsFromReport(transactionID, activePolicyExpenseChat);
Navigation.navigate(
ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(
CONST.IOU.ACTION.CREATE,
iouType === CONST.IOU.TYPE.CREATE ? CONST.IOU.TYPE.SUBMIT : iouType,
transactionID,
activePolicyExpenseChat?.reportID,
),
);
setMoneyRequestParticipantsFromReport(transactionID, activePolicyExpenseChat).then(() => {
Navigation.navigate(
ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(
CONST.IOU.ACTION.CREATE,
iouType === CONST.IOU.TYPE.CREATE ? CONST.IOU.TYPE.SUBMIT : iouType,
transactionID,
activePolicyExpenseChat?.reportID,
),
);
});
} else {
navigateToParticipantPage();
}
Expand All @@ -321,7 +323,7 @@ function IOURequestStepScan({

// If the transaction was created from the + menu from the composer inside of a chat, the participants can automatically
// be added to the transaction (taken from the chat report participants) and then the person is taken to the confirmation step.
const selectedParticipants = setMoneyRequestParticipantsFromReport(transactionID, report);
const selectedParticipants = getMoneyRequestParticipantsFromReport(report);
const participants = selectedParticipants.map((participant) => {
const participantAccountID = participant?.accountID ?? CONST.DEFAULT_NUMBER_ID;
return participantAccountID ? getParticipantsOption(participant, personalDetails) : getReportOption(participant);
Expand Down Expand Up @@ -425,7 +427,9 @@ function IOURequestStepScan({
createTransaction(receipt, participant);
return;
}
navigateToConfirmationPage();
setMoneyRequestParticipantsFromReport(transactionID, report).then(() => {
navigateToConfirmationPage();
});
},
[
backTo,
Expand Down
26 changes: 15 additions & 11 deletions src/pages/iou/request/step/IOURequestStepScan/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import StepScreenDragAndDropWrapper from '@pages/iou/request/step/StepScreenDrag
import withFullTransactionOrNotFound from '@pages/iou/request/step/withFullTransactionOrNotFound';
import withWritableReportOrNotFound from '@pages/iou/request/step/withWritableReportOrNotFound';
import {
getMoneyRequestParticipantsFromReport,
replaceReceipt,
requestMoney,
setMoneyRequestParticipantsFromReport,
Expand Down Expand Up @@ -359,15 +360,16 @@ function IOURequestStepScan({
if ((transaction?.isFromGlobalCreate && iouType !== CONST.IOU.TYPE.TRACK && !report?.reportID) || iouType === CONST.IOU.TYPE.CREATE) {
if (activePolicy && isPaidGroupPolicy(activePolicy) && !shouldRestrictUserBillableActions(activePolicy.id)) {
const activePolicyExpenseChat = getPolicyExpenseChat(currentUserPersonalDetails.accountID, activePolicy?.id);
setMoneyRequestParticipantsFromReport(transactionID, activePolicyExpenseChat);
Navigation.navigate(
ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(
CONST.IOU.ACTION.CREATE,
iouType === CONST.IOU.TYPE.CREATE ? CONST.IOU.TYPE.SUBMIT : iouType,
transactionID,
activePolicyExpenseChat?.reportID,
),
);
setMoneyRequestParticipantsFromReport(transactionID, activePolicyExpenseChat).then(() => {
Navigation.navigate(
ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(
CONST.IOU.ACTION.CREATE,
iouType === CONST.IOU.TYPE.CREATE ? CONST.IOU.TYPE.SUBMIT : iouType,
transactionID,
activePolicyExpenseChat?.reportID,
),
);
});
} else {
navigateToParticipantPage();
}
Expand All @@ -376,7 +378,7 @@ function IOURequestStepScan({

// If the transaction was created from the + menu from the composer inside of a chat, the participants can automatically
// be added to the transaction (taken from the chat report participants) and then the person is taken to the confirmation step.
const selectedParticipants = setMoneyRequestParticipantsFromReport(transactionID, report);
const selectedParticipants = getMoneyRequestParticipantsFromReport(report);
const participants = selectedParticipants.map((participant) => {
const participantAccountID = participant?.accountID ?? CONST.DEFAULT_NUMBER_ID;
return participantAccountID ? getParticipantsOption(participant, personalDetails) : getReportOption(participant);
Expand Down Expand Up @@ -480,7 +482,9 @@ function IOURequestStepScan({
createTransaction(receipt, participant);
return;
}
navigateToConfirmationPage();
setMoneyRequestParticipantsFromReport(transactionID, report).then(() => {
navigateToConfirmationPage();
});
},
[
backTo,
Expand Down
Loading