Repository navigation
Fix duplicate broken company card feed time-sensitive tasks #93406
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c01ceda
Fix duplicate broken company card feed time-sensitive tasks
mountiny 60ed587
Rename broken feed helper to useBrokenDirectCompanyCardFeedsForAdmin
mountiny 1b65b2d
Revert accidental Mobile-Expensify submodule bump
mountiny e455a6a
Fix TS error and Knip unused export in useBrokenDirectCompanyCardFeed…
mountiny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
src/pages/home/TimeSensitiveSection/hooks/useBrokenDirectCompanyCardFeedsForAdmin.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import {useMemo} from 'react'; | ||
| import useCardFeedErrors from '@hooks/useCardFeedErrors'; | ||
| import {getCardFeedWithDomainID, isDirectFeed} from '@libs/CardUtils'; | ||
| import type {Policy} from '@src/types/onyx'; | ||
|
|
||
| type BrokenCompanyCardConnection = { | ||
| /** The policy ID associated with this connection */ | ||
| policyID: string; | ||
|
|
||
| /** The policy name associated with this connection */ | ||
| policyName: string; | ||
|
|
||
| /** A representative card ID on this feed, used to render FixCompanyCardConnection */ | ||
| cardID: string; | ||
|
|
||
| /** feedNameWithDomainID for this broken direct feed */ | ||
| feedKey: string; | ||
| }; | ||
|
|
||
| /** | ||
| * Returns at most one broken company card connection per direct (OAuth/Plaid) feed for workspaces | ||
| * where the current user is an admin. Commercial feeds (vcf/cdf/etc.) are excluded because they | ||
| * are file-based and not user-fixable via bank login. | ||
| */ | ||
| function useBrokenDirectCompanyCardFeedsForAdmin(adminPolicies: Policy[] | undefined): BrokenCompanyCardConnection[] { | ||
| const {cardsWithBrokenFeedConnection} = useCardFeedErrors(); | ||
|
|
||
| return useMemo(() => { | ||
| if (!cardsWithBrokenFeedConnection || !adminPolicies) { | ||
| return []; | ||
| } | ||
|
|
||
| const brokenCompanyCardConnectionsByFeed = new Map<string, BrokenCompanyCardConnection>(); | ||
|
|
||
| for (const card of Object.values(cardsWithBrokenFeedConnection)) { | ||
| if (!card?.fundID) { | ||
| continue; | ||
| } | ||
|
|
||
| // Only direct OAuth/Plaid feeds are user-fixable; commercial feeds (vcf/cdf/etc.) are file-based | ||
| if (!isDirectFeed(card.bank)) { | ||
| continue; | ||
| } | ||
|
|
||
| const cardFundID = Number(card.fundID); | ||
| const matchingPolicy = adminPolicies.find((policy) => policy.policyAccountID === cardFundID); | ||
|
|
||
| if (!matchingPolicy) { | ||
| continue; | ||
| } | ||
|
|
||
| const feedKey = getCardFeedWithDomainID(card.bank, card.fundID); | ||
| if (brokenCompanyCardConnectionsByFeed.has(feedKey)) { | ||
| continue; | ||
| } | ||
|
|
||
| brokenCompanyCardConnectionsByFeed.set(feedKey, { | ||
| policyID: matchingPolicy.id, | ||
| policyName: matchingPolicy.name, | ||
| cardID: String(card.cardID), | ||
| feedKey, | ||
| }); | ||
| } | ||
|
|
||
| return Array.from(brokenCompanyCardConnectionsByFeed.values()); | ||
| }, [adminPolicies, cardsWithBrokenFeedConnection]); | ||
| } | ||
|
|
||
| export default useBrokenDirectCompanyCardFeedsForAdmin; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
tests/unit/hooks/useBrokenDirectCompanyCardFeedsForAdmin.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| import {renderHook} from '@testing-library/react-native'; | ||
| import {getCardFeedWithDomainID} from '@libs/CardUtils'; | ||
| import useBrokenDirectCompanyCardFeedsForAdmin from '@pages/home/TimeSensitiveSection/hooks/useBrokenDirectCompanyCardFeedsForAdmin'; | ||
| import CONST from '@src/CONST'; | ||
| import type {Card, CardList} from '@src/types/onyx'; | ||
| import type {CardFeedWithNumber} from '@src/types/onyx/CardFeeds'; | ||
| import type {CardFeedErrors} from '@src/types/onyx/DerivedValues'; | ||
| import type Policy from '@src/types/onyx/Policy'; | ||
|
|
||
| const WORKSPACE_ACCOUNT_ID = 44444444; | ||
| const POLICY_ID = 'POLICY_1'; | ||
| const POLICY_NAME = 'SPS Health'; | ||
|
|
||
| const OAUTH_CHASE_FEED = CONST.COMPANY_CARD.FEED_BANK_NAME.CHASE; | ||
| const OAUTH_AMEX_FEED = CONST.COMPANY_CARD.FEED_BANK_NAME.AMEX_DIRECT; | ||
| const COMMERCIAL_VISA_FEED = CONST.COMPANY_CARD.FEED_BANK_NAME.VISA; | ||
|
|
||
| const DEFAULT_CARD_FEED_ERROR_STATE = { | ||
| shouldShowRBR: false, | ||
| hasFeedErrors: false, | ||
| hasWorkspaceErrors: false, | ||
| isFeedConnectionBroken: false, | ||
| }; | ||
|
|
||
| function createDefaultCardFeedErrors(): CardFeedErrors { | ||
| return { | ||
| cardFeedErrors: {}, | ||
| cardsWithBrokenFeedConnection: {}, | ||
| personalCardsWithBrokenConnection: {}, | ||
| shouldShowRbrForWorkspaceAccountID: {}, | ||
| shouldShowRbrForFeedNameWithDomainID: {}, | ||
| all: DEFAULT_CARD_FEED_ERROR_STATE, | ||
| companyCards: DEFAULT_CARD_FEED_ERROR_STATE, | ||
| expensifyCard: DEFAULT_CARD_FEED_ERROR_STATE, | ||
| personalCard: DEFAULT_CARD_FEED_ERROR_STATE, | ||
| }; | ||
| } | ||
|
|
||
| const mockUseCardFeedErrors = jest.fn<CardFeedErrors, []>(); | ||
|
|
||
| jest.mock('@hooks/useCardFeedErrors', () => ({ | ||
| __esModule: true, | ||
| default: () => mockUseCardFeedErrors(), | ||
| })); | ||
|
|
||
| function createAdminPolicy(overrides: Partial<Policy> & {id: string} = {id: POLICY_ID}): Policy { | ||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion | ||
| return { | ||
| name: POLICY_NAME, | ||
| role: CONST.POLICY.ROLE.ADMIN, | ||
| type: CONST.POLICY.TYPE.TEAM, | ||
| isPolicyExpenseChatEnabled: true, | ||
| policyAccountID: WORKSPACE_ACCOUNT_ID, | ||
| ...overrides, | ||
| } as Policy; | ||
| } | ||
|
|
||
| function createBrokenCard(cardID: number, bank: CardFeedWithNumber): Card { | ||
| return { | ||
| cardID, | ||
| accountID: 1, | ||
| bank, | ||
| cardName: 'Test Card', | ||
| domainName: 'test.exfy', | ||
| fraud: 'none', | ||
| fundID: String(WORKSPACE_ACCOUNT_ID), | ||
| lastFourPAN: '1234', | ||
| lastScrape: '', | ||
| lastScrapeResult: 403, | ||
| lastUpdated: '', | ||
| state: CONST.EXPENSIFY_CARD.STATE.OPEN, | ||
| }; | ||
| } | ||
|
|
||
| function setMockBrokenCards(cardsWithBrokenFeedConnection: CardList) { | ||
| mockUseCardFeedErrors.mockReturnValue({ | ||
| ...createDefaultCardFeedErrors(), | ||
| cardsWithBrokenFeedConnection, | ||
| }); | ||
| } | ||
|
|
||
| describe('useBrokenDirectCompanyCardFeedsForAdmin', () => { | ||
| const adminPolicies = [createAdminPolicy()]; | ||
|
|
||
| beforeEach(() => { | ||
| mockUseCardFeedErrors.mockReturnValue(createDefaultCardFeedErrors()); | ||
| }); | ||
|
|
||
| it('returns one connection when multiple broken cards share the same direct feed', () => { | ||
| setMockBrokenCards({ | ||
| card1: createBrokenCard(1, OAUTH_CHASE_FEED), | ||
| card2: createBrokenCard(2, OAUTH_CHASE_FEED), | ||
| card3: createBrokenCard(3, OAUTH_CHASE_FEED), | ||
| }); | ||
|
|
||
| const {result} = renderHook(() => useBrokenDirectCompanyCardFeedsForAdmin(adminPolicies)); | ||
|
|
||
| expect(result.current).toHaveLength(1); | ||
| expect(result.current.at(0)).toStrictEqual({ | ||
| policyID: POLICY_ID, | ||
| policyName: POLICY_NAME, | ||
| cardID: '1', | ||
| feedKey: getCardFeedWithDomainID(OAUTH_CHASE_FEED, WORKSPACE_ACCOUNT_ID), | ||
| }); | ||
| }); | ||
|
|
||
| it('returns one connection per distinct direct feed', () => { | ||
| setMockBrokenCards({ | ||
| card1: createBrokenCard(1, OAUTH_CHASE_FEED), | ||
| card2: createBrokenCard(2, OAUTH_AMEX_FEED), | ||
| }); | ||
|
|
||
| const {result} = renderHook(() => useBrokenDirectCompanyCardFeedsForAdmin(adminPolicies)); | ||
|
|
||
| expect(result.current).toHaveLength(2); | ||
| expect(result.current.map((connection) => connection.feedKey)).toStrictEqual([ | ||
| getCardFeedWithDomainID(OAUTH_CHASE_FEED, WORKSPACE_ACCOUNT_ID), | ||
| getCardFeedWithDomainID(OAUTH_AMEX_FEED, WORKSPACE_ACCOUNT_ID), | ||
| ]); | ||
| }); | ||
|
|
||
| it('returns no connections for commercial feeds', () => { | ||
| setMockBrokenCards({ | ||
| card1: createBrokenCard(1, COMMERCIAL_VISA_FEED), | ||
| card2: createBrokenCard(2, COMMERCIAL_VISA_FEED), | ||
| card3: createBrokenCard(3, COMMERCIAL_VISA_FEED), | ||
| }); | ||
|
|
||
| const {result} = renderHook(() => useBrokenDirectCompanyCardFeedsForAdmin(adminPolicies)); | ||
|
|
||
| expect(result.current).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('returns only direct feed connections when direct and commercial feeds are both broken', () => { | ||
| setMockBrokenCards({ | ||
| card1: createBrokenCard(1, OAUTH_CHASE_FEED), | ||
| card2: createBrokenCard(2, OAUTH_CHASE_FEED), | ||
| card3: createBrokenCard(3, COMMERCIAL_VISA_FEED), | ||
| card4: createBrokenCard(4, COMMERCIAL_VISA_FEED), | ||
| }); | ||
|
|
||
| const {result} = renderHook(() => useBrokenDirectCompanyCardFeedsForAdmin(adminPolicies)); | ||
|
|
||
| expect(result.current).toHaveLength(1); | ||
| expect(result.current.at(0)?.feedKey).toBe(getCardFeedWithDomainID(OAUTH_CHASE_FEED, WORKSPACE_ACCOUNT_ID)); | ||
| }); | ||
|
|
||
| it('returns an empty array when admin policies are undefined', () => { | ||
| setMockBrokenCards({ | ||
| card1: createBrokenCard(1, OAUTH_CHASE_FEED), | ||
| }); | ||
|
|
||
| const {result} = renderHook(() => useBrokenDirectCompanyCardFeedsForAdmin(undefined)); | ||
|
|
||
| expect(result.current).toStrictEqual([]); | ||
| }); | ||
|
|
||
| it('skips cards without a matching admin workspace policy', () => { | ||
| setMockBrokenCards({ | ||
| card1: createBrokenCard(1, OAUTH_CHASE_FEED), | ||
| }); | ||
|
|
||
| const {result} = renderHook(() => | ||
| useBrokenDirectCompanyCardFeedsForAdmin([ | ||
| createAdminPolicy({ | ||
| id: POLICY_ID, | ||
| policyAccountID: 99999999, | ||
| }), | ||
| ]), | ||
| ); | ||
|
|
||
| expect(result.current).toStrictEqual([]); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.