Skip to content

Commit

Permalink
Merge pull request #34402 from infinitered/lindboe/violations/money-r…
Browse files Browse the repository at this point in the history
…equest-updates
  • Loading branch information
cead22 authored Feb 9, 2024
2 parents 53310db + d528580 commit 1923870
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 68 deletions.
5 changes: 3 additions & 2 deletions src/components/ReportActionItem/MoneyRequestView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,11 @@ function MoneyRequestView({
Navigation.dismissModal();
return;
}
IOU.updateMoneyRequestBillable(transaction?.transactionID ?? '', report?.reportID, newBillable);
// @ts-expect-error: the type used across the app for policyTags is not what is returned by Onyx, PolicyTagList represents that, but existing policy tag utils need a refactor to fix this
IOU.updateMoneyRequestBillable(transaction?.transactionID ?? '', report?.reportID, newBillable, policy, policyTags, policyCategories);
Navigation.dismissModal();
},
[transaction, report],
[transaction, report, policy, policyTags, policyCategories],
);

if (isCardTransaction) {
Expand Down
27 changes: 15 additions & 12 deletions src/libs/Violations/ViolationsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import type {OnyxUpdate} from 'react-native-onyx';
import type {Phrase, PhraseParameters} from '@libs/Localize';
import type {TranslationPaths} from '@src/languages/types';
import ONYXKEYS from '@src/ONYXKEYS';
import type {PolicyCategories, PolicyTags, Transaction, TransactionViolation} from '@src/types/onyx';
import type {PolicyCategories, PolicyTagList, 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,
updatedTransaction: Transaction,
transactionViolations: TransactionViolation[],
policyRequiresTags: boolean,
policyTags: PolicyTags,
policyTagList: PolicyTagList,
policyRequiresCategories: boolean,
policyCategories: PolicyCategories,
): OnyxUpdate {
Expand All @@ -24,15 +24,16 @@ const ViolationsUtils = {
if (policyRequiresCategories) {
const hasCategoryOutOfPolicyViolation = transactionViolations.some((violation) => violation.name === 'categoryOutOfPolicy');
const hasMissingCategoryViolation = transactionViolations.some((violation) => violation.name === 'missingCategory');
const isCategoryInPolicy = !!policyCategories[transaction.category ?? '']?.enabled;
const categoryKey = updatedTransaction.category;
const isCategoryInPolicy = categoryKey ? policyCategories?.[categoryKey]?.enabled : false;

// Add 'categoryOutOfPolicy' violation if category is not in policy
if (!hasCategoryOutOfPolicyViolation && transaction.category && !isCategoryInPolicy) {
if (!hasCategoryOutOfPolicyViolation && categoryKey && !isCategoryInPolicy) {
newTransactionViolations.push({name: 'categoryOutOfPolicy', type: 'violation', userMessage: ''});
}

// Remove 'categoryOutOfPolicy' violation if category is in policy
if (hasCategoryOutOfPolicyViolation && transaction.category && isCategoryInPolicy) {
if (hasCategoryOutOfPolicyViolation && updatedTransaction.category && isCategoryInPolicy) {
newTransactionViolations = reject(newTransactionViolations, {name: 'categoryOutOfPolicy'});
}

Expand All @@ -42,23 +43,25 @@ const ViolationsUtils = {
}

// Add 'missingCategory' violation if category is required and not set
if (!hasMissingCategoryViolation && policyRequiresCategories && !transaction.category) {
if (!hasMissingCategoryViolation && policyRequiresCategories && !categoryKey) {
newTransactionViolations.push({name: 'missingCategory', type: 'violation', userMessage: ''});
}
}

if (policyRequiresTags) {
const policyTagListName = Object.keys(policyTagList)[0];
const policyTags = policyTagList[policyTagListName]?.tags;
const hasTagOutOfPolicyViolation = transactionViolations.some((violation) => violation.name === 'tagOutOfPolicy');
const hasMissingTagViolation = transactionViolations.some((violation) => violation.name === 'missingTag');
const isTagInPolicy = !!policyTags[transaction.tag ?? '']?.enabled;
const isTagInPolicy = policyTags ? !!policyTags[updatedTransaction.tag ?? '']?.enabled : false;

// Add 'tagOutOfPolicy' violation if tag is not in policy
if (!hasTagOutOfPolicyViolation && transaction.tag && !isTagInPolicy) {
if (!hasTagOutOfPolicyViolation && updatedTransaction.tag && !isTagInPolicy) {
newTransactionViolations.push({name: 'tagOutOfPolicy', type: 'violation', userMessage: ''});
}

// Remove 'tagOutOfPolicy' violation if tag is in policy
if (hasTagOutOfPolicyViolation && transaction.tag && isTagInPolicy) {
if (hasTagOutOfPolicyViolation && updatedTransaction.tag && isTagInPolicy) {
newTransactionViolations = reject(newTransactionViolations, {name: 'tagOutOfPolicy'});
}

Expand All @@ -68,14 +71,14 @@ const ViolationsUtils = {
}

// Add 'missingTag violation' if tag is required and not set
if (!hasMissingTagViolation && !transaction.tag && policyRequiresTags) {
if (!hasMissingTagViolation && !updatedTransaction.tag && policyRequiresTags) {
newTransactionViolations.push({name: 'missingTag', type: 'violation', userMessage: ''});
}
}

return {
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transaction.transactionID}`,
key: `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${updatedTransaction.transactionID}`,
value: newTransactionViolations,
};
},
Expand Down
Loading

0 comments on commit 1923870

Please sign in to comment.