Skip to content

Commit c642653

Browse files
committed
fix: reimplement merchant clearing logic using wasMerchantCleared Expensify#63587
1 parent b69ad10 commit c642653

4 files changed

Lines changed: 38 additions & 12 deletions

File tree

src/libs/TransactionUtils/index.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,10 @@ function getUpdatedTransaction({
420420
shouldStopSmartscan = true;
421421
}
422422

423+
if (Object.hasOwn(transactionChanges, 'wasMerchantCleared')) {
424+
updatedTransaction.wasMerchantCleared = transactionChanges.wasMerchantCleared;
425+
}
426+
423427
if (Object.hasOwn(transactionChanges, 'waypoints')) {
424428
updatedTransaction.modifiedWaypoints = transactionChanges.waypoints;
425429
updatedTransaction.isLoading = true;
@@ -665,8 +669,12 @@ function isFetchingWaypointsFromServer(transaction: OnyxInputOrEntry<Transaction
665669
* Return the merchant field from the transaction, return the modifiedMerchant if present.
666670
*/
667671
function getMerchant(transaction: OnyxInputOrEntry<Transaction>, policyParam: OnyxEntry<Policy> = undefined): string {
668-
if (transaction && isDistanceRequest(transaction)) {
669-
const report = getReportOrDraftReport(transaction.reportID);
672+
if (!transaction) {
673+
return '';
674+
}
675+
676+
const report = getReportOrDraftReport(transaction.reportID);
677+
if (isDistanceRequest(transaction)) {
670678
// This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
671679
// eslint-disable-next-line deprecation/deprecation
672680
const policy = policyParam ?? getPolicy(report?.policyID);
@@ -678,15 +686,11 @@ function getMerchant(transaction: OnyxInputOrEntry<Transaction>, policyParam: On
678686
);
679687
}
680688

681-
// Check if this is a workspace expense transaction
682-
const report = getReportOrDraftReport(transaction?.reportID);
683-
const isWorkspaceExpense = report?.type === CONST.REPORT.TYPE.EXPENSE;
684-
685-
// Allow empty merchant only for personal expenses, not workspace ones
686-
if (transaction?.modifiedMerchant === '' && !isWorkspaceExpense) {
689+
// Check if merchant was intentionally cleared by user (only for personal expenses)
690+
if (transaction.wasMerchantCleared) {
687691
return '';
688692
}
689-
return transaction?.modifiedMerchant ? transaction.modifiedMerchant : (transaction?.merchant ?? '');
693+
return transaction.modifiedMerchant ? transaction.modifiedMerchant : (transaction.merchant ?? '');
690694
}
691695

692696
function getMerchantOrDescription(transaction: OnyxEntry<Transaction>) {

src/libs/actions/IOU.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4592,10 +4592,16 @@ function updateMoneyRequestMerchant(
45924592
policy: OnyxEntry<OnyxTypes.Policy>,
45934593
policyTagList: OnyxEntry<OnyxTypes.PolicyTagLists>,
45944594
policyCategories: OnyxEntry<OnyxTypes.PolicyCategories>,
4595+
wasMerchantCleared?: boolean,
45954596
) {
45964597
const transactionChanges: TransactionChanges = {
45974598
merchant: value,
45984599
};
4600+
4601+
if (wasMerchantCleared !== undefined) {
4602+
transactionChanges.wasMerchantCleared = wasMerchantCleared;
4603+
}
4604+
45994605
const transactionThreadReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`] ?? null;
46004606
const parentReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReport?.parentReportID}`] ?? null;
46014607
let data: UpdateMoneyRequestData;

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,25 @@ function IOURequestStepMerchant({
101101
return;
102102
}
103103

104-
// Fallback to PARTIAL_TRANSACTION_MERCHANT only if merchant is null or undefined
105-
const merchantValue = newMerchant ?? CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT;
104+
// Check if merchant was intentionally cleared by user
105+
const existingMerchantValue = merchant === CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT ? '' : merchant;
106+
const wasMerchantCleared = newMerchant === '' && existingMerchantValue !== '';
107+
108+
// Determine the merchant value to save
109+
let merchantValue: string;
110+
if (wasMerchantCleared) {
111+
if (isMerchantRequired) {
112+
return;
113+
}
114+
merchantValue = '';
115+
} else {
116+
// Fallback to PARTIAL_TRANSACTION_MERCHANT only if merchant is null or undefined
117+
merchantValue = newMerchant ?? CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT;
118+
}
106119

107120
setMoneyRequestMerchant(transactionID, merchantValue, !isEditing);
108121
if (isEditing) {
109-
updateMoneyRequestMerchant(transactionID, reportID, merchantValue, policy, policyTags, policyCategories);
122+
updateMoneyRequestMerchant(transactionID, reportID, merchantValue, policy, policyTags, policyCategories, wasMerchantCleared && !isMerchantRequired);
110123
}
111124
navigateBack();
112125
};

src/types/onyx/Transaction.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,9 @@ type Transaction = OnyxCommon.OnyxValueWithOfflineFeedback<
548548

549549
/** The inserted time of the transaction */
550550
inserted?: string;
551+
552+
/** Whether the merchant field was intentionally cleared by the user */
553+
wasMerchantCleared?: boolean;
551554
},
552555
keyof Comment | keyof TransactionCustomUnit | 'attendees'
553556
>;

0 commit comments

Comments
 (0)