-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31656 from infinitered/violation-utils
- Loading branch information
Showing
9 changed files
with
475 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import {useCallback, useMemo} from 'react'; | ||
import {TransactionViolation, ViolationName} from '@src/types/onyx'; | ||
|
||
/** | ||
* Names of Fields where violations can occur | ||
*/ | ||
type ViolationField = 'amount' | 'billable' | 'category' | 'comment' | 'date' | 'merchant' | 'receipt' | 'tag' | 'tax'; | ||
|
||
/** | ||
* Map from Violation Names to the field where that violation can occur | ||
*/ | ||
const violationFields: Record<ViolationName, ViolationField> = { | ||
allTagLevelsRequired: 'tag', | ||
autoReportedRejectedExpense: 'merchant', | ||
billableExpense: 'billable', | ||
cashExpenseWithNoReceipt: 'receipt', | ||
categoryOutOfPolicy: 'category', | ||
conversionSurcharge: 'amount', | ||
customUnitOutOfPolicy: 'merchant', | ||
duplicatedTransaction: 'merchant', | ||
fieldRequired: 'merchant', | ||
futureDate: 'date', | ||
invoiceMarkup: 'amount', | ||
maxAge: 'date', | ||
missingCategory: 'category', | ||
missingComment: 'comment', | ||
missingTag: 'tag', | ||
modifiedAmount: 'amount', | ||
modifiedDate: 'date', | ||
nonExpensiworksExpense: 'merchant', | ||
overAutoApprovalLimit: 'amount', | ||
overCategoryLimit: 'amount', | ||
overLimit: 'amount', | ||
overLimitAttendee: 'amount', | ||
perDayLimit: 'amount', | ||
receiptNotSmartScanned: 'receipt', | ||
receiptRequired: 'receipt', | ||
rter: 'merchant', | ||
smartscanFailed: 'receipt', | ||
someTagLevelsRequired: 'tag', | ||
tagOutOfPolicy: 'tag', | ||
taxAmountChanged: 'tax', | ||
taxOutOfPolicy: 'tax', | ||
taxRateChanged: 'tax', | ||
taxRequired: 'tax', | ||
}; | ||
|
||
type ViolationsMap = Map<ViolationField, TransactionViolation[]>; | ||
|
||
function useViolations(violations: TransactionViolation[]) { | ||
const violationsByField = useMemo((): ViolationsMap => { | ||
const violationGroups = new Map<ViolationField, TransactionViolation[]>(); | ||
|
||
for (const violation of violations) { | ||
const field = violationFields[violation.name]; | ||
const existingViolations = violationGroups.get(field) ?? []; | ||
violationGroups.set(field, [...existingViolations, violation]); | ||
} | ||
|
||
return violationGroups ?? new Map(); | ||
}, [violations]); | ||
|
||
const hasViolations = useCallback((field: ViolationField) => Boolean(violationsByField.get(field)?.length), [violationsByField]); | ||
const getViolationsForField = useCallback((field: ViolationField) => violationsByField.get(field) ?? [], [violationsByField]); | ||
|
||
return { | ||
hasViolations, | ||
getViolationsForField, | ||
}; | ||
} | ||
|
||
export default useViolations; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import reject from 'lodash/reject'; | ||
import Onyx from 'react-native-onyx'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import {PolicyCategories, PolicyTags, Transaction, TransactionViolation} from '@src/types/onyx'; | ||
|
||
const ViolationsUtils = { | ||
/** | ||
* Checks a transaction for policy violations and returns an object with Onyx method, key and updated transaction | ||
* violations. | ||
*/ | ||
getViolationsOnyxData( | ||
transaction: Transaction, | ||
transactionViolations: TransactionViolation[], | ||
policyRequiresTags: boolean, | ||
policyTags: PolicyTags, | ||
policyRequiresCategories: boolean, | ||
policyCategories: PolicyCategories, | ||
): { | ||
onyxMethod: string; | ||
key: string; | ||
value: TransactionViolation[]; | ||
} { | ||
let newTransactionViolations = [...transactionViolations]; | ||
|
||
if (policyRequiresCategories) { | ||
const hasCategoryOutOfPolicyViolation = transactionViolations.some((violation) => violation.name === 'categoryOutOfPolicy'); | ||
const hasMissingCategoryViolation = transactionViolations.some((violation) => violation.name === 'missingCategory'); | ||
const isCategoryInPolicy = Boolean(policyCategories[transaction.category]?.enabled); | ||
|
||
// Add 'categoryOutOfPolicy' violation if category is not in policy | ||
if (!hasCategoryOutOfPolicyViolation && transaction.category && !isCategoryInPolicy) { | ||
newTransactionViolations.push({name: 'categoryOutOfPolicy', type: 'violation', userMessage: ''}); | ||
} | ||
|
||
// Remove 'categoryOutOfPolicy' violation if category is in policy | ||
if (hasCategoryOutOfPolicyViolation && transaction.category && isCategoryInPolicy) { | ||
newTransactionViolations = reject(newTransactionViolations, {name: 'categoryOutOfPolicy'}); | ||
} | ||
|
||
// Remove 'missingCategory' violation if category is valid according to policy | ||
if (hasMissingCategoryViolation && isCategoryInPolicy) { | ||
newTransactionViolations = reject(newTransactionViolations, {name: 'missingCategory'}); | ||
} | ||
|
||
// Add 'missingCategory' violation if category is required and not set | ||
if (!hasMissingCategoryViolation && policyRequiresCategories && !transaction.category) { | ||
newTransactionViolations.push({name: 'missingCategory', type: 'violation', userMessage: ''}); | ||
} | ||
} | ||
|
||
if (policyRequiresTags) { | ||
const hasTagOutOfPolicyViolation = transactionViolations.some((violation) => violation.name === 'tagOutOfPolicy'); | ||
const hasMissingTagViolation = transactionViolations.some((violation) => violation.name === 'missingTag'); | ||
const isTagInPolicy = Boolean(policyTags[transaction.tag]?.enabled); | ||
|
||
// Add 'tagOutOfPolicy' violation if tag is not in policy | ||
if (!hasTagOutOfPolicyViolation && transaction.tag && !isTagInPolicy) { | ||
newTransactionViolations.push({name: 'tagOutOfPolicy', type: 'violation', userMessage: ''}); | ||
} | ||
|
||
// Remove 'tagOutOfPolicy' violation if tag is in policy | ||
if (hasTagOutOfPolicyViolation && transaction.tag && isTagInPolicy) { | ||
newTransactionViolations = reject(newTransactionViolations, {name: 'tagOutOfPolicy'}); | ||
} | ||
|
||
// Remove 'missingTag' violation if tag is valid according to policy | ||
if (hasMissingTagViolation && isTagInPolicy) { | ||
newTransactionViolations = reject(newTransactionViolations, {name: 'missingTag'}); | ||
} | ||
|
||
// Add 'missingTag violation' if tag is required and not set | ||
if (!hasMissingTagViolation && !transaction.tag && policyRequiresTags) { | ||
newTransactionViolations.push({name: 'missingTag', type: 'violation', userMessage: ''}); | ||
} | ||
} | ||
|
||
return { | ||
onyxMethod: Onyx.METHOD.SET, | ||
key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transaction.transactionID}`, | ||
value: newTransactionViolations, | ||
}; | ||
}, | ||
}; | ||
|
||
export default ViolationsUtils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* Names of transaction violations | ||
*/ | ||
type ViolationName = | ||
| 'allTagLevelsRequired' | ||
| 'autoReportedRejectedExpense' | ||
| 'billableExpense' | ||
| 'cashExpenseWithNoReceipt' | ||
| 'categoryOutOfPolicy' | ||
| 'conversionSurcharge' | ||
| 'customUnitOutOfPolicy' | ||
| 'duplicatedTransaction' | ||
| 'fieldRequired' | ||
| 'futureDate' | ||
| 'invoiceMarkup' | ||
| 'maxAge' | ||
| 'missingCategory' | ||
| 'missingComment' | ||
| 'missingTag' | ||
| 'modifiedAmount' | ||
| 'modifiedDate' | ||
| 'nonExpensiworksExpense' | ||
| 'overAutoApprovalLimit' | ||
| 'overCategoryLimit' | ||
| 'overLimit' | ||
| 'overLimitAttendee' | ||
| 'perDayLimit' | ||
| 'receiptNotSmartScanned' | ||
| 'receiptRequired' | ||
| 'rter' | ||
| 'smartscanFailed' | ||
| 'someTagLevelsRequired' | ||
| 'tagOutOfPolicy' | ||
| 'taxAmountChanged' | ||
| 'taxOutOfPolicy' | ||
| 'taxRateChanged' | ||
| 'taxRequired'; | ||
|
||
type TransactionViolation = { | ||
type: string; | ||
name: ViolationName; | ||
userMessage: string; | ||
data?: Record<string, string>; | ||
}; | ||
|
||
export type {TransactionViolation, ViolationName}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.