Enable Banking: warn before the bank's permission runs out, and link again cleanly
- save when the permission ends (consent_until, migration 0032) - Accounts warns two weeks ahead, says Needs attention once it ran out, and offers Link again; an expired or revoked session no longer shows a raw API error - accounts and rows are keyed on identification_hash, which stays the same across links (the uid doesn't), so linking again adds no copies and keeps edits - links made with the per-link uid move to the stable key on their next sync - linking the same bank again replaces the old connection Tested against the stand-in: first link, moving old IDs, linking again, the warning and an expired permission. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0a97ddbcae
commit
514b2aac77
8 files changed
+88
-15
No files matched your search
@@ -55,11 +55,17 @@ export async function ebStart(env: Env, o: { bank: string; country: string; stat
|
||||
}, f);
|
||||
return r.url;
|
||||
}
|
||||
type EbAccount = { uid: string; name?: string; product?: string; details?: string; cash_account_type?: string; currency?: string; account_id?: { iban?: string; other?: { identification?: string } } };
|
||||
type EbAccount = { uid: string; identification_hash?: string; name?: string; product?: string; details?: string; cash_account_type?: string; currency?: string; account_id?: { iban?: string; other?: { identification?: string } } };
|
||||
// An account's uid is new with every link, so it only addresses the API. identification_hash stays the same for the same
|
||||
// account across links: it keys the account and its rows, so linking again after the permission ran out adds no copies.
|
||||
export const ebKey = (a: EbAccount) => a.identification_hash || a.uid;
|
||||
export async function ebFinish(env: Env, code: string, f?: Fetch) {
|
||||
const s = await eb<{ session_id: string; accounts: EbAccount[]; aspsp?: { name?: string } }>(env, "/sessions", { method: "POST", body: JSON.stringify({ code }) }, f);
|
||||
return { ref: s.session_id, institution: s.aspsp?.name ?? "Bank", token: JSON.stringify({ session_id: s.session_id, accounts: s.accounts }) };
|
||||
const s = await eb<{ session_id: string; accounts: EbAccount[]; aspsp?: { name?: string }; access?: { valid_until?: string } }>(env, "/sessions", { method: "POST", body: JSON.stringify({ code }) }, f);
|
||||
const validUntil = s.access?.valid_until ?? null;
|
||||
return { ref: s.session_id, institution: s.aspsp?.name ?? "Bank", validUntil, token: JSON.stringify({ session_id: s.session_id, accounts: s.accounts, valid_until: validUntil }) };
|
||||
}
|
||||
/** The bank's permission ran out or was taken back: only linking again helps. */
|
||||
export const consentExpired = () => Object.assign(new Error("Your bank's permission ran out. Link your bank again to keep syncing. Your history stays."), { code: "CONSENT_EXPIRED" });
|
||||
const EB_TYPES: Record<string, [string, string]> = { CACC: ["depository", "checking"], CASH: ["depository", "checking"], SVGS: ["depository", "savings"], CARD: ["credit", "credit card"], LOAN: ["loan", "loan"] };
|
||||
type EbBalance = { balance_amount?: { amount?: string | number; currency?: string }; balance_type?: string };
|
||||
// Booked balances first: the closing or interim figure on the account, then whatever the bank sends.
|
||||
@@ -68,7 +74,7 @@ export function ebAccount(a: EbAccount, bs: EbBalance[], bank: string): FeedAcco
|
||||
const [type, subtype] = EB_TYPES[a.cash_account_type ?? ""] ?? ["other", (a.cash_account_type ?? "other").toLowerCase()];
|
||||
const b = ebBalance(bs);
|
||||
return {
|
||||
plaid_account_id: `enablebanking:${a.uid}`, name: a.product || a.details || a.name || bank, official_name: bank,
|
||||
plaid_account_id: `enablebanking:${ebKey(a)}`, name: a.product || a.details || a.name || bank, official_name: bank,
|
||||
mask: last4(a.account_id?.iban ?? a.account_id?.other?.identification), type, subtype,
|
||||
current_balance: owed(type, num(b?.balance_amount?.amount)), available_balance: null, credit_limit: null, currency: b?.balance_amount?.currency ?? a.currency ?? null,
|
||||
};
|
||||
@@ -91,8 +97,19 @@ export function ebTxn(t: EbTxn, uid: string): FeedTxn | null {
|
||||
const id = t.entry_reference || t.transaction_id || `${date}|${n}|${text}`;
|
||||
return { plaid_transaction_id: `enablebanking:${uid}:${id}`, account_ref: `enablebanking:${uid}`, date: date.slice(0, 10), amount: out ? Math.abs(n) : -Math.abs(n), name: text || other || "Transaction", merchant_name: other, logo_url: null, pending: false, plaid_category: t.bank_transaction_code?.code ?? null };
|
||||
}
|
||||
async function ebFeed(env: Env, token: string, since: string, f?: Fetch) {
|
||||
const s = JSON.parse(token) as { accounts: EbAccount[]; institution?: string };
|
||||
async function ebFeed(env: Env, token: string, since: string, f?: Fetch, now = Date.now()) {
|
||||
const s = JSON.parse(token) as { accounts: EbAccount[]; institution?: string; valid_until?: string | null };
|
||||
if (s.valid_until && Date.parse(s.valid_until) <= now) throw consentExpired();
|
||||
try {
|
||||
return await ebRead(env, s, since, f);
|
||||
} catch (e) {
|
||||
// A session that expired early or was revoked at the bank: Enable Banking turns the token away.
|
||||
const status = (e as { status?: number }).status;
|
||||
if (status === 401 || status === 403 || /EXPIRED|REVOKED|CLOSED/.test(e instanceof Error ? e.message : "")) throw consentExpired();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
async function ebRead(env: Env, s: { accounts: EbAccount[]; institution?: string }, since: string, f?: Fetch) {
|
||||
const accounts: FeedAccount[] = [], txns: FeedTxn[] = [];
|
||||
for (const a of s.accounts) {
|
||||
const { balances } = await eb<{ balances: EbBalance[] }>(env, `/accounts/${a.uid}/balances`, {}, f);
|
||||
@@ -101,7 +118,7 @@ async function ebFeed(env: Env, token: string, since: string, f?: Fetch) {
|
||||
const q = new URLSearchParams({ date_from: since.slice(0, 10), ...(key ? { continuation_key: key } : {}) });
|
||||
const page: { transactions?: EbTxn[]; continuation_key?: string | null } = await eb(env, `/accounts/${a.uid}/transactions?${q}`, {}, f);
|
||||
for (const t of page.transactions ?? []) {
|
||||
const row = ebTxn(t, a.uid);
|
||||
const row = ebTxn(t, ebKey(a));
|
||||
if (row) txns.push(row);
|
||||
}
|
||||
key = page.continuation_key ?? null;
|
||||
|
||||
Reference in new issue
Block a user