From 89f9373ed4ca2dd6c374049c84e866ebfc0da87c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucien=20Akchot=C3=A9?= Date: Wed, 1 May 2024 16:04:43 +0200 Subject: [PATCH] create xero utils functions --- src/libs/PolicyUtils.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/libs/PolicyUtils.ts b/src/libs/PolicyUtils.ts index 0919a93f91bb..4836c42c7302 100644 --- a/src/libs/PolicyUtils.ts +++ b/src/libs/PolicyUtils.ts @@ -6,7 +6,7 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type {Policy, PolicyCategories, PolicyEmployeeList, PolicyTagList, PolicyTags, TaxRate} from '@src/types/onyx'; -import type {PolicyFeatureName, Rate} from '@src/types/onyx/Policy'; +import type {PolicyFeatureName, Rate, Tenant} from '@src/types/onyx/Policy'; import type PolicyEmployee from '@src/types/onyx/PolicyEmployee'; import type {EmptyObject} from '@src/types/utils/EmptyObject'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; @@ -390,6 +390,20 @@ function canSendInvoice(policies: OnyxCollection): boolean { return getActiveAdminWorkspaces(policies).length > 0; } +/** Get the Xero organizations connected to the policy */ +function getXeroTenants(policy: Policy | undefined): Tenant[] { + // Due to the way optional chain is being handled in this useMemo we are forced to use this approach to properly handle undefined values + // eslint-disable-next-line @typescript-eslint/prefer-optional-chain + if (!policy || !policy.connections || !policy.connections.xero || !policy.connections.xero.data) { + return []; + } + return policy.connections.xero.data.tenants ?? []; +}; + +function findCurrentXeroOrganization(tenants: Tenant[] | undefined, organizationID: string | undefined): Tenant | undefined { + return tenants?.find((tenant) => tenant.id === organizationID); +} + export { getActivePolicies, hasAccountingConnections, @@ -435,6 +449,8 @@ export { getPolicy, getActiveAdminWorkspaces, canSendInvoice, + getXeroTenants, + findCurrentXeroOrganization, }; export type {MemberEmailsToAccountIDs};