Skip to content

Commit

Permalink
Merge pull request #39034 from bernhardoj/fix/38131-optimsitic-violat…
Browse files Browse the repository at this point in the history
…ion-for-partial-transaction
  • Loading branch information
cead22 authored Apr 1, 2024
2 parents 877f867 + 37a9a6a commit 4e6bd2d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/libs/Violations/ViolationsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Onyx from 'react-native-onyx';
import type {OnyxUpdate} from 'react-native-onyx';
import type {Phrase, PhraseParameters} from '@libs/Localize';
import {getSortedTagKeys} from '@libs/PolicyUtils';
import * as TransactionUtils from '@libs/TransactionUtils';
import CONST from '@src/CONST';
import type {TranslationPaths} from '@src/languages/types';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -121,6 +122,15 @@ const ViolationsUtils = {
policyRequiresCategories: boolean,
policyCategories: PolicyCategories,
): OnyxUpdate {
const isPartialTransaction = TransactionUtils.isPartialMerchant(TransactionUtils.getMerchant(updatedTransaction)) && TransactionUtils.isAmountMissing(updatedTransaction);
if (isPartialTransaction) {
return {
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${updatedTransaction.transactionID}`,
value: transactionViolations,
};
}

let newTransactionViolations = [...transactionViolations];

// Calculate client-side category violations
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/ViolationUtilsTest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {beforeEach} from '@jest/globals';
import Onyx from 'react-native-onyx';
import ViolationsUtils from '@libs/Violations/ViolationsUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {PolicyCategories, PolicyTagList, Transaction, TransactionViolation} from '@src/types/onyx';

Expand Down Expand Up @@ -84,6 +85,12 @@ describe('getViolationsOnyxData', () => {
expect(result.value).not.toContainEqual(categoryOutOfPolicyViolation);
});

it('should not add a category violation when the transaction is partial', () => {
const partialTransaction = {...transaction, amount: 0, merchant: CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT, category: undefined};
const result = ViolationsUtils.getViolationsOnyxData(partialTransaction, transactionViolations, policyRequiresTags, policyTags, policyRequiresCategories, policyCategories);
expect(result.value).not.toContainEqual(missingCategoryViolation);
});

it('should add categoryOutOfPolicy violation to existing violations if they exist', () => {
transaction.category = 'Bananas';
transactionViolations = [
Expand Down Expand Up @@ -161,6 +168,12 @@ describe('getViolationsOnyxData', () => {
expect(result.value).toEqual([]);
});

it('should not add a tag violation when the transaction is partial', () => {
const partialTransaction = {...transaction, amount: 0, merchant: CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT, tag: undefined};
const result = ViolationsUtils.getViolationsOnyxData(partialTransaction, transactionViolations, policyRequiresTags, policyTags, policyRequiresCategories, policyCategories);
expect(result.value).not.toContainEqual(missingTagViolation);
});

it('should add tagOutOfPolicy violation to existing violations if transaction has tag that is not in the policy', () => {
transaction.tag = 'Bananas';
transactionViolations = [
Expand Down

0 comments on commit 4e6bd2d

Please sign in to comment.