Skip to content

Commit f90ac1b

Browse files
authored
Merge pull request Expensify#64761 from ganzz4/fix/63587
fix: After clearing merchant and saving it, Merchant field reverts to previous value Expensify#63587
2 parents 3cb5383 + ac33e5c commit f90ac1b

5 files changed

Lines changed: 46 additions & 6 deletions

File tree

src/libs/DebugUtils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,7 @@ function validateTransactionDraftProperty(key: keyof Transaction, value: string)
951951
case 'hasEReceipt':
952952
case 'shouldShowOriginalAmount':
953953
case 'managedCard':
954+
case 'wasMerchantCleared':
954955
return validateBoolean(value);
955956
case 'amount':
956957
case 'taxAmount':
@@ -1073,6 +1074,7 @@ function validateTransactionDraftProperty(key: keyof Transaction, value: string)
10731074
inserted: CONST.RED_BRICK_ROAD_PENDING_ACTION,
10741075
accountant: CONST.RED_BRICK_ROAD_PENDING_ACTION,
10751076
splitExpenses: CONST.RED_BRICK_ROAD_PENDING_ACTION,
1077+
wasMerchantCleared: CONST.RED_BRICK_ROAD_PENDING_ACTION,
10761078
},
10771079
'string',
10781080
);

src/libs/TransactionUtils/index.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,10 @@ function getUpdatedTransaction({
421421
shouldStopSmartscan = true;
422422
}
423423

424+
if (Object.hasOwn(transactionChanges, 'wasMerchantCleared')) {
425+
updatedTransaction.wasMerchantCleared = transactionChanges.wasMerchantCleared;
426+
}
427+
424428
if (Object.hasOwn(transactionChanges, 'waypoints')) {
425429
updatedTransaction.modifiedWaypoints = transactionChanges.waypoints;
426430
updatedTransaction.isLoading = true;
@@ -680,8 +684,12 @@ function isUnreportedAndHasInvalidDistanceRateTransaction(transaction: OnyxInput
680684
* Return the merchant field from the transaction, return the modifiedMerchant if present.
681685
*/
682686
function getMerchant(transaction: OnyxInputOrEntry<Transaction>, policyParam: OnyxEntry<Policy> = undefined): string {
683-
if (transaction && isDistanceRequest(transaction)) {
684-
const report = getReportOrDraftReport(transaction.reportID);
687+
if (!transaction) {
688+
return '';
689+
}
690+
691+
const report = getReportOrDraftReport(transaction.reportID);
692+
if (isDistanceRequest(transaction)) {
685693
// This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
686694
// eslint-disable-next-line deprecation/deprecation
687695
const policy = policyParam ?? getPolicy(report?.policyID);
@@ -694,7 +702,12 @@ function getMerchant(transaction: OnyxInputOrEntry<Transaction>, policyParam: On
694702
);
695703
}
696704
}
697-
return transaction?.modifiedMerchant ? transaction.modifiedMerchant : (transaction?.merchant ?? '');
705+
706+
// Check if merchant was intentionally cleared by user (only for personal expenses)
707+
if (transaction.wasMerchantCleared) {
708+
return '';
709+
}
710+
return transaction.modifiedMerchant ? transaction.modifiedMerchant : (transaction.merchant ?? '');
698711
}
699712

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

src/libs/actions/IOU.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4619,10 +4619,16 @@ function updateMoneyRequestMerchant(
46194619
policy: OnyxEntry<OnyxTypes.Policy>,
46204620
policyTagList: OnyxEntry<OnyxTypes.PolicyTagLists>,
46214621
policyCategories: OnyxEntry<OnyxTypes.PolicyCategories>,
4622+
wasMerchantCleared?: boolean,
46224623
) {
46234624
const transactionChanges: TransactionChanges = {
46244625
merchant: value,
46254626
};
4627+
4628+
if (wasMerchantCleared !== undefined) {
4629+
transactionChanges.wasMerchantCleared = wasMerchantCleared;
4630+
}
4631+
46264632
const transactionThreadReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`] ?? null;
46274633
const parentReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReport?.parentReportID}`] ?? null;
46284634
let data: UpdateMoneyRequestData;

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,26 @@ function IOURequestStepMerchant({
100100
navigateBack();
101101
return;
102102
}
103-
// When creating/editing an expense, newMerchant can be blank so we fall back on PARTIAL_TRANSACTION_MERCHANT
104-
setMoneyRequestMerchant(transactionID, newMerchant || CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT, !isEditing);
103+
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+
}
119+
120+
setMoneyRequestMerchant(transactionID, merchantValue, !isEditing);
105121
if (isEditing) {
106-
updateMoneyRequestMerchant(transactionID, reportID, newMerchant || CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT, policy, policyTags, policyCategories);
122+
updateMoneyRequestMerchant(transactionID, reportID, merchantValue, policy, policyTags, policyCategories, wasMerchantCleared && !isMerchantRequired);
107123
}
108124
navigateBack();
109125
};

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)