Skip to content

Commit

Permalink
Fix case of passing empty object to policyTags
Browse files Browse the repository at this point in the history
  • Loading branch information
lindboe committed Jan 12, 2024
1 parent 72cfa45 commit 8ff7c69
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/libs/ViolationsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import reject from 'lodash/reject';
import Onyx from 'react-native-onyx';
import type {TranslationPaths} from '@src/languages/types';
import ONYXKEYS from '@src/ONYXKEYS';
import type {PolicyCategories, PolicyTagList, Transaction, TransactionViolation} from '@src/types/onyx';
import type {MaybePolicyTagList, PolicyCategories, Transaction, TransactionViolation} from '@src/types/onyx';
import type {Phrase, PhraseParameters} from './Localize';

const ViolationsUtils = {
Expand All @@ -14,7 +14,7 @@ const ViolationsUtils = {
updatedTransaction: Transaction,
transactionViolations: TransactionViolation[],
policyRequiresTags: boolean,
policyTagList: PolicyTagList,
policyTagList: MaybePolicyTagList,
policyRequiresCategories: boolean,
policyCategories: PolicyCategories,
): {
Expand Down Expand Up @@ -52,10 +52,10 @@ const ViolationsUtils = {

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

// Add 'tagOutOfPolicy' violation if tag is not in policy
if (!hasTagOutOfPolicyViolation && updatedTransaction.tag && !isTagInPolicy) {
Expand Down
6 changes: 5 additions & 1 deletion src/types/onyx/PolicyTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ type PolicyTagList<T extends string = string> = Record<
}
>;

export type {PolicyTag, PolicyTags, PolicyTagList};
// When queried from Onyx, if there is no matching policy tag list, the data
// returned will be an empty object.
type MaybePolicyTagList = PolicyTagList | Record<string, undefined>;

export type {PolicyTag, PolicyTags, PolicyTagList, MaybePolicyTagList};
3 changes: 2 additions & 1 deletion src/types/onyx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import type {PolicyCategories, PolicyCategory} from './PolicyCategory';
import type {PolicyMembers} from './PolicyMember';
import type PolicyMember from './PolicyMember';
import type PolicyReportField from './PolicyReportField';
import type {PolicyTag, PolicyTagList, PolicyTags} from './PolicyTag';
import type {MaybePolicyTagList, PolicyTag, PolicyTagList, PolicyTags} from './PolicyTag';
import type PrivatePersonalDetails from './PrivatePersonalDetails';
import type RecentlyUsedCategories from './RecentlyUsedCategories';
import type RecentlyUsedReportFields from './RecentlyUsedReportFields';
Expand Down Expand Up @@ -104,6 +104,7 @@ export type {
PolicyTag,
PolicyTags,
PolicyTagList,
MaybePolicyTagList,
PrivatePersonalDetails,
RecentWaypoint,
RecentlyUsedCategories,
Expand Down

0 comments on commit 8ff7c69

Please sign in to comment.