Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix - Multilevel tags - "no longer valid" violation is shown after selecting all the tags #38881

Merged
merged 6 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ function getTagLists(policyTagList: OnyxEntry<PolicyTagList>): Array<PolicyTagLi
.sort((tagA, tagB) => tagA.orderWeight - tagB.orderWeight);
}

function getSortedTagKeys(policyTagList: OnyxEntry<PolicyTagList>): Array<keyof PolicyTagList> {
if (isEmptyObject(policyTagList)) {
return [];
}

return Object.keys(policyTagList).sort((key1, key2) => policyTagList[key1].orderWeight - policyTagList[key2].orderWeight);
FitseTLT marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Gets a tag list of a policy by a tag index
*/
Expand Down Expand Up @@ -317,6 +325,7 @@ export {
getIneligibleInvitees,
getTagLists,
getTagListName,
getSortedTagKeys,
canEditTaxRate,
getTagList,
getCleanedTagName,
Expand Down
3 changes: 2 additions & 1 deletion src/libs/Violations/ViolationsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import reject from 'lodash/reject';
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 CONST from '@src/CONST';
import type {TranslationPaths} from '@src/languages/types';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -55,7 +56,7 @@ function getTagViolationsForMultiLevelTags(
policyRequiresTags: boolean,
policyTagList: PolicyTagList,
): TransactionViolation[] {
const policyTagKeys = Object.keys(policyTagList);
const policyTagKeys = getSortedTagKeys(policyTagList);
const selectedTags = updatedTransaction.tag?.split(CONST.COLON) ?? [];
let newTransactionViolations = [...transactionViolations];
newTransactionViolations = newTransactionViolations.filter(
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/ViolationUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ describe('getViolationsOnyxData', () => {
},
},
required: true,
orderWeight: 1,
orderWeight: 2,
},
Region: {
name: 'Region',
Expand All @@ -222,7 +222,7 @@ describe('getViolationsOnyxData', () => {
},
},
required: true,
orderWeight: 2,
orderWeight: 1,
},
Project: {
name: 'Project',
Expand Down Expand Up @@ -251,25 +251,25 @@ describe('getViolationsOnyxData', () => {
expect(result.value).toEqual([someTagLevelsRequiredViolation]);

// Test case where transaction has 1 tag
transaction.tag = 'Accounting';
transaction.tag = 'Africa';
someTagLevelsRequiredViolation.data = {errorIndexes: [1, 2]};
result = ViolationsUtils.getViolationsOnyxData(transaction, transactionViolations, policyRequiresTags, policyTags, policyRequiresCategories, policyCategories);
expect(result.value).toEqual([someTagLevelsRequiredViolation]);

// Test case where transaction has 2 tags
transaction.tag = 'Accounting::Project1';
transaction.tag = 'Africa::Project1';
someTagLevelsRequiredViolation.data = {errorIndexes: [1]};
result = ViolationsUtils.getViolationsOnyxData(transaction, transactionViolations, policyRequiresTags, policyTags, policyRequiresCategories, policyCategories);
expect(result.value).toEqual([someTagLevelsRequiredViolation]);

// Test case where transaction has all tags
transaction.tag = 'Accounting:Africa:Project1';
transaction.tag = 'Africa:Accounting:Project1';
result = ViolationsUtils.getViolationsOnyxData(transaction, transactionViolations, policyRequiresTags, policyTags, policyRequiresCategories, policyCategories);
expect(result.value).toEqual([]);
});
it('should return tagOutOfPolicy when a tag is not enabled in the policy but is set in the transaction', () => {
policyTags.Department.tags.Accounting.enabled = false;
transaction.tag = 'Accounting:Africa:Project1';
transaction.tag = 'Africa:Accounting:Project1';
const result = ViolationsUtils.getViolationsOnyxData(transaction, transactionViolations, policyRequiresTags, policyTags, policyRequiresCategories, policyCategories);
const violation = {...tagOutOfPolicyViolation, data: {tagName: 'Department'}};
expect(result.value).toEqual([violation]);
Expand Down
Loading