Skip to content

Commit

Permalink
fix require cycles containing Navigation.ts -> reportUtils.ts -> ... …
Browse files Browse the repository at this point in the history
…-> TransactionUtils/index.ts chain
  • Loading branch information
JKobrynski committed Nov 15, 2024
1 parent 8dc09af commit d24daac
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions src/libs/TransactionUtils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import lodashSet from 'lodash/set';
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import {getPolicyCategoriesData} from '@libs/actions/Policy/Category';
import {getPolicyTagsData} from '@libs/actions/Policy/Tag';
import type {TransactionMergeParams} from '@libs/API/parameters';
import {getCurrencyDecimals} from '@libs/CurrencyUtils';
import DateUtils from '@libs/DateUtils';
Expand All @@ -32,6 +30,10 @@ import type DeepValueOf from '@src/types/utils/DeepValueOf';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import getDistanceInMeters from './getDistanceInMeters';

// Dynamic Imports to avoid circular dependencies
const CategoryActions = () => import('@libs/actions/Policy/Category');
const TagActions = () => import('@libs/actions/Policy/Tag');

let allTransactions: OnyxCollection<Transaction> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.TRANSACTION,
Expand Down Expand Up @@ -1136,37 +1138,41 @@ function compareDuplicateTransactionFields(transactionID: string, reportID: stri
}
} else if (fieldName === 'category') {
const differentValues = getDifferentValues(transactions, keys);
const policyCategories = getPolicyCategoriesData(report?.policyID ?? '-1');
const availableCategories = Object.values(policyCategories)
.filter((category) => differentValues.includes(category.name) && category.enabled && category.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE)
.map((e) => e.name);

if (!areAllFieldsEqualForKey && policy?.areCategoriesEnabled && (availableCategories.length > 1 || (availableCategories.length === 1 && differentValues.includes('')))) {
change[fieldName] = [...availableCategories, ...(differentValues.includes('') ? [''] : [])];
} else if (areAllFieldsEqualForKey) {
keep[fieldName] = firstTransaction?.[keys[0]] ?? firstTransaction?.[keys[1]];
}
} else if (fieldName === 'tag') {
const policyTags = getPolicyTagsData(report?.policyID ?? '-1');
const isMultiLevelTags = PolicyUtils.isMultiLevelTags(policyTags);
if (isMultiLevelTags) {
if (areAllFieldsEqualForKey || !policy?.areTagsEnabled) {
keep[fieldName] = firstTransaction?.[keys[0]] ?? firstTransaction?.[keys[1]];
} else {
processChanges(fieldName, transactions, keys);
}
} else {
const differentValues = getDifferentValues(transactions, keys);
const policyTagsObj = Object.values(Object.values(policyTags).at(0)?.tags ?? {});
const availableTags = policyTagsObj
.filter((tag) => differentValues.includes(tag.name) && tag.enabled && tag.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE)
CategoryActions().then((actions) => {
const policyCategories = actions.getPolicyCategoriesData(report?.policyID ?? '-1');
const availableCategories = Object.values(policyCategories)
.filter((category) => differentValues.includes(category.name) && category.enabled && category.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE)
.map((e) => e.name);
if (!areAllFieldsEqualForKey && policy?.areTagsEnabled && (availableTags.length > 1 || (availableTags.length === 1 && differentValues.includes('')))) {
change[fieldName] = [...availableTags, ...(differentValues.includes('') ? [''] : [])];

if (!areAllFieldsEqualForKey && policy?.areCategoriesEnabled && (availableCategories.length > 1 || (availableCategories.length === 1 && differentValues.includes('')))) {
change[fieldName] = [...availableCategories, ...(differentValues.includes('') ? [''] : [])];
} else if (areAllFieldsEqualForKey) {
keep[fieldName] = firstTransaction?.[keys[0]] ?? firstTransaction?.[keys[1]];
}
}
});
} else if (fieldName === 'tag') {
TagActions().then((module) => {
const policyTags = module.getPolicyTagsData(report?.policyID ?? '-1');
const isMultiLevelTags = PolicyUtils.isMultiLevelTags(policyTags);
if (isMultiLevelTags) {
if (areAllFieldsEqualForKey || !policy?.areTagsEnabled) {
keep[fieldName] = firstTransaction?.[keys[0]] ?? firstTransaction?.[keys[1]];
} else {
processChanges(fieldName, transactions, keys);
}
} else {
const differentValues = getDifferentValues(transactions, keys);
const policyTagsObj = Object.values(Object.values(policyTags).at(0)?.tags ?? {});
const availableTags = policyTagsObj
.filter((tag) => differentValues.includes(tag.name) && tag.enabled && tag.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE)
.map((e) => e.name);
if (!areAllFieldsEqualForKey && policy?.areTagsEnabled && (availableTags.length > 1 || (availableTags.length === 1 && differentValues.includes('')))) {
change[fieldName] = [...availableTags, ...(differentValues.includes('') ? [''] : [])];
} else if (areAllFieldsEqualForKey) {
keep[fieldName] = firstTransaction?.[keys[0]] ?? firstTransaction?.[keys[1]];
}
}
});
} else if (areAllFieldsEqualForKey) {
keep[fieldName] = firstTransaction?.[keys[0]] ?? firstTransaction?.[keys[1]];
} else {
Expand Down

0 comments on commit d24daac

Please sign in to comment.