-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Add violation logic for multi level tags #37461
Changes from all commits
01281ed
f0093d6
7520f36
b17a7c9
bf609e7
2dd37c1
1b2f767
5c1a77e
33f437a
3a36c34
38d0051
2402537
983e245
6b4de30
954c3a7
9c773cd
5c4f7c3
e12c906
20889e5
f167b3a
a47a77c
ff3bebb
32e20ed
e77b01a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,106 @@ import type {TranslationPaths} from '@src/languages/types'; | |
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import type {PolicyCategories, PolicyTagList, Transaction, TransactionViolation} from '@src/types/onyx'; | ||
|
||
/** | ||
* Calculates tag out of policy and missing tag violations for the given transaction | ||
*/ | ||
function getTagViolationsForSingleLevelTags( | ||
updatedTransaction: Transaction, | ||
transactionViolations: TransactionViolation[], | ||
policyRequiresTags: boolean, | ||
policyTagList: PolicyTagList, | ||
): TransactionViolation[] { | ||
const policyTagKeys = Object.keys(policyTagList); | ||
const policyTagListName = policyTagKeys[0]; | ||
const policyTags = policyTagList[policyTagListName]?.tags; | ||
const hasTagOutOfPolicyViolation = transactionViolations.some((violation) => violation.name === CONST.VIOLATIONS.TAG_OUT_OF_POLICY); | ||
const hasMissingTagViolation = transactionViolations.some((violation) => violation.name === CONST.VIOLATIONS.MISSING_TAG); | ||
const isTagInPolicy = policyTags ? !!policyTags[updatedTransaction.tag ?? '']?.enabled : false; | ||
let newTransactionViolations = [...transactionViolations]; | ||
|
||
// Add 'tagOutOfPolicy' violation if tag is not in policy | ||
if (!hasTagOutOfPolicyViolation && updatedTransaction.tag && !isTagInPolicy) { | ||
newTransactionViolations.push({name: CONST.VIOLATIONS.TAG_OUT_OF_POLICY, type: 'violation'}); | ||
} | ||
|
||
// Remove 'tagOutOfPolicy' violation if tag is in policy | ||
if (hasTagOutOfPolicyViolation && updatedTransaction.tag && isTagInPolicy) { | ||
newTransactionViolations = reject(newTransactionViolations, {name: CONST.VIOLATIONS.TAG_OUT_OF_POLICY}); | ||
} | ||
|
||
// Remove 'missingTag' violation if tag is valid according to policy | ||
if (hasMissingTagViolation && isTagInPolicy) { | ||
newTransactionViolations = reject(newTransactionViolations, {name: CONST.VIOLATIONS.MISSING_TAG}); | ||
} | ||
|
||
// Add 'missingTag violation' if tag is required and not set | ||
if (!hasMissingTagViolation && !updatedTransaction.tag && policyRequiresTags) { | ||
newTransactionViolations.push({name: CONST.VIOLATIONS.MISSING_TAG, type: 'violation'}); | ||
} | ||
return newTransactionViolations; | ||
} | ||
|
||
/** | ||
* Calculates some tag levels required and missing tag violations for the given transaction | ||
*/ | ||
function getTagViolationsForMultiLevelTags( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi team, come from #38095, this function was missing to handle multilevel dependent tags cases. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! This wasn't really missing, as much as it just hadn't been implemented yet. When we added support for multi level tags, we broke the violations for tags, and this PR was meant to fix that. We then added support for dependent tags, and then implemented |
||
updatedTransaction: Transaction, | ||
transactionViolations: TransactionViolation[], | ||
policyRequiresTags: boolean, | ||
policyTagList: PolicyTagList, | ||
): TransactionViolation[] { | ||
const policyTagKeys = Object.keys(policyTagList); | ||
const selectedTags = updatedTransaction.tag?.split(CONST.COLON) ?? []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should have split the tags using |
||
let newTransactionViolations = [...transactionViolations]; | ||
newTransactionViolations = newTransactionViolations.filter( | ||
(violation) => violation.name !== CONST.VIOLATIONS.SOME_TAG_LEVELS_REQUIRED && violation.name !== CONST.VIOLATIONS.TAG_OUT_OF_POLICY, | ||
); | ||
|
||
// We first get the errorIndexes for someTagLevelsRequired. If it's not empty, we puth SOME_TAG_LEVELS_REQUIRED in Onyx. | ||
// Otherwise, we put TAG_OUT_OF_POLICY in Onyx (when applicable) | ||
const errorIndexes = []; | ||
for (let i = 0; i < policyTagKeys.length; i++) { | ||
const isTagRequired = policyTagList[policyTagKeys[i]].required ?? true; | ||
const isTagSelected = Boolean(selectedTags[i]); | ||
if (isTagRequired && (!isTagSelected || (selectedTags.length === 1 && selectedTags[0] === ''))) { | ||
errorIndexes.push(i); | ||
} | ||
} | ||
if (errorIndexes.length !== 0) { | ||
newTransactionViolations.push({ | ||
name: CONST.VIOLATIONS.SOME_TAG_LEVELS_REQUIRED, | ||
type: 'violation', | ||
data: { | ||
errorIndexes, | ||
}, | ||
}); | ||
} else { | ||
let hasInvalidTag = false; | ||
for (let i = 0; i < policyTagKeys.length; i++) { | ||
const selectedTag = selectedTags[i]; | ||
const tags = policyTagList[policyTagKeys[i]].tags; | ||
const isTagInPolicy = Object.values(tags).some((tag) => tag.name === selectedTag && Boolean(tag.enabled)); | ||
if (!isTagInPolicy) { | ||
newTransactionViolations.push({ | ||
name: CONST.VIOLATIONS.TAG_OUT_OF_POLICY, | ||
type: 'violation', | ||
data: { | ||
tagName: policyTagKeys[i], | ||
}, | ||
}); | ||
hasInvalidTag = true; | ||
break; | ||
} | ||
} | ||
if (!hasInvalidTag) { | ||
newTransactionViolations = reject(newTransactionViolations, { | ||
name: CONST.VIOLATIONS.TAG_OUT_OF_POLICY, | ||
}); | ||
} | ||
} | ||
return newTransactionViolations; | ||
} | ||
|
||
const ViolationsUtils = { | ||
/** | ||
* Checks a transaction for policy violations and returns an object with Onyx method, key and updated transaction | ||
|
@@ -22,6 +122,7 @@ const ViolationsUtils = { | |
): OnyxUpdate { | ||
let newTransactionViolations = [...transactionViolations]; | ||
|
||
// Calculate client-side category violations | ||
if (policyRequiresCategories) { | ||
const hasCategoryOutOfPolicyViolation = transactionViolations.some((violation) => violation.name === 'categoryOutOfPolicy'); | ||
const hasMissingCategoryViolation = transactionViolations.some((violation) => violation.name === 'missingCategory'); | ||
|
@@ -49,36 +150,12 @@ const ViolationsUtils = { | |
} | ||
} | ||
|
||
// Calculate client-side tag violations | ||
if (policyRequiresTags) { | ||
const policyTagKeys = Object.keys(policyTagList); | ||
|
||
// At the moment, we only return violations for tags for workspaces with single-level tags | ||
if (policyTagKeys.length === 1) { | ||
const policyTagListName = policyTagKeys[0]; | ||
const policyTags = policyTagList[policyTagListName]?.tags; | ||
const hasTagOutOfPolicyViolation = transactionViolations.some((violation) => violation.name === CONST.VIOLATIONS.TAG_OUT_OF_POLICY); | ||
const hasMissingTagViolation = transactionViolations.some((violation) => violation.name === CONST.VIOLATIONS.MISSING_TAG); | ||
const isTagInPolicy = policyTags ? !!policyTags[updatedTransaction.tag ?? '']?.enabled : false; | ||
|
||
// Add 'tagOutOfPolicy' violation if tag is not in policy | ||
if (!hasTagOutOfPolicyViolation && updatedTransaction.tag && !isTagInPolicy) { | ||
newTransactionViolations.push({name: CONST.VIOLATIONS.TAG_OUT_OF_POLICY, type: 'violation'}); | ||
} | ||
|
||
// Remove 'tagOutOfPolicy' violation if tag is in policy | ||
if (hasTagOutOfPolicyViolation && updatedTransaction.tag && isTagInPolicy) { | ||
newTransactionViolations = reject(newTransactionViolations, {name: CONST.VIOLATIONS.TAG_OUT_OF_POLICY}); | ||
} | ||
|
||
// Remove 'missingTag' violation if tag is valid according to policy | ||
if (hasMissingTagViolation && isTagInPolicy) { | ||
newTransactionViolations = reject(newTransactionViolations, {name: CONST.VIOLATIONS.MISSING_TAG}); | ||
} | ||
// Add 'missingTag violation' if tag is required and not set | ||
if (!hasMissingTagViolation && !updatedTransaction.tag && policyRequiresTags) { | ||
newTransactionViolations.push({name: CONST.VIOLATIONS.MISSING_TAG, type: 'violation'}); | ||
} | ||
} | ||
newTransactionViolations = | ||
Object.keys(policyTagList).length === 1 | ||
? getTagViolationsForSingleLevelTags(updatedTransaction, newTransactionViolations, policyRequiresTags, policyTagList) | ||
: getTagViolationsForMultiLevelTags(updatedTransaction, newTransactionViolations, policyRequiresTags, policyTagList); | ||
} | ||
|
||
return { | ||
|
@@ -181,7 +258,7 @@ const ViolationsUtils = { | |
case 'smartscanFailed': | ||
return translate('violations.smartscanFailed'); | ||
case 'someTagLevelsRequired': | ||
return translate('violations.someTagLevelsRequired'); | ||
return translate('violations.someTagLevelsRequired', {tagName}); | ||
case 'tagOutOfPolicy': | ||
return translate('violations.tagOutOfPolicy', {tagName}); | ||
case 'taxAmountChanged': | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,11 +132,6 @@ describe('getViolationsOnyxData', () => { | |
Lunch: {name: 'Lunch', enabled: true}, | ||
Dinner: {name: 'Dinner', enabled: true}, | ||
}, | ||
Tag: { | ||
name: 'Tag', | ||
required: true, | ||
tags: {Lunch: {enabled: true}, Dinner: {enabled: true}}, | ||
}, | ||
}, | ||
}; | ||
transaction.tag = 'Lunch'; | ||
|
@@ -201,4 +196,77 @@ describe('getViolationsOnyxData', () => { | |
expect(result.value).not.toContainEqual([missingTagViolation]); | ||
}); | ||
}); | ||
describe('policy has multi level tags', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NAB: can we add a line break in between describe statements? |
||
beforeEach(() => { | ||
policyRequiresTags = true; | ||
policyTags = { | ||
Department: { | ||
name: 'Department', | ||
tags: { | ||
Accounting: { | ||
name: 'Accounting', | ||
enabled: true, | ||
}, | ||
}, | ||
required: true, | ||
}, | ||
Region: { | ||
name: 'Region', | ||
tags: { | ||
Africa: { | ||
name: 'Africa', | ||
enabled: true, | ||
}, | ||
}, | ||
}, | ||
Project: { | ||
name: 'Project', | ||
tags: { | ||
Project1: { | ||
name: 'Project1', | ||
enabled: true, | ||
}, | ||
}, | ||
required: true, | ||
}, | ||
}; | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NAB: can we add a line break in between tests? |
||
it('should return someTagLevelsRequired when a required tag is missing', () => { | ||
const someTagLevelsRequiredViolation = { | ||
name: 'someTagLevelsRequired', | ||
type: 'violation', | ||
data: { | ||
errorIndexes: [0, 1, 2], | ||
}, | ||
}; | ||
|
||
// Test case where transaction has no tags | ||
let result = ViolationsUtils.getViolationsOnyxData(transaction, transactionViolations, policyRequiresTags, policyTags, policyRequiresCategories, policyCategories); | ||
expect(result.value).toEqual([someTagLevelsRequiredViolation]); | ||
|
||
// Test case where transaction has 1 tag | ||
transaction.tag = 'Accounting'; | ||
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'; | ||
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'; | ||
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', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NAB: can we add a line break in between tests? |
||
policyTags.Department.tags.Accounting.enabled = false; | ||
transaction.tag = 'Accounting:Africa:Project1'; | ||
const result = ViolationsUtils.getViolationsOnyxData(transaction, transactionViolations, policyRequiresTags, policyTags, policyRequiresCategories, policyCategories); | ||
const violation = {...tagOutOfPolicyViolation, data: {tagName: 'Department'}}; | ||
expect(result.value).toEqual([violation]); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will the
currentViolations
always have a single element?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, because we only return 1 violation per field per request