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

Client-side violations for money request updates #34402

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
a551927
Add optimistic violations to money request edits
lindboe Jan 10, 2024
5ea3000
Merge remote-tracking branch 'upstream/main' into lindboe/violations/…
lindboe Jan 10, 2024
56dd74d
Lint fix
lindboe Jan 10, 2024
fe4b178
Fix bugs
lindboe Jan 10, 2024
a189970
Fix data passing
lindboe Jan 10, 2024
69ad695
Temp fix for tag issue
lindboe Jan 11, 2024
41e87ca
Use full policyTags data as ViolationsUtils input
lindboe Jan 12, 2024
a09e4ac
getViolationsOnyxData requires new transaction data but previous tran…
lindboe Jan 12, 2024
72cfa45
Improve types
lindboe Jan 12, 2024
8ff7c69
Fix case of passing empty object to policyTags
lindboe Jan 12, 2024
f2942ea
We should only run getViolationsOnyxData if there's a policy
lindboe Jan 12, 2024
37bbacc
Feedback: Don't use Maybe type
lindboe Jan 15, 2024
0a3e818
Ensure an array is always passed to getViolationsOnyxData
lindboe Jan 15, 2024
d211324
Merge remote-tracking branch 'upstream/main' into lindboe/violations/…
lindboe Jan 15, 2024
ede3bbe
More prettier changes
lindboe Jan 17, 2024
87d2f07
Merge remote-tracking branch 'upstream/main' into lindboe/violations/…
lindboe Jan 19, 2024
04fcb21
Merge remote-tracking branch 'upstream/main' into lindboe/violations/…
lindboe Jan 23, 2024
d87ded8
Merge remote-tracking branch 'upstream/main' into lindboe/violations/…
lindboe Jan 24, 2024
371e166
Support client-side violations in new commands
lindboe Jan 25, 2024
38ffff6
Pass policy args to all commands
lindboe Jan 26, 2024
210cfea
Make currentTransactionViolations always an empty array if falsy
lindboe Jan 31, 2024
269b7df
Merge remote-tracking branch 'upstream/main' into lindboe/violations/…
lindboe Jan 31, 2024
699dc34
Merge remote-tracking branch 'upstream/main' into lindboe/violations/…
lindboe Feb 5, 2024
938c6bc
Manage MoneyRequestView type error
lindboe Feb 5, 2024
84267ea
Merge remote-tracking branch 'upstream/main' into lindboe/violations/…
lindboe Feb 5, 2024
b8248ef
Remove obsolete ts-expect-error
lindboe Feb 5, 2024
32d6617
First stab at fixing typing
lindboe Feb 6, 2024
28a1f95
WIP update
lindboe Feb 6, 2024
3493040
editMoneyRequest is no longer used, delete
lindboe Feb 6, 2024
86ea25f
Finish typing IOU
lindboe Feb 6, 2024
4197a85
Merge remote-tracking branch 'upstream/main' into lindboe/violations/…
lindboe Feb 6, 2024
352f5b1
Expect error in MoneyRequestView
lindboe Feb 6, 2024
934bab4
Random lint change
lindboe Feb 6, 2024
fbbcdae
Revert "editMoneyRequest is no longer used, delete"
lindboe Feb 6, 2024
1eebb39
Merge remote-tracking branch 'upstream/main' into lindboe/violations/…
lindboe Feb 7, 2024
30a2f24
Update command in new description component
lindboe Feb 7, 2024
9cbb446
Merge remote-tracking branch 'upstream/main' into lindboe/violations/…
lindboe Feb 9, 2024
d528580
Apply suggestions from code review
lindboe Feb 9, 2024
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
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
Loading