Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/domain/accounts/external-us-bank.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { describe, expect, test } from 'bun:test';
import {
blocksUsBankLink,
isFailedUsBank,
isLinkingUsBank,
isPendingLinkUsBank,
isUsableUsBank,
isUsBankLinkSettled,
visibleProducts,
Expand Down Expand Up @@ -117,3 +119,42 @@ describe('visibleProducts', () => {
]);
});
});

describe('isPendingLinkUsBank', () => {
test('accepts an account whose link was never finished', () => {
expect(
isPendingLinkUsBank(bank('creation_in_progress', 'pending_link')),
).toBe(true);
});

test('rejects a link waiting on activation', () => {
expect(isPendingLinkUsBank(bank('creation_in_progress', 'active'))).toBe(
false,
);
});

test('rejects other product kinds', () => {
expect(isPendingLinkUsBank(pocket)).toBe(false);
});
});

describe('blocksUsBankLink', () => {
test('blocks while a bank is usable or activating', () => {
expect(blocksUsBankLink(bank('active', 'active'))).toBe(true);
expect(blocksUsBankLink(bank('creation_in_progress', 'active'))).toBe(true);
});

test('does not block on failed or unfinished links', () => {
expect(blocksUsBankLink(bank('creation_failed', 'pending_link'))).toBe(
false,
);
expect(blocksUsBankLink(bank('active', 'link_failed'))).toBe(false);
expect(blocksUsBankLink(bank('creation_in_progress', 'pending_link'))).toBe(
false,
);
});

test('ignores other product kinds', () => {
expect(blocksUsBankLink(pocket)).toBe(false);
});
});
22 changes: 22 additions & 0 deletions src/domain/accounts/external-us-bank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,28 @@ export function isLinkingUsBank(product: Product): boolean {
);
}

/** A linking attempt whose Plaid flow was opened but never finished. */
export function isPendingLinkUsBank(product: Product): boolean {
return (
isExternalUsBank(product) &&
product.status === 'creation_in_progress' &&
product.linkStatus === 'pending_link'
);
}

/**
* A US bank that keeps its pocket from starting a new link. Failed and
* unfinished attempts don't: retrying on the same pocket reuses or replaces
* the unfinished attempt instead of adding another one.
*/
export function blocksUsBankLink(product: Product): boolean {
return (
isExternalUsBank(product) &&
!isFailedUsBank(product) &&
!isPendingLinkUsBank(product)
);
}

/** A linking attempt that won't change state anymore without user action. */
export function isUsBankLinkSettled(product: Product): boolean {
return isUsableUsBank(product) || isFailedUsBank(product);
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@
"addProductBrebLabel": "BRE-B key (COP)",
"addProductPlaidLabel": "Plaid (USD)",
"addProductNeedsVirtualAccount": "You can only add products to an active pocket.",
"usBankLinking": "Linking in progress"
"usBankLinking": "Linking in progress",
"usBankPendingLink": "Linking not finished"
}
},
"brebKeys": {
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@
"addProductBrebLabel": "Llave BRE-B (COP)",
"addProductPlaidLabel": "Plaid (USD)",
"addProductNeedsVirtualAccount": "Solo puedes agregar productos a un bolsillo activo.",
"usBankLinking": "Vinculación en proceso"
"usBankLinking": "Vinculación en proceso",
"usBankPendingLink": "Vinculación sin terminar"
}
},
"brebKeys": {
Expand Down
12 changes: 9 additions & 3 deletions src/routes/_authed/accounts/$urn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ import {
USD_ENABLED,
} from '~/config/features';
import {
blocksUsBankLink,
isLinkingUsBank,
isPendingLinkUsBank,
visibleProducts,
} from '~/domain/accounts/external-us-bank';
import type { AssetBalance, Product } from '~/domain/accounts/types';
Expand Down Expand Up @@ -445,7 +447,9 @@ function RouteComponent() {
{getProductKindLabel(product.kind)} •{' '}
{isLinkingUsBank(product)
? t('accounts.detail.usBankLinking')
: product.status}
: isPendingLinkUsBank(product)
? t('accounts.detail.usBankPendingLink')
: product.status}
</p>
</div>
{link ? (
Expand Down Expand Up @@ -573,8 +577,10 @@ function RouteComponent() {
]
.filter(
(option) =>
!shownProducts.some(
(product) => product.kind === option.matchKind,
!shownProducts.some((product) =>
option.matchKind === 'external-us-bank'
? blocksUsBankLink(product)
: product.kind === option.matchKind,
),
)
.map((option) => (
Expand Down
10 changes: 3 additions & 7 deletions src/routes/_authed/topup/us-banks/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ComingSoonScreen } from '~/components/coming-soon';
import { US_RAILS_ENABLED } from '~/config/features';
import { useAuth } from '~/contexts/auth/auth-context';
import {
blocksUsBankLink,
isFailedUsBank,
isLinkingUsBank,
isUsableUsBank,
Expand Down Expand Up @@ -134,7 +135,7 @@ function RouteComponent() {

// Which pocket the new Plaid link itself gets associated with. Only one
// `external-us-bank` product per pocket, same rule as card/BRE-B — a failed
// link doesn't count, so the pocket can be linked again.
// or unfinished link doesn't count, so the pocket can be linked again.
const [selectedLinkLedgerId, setSelectedLinkLedgerId] = useState<
string | null
>(null);
Expand All @@ -143,12 +144,7 @@ function RouteComponent() {
});
const linkablePockets = useMemo(
() =>
allPockets.filter(
(account) =>
!account.products.some(
(p) => p.kind === 'external-us-bank' && !isFailedUsBank(p),
),
),
allPockets.filter((account) => !account.products.some(blocksUsBankLink)),
[allPockets],
);
const contextLinkLedgerId = allPockets.some(
Expand Down
Loading