Skip to content

Commit 2684a1f

Browse files
authored
Merge pull request Expensify#87962 from jayeshmangwani/87441_fix_moving_individually_expense
updated expense-moving logic to use the default workspace, except when an admin moves a member's report
2 parents e07f6f6 + a685f91 commit 2684a1f

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

src/pages/iou/request/step/IOURequestEditReport.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,14 @@ function IOURequestEditReport({route}: IOURequestEditReportProps) {
5858

5959
const hasPerDiemTransactions = useHasPerDiemTransactions(transactionIDs);
6060

61-
const {policyForMovingExpensesID, shouldSelectPolicy} = usePolicyForMovingExpenses(hasPerDiemTransactions, undefined, undefined);
61+
// When moving an expense that belongs to another user, or when the selection includes per diem
62+
// transactions, use the policy of their report (or the transaction's policy as fallback) so the
63+
// selected workspace is preserved.
64+
// For the current user's own non-per-diem expenses, fall back to undefined to let the default workspace apply.
65+
const isOwnedByOther = selectedReport?.ownerAccountID !== session?.accountID;
66+
const isOwnedByOtherOrHasPerDiem = isOwnedByOther || hasPerDiemTransactions;
67+
const targetExpensePolicyID = isOwnedByOtherOrHasPerDiem ? selectedReport?.policyID : undefined;
68+
const {policyForMovingExpensesID, shouldSelectPolicy} = usePolicyForMovingExpenses(hasPerDiemTransactions, undefined, targetExpensePolicyID);
6269
const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS);
6370
const hasViolations = hasViolationsReportUtils(undefined, transactionViolations, session?.accountID ?? CONST.DEFAULT_NUMBER_ID, session?.email ?? '');
6471
const policyForMovingExpenses = policyForMovingExpensesID ? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyForMovingExpensesID}`] : undefined;
@@ -167,6 +174,7 @@ function IOURequestEditReport({route}: IOURequestEditReportProps) {
167174
isEditing={action === CONST.IOU.ACTION.EDIT}
168175
createReport={createReport}
169176
isPerDiemRequest={hasPerDiemTransactions}
177+
transactionPolicyID={targetExpensePolicyID}
170178
/>
171179
);
172180
}

src/pages/iou/request/step/IOURequestStepReport.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,24 @@ function IOURequestStepReport({route, transaction}: IOURequestStepReportProps) {
7575
? iouActions?.find((iouAction) => getOriginalMessage(iouAction as ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU>)?.IOUTransactionID === transaction.transactionID)?.actorAccountID
7676
: selectedReport?.ownerAccountID;
7777
const ownerPersonalDetails = getPersonalDetailsForAccountID(ownerAccountID, personalDetails) as PersonalDetails;
78+
const isPerDiemTransaction = isPerDiemRequest(transaction);
7879

7980
const transactionPolicyID = transaction?.participants?.at(0)?.isPolicyExpenseChat ? transaction?.participants.at(0)?.policyID : undefined;
81+
// When moving an expense that belongs to another user, or when the selection includes per diem
82+
// transactions, use the policy of their report (or the transaction's policy as fallback) so the
83+
// selected workspace is preserved.
84+
// For the current user's own non-per-diem expenses, fall back to undefined to let the default workspace apply.
85+
const targetExpensePolicyID = ownerAccountID !== session?.accountID || isPerDiemTransaction ? (selectedReport?.policyID ?? transactionPolicyID) : undefined;
86+
8087
// we need to fall back to transactionPolicyID because for a new workspace there is no report created yet
8188
// and if we choose this workspace as participant we want to create a new report in the chosen workspace
82-
const {policyForMovingExpensesID, shouldSelectPolicy} = usePolicyForMovingExpenses(
83-
isPerDiemRequest(transaction),
84-
isTimeRequestUtil(transaction),
85-
selectedReport?.policyID ?? transactionPolicyID,
86-
);
89+
const {policyForMovingExpensesID, shouldSelectPolicy} = usePolicyForMovingExpenses(isPerDiemTransaction, isTimeRequestUtil(transaction), targetExpensePolicyID);
8790

8891
const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS);
8992
const [allTransactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION);
9093
const hasViolations = hasViolationsReportUtils(undefined, transactionViolations, session?.accountID ?? CONST.DEFAULT_NUMBER_ID, session?.email ?? '');
9194
const policyForMovingExpenses = policyForMovingExpensesID ? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyForMovingExpensesID}`] : undefined;
9295
useRestartOnReceiptFailure(transaction, reportIDFromRoute, iouType, action);
93-
const isPerDiemTransaction = isPerDiemRequest(transaction);
9496
const perDiemOriginalPolicy = getPolicyByCustomUnitID(transaction, allPolicies);
9597
const [transactions] = useOptimisticDraftTransactions(transaction);
9698
const [betas] = useOnyx(ONYXKEYS.BETAS);
@@ -293,12 +295,12 @@ function IOURequestStepReport({route, transaction}: IOURequestStepReportProps) {
293295
transactionIDs={transaction ? [transaction.transactionID] : []}
294296
selectedReportID={selectedReportID}
295297
selectedPolicyID={selectedPolicyID}
296-
transactionPolicyID={selectedReport?.policyID ?? transactionPolicyID}
298+
transactionPolicyID={targetExpensePolicyID}
297299
removeFromReport={removeFromReport}
298300
isEditing={isEditing}
299301
isUnreported={isUnreported}
300302
shouldShowNotFoundPage={shouldShowNotFoundPage}
301-
isPerDiemRequest={transaction ? isPerDiemRequest(transaction) : false}
303+
isPerDiemRequest={transaction ? isPerDiemTransaction : false}
302304
isTimeRequest={transaction ? isTimeRequestUtil(transaction) : false}
303305
createReport={policyForMovingExpensesID || shouldSelectPolicy || isPerDiemTransaction ? createReport : undefined}
304306
targetOwnerAccountID={ownerAccountID}

0 commit comments

Comments
 (0)