From 206a6811eec45d8059c030e3d4f7570cbe627436 Mon Sep 17 00:00:00 2001 From: VickyStash Date: Fri, 8 Nov 2024 16:59:34 +0100 Subject: [PATCH 01/15] Add getCompanyFeeds function which returns both custom and direct feeds --- src/libs/CardUtils.ts | 21 ++++++-- src/libs/actions/CompanyCards.ts | 46 ++++++++-------- src/pages/workspace/WorkspaceInitialPage.tsx | 3 +- .../workspace/WorkspaceMoreFeaturesPage.tsx | 2 +- .../WorkspaceCompanyCardFeedSelectorPage.tsx | 7 +-- ...WorkspaceCompanyCardsListHeaderButtons.tsx | 14 +++-- .../WorkspaceCompanyCardsPage.tsx | 6 +-- .../WorkspaceCompanyCardsSettingsPage.tsx | 3 +- .../members/WorkspaceMemberDetailsPage.tsx | 2 +- .../members/WorkspaceMemberNewCardPage.tsx | 2 +- src/types/onyx/CardFeeds.ts | 54 ++++++++++--------- 11 files changed, 91 insertions(+), 69 deletions(-) diff --git a/src/libs/CardUtils.ts b/src/libs/CardUtils.ts index ad9bf7f4b90e..162e16f26e2e 100644 --- a/src/libs/CardUtils.ts +++ b/src/libs/CardUtils.ts @@ -227,11 +227,22 @@ function getCardFeedIcon(cardFeed: CompanyCardFeed | typeof CONST.EXPENSIFY_CARD return Illustrations.AmexCompanyCards; } -function removeExpensifyCardFromCompanyCards(companyCards?: Record) { - if (!companyCards) { +function isCustomFeed(feed: CompanyCardFeed): boolean { + return CONST.COMPANY_CARD.FEED_BANK_NAME.MASTER_CARD === feed || CONST.COMPANY_CARD.FEED_BANK_NAME.VISA === feed || CONST.COMPANY_CARD.FEED_BANK_NAME.AMEX === feed; +} + +function getCompanyFeeds(cardFeeds: OnyxEntry): Partial> { + return {...cardFeeds?.settings?.companyCards, ...cardFeeds?.settings?.oAuthAccountDetails}; +} + +function removeExpensifyCardFromCompanyCards(cardFeeds: OnyxEntry): Partial> { + if (!cardFeeds) { return {}; } - return Object.fromEntries(Object.entries(companyCards).filter(([key]) => key !== CONST.EXPENSIFY_CARD.BANK)); + + const companyCards = getCompanyFeeds(cardFeeds); + + return Object.fromEntries(Object.entries(companyCards).filter(([key]) => key !== CONST.EXPENSIFY_CARD.BANK)) as Partial>; } function getCardFeedName(feedType: CompanyCardFeed): string { @@ -301,7 +312,7 @@ const getCorrectStepForSelectedBank = (selectedBank: ValueOf, cardFeeds: OnyxEntry): CompanyCardFeed { - const defaultFeed = Object.keys(removeExpensifyCardFromCompanyCards(cardFeeds?.settings?.companyCards)).at(0) as CompanyCardFeed; + const defaultFeed = Object.keys(removeExpensifyCardFromCompanyCards(cardFeeds)).at(0) as CompanyCardFeed; return lastSelectedFeed ?? defaultFeed; } @@ -323,6 +334,8 @@ export { getCompanyCardNumber, getCardFeedIcon, getCardFeedName, + getCompanyFeeds, + isCustomFeed, getBankCardDetailsImage, getSelectedFeed, getCorrectStepForSelectedBank, diff --git a/src/libs/actions/CompanyCards.ts b/src/libs/actions/CompanyCards.ts index 18779a284278..1250117a9323 100644 --- a/src/libs/actions/CompanyCards.ts +++ b/src/libs/actions/CompanyCards.ts @@ -10,6 +10,7 @@ import type { UpdateCompanyCardNameParams, } from '@libs/API/parameters'; import {READ_COMMANDS, WRITE_COMMANDS} from '@libs/API/types'; +import * as CardUtils from '@libs/CardUtils'; import * as ErrorUtils from '@libs/ErrorUtils'; import * as NetworkStore from '@libs/Network/NetworkStore'; import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; @@ -123,17 +124,18 @@ function setWorkspaceCompanyCardFeedName(policyID: string, workspaceAccountID: n function setWorkspaceCompanyCardTransactionLiability(workspaceAccountID: number, policyID: string, bankName: string, liabilityType: string) { const authToken = NetworkStore.getAuthToken(); + const isCustomFeed = CardUtils.isCustomFeed(bankName as CompanyCardFeed); + const feedUpdates = { + [bankName]: {liabilityType}, + }; + const onyxData: OnyxData = { optimisticData: [ { onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER}${workspaceAccountID}`, value: { - settings: { - companyCards: { - [bankName]: {liabilityType}, - }, - }, + settings: isCustomFeed ? {companyCards: feedUpdates} : {oAuthAccountDetails: feedUpdates}, }, }, ], @@ -151,6 +153,8 @@ function setWorkspaceCompanyCardTransactionLiability(workspaceAccountID: number, function deleteWorkspaceCompanyCardFeed(policyID: string, workspaceAccountID: number, bankName: string) { const authToken = NetworkStore.getAuthToken(); + const isCustomFeed = CardUtils.isCustomFeed(bankName as CompanyCardFeed); + const feedUpdates = isCustomFeed ? {companyCards: {[bankName]: null}} : {oAuthAccountDetails: {[bankName]: null}}; const onyxData: OnyxData = { optimisticData: [ @@ -159,9 +163,7 @@ function deleteWorkspaceCompanyCardFeed(policyID: string, workspaceAccountID: nu key: `${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER}${workspaceAccountID}`, value: { settings: { - companyCards: { - [bankName]: null, - }, + ...feedUpdates, companyCardNicknames: { [bankName]: null, }, @@ -290,6 +292,8 @@ function unassignWorkspaceCompanyCard(workspaceAccountID: number, bankName: stri function updateWorkspaceCompanyCard(workspaceAccountID: number, cardID: string, bankName: string) { const authToken = NetworkStore.getAuthToken(); + const isCustomFeed = CardUtils.isCustomFeed(bankName as CompanyCardFeed); + const optimisticData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -325,13 +329,7 @@ function updateWorkspaceCompanyCard(workspaceAccountID: number, cardID: string, onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER}${workspaceAccountID}`, value: { - settings: { - companyCards: { - [bankName]: { - errors: null, - }, - }, - }, + settings: isCustomFeed ? {companyCards: {[bankName]: {errors: null}}} : {oAuthAccountDetails: {[bankName]: {errors: null}}}, }, }, ]; @@ -398,13 +396,17 @@ function updateWorkspaceCompanyCard(workspaceAccountID: number, cardID: string, onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER}${workspaceAccountID}`, value: { - settings: { - companyCards: { - [bankName]: { - errors: {error: CONST.COMPANY_CARDS.CONNECTION_ERROR}, - }, - }, - }, + settings: isCustomFeed + ? { + companyCards: { + [bankName]: {errors: {error: CONST.COMPANY_CARDS.CONNECTION_ERROR}}, + }, + } + : { + oAuthAccountDetails: { + [bankName]: {errors: {error: CONST.COMPANY_CARDS.CONNECTION_ERROR}}, + }, + }, }, }, ]; diff --git a/src/pages/workspace/WorkspaceInitialPage.tsx b/src/pages/workspace/WorkspaceInitialPage.tsx index 971cc064f9a5..16c036bf5d6f 100644 --- a/src/pages/workspace/WorkspaceInitialPage.tsx +++ b/src/pages/workspace/WorkspaceInitialPage.tsx @@ -20,6 +20,7 @@ import useSingleExecution from '@hooks/useSingleExecution'; import useThemeStyles from '@hooks/useThemeStyles'; import useWaitForNavigation from '@hooks/useWaitForNavigation'; import {isConnectionInProgress} from '@libs/actions/connections'; +import * as CardUtils from '@libs/CardUtils'; import * as CurrencyUtils from '@libs/CurrencyUtils'; import getTopmostRouteName from '@libs/Navigation/getTopmostRouteName'; import Navigation from '@libs/Navigation/Navigation'; @@ -222,7 +223,7 @@ function WorkspaceInitialPage({policyDraft, policy: policyProp, route}: Workspac icon: Expensicons.CreditCard, action: singleExecution(waitForNavigate(() => Navigation.navigate(ROUTES.WORKSPACE_COMPANY_CARDS.getRoute(policyID)))), routeName: SCREENS.WORKSPACE.COMPANY_CARDS, - brickRoadIndicator: PolicyUtils.hasPolicyFeedsError(cardFeeds?.settings?.companyCards ?? {}) ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined, + brickRoadIndicator: PolicyUtils.hasPolicyFeedsError(CardUtils.getCompanyFeeds(cardFeeds)) ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined, }); } diff --git a/src/pages/workspace/WorkspaceMoreFeaturesPage.tsx b/src/pages/workspace/WorkspaceMoreFeaturesPage.tsx index 4eb1f752d176..805cda9a2eba 100644 --- a/src/pages/workspace/WorkspaceMoreFeaturesPage.tsx +++ b/src/pages/workspace/WorkspaceMoreFeaturesPage.tsx @@ -129,7 +129,7 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro subtitleTranslationKey: 'workspace.moreFeatures.companyCards.subtitle', isActive: policy?.areCompanyCardsEnabled ?? false, pendingAction: policy?.pendingFields?.areCompanyCardsEnabled, - disabled: !isEmptyObject(CardUtils.removeExpensifyCardFromCompanyCards(cardFeeds?.settings?.companyCards)), + disabled: !isEmptyObject(CardUtils.removeExpensifyCardFromCompanyCards(cardFeeds)), action: (isEnabled: boolean) => { if (!policyID) { return; diff --git a/src/pages/workspace/companyCards/WorkspaceCompanyCardFeedSelectorPage.tsx b/src/pages/workspace/companyCards/WorkspaceCompanyCardFeedSelectorPage.tsx index 684ee0660d28..50eb27df790b 100644 --- a/src/pages/workspace/companyCards/WorkspaceCompanyCardFeedSelectorPage.tsx +++ b/src/pages/workspace/companyCards/WorkspaceCompanyCardFeedSelectorPage.tsx @@ -41,15 +41,16 @@ function WorkspaceCompanyCardFeedSelectorPage({route}: WorkspaceCompanyCardFeedS const [cardFeeds] = useOnyx(`${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER}${workspaceAccountID}`); const [lastSelectedFeed] = useOnyx(`${ONYXKEYS.COLLECTION.LAST_SELECTED_FEED}${policyID}`); const selectedFeed = CardUtils.getSelectedFeed(lastSelectedFeed, cardFeeds); - const availableCards = CardUtils.removeExpensifyCardFromCompanyCards(cardFeeds?.settings?.companyCards); + const companyFeeds = CardUtils.getCompanyFeeds(cardFeeds); + const availableCards = CardUtils.removeExpensifyCardFromCompanyCards(cardFeeds); const feeds: CardFeedListItem[] = (Object.keys(availableCards) as CompanyCardFeed[]).map((feed) => ({ value: feed, text: cardFeeds?.settings?.companyCardNicknames?.[feed] ?? CardUtils.getCardFeedName(feed), keyForList: feed, isSelected: feed === selectedFeed, - brickRoadIndicator: cardFeeds?.settings?.companyCards?.[feed]?.errors ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined, - canShowSeveralIndicators: !!cardFeeds?.settings?.companyCards?.[feed]?.errors, + brickRoadIndicator: companyFeeds[feed]?.errors ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : undefined, + canShowSeveralIndicators: !!companyFeeds[feed]?.errors, leftElement: ( @@ -65,7 +64,7 @@ function WorkspaceCompanyCardsListHeaderButtons({policyID, selectedFeed}: Worksp {formattedFeedName} - {PolicyUtils.hasPolicyFeedsError(cardFeeds?.settings?.companyCards ?? {}, selectedFeed) && ( + {PolicyUtils.hasPolicyFeedsError(companyFeeds, selectedFeed) && (