-
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 19 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 | ||||
---|---|---|---|---|---|---|
|
@@ -50,6 +50,7 @@ const ViolationsUtils = { | |||||
} | ||||||
|
||||||
if (policyRequiresTags) { | ||||||
const selectedTags = updatedTransaction.tag?.split(CONST.COLON) ?? []; | ||||||
const policyTagKeys = Object.keys(policyTagList); | ||||||
|
||||||
// At the moment, we only return violations for tags for workspaces with single-level tags | ||||||
|
@@ -78,6 +79,53 @@ const ViolationsUtils = { | |||||
if (!hasMissingTagViolation && !updatedTransaction.tag && policyRequiresTags) { | ||||||
newTransactionViolations.push({name: CONST.VIOLATIONS.MISSING_TAG, type: 'violation'}); | ||||||
} | ||||||
} else { | ||||||
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 consider extracting some part of this new code into a helper function? I find the logic hard to follow with all the if/else/for statements nested together. Abstracting out some of the logic would make this a lot more readable IMO. |
||||||
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) { | ||||||
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: since 0 is falsy in JS this can be simplified.
Suggested change
|
||||||
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; | ||||||
} | ||||||
} | ||||||
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 some line breaks to the new code for readability? |
||||||
if (!hasInvalidTag) { | ||||||
newTransactionViolations = reject(newTransactionViolations, { | ||||||
name: CONST.VIOLATIONS.TAG_OUT_OF_POLICY, | ||||||
}); | ||||||
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.
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. There's no difference here since we're only calling reject once, so it should go over the array only once, right? 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. Yes, but aren't we preferring native JS methods for filtering after TS migration? |
||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -181,7 +229,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