import Link from "next/link"; import { Merchant } from "@/components/Merchant"; import { billOnCards, CARDS, catOf, fitsCredit, ownedCards, withReferrals, type Card } from "@/lib/cards"; import { referralLinks } from "@/lib/cards-data"; import { countryOf } from "@/lib/countries"; import { creditOf } from "@/lib/credit"; import { getUser } from "@/lib/data"; import { fmtFor, fmtWith } from "@/lib/format"; const PER_YEAR: Record = { WEEKLY: 52, BIWEEKLY: 26, SEMI_MONTHLY: 24, MONTHLY: 12, ANNUALLY: 1 }; // Payments that can't go on a card: loans, card payments, transfers. Rent usually costs a card fee, so it's left alone. const NOT_ON_A_CARD = new Set(["LOAN_PAYMENTS", "CREDIT_CARD_PAYMENT", "TRANSFER_IN", "TRANSFER_OUT"]); const RENT = /\b(rent|apartment|apartments|property|landlord|leasing)\b/i; // Bank feeds leave an empty "()" on some names ("Instacart ()"). export const billName = (name: string) => name.replace(/\s*\(\s*\)\s*$/, ""); type Row = { id: string; merchant_name: string; logo_url: string | null; frequency: string; average_amount: number; next_date: string | null; category: { key: string } | null; account: { name: string; custom_name: string | null; official_name: string | null; mask: string | null; type: string } | null; }; // Every subscription and bill that repeats, which account pays it, and what moving it to a card would earn. // Cards only where the card catalogue applies (US); elsewhere the list on its own. Shared with the plan summary. export async function loadBills() { const { supabase, user } = await getUser(); const profile = (user?.user_metadata?.profile ?? {}) as Record; const country = countryOf(profile.country as string | undefined); const money = fmtWith(fmtFor(profile)).money0; const [{ data: rec }, { data: credit }] = await Promise.all([ supabase.from("recurring") .select("id, merchant_name, logo_url, frequency, average_amount:home_average, next_date, category:categories(key), account:accounts(name, custom_name, official_name, mask, type)") .eq("user_id", user!.id).eq("active", true).eq("is_income", false).neq("status", "TOMBSTONED"), supabase.from("accounts").select("name, official_name").eq("user_id", user!.id).eq("type", "credit"), ]); const rows = ((rec ?? []) as unknown as Row[]).filter((r) => !NOT_ON_A_CARD.has(r.category?.key ?? "")); if (!rows.length) return { items: [], money, country }; const catalogue = country.cards ? withReferrals(CARDS, referralLinks()) : []; const owned = ownedCards(credit ?? [], catalogue); const band = creditOf(profile)?.band; // Cards to suggest: not one they hold, inside their credit range, no yearly fee (a fee isn't worth it for one bill). const others = catalogue.filter((c) => !owned.includes(c) && fitsCredit(c, band && band !== "unsure" ? band : null) !== false && !(c.fee ?? 0) && !(c.feeYear1 ?? 0)); const items = rows.map((r) => { const yearly = Number(r.average_amount) * (PER_YEAR[r.frequency] ?? 12); const payingCard: Card | null = r.account?.type === "credit" ? ownedCards([r.account], catalogue)[0] ?? null : null; const rent = RENT.test(r.merchant_name); // On a card that isn't in the catalogue: its rewards are unknown, so no suggestion would be honest. const unknownCard = r.account?.type === "credit" && !payingCard; const o = catalogue.length && !rent && !unknownCard ? billOnCards({ yearly, cat: catOf(r.category?.key), payingCard }, owned, others) : null; const gainMine = o?.mine ? o.mine.earn - o.now : 0; const gainOther = o?.other ? o.other.earn - o.now : 0; return { r, yearly, rent, unknownCard, o, gainMine, gainOther }; }).sort((a, b) => b.yearly - a.yearly); return { items, money, country }; } export const CARD_NOTE = "Earnings use each card's rate for that kind of bill, with points valued as on the Cards page. Some billers charge a card fee, so check first. Pay the card in full every month, or the interest wipes out the points."; type Item = Awaited>["items"][number]; // The one line of card advice for a bill, or nothing to say. Also on the Recurring page. export function BillAdvice({ item: { r, rent, unknownCard, o, gainMine, gainOther }, money }: { item: Item; money: (n: number) => string }) { if (/\bbilt\b/i.test(r.merchant_name)) return

Paid through Bilt. Your Bilt account shows what it earns.

; if (unknownCard) return

Already on a card. broke doesn't know that card's rewards.

; if (rent) return

Rent on a card usually costs a fee bigger than the points, so it stays as it is.

; if (o && gainMine >= 1) return

Pay it with your {o.mine!.card.name}: about {money(gainMine)} a year back{o.now ? `, ${money(gainMine)} more than now` : ""}.

; if (o && o.now > 0) return

Already earning about {money(o.now)} a year on your {o.mine?.card.name ?? "card"}.

; if (o?.other && gainOther >= 5) return

A {o.other.card.issuer} {o.other.card.name} would earn about {money(gainOther)} a year on it. Compare cards

; return null; } export async function BillsOnCards() { const { items, money, country } = await loadBills(); if (!items.length) return null; const total = items.reduce((s, x) => s + x.yearly, 0); const switchable = items.filter((x) => x.gainMine >= 1); const gain = switchable.reduce((s, x) => s + x.gainMine, 0); const from = (a: Row["account"]) => { if (!a) return "an account broke can't see"; const n = a.custom_name || a.name; return a.mask && !n.includes(a.mask) ? `${n} ยทยท${a.mask}` : n; }; return (
Your subscriptions and bills

{items.length} that repeat, about {money(total)} a year. {gain >= 1 && <> Paying {switchable.length === 1 ? "one" : switchable.length} with a card you already have would earn about {money(gain)} a year back.}

    {items.map((item) => { const { r, yearly } = item; return (
  • {billName(r.merchant_name)} {money(yearly)} a year

    Paid from {from(r.account)}{r.next_date ? `, next on ${new Date(r.next_date + "T12:00:00Z").toLocaleDateString("en-US", { month: "short", day: "numeric", timeZone: "UTC" })}` : ""}

  • ); })}
{country.cards && (

{CARD_NOTE}

)}
); }