Skip to content

Commit fe70529

Browse files
authored
Merge pull request Expensify#76706 from dmkt9/fix/75364
Fix/75364 - Double violation briefly visible when removing a disabled category from report
2 parents 87be80e + c12b05b commit fe70529

2 files changed

Lines changed: 60 additions & 1 deletion

File tree

src/libs/actions/IOU.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4760,10 +4760,15 @@ function getUpdateMoneyRequestParams(params: GetUpdateMoneyRequestParamsType): U
47604760
) {
47614761
const currentTransactionViolations = allTransactionViolations[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`] ?? [];
47624762
// If the amount, currency or date have been modified, we remove the duplicate violations since they would be out of date as the transaction has changed
4763-
const optimisticViolations =
4763+
let optimisticViolations =
47644764
hasModifiedAmount || hasModifiedDate || hasModifiedCurrency
47654765
? currentTransactionViolations.filter((violation) => violation.name !== CONST.VIOLATIONS.DUPLICATED_TRANSACTION)
47664766
: currentTransactionViolations;
4767+
optimisticViolations =
4768+
hasModifiedCategory && transactionChanges.category === ''
4769+
? optimisticViolations.filter((violation) => violation.name !== CONST.VIOLATIONS.CATEGORY_OUT_OF_POLICY)
4770+
: optimisticViolations;
4771+
47674772
const violationsOnyxData = ViolationsUtils.getViolationsOnyxData(
47684773
updatedTransaction,
47694774
optimisticViolations,

tests/actions/IOUTest.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6396,6 +6396,60 @@ describe('actions/IOU', () => {
63966396
});
63976397
});
63986398
});
6399+
6400+
it('should remove all existing category violations when the transaction "Category" is unset', async () => {
6401+
const transactionID = '1';
6402+
const policyID = '2';
6403+
const transactionThreadReportID = '3';
6404+
const category = '';
6405+
const fakePolicy: Policy = {
6406+
...createRandomPolicy(Number(policyID)),
6407+
requiresCategory: true,
6408+
};
6409+
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, {
6410+
amount: 100,
6411+
transactionID,
6412+
});
6413+
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`, [
6414+
{
6415+
type: CONST.VIOLATION_TYPES.VIOLATION,
6416+
name: CONST.VIOLATIONS.CATEGORY_OUT_OF_POLICY,
6417+
data: {},
6418+
showInReview: true,
6419+
},
6420+
]);
6421+
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, fakePolicy);
6422+
await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`, {reportID: transactionThreadReportID});
6423+
6424+
// When updating a money request category
6425+
updateMoneyRequestCategory({
6426+
transactionID,
6427+
transactionThreadReportID,
6428+
category,
6429+
policy: fakePolicy,
6430+
policyTagList: undefined,
6431+
policyCategories: undefined,
6432+
policyRecentlyUsedCategories: [],
6433+
currentUserAccountIDParam: 123,
6434+
currentUserEmailParam: 'existing@example.com',
6435+
isASAPSubmitBetaEnabled: false,
6436+
});
6437+
6438+
await waitForBatchedUpdates();
6439+
6440+
// Any existing category violations will be removed, leaving only the MISSING_CATEGORY violation in the end
6441+
await new Promise<void>((resolve) => {
6442+
const connection = Onyx.connect({
6443+
key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`,
6444+
callback: (transactionViolations) => {
6445+
Onyx.disconnect(connection);
6446+
expect(transactionViolations).toHaveLength(1);
6447+
expect(transactionViolations?.at(0)?.name).toEqual(CONST.VIOLATIONS.MISSING_CATEGORY);
6448+
resolve();
6449+
},
6450+
});
6451+
});
6452+
});
63996453
});
64006454

64016455
describe('setDraftSplitTransaction', () => {

0 commit comments

Comments
 (0)