Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
fix(jobs): make the weekly inactive-company cleanup actually delete (#1754)
Sida11b57bb9a37
diff --git a/.claude/rules/billing-system.md b/.claude/rules/billing-system.mdindex fb009fb898..aa9cf8892c 100644--- a/.claude/rules/billing-system.md+++ b/.claude/rules/billing-system.md@@ -5,6 +5,9 @@ paths:- packages/ee/src/plan*.ts- packages/database/supabase/migrations/*billing*.sql- apps/erp/app/routes/api+/webhook.stripe.ts+ - packages/jobs/src/inngest/functions/scheduled/weekly.ts+ - packages/jobs/src/inngest/functions/scheduled/inactive-companies.ts+ - packages/jobs/src/inngest/functions/scheduled/purge-company.ts---# Billing System@@ -161,6 +164,67 @@ Env: `STRIPE_BYPASS_COMPANY_IDS`, `STRIPE_BYPASS_USER_IDS` (comma-separated, ser- In `getStripeCustomerByCompanyId`: bypass returns a synthetic active subscription with`planId: Plan.Partner` (highest tier, ~1-year period) — no real Stripe call.+## Inactive-company cleanup (Cloud only)++The weekly job (`packages/jobs/.../scheduled/weekly.ts`, Sunday 21:00 UTC) deletes+companies that stopped paying. Until 2026-09 it never deleted anything: it fetched its+bypass list from a scheme-less `VERCEL_URL` and failed. Each run logs whom it warned and+whom it deleted.++- **Warned first, always.** A company that has never been warned gets a+ `CompanyDeletionWarningEmail` (`@carbon/documents/email`) sent to its group owner, naming+ the deletion date, and a marker row in `externalIntegrationMapping`+ (`integration = "inactive-company-warning"`, `metadata.warnedAt`).+ - The marker also stores `deleteAfter`, the UTC date the email names (today + 7). The+ company is deleted at the first run on or after that date, never before+ (`isDueForDeletion`, tested). A warning retried after UTC midnight therefore names,+ and waits for, the later date.+ - A company with no group owner is never warned, so it is never deleted. It is also+ left out of the capped lists, where it would otherwise hold a slot every week.+ - A failed send is recorded as `metadata.failedAt`, and that company queues behind+ the never-tried ones next week, so a bad address cannot starve the rest.+ - A warning expires after 30 days, and the company is warned again. A marker whose+ company is no longer inactive (it bought a plan) is cleared, so a later lapse starts+ a fresh warning. The purge deletes the marker along with the company.+ - Right before each purge, `isStillDueForDeletion` re-reads, inside the purge's own+ transaction, the company, the group's plan rows, the bypass list, the group owner and+ the warning. The marker stores the warned owner's id (`ownerId`); the purge requires+ the group's current owner to be that same person. A plan bought, or an owner cleared+ or changed, after the warning stops the delete.+ - Each warn or delete batch that still fails after its retries is logged and skipped.+ It never ends the run, so the training reminders after it still go out.++- **The group is the unit** (`selectInactiveCompanies`, `inactive-companies.ts`, tested).+ A company goes when it has no `companyPlan` row, no company in its group has one, it is+ over 7 days old, and it is not protected. `companyPlan` rows are written only by Stripe+ checkout/sync. A paying customer's second company (Settings → New Company) has no row of+ its own, and bypass and Carbon-owned access is served without one.+- **Protected:** `STRIPE_BYPASS_COMPANY_IDS`, plus any group whose owner is in+ `STRIPE_BYPASS_USER_IDS` or has an internal email (the `isCarbonOwnedCompany` rule).+- **`Canceled` is not deleted.** It is set when the customer cancels at period end, while+ the period is still paid. `customer.subscription.deleted` removes the plan row when the+ subscription actually ends, and the company becomes a candidate then.+- **Deletion** (`purgeCompany`, `purge-company.ts`):+ - Posted journals and invoices are trigger-immutable, so a plain `DELETE FROM company`+ fails for any company that ever posted. The purge instead wipes every catalog table in+ `session_replication_role = 'replica'`, as a restore does, then deletes the company.+ The group and its shared data go only when no other company is left in it.+ - Without replica permission it falls back to the plain cascade. A company with posted+ documents then fails, and is logged and skipped. So does the last company in a group,+ because its group's system accounts refuse deletion and the group would be stranded.+ - Inside the same transaction, after the wipe and before the commit,+ `removeCompanyLeftovers` removes the Vault `integration:<companyId>:*` secrets, the+ per-company bucket (already missing counts as done), and legacy files under+ `private/<companyId>/` (listed strictly, so a listing error is a failure). Any failure+ rolls the delete back, so the company row stays as the retry target and nothing is+ orphaned once a delete commits. A company whose cleanup failed part-way may have+ lost some files; it is still warned and due, so the next run finishes it. The search+ index is dropped after the commit, and a failure there is only logged. Provider+ tokens are not revoked at the provider.+- At most 100 companies per run (Inngest's per-run step limit), 10 per step. Oldest go first.+- A company with live intercompany history (`intercompanyTransaction` is NO ACTION) fails+ its delete, is logged, and is retried every week.+## Env vars (`packages/env/src/index.ts`, server-only / secret)`STRIPE_SECRET_KEY`, `STRIPE_WEBHOOK_SECRET` (both optional — Cloud only),diff --git a/.claude/rules/company-backup-restore.md b/.claude/rules/company-backup-restore.mdindex 49863a7e61..a97ba60fe4 100644--- a/.claude/rules/company-backup-restore.md+++ b/.claude/rules/company-backup-restore.md@@ -489,6 +489,14 @@ also used by `company-template.ts`: applying a demo template from Settingssnapshots with `buildCompanyBackup`, and reverting it wipes-and-loads thatsnapshot through exactly this path. See `onboarding-company-templates.md`.+**Third caller: company deletion.** `purgeCompany` (`packages/jobs/.../scheduled/purge-company.ts`,+used by the weekly inactive-company cleanup) runs `wipeScopedData` over the WHOLE catalog in+replica mode, secret and identity tables included, because nothing is reloaded after it. It+then deletes the `company` row with triggers back on, so tables outside the catalog cascade.+`canSetReplicationRole` decides whether it can wipe at all. See `billing-system.md`.+`listBucketFilesRecursive` lives in `src/backups/storage.ts`, shared by the export and that+cleanup.+### Known caveats in the committed code (not yet hardened)- **Storage restore is best-effort** — copy failures `console.warn` only; thediff --git a/packages/checks/AGENTS.md b/packages/checks/AGENTS.mdindex 2b6e23af8d..5c19ca4b1d 100644--- a/packages/checks/AGENTS.md+++ b/packages/checks/AGENTS.md@@ -35,7 +35,7 @@ pnpm --filter @carbon/checks baseline # regenerate baseline (careful!)- **Authz ownership** (`CONFORMANCE_CHECKS` via `collectFindings`): `noAuthzDdlInMigrations(helpers)` fails a migration newer than `20260927000000` that runs `CREATE`/`ALTER POLICY` on a public table (storage bucket policies stay allowed), or defines, alters or drops a managed RLS helper — policies live in `packages/database/src/authz/manifest.ts`, helpers in `packages/database/src/authz/helpers/<name>.sql`, and `authz sync` applies both after migrations. The helper list is read from that directory (`loadAuthzHelperNames`), so it cannot drift from the files. `DROP POLICY` is allowed (dropping a column a policy uses). Files starting with the `authz migration` header are exempt; `@carbon/database`'s `migration.test.ts` verifies their contents. Older migrations are history, so there are no baseline entries- **Forwarded headers** (own source, `REQUEST_HANDLING_ROOTS`: every app + auth, ee, jobs, lib, utils, edge functions): `noRawForwardedHeaders` fails a raw read of `X-Forwarded-For/-Host/-Proto` or `X-Real-IP` outside `packages/utils/src/headers.ts` — use `getClientIp` / `getRequestProtocol` / `getRequestHost` / `getRequestOrigin`. Hand-rolled reads keyed login rate limits and ITAR audit records on the forgeable first `X-Forwarded-For` entry. No baseline entries- **Numeric-precision checks** (in `TS_CHECKS`, over `sources/typescript.ts`): `noRawRounding` flags `Math.round/ceil/floor` and `.toFixed` on value-bearing numbers (use `round`/`scrapAllowance`/`applyRate`/`deriveRate` from `@carbon/utils`); `noInlineFractionDigits` flags `minimum`/`maximumFractionDigits` at a call site (pick a named kind from `@carbon/utils` `format.ts`, and `INPUT_FORMAT.*` for anything editable); `noUnroundedTrackedQuantity` flags inline unrounded arithmetic inside a `.updateTable("trackedEntity")` statement's `quantity:` set, and a raw `===`/`!==`/`<` compare against a `*[Ee]ntity*.quantity` read (use `round`/`settleQuantity` at the write, `isFullDraw` at a split gate — see `.claude/rules/traceability-model.md`). It is scoped to what one file can prove: a `quantity:` set from an already-rounded variable is unflaggable either way, and a compare of two locals is presumed rounded where they were defined. It has **no baseline entries** — the whole tree was fixed in the pass that added it, so any finding is new. `sources/typescript.ts` walks `apps/erp/app/{components,hooks,modules,routes}`, `apps/mes/app`, `packages/database/supabase/functions`, `packages/{ee,jobs}/src`, `packages/documents/src/{pdf,utils}` and the shared packages `packages/{utils,form,react,printing,workflows}/src` — tests and the image/logo resizers excluded. The shared packages matter: `@carbon/utils` is where the standard's own helpers live, and `form`/`react` own the number inputs whose `formatOptions` are part of the storage round-trip. See `.claude/rules/numeric-precision.md`-- **Tenant-scoped Kysely writes** (in `TS_CHECKS`): `noUnscopedKyselyWrite` fails any `.updateTable(`/`.deleteFrom(` under `apps/erp/app/{modules,routes}`, `apps/mes/app` or `packages/jobs/src` whose statement (up to `.execute…(`/`.compile(`/`;`/`,` at depth 0, comments blanked) has no `.where("companyId", …)` / `.where("alias.companyId", …)`. Kysely is the superuser connection, and the public API hands a service's `db` param that same client, so the statement's own predicate is the only tenant boundary — ten drag-sort services (`updateQuoteLineOrder` and friends) were a reported cross-tenant write because they keyed on `id` alone. `company` is exempt (its `id` is the tenant); a `companyId` in `.set()` does not count; a link table with no `companyId` column passes by scoping through its parent in a subquery. Edge functions and `packages/ee` are out of scope (see the file header). No baseline entries — every hit was fixed when it was added+- **Tenant-scoped Kysely writes** (in `TS_CHECKS`): `noUnscopedKyselyWrite` fails any `.updateTable(`/`.deleteFrom(` under `apps/erp/app/{modules,routes}`, `apps/mes/app` or `packages/jobs/src` whose statement (up to `.execute…(`/`.compile(`/`;`/`,` at depth 0, comments blanked) has no `.where("companyId", …)` / `.where("alias.companyId", …)`. Kysely is the superuser connection, and the public API hands a service's `db` param that same client, so the statement's own predicate is the only tenant boundary — ten drag-sort services (`updateQuoteLineOrder` and friends) were a reported cross-tenant write because they keyed on `id` alone. `company` is exempt (its `id` is the tenant); a `companyId` in `.set()` does not count; a link table with no `companyId` column passes by scoping through its parent in a subquery. Edge functions and `packages/ee` are out of scope (see the file header). One baseline entry: the `companyGroup` delete in `packages/jobs/.../scheduled/purge-company.ts` (a group has no `companyId` column; its `id` is the tenant-group key). Every other hit was fixed when it was added- **Edge-function auth** (`EDGE_FUNCTION_CHECKS`): `edgeFunctionAuthorizesCaller` fails any function under `packages/database/supabase/functions/` that never calls `requirePermissions` / `requireCaller` / `requireServiceRole` / `getSupabaseServiceRole`, because `verify_jwt` accepts the published anon key. `ALLOWED` in the check holds the functions that are correct without one, each with its reason (`logo-resizer`, `transcription`). `embed`, `event-wake` and `trigger` are baselined, not allowed: Postgres calls them over pg_net with the anon key from `config`, so gating them needs a migration that makes Postgres send a server credential first- **Structure checks**: `moduleShape` — validates ERP modules have `types.ts`, `ui/`, `index.ts`, `<name>.service.ts`, `<name>.models.ts`- **Clobber detection**: `findClobbers(branch, main)` — identifies DB objects redefined on both sidesdiff --git a/packages/checks/src/conformance/baseline.json b/packages/checks/src/conformance/baseline.jsonindex 472d8b651b..4330752496 100644--- a/packages/checks/src/conformance/baseline.json+++ b/packages/checks/src/conformance/baseline.json@@ -568,6 +568,7 @@"no-required-column-without-default::20241210214820_purchasing-redux.sql::ADD COLUMN \"supplierInteractionId\" TEXT NOT NULL","no-required-column-without-default::20241210214820_purchasing-redux.sql::ADD COLUMN IF NOT EXISTS \"supplierInteractionId\" TEXT NOT NULL","no-required-column-without-default::20260402000000_journal-entries.sql::ADD COLUMN \"journalEntryId\" TEXT NOT NULL",+ "no-unscoped-kysely-write::packages/jobs/src/inngest/functions/scheduled/purge-company.ts::.deleteFrom(\"companyGroup\")","no-view-without-invoker::20230123004409_permissions.sql::CREATE VIEW \"modules\" AS","no-view-without-invoker::20230123004436_employee.sql::CREATE OR REPLACE VIEW \"companies\" AS","no-view-without-invoker::20230123004632_groups.sql::CREATE OR REPLACE VIEW \"groupMembers\" AS",diff --git a/packages/documents/src/email/CompanyDeletionWarningEmail.tsx b/packages/documents/src/email/CompanyDeletionWarningEmail.tsxnew file mode 100644index 0000000000..ca51002edc--- /dev/null+++ b/packages/documents/src/email/CompanyDeletionWarningEmail.tsx@@ -0,0 +1,141 @@+import {+ Body,+ Button,+ Container,+ Heading,+ Link,+ Preview,+ Section,+ Text+} from "@react-email/components";+import { Logo } from "./components/Logo";+import { notificationStyles } from "./components/notificationStyles";+import { EmailThemeProvider, getEmailThemeClasses } from "./components/Theme";++interface Props {+ recipientName?: string;+ companyName: string;+ /** Already formatted for display, e.g. "October 11, 2026". Nothing is deleted before it. */+ deletionDate: string;+ billingUrl: string;+}++// Sent once to a company group's owner when the weekly cleanup finds the company+// has no active subscription. The company is deleted no sooner than a week later,+// and only if it is still without one.+export const CompanyDeletionWarningEmail = ({+ recipientName = "there",+ companyName,+ deletionDate,+ billingUrl+}: Props) => {+ const themeClasses = getEmailThemeClasses();++ return (+ <EmailThemeProvider+ preview={+ <Preview>{`${companyName} will be deleted on or after ${deletionDate}`}</Preview>+ }+ additionalHeadContent={<style>{notificationStyles}</style>}+ >+ <Body+ className={`my-auto mx-auto font-sans nf-body ${themeClasses.body}`}+ >+ <Container+ className={`my-[40px] mx-auto p-[36px] max-w-[560px] rounded-[16px] nf-card ${themeClasses.container}`}+ style={{+ borderRadius: 16,+ borderStyle: "solid",+ borderWidth: 1+ }}+ >+ <Logo />++ <Text+ className={`text-[11px] leading-[16px] uppercase text-center font-medium m-0 mt-[40px] mb-[10px] nf-eyebrow ${themeClasses.mutedText}`}+ style={{ letterSpacing: "0.14em" }}+ >+ Account+ </Text>++ <Heading+ className={`text-[26px] font-medium text-center tracking-tight p-0 mt-0 mb-[32px] mx-0 ${themeClasses.heading}`}+ >+ {companyName} will be deleted+ </Heading>++ <Section>+ <Text+ className={`text-[15px] leading-[26px] m-0 mb-[16px] ${themeClasses.text}`}+ >+ Hi {recipientName},+ </Text>+ </Section>++ <Section+ className="nf-callout"+ style={{+ backgroundColor: "#fafafa",+ borderColor: "#ececef",+ borderRadius: 12,+ borderStyle: "solid",+ borderWidth: 1,+ marginBottom: 28,+ padding: "18px 20px"+ }}+ >+ <Text+ className={`text-[15px] leading-[24px] m-0 ${themeClasses.text}`}+ >+ {companyName} doesn't have an active Carbon subscription. On or+ after {deletionDate} we'll permanently delete it, along with its+ data and files.+ </Text>+ <Text+ className={`text-[15px] leading-[24px] m-0 mt-[12px] ${themeClasses.text}`}+ >+ To keep it, choose a plan before then. Nothing else is needed.+ </Text>+ </Section>++ <Section className="text-center mb-[24px]">+ <Button+ href={billingUrl}+ className="nf-cta"+ style={{+ backgroundColor: "#0e0e0e",+ borderColor: "#0e0e0e",+ borderRadius: 10,+ borderStyle: "solid",+ borderWidth: 1,+ color: "#ffffff",+ display: "inline-block",+ fontSize: 14,+ fontWeight: 500,+ padding: "13px 24px",+ textAlign: "center",+ textDecoration: "none"+ }}+ >+ <span style={{ verticalAlign: "middle" }}>Choose a plan</span>+ </Button>+ </Section>++ <Text+ className={`text-[13px] leading-[20px] m-0 text-center break-all nf-fallback ${themeClasses.mutedText}`}+ >+ Or open this link in your browser:{" "}+ <Link+ href={billingUrl}+ className={`${themeClasses.mutedText} underline nf-fallback`}+ >+ {billingUrl}+ </Link>+ </Text>+ </Container>+ </Body>+ </EmailThemeProvider>+ );+};++export default CompanyDeletionWarningEmail;diff --git a/packages/documents/src/email/index.ts b/packages/documents/src/email/index.tsindex 8424dca855..3bdd26e2b1 100644--- a/packages/documents/src/email/index.ts+++ b/packages/documents/src/email/index.ts@@ -1,3 +1,4 @@+import CompanyDeletionWarningEmail from "./CompanyDeletionWarningEmail";import GetStartedEmail from "./GetStartedEmail";import ImplementationHubEmail from "./ImplementationHubEmail";import InviteEmail from "./InviteEmail";@@ -19,6 +20,7 @@ export {} from "./WeeklyReminderEmail";export {+ CompanyDeletionWarningEmail,GetStartedEmail,ImplementationHubEmail,InviteEmail,diff --git a/packages/documents/src/email/previews/CompanyDeletionWarningEmail.tsx b/packages/documents/src/email/previews/CompanyDeletionWarningEmail.tsxnew file mode 100644index 0000000000..f5233e0d36--- /dev/null+++ b/packages/documents/src/email/previews/CompanyDeletionWarningEmail.tsx@@ -0,0 +1,12 @@+import CompanyDeletionWarningEmail from "../CompanyDeletionWarningEmail";++export default function CompanyDeletionWarningEmailPreview() {+ return (+ <CompanyDeletionWarningEmail+ recipientName={"John"}+ companyName={"Acme Manufacturing"}+ deletionDate={"October 11, 2026"}+ billingUrl={"https://app.carbon.ms/x/settings/billing"}+ />+ );+}diff --git a/packages/jobs/src/backups/storage.ts b/packages/jobs/src/backups/storage.tsnew file mode 100644index 0000000000..f2a9a4bb59--- /dev/null+++ b/packages/jobs/src/backups/storage.ts@@ -0,0 +1,41 @@+import type { SupabaseClient } from "@supabase/supabase-js";++/**+ * Every file under `prefix` in `bucket`, recursing into folders. `list(prefix)`+ * matches the exact folder, never a sibling that shares the prefix string. At+ * most 1000 entries per folder, so a caller that deletes as it goes can drain a+ * larger folder in repeated passes.+ *+ * A listing error reads as "no files" (a backup of a company with no bucket yet),+ * unless `strict`: a caller that must know the folder is really empty, such as a+ * delete, gets the error thrown instead.+ */+export async function listBucketFilesRecursive(+ client: SupabaseClient,+ bucket: string,+ prefix = "",+ { strict = false }: { strict?: boolean } = {}+): Promise<Array<{ path: string; size: number }>> {+ const files: Array<{ path: string; size: number }> = [];+ const { data, error } = await client.storage.from(bucket).list(prefix, {+ limit: 1000+ });+ if (error && strict) throw error;+ if (error || !data) return files;++ for (const entry of data) {+ const path = prefix ? `${prefix}/${entry.name}` : entry.name;+ if (entry.id === null) {+ // folder+ files.push(+ ...(await listBucketFilesRecursive(client, bucket, path, { strict }))+ );+ } else {+ files.push({+ path,+ size: (entry.metadata as { size?: number } | null)?.size ?? 0+ });+ }+ }+ return files;+}diff --git a/packages/jobs/src/inngest/functions/scheduled/inactive-companies.test.ts b/packages/jobs/src/inngest/functions/scheduled/inactive-companies.test.tsnew file mode 100644index 0000000000..66f8b9b1ed--- /dev/null+++ b/packages/jobs/src/inngest/functions/scheduled/inactive-companies.test.ts@@ -0,0 +1,168 @@+import { describe, expect, it } from "vitest";+import {+ type CompanyCandidate,+ isDueForDeletion,+ selectInactiveCompanies,+ splitByWarning,+ type Warning+} from "./inactive-companies";++const now = Date.parse("2026-10-04T21:00:00Z");+const old = "2026-09-01T00:00:00Z";++const company = (+ id: string,+ overrides: Partial<CompanyCandidate> = {}+): CompanyCandidate => ({+ id,+ name: id,+ createdAt: old,+ companyGroupId: `group-${id}`,+ ...overrides+});++const select = (+ companies: CompanyCandidate[],+ opts: Partial<Parameters<typeof selectInactiveCompanies>[0]> = {}+) =>+ selectInactiveCompanies({+ companies,+ planCompanyIds: new Set(),+ protectedCompanyIds: new Set(),+ protectedGroupIds: new Set(),+ now,+ limit: 100,+ ...opts+ }).map((c) => c.id);++describe("selectInactiveCompanies", () => {+ it("selects a planless company older than a week", () => {+ expect(select([company("a")])).toEqual(["a"]);+ });++ it("keeps a company created within the last week", () => {+ expect(+ select([company("a", { createdAt: "2026-09-30T00:00:00Z" })])+ ).toEqual([]);+ });++ it("keeps a company with a plan row, whatever its status", () => {+ expect(select([company("a")], { planCompanyIds: new Set(["a"]) })).toEqual(+ []+ );+ });++ it("keeps a planless company whose group has a paying company", () => {+ const companies = [+ company("paying", { companyGroupId: "g" }),+ company("second", { companyGroupId: "g" })+ ];+ expect(select(companies, { planCompanyIds: new Set(["paying"]) })).toEqual(+ []+ );+ });++ it("keeps bypassed companies and protected groups", () => {+ const companies = [+ company("bypassed"),+ company("carbon-owned", { companyGroupId: "internal" }),+ company("trial")+ ];+ expect(+ select(companies, {+ protectedCompanyIds: new Set(["bypassed"]),+ protectedGroupIds: new Set(["internal"])+ })+ ).toEqual(["trial"]);+ });++ it("returns the oldest first, capped at the limit", () => {+ const companies = [+ company("newer", { createdAt: "2026-09-10T00:00:00Z" }),+ company("oldest", { createdAt: "2026-08-01T00:00:00Z" }),+ company("middle", { createdAt: "2026-09-05T00:00:00Z" })+ ];+ expect(select(companies, { limit: 2 })).toEqual(["oldest", "middle"]);+ });+});++// The weekly run, Sunday 2026-10-04 21:00 UTC.+const clock = { now, today: "2026-10-04" };++describe("splitByWarning", () => {+ const split = (warnings: Record<string, Warning>, limit = 100) => {+ const { toWarn, toDelete } = splitByWarning({+ inactive: [company("a"), company("b"), company("c")],+ warnings: new Map(Object.entries(warnings)),+ clock,+ limit+ });+ return {+ toWarn: toWarn.map((c) => c.id),+ toDelete: toDelete.map((c) => c.id)+ };+ };++ it("warns a company before it can ever be deleted", () => {+ expect(split({})).toEqual({ toWarn: ["a", "b", "c"], toDelete: [] });+ });++ it("deletes on the date the email named", () => {+ // Warned a few minutes into last Sunday's run: the email said 2026-10-04.+ const warning = {+ warnedAt: "2026-09-27T21:04:00Z",+ deleteAfter: "2026-10-04"+ };+ expect(split({ a: warning }).toDelete).toEqual(["a"]);+ });++ it("never deletes before the date the email named", () => {+ // A retry after UTC midnight named Monday, so Sunday's run waits.+ const warning = {+ warnedAt: "2026-09-28T00:10:00Z",+ deleteAfter: "2026-10-05"+ };+ expect(split({ a: warning })).toEqual({+ toWarn: ["b", "c"],+ toDelete: []+ });+ });++ it("warns again instead of deleting when the warning has expired", () => {+ const warning = {+ warnedAt: "2026-08-01T21:00:00Z",+ deleteAfter: "2026-08-08"+ };+ expect(split({ a: warning })).toEqual({+ toWarn: ["a", "b", "c"],+ toDelete: []+ });+ });++ it("puts a company whose last send failed behind the others", () => {+ const failed = { failedAt: "2026-09-27T21:04:00Z" };+ expect(split({ a: failed }, 2).toWarn).toEqual(["b", "c"]);+ });++ it("caps both lists", () => {+ expect(split({}, 2).toWarn).toEqual(["a", "b"]);+ });+});++describe("isDueForDeletion", () => {+ it("needs a live warning whose named date has arrived", () => {+ expect(isDueForDeletion(undefined, clock)).toBe(false);+ expect(isDueForDeletion({ failedAt: "2026-09-01T00:00:00Z" }, clock)).toBe(+ false+ );+ expect(isDueForDeletion({ warnedAt: "2026-09-27T21:04:00Z" }, clock)).toBe(+ false+ );+ expect(+ isDueForDeletion(+ { warnedAt: "2026-09-27T21:04:00Z", deleteAfter: "2026-10-04" },+ clock+ )+ ).toBe(true);+ });+});diff --git a/packages/jobs/src/inngest/functions/scheduled/inactive-companies.ts b/packages/jobs/src/inngest/functions/scheduled/inactive-companies.tsnew file mode 100644index 0000000000..ca2f1b6fac--- /dev/null+++ b/packages/jobs/src/inngest/functions/scheduled/inactive-companies.ts@@ -0,0 +1,121 @@+export type CompanyCandidate = {+ id: string;+ name: string;+ createdAt: string;+ companyGroupId: string | null;+};++const WEEK_MS = 7 * 24 * 60 * 60 * 1000;++/**+ * Companies the weekly cleanup may delete, oldest first: no plan row anywhere in+ * the company's group, older than a week, and not protected.+ *+ * The group is the unit. A paying customer's second company (Settings → New+ * Company) never gets its own `companyPlan` row, and Carbon-owned or bypass-user+ * groups get plan access with no row at all (`getStripeCustomerByCompanyId`), so+ * "this company has no row" alone would delete all three.+ */+export function selectInactiveCompanies({+ companies,+ planCompanyIds,+ protectedCompanyIds,+ protectedGroupIds,+ now,+ limit+}: {+ companies: CompanyCandidate[];+ planCompanyIds: Set<string>;+ protectedCompanyIds: Set<string>;+ protectedGroupIds: Set<string>;+ now: number;+ limit: number;+}): CompanyCandidate[] {+ const payingGroups = new Set(+ companies+ .filter((c) => c.companyGroupId && planCompanyIds.has(c.id))+ .map((c) => c.companyGroupId)+ );+ const cutoff = now - WEEK_MS;++ return companies+ .filter(+ (c) =>+ !planCompanyIds.has(c.id) &&+ !protectedCompanyIds.has(c.id) &&+ !(c.companyGroupId && payingGroups.has(c.companyGroupId)) &&+ !(c.companyGroupId && protectedGroupIds.has(c.companyGroupId)) &&+ Date.parse(c.createdAt) < cutoff+ )+ .sort((a, b) => Date.parse(a.createdAt) - Date.parse(b.createdAt))+ .slice(0, limit);+}++/**+ * A warning older than this no longer counts, and the company is warned again. A+ * warning that could not be cleared when its company regained a plan therefore+ * cannot qualify it for deletion months later without a fresh email.+ */+const WARNING_TTL_MS = 30 * 24 * 60 * 60 * 1000;++/**+ * What the `inactive-company-warning` marker records. `deleteAfter` is the UTC+ * date (`YYYY-MM-DD`) the email named; nothing is deleted before it.+ */+export type Warning = {+ warnedAt?: string;+ deleteAfter?: string;+ failedAt?: string;+ /** The group owner the email went to; a purge requires the same owner. */+ ownerId?: string;+};++/** Now as an instant (ms) and as today's UTC date (`YYYY-MM-DD`). */+export type Clock = { now: number; today: string };++const liveWarning = (warning: Warning | undefined, { now }: Clock) =>+ warning?.warnedAt !== undefined &&+ Date.parse(warning.warnedAt) > now - WARNING_TTL_MS;++/**+ * The date the owner was told has arrived, and the warning has not expired.+ * `YYYY-MM-DD` strings compare chronologically.+ */+export const isDueForDeletion = (warning: Warning | undefined, clock: Clock) =>+ liveWarning(warning, clock) &&+ warning?.deleteAfter !== undefined &&+ warning.deleteAfter <= clock.today;++/**+ * Split inactive companies (oldest first) by their warning: no live warning →+ * warn now; its `deleteAfter` date reached → delete; otherwise → wait. A company+ * is never deleted without a warning, and each list is capped at `limit`.+ * A company whose last send failed goes after the rest, so a bad address cannot+ * hold the front of the capped list every week.+ */+export function splitByWarning({+ inactive,+ warnings,+ clock,+ limit+}: {+ inactive: CompanyCandidate[];+ warnings: Map<string, Warning>;+ clock: Clock;+ limit: number;+}): { toWarn: CompanyCandidate[]; toDelete: CompanyCandidate[] } {+ const toWarn: CompanyCandidate[] = [];+ const retryWarn: CompanyCandidate[] = [];+ const toDelete: CompanyCandidate[] = [];+ for (const company of inactive) {+ const warning = warnings.get(company.id);+ if (isDueForDeletion(warning, clock)) toDelete.push(company);+ else if (liveWarning(warning, clock)) continue;+ else if (warning?.failedAt) retryWarn.push(company);+ else toWarn.push(company);+ }+ return {+ toWarn: [...toWarn, ...retryWarn].slice(0, limit),+ toDelete: toDelete.slice(0, limit)+ };+}diff --git a/packages/jobs/src/inngest/functions/scheduled/purge-company.integration.test.ts b/packages/jobs/src/inngest/functions/scheduled/purge-company.integration.test.tsnew file mode 100644index 0000000000..8a173fba88--- /dev/null+++ b/packages/jobs/src/inngest/functions/scheduled/purge-company.integration.test.ts@@ -0,0 +1,96 @@+import type { KyselyDatabase } from "@carbon/database/client";+import { type Kysely, sql } from "kysely";+import { describe, expect, it } from "vitest";+import { getJobDatabaseClient } from "../../../db";+import {+ canSetReplicationRole,+ getCompanyTableCatalog+} from "../tasks/company-backup";+import { purgeCompany } from "./purge-company";++const runDatabaseTests = process.env.RUN_PURGE_DB_TESTS === "true";++class Rollback extends Error {}++/** Rows whose foreign key points at a row that no longer exists, per constraint. */+async function danglingForeignKeys(+ trx: Kysely<KyselyDatabase>+): Promise<Map<string, number>> {+ const fks = await sql<{+ child: string;+ parent: string;+ cc: string[];+ pc: string[];+ }>`+ SELECT c.conrelid::regclass::text AS child, c.confrelid::regclass::text AS parent,+ array(SELECT attname FROM unnest(c.conkey) WITH ORDINALITY k(n, o)+ JOIN pg_attribute a ON a.attrelid = c.conrelid AND a.attnum = k.n ORDER BY o)::text[] AS cc,+ array(SELECT attname FROM unnest(c.confkey) WITH ORDINALITY k(n, o)+ JOIN pg_attribute a ON a.attrelid = c.confrelid AND a.attnum = k.n ORDER BY o)::text[] AS pc+ FROM pg_constraint c JOIN pg_namespace n ON n.oid = c.connamespace+ WHERE c.contype = 'f' AND n.nspname = 'public'`.execute(trx);++ const counts = new Map<string, number>();+ for (const fk of fks.rows) {+ const notNull = sql.join(+ fk.cc.map((c) => sql`x.${sql.id(c)} IS NOT NULL`),+ sql` AND `+ );+ const match = sql.join(+ fk.cc.map((c, i) => sql`p.${sql.id(fk.pc[i]!)} = x.${sql.id(c)}`),+ sql` AND `+ );+ const { rows } = await sql<{ n: number }>`+ SELECT count(*)::int AS n FROM ${sql.raw(fk.child)} x+ WHERE ${notNull} AND NOT EXISTS (SELECT 1 FROM ${sql.raw(fk.parent)} p WHERE ${match})`.execute(+ trx+ );+ if (rows[0]!.n > 0)+ counts.set(`${fk.child}(${fk.cc}) -> ${fk.parent}`, rows[0]!.n);+ }+ return counts;+}++describe.skipIf(!runDatabaseTests)("purgeCompany (Postgres)", () => {+ it("deletes a company, posted journals included, leaving no dangling rows", async () => {+ const db = getJobDatabaseClient(1);+ const replica = await canSetReplicationRole(db);+ expect(replica).toBe(true);+ const catalog = await getCompanyTableCatalog(db);++ // Prefer a company that has posted: a plain cascade cannot delete it.+ const { companyId } = await db+ .selectFrom("company")+ .leftJoin("journal", (join) =>+ join+ .onRef("journal.companyId", "=", "company.id")+ .on("journal.status", "=", "Posted")+ )+ .select("company.id as companyId")+ .orderBy(sql`count("journal"."id")`, "desc")+ .groupBy("company.id")+ .limit(1)+ .executeTakeFirstOrThrow();++ await expect(+ db.transaction().execute(async (trx) => {+ const before = await danglingForeignKeys(trx);+ await purgeCompany(trx, catalog, companyId, { replica });++ const left = await trx+ .selectFrom("company")+ .select("id")+ .where("id", "=", companyId)+ .execute();+ expect(left).toEqual([]);++ const after = await danglingForeignKeys(trx);+ const added = [...after].filter(+ ([key, n]) => n > (before.get(key) ?? 0)+ );+ expect(added).toEqual([]);+ throw new Rollback();+ })+ ).rejects.toBeInstanceOf(Rollback);+ }, 300_000);+});diff --git a/packages/jobs/src/inngest/functions/scheduled/purge-company.ts b/packages/jobs/src/inngest/functions/scheduled/purge-company.tsnew file mode 100644index 0000000000..980fc48f60--- /dev/null+++ b/packages/jobs/src/inngest/functions/scheduled/purge-company.ts@@ -0,0 +1,174 @@+import type { getCarbonServiceRole } from "@carbon/auth/client.server";+import type { KyselyDatabase } from "@carbon/database/client";+import { getCompanyPrivateBucket } from "@carbon/files";+import { chunkArray } from "@carbon/utils";+import { type Kysely, sql } from "kysely";+import { listBucketFilesRecursive } from "../../../backups/storage";+import {+ type Catalog,+ STORAGE_BUCKET,+ wipeScopedData+} from "../tasks/company-backup";++/**+ * Delete one company and everything it owns, inside the caller's transaction.+ *+ * A plain `DELETE FROM company` cascades, but posted journals and invoices are+ * trigger-immutable, and settlements hold RESTRICT FKs, so it fails for any company+ * that ever posted (every demo-template company). With `replica` (triggers and FK+ * enforcement off, as the backup restore uses) every tenant table is wiped first,+ * children before parents; the final delete then cascades whatever the catalog+ * does not cover. Group-shared data (chart of accounts, currencies) goes only when+ * no other company is left in the group.+ *+ * Unlike a restore, the wipe includes the secret and identity tables+ * (`companyIntegration`, `apiKey`, `userToCompany`, …): nothing is reloaded.+ */+export async function purgeCompany(+ trx: Kysely<KyselyDatabase>,+ catalog: Catalog,+ companyId: string,+ { replica }: { replica: boolean }+): Promise<void> {+ const company = await trx+ .selectFrom("company")+ .select("companyGroupId")+ .where("id", "=", companyId)+ .executeTakeFirst();+ if (!company) return;++ const groupId = company.companyGroupId;+ // Two purges in one group serialize on the group row, so they cannot both see+ // the other company and both leave the group's shared data behind.+ if (groupId !== null) {+ await trx+ .selectFrom("companyGroup")+ .select("id")+ .where("id", "=", groupId)+ .forUpdate()+ .execute();+ }+ const lastInGroup =+ groupId !== null &&+ (await trx+ .selectFrom("company")+ .select("id")+ .where("companyGroupId", "=", groupId)+ .where("id", "<>", companyId)+ .limit(1)+ .executeTakeFirst()) === undefined;++ // Without replica the group's system accounts refuse deletion+ // (protect_system_accounts), so deleting only the company would strand the+ // group and its shared data. Refuse instead; the company stays for a later run.+ if (!replica && lastInGroup) {+ throw new Error(+ "The last company in a group can only be purged in replica mode"+ );+ }++ if (replica) {+ await sql`SET LOCAL session_replication_role = 'replica'`.execute(trx);+ await wipeScopedData(+ trx,+ catalog.tables,+ new Map(catalog.tables.map((t) => [t.name, t])),+ { companyId, companyGroupId: lastInGroup ? groupId : null }+ );+ await sql`SET LOCAL session_replication_role = 'origin'`.execute(trx);+ }++ await trx.deleteFrom("company").where("id", "=", companyId).execute();+ if (lastInGroup && groupId) {+ await trx.deleteFrom("companyGroup").where("id", "=", groupId).execute();+ }+}++/**+ * What a company owns outside its tables: integration secrets in Vault, its own+ * storage bucket, and pre-bucket-migration files under `<companyId>/` in the+ * shared private bucket. Returns the failures; each part is attempted+ * regardless.+ *+ * Run inside the purge transaction, after `purgeCompany`, and roll the purge back+ * when anything failed: the company row is then the retry target, so nothing is+ * left behind once the delete commits. The price is that a company whose cleanup+ * failed part-way may already have lost some files. It was warned and is still+ * due, so the next run finishes it.+ */+export async function removeCompanyLeftovers(+ db: Kysely<KyselyDatabase>,+ serviceRole: ReturnType<typeof getCarbonServiceRole>,+ companyId: string+): Promise<{ part: string; error: unknown }[]> {+ const failures: { part: string; error: unknown }[] = [];++ try {+ // starts_with, not LIKE: ids may contain `_`, a LIKE wildcard.+ await sql`DELETE FROM vault.secrets WHERE starts_with(name, ${`integration:${companyId}:`})`.execute(+ db+ );+ } catch (error) {+ failures.push({ part: "vault secrets", error });+ }++ // A bucket already gone (removed by an earlier attempt that then rolled back)+ // is done, not a failure, or that company would roll back every week.+ const bucket = getCompanyPrivateBucket(companyId);+ const emptied = await serviceRole.storage.emptyBucket(bucket);+ const removed = emptied.error+ ? emptied+ : await serviceRole.storage.deleteBucket(bucket);+ if (removed.error && !/not found/i.test(removed.error.message))+ failures.push({ part: "company bucket", error: removed.error });++ const legacyError = await drainLegacyFolder(serviceRole, companyId);+ if (legacyError) {+ failures.push({ part: "legacy private files", error: legacyError });+ }++ return failures;+}++/** Deletion passes per run; each listing returns at most 1000 entries per folder. */+const MAX_LEGACY_PASSES = 20;++/**+ * Empty `private/<companyId>/`. Returns the error that stopped it, including a+ * folder still not empty after `MAX_LEGACY_PASSES`: the purge then rolls back,+ * and the next run carries on from what this one already removed.+ */+async function drainLegacyFolder(+ serviceRole: ReturnType<typeof getCarbonServiceRole>,+ companyId: string+): Promise<unknown> {+ for (let pass = 0; ; pass++) {+ let files: { path: string }[];+ try {+ files = await listBucketFilesRecursive(+ serviceRole,+ STORAGE_BUCKET,+ companyId,+ { strict: true }+ );+ } catch (error) {+ return error;+ }+ if (files.length === 0) return null;+ if (pass === MAX_LEGACY_PASSES) {+ return new Error(+ `${files.length}+ legacy files remain after ${MAX_LEGACY_PASSES} passes`+ );+ }++ for (const paths of chunkArray(+ files.map((f) => f.path),+ 1000+ )) {+ const { error } = await serviceRole.storage+ .from(STORAGE_BUCKET)+ .remove(paths);+ if (error) return error;+ }+ }+}diff --git a/packages/jobs/src/inngest/functions/scheduled/weekly.ts b/packages/jobs/src/inngest/functions/scheduled/weekly.tsindex 22ccdc42df..50cacc1574 100644--- a/packages/jobs/src/inngest/functions/scheduled/weekly.ts+++ b/packages/jobs/src/inngest/functions/scheduled/weekly.ts@@ -1,141 +1,507 @@import { getCarbonServiceRole } from "@carbon/auth/client.server";import { fetchAllFromTable } from "@carbon/database";-import { isReminderItemStatus } from "@carbon/documents/email";-import { STRIPE_BYPASS_COMPANY_IDS } from "@carbon/env";+import type { KyselyDatabase } from "@carbon/database/client";+import {+ CompanyDeletionWarningEmail,+ isReminderItemStatus+} from "@carbon/documents/email";+import {+ getAppUrl,+ STRIPE_BYPASS_COMPANY_IDS,+ STRIPE_BYPASS_USER_IDS+} from "@carbon/env";+import { sendEmail } from "@carbon/lib/email.server";import {MAX_NOTIFICATION_DELIVERIES,NotificationEvent} from "@carbon/notifications";-import { Edition } from "@carbon/utils";+import {+ chunkArray,+ datetime,+ Edition,+ formatDate,+ isInternalEmail+} from "@carbon/utils";+import { render } from "@react-email/components";+import type { Kysely } from "kysely";+import { getJobDatabaseClient } from "../../../db";import { inngest } from "../../client";+import {+ canSetReplicationRole,+ getCompanyTableCatalog+} from "../tasks/company-backup";+import {+ type CompanyCandidate,+ isDueForDeletion,+ selectInactiveCompanies,+ splitByWarning,+ type Warning+} from "./inactive-companies";+import { purgeCompany, removeCompanyLeftovers } from "./purge-company";++/** Keeps a backlog under Inngest's per-run step limit; later weeks drain the rest. */+const MAX_COMPANY_DELETIONS_PER_RUN = 100;++const splitIds = (value: string | undefined) =>+ (value ?? "")+ .split(",")+ .map((id) => id.trim())+ .filter(Boolean);++/** Marker per warned company in `externalIntegrationMapping`; the purge clears it. */+const WARNING_INTEGRATION = "inactive-company-warning";++type CleanupTarget = Pick<CompanyCandidate, "id" | "name" | "companyGroupId">;+const NOTHING_TO_DO: {+ plannedAt: number;+ toWarn: CleanupTarget[];+ toDelete: CleanupTarget[];+ staleWarningIds: string[];+} = { plannedAt: 0, toWarn: [], toDelete: [], staleWarningIds: [] };++type GroupOwner = { id: string; email: string; firstName: string | null };++/** The owner of each company group, for protection and for the warning email. */+async function getGroupOwners(+ groupIds: string[]+): Promise<Map<string, GroupOwner>> {+ const serviceRole = getCarbonServiceRole();+ const ownerIds = new Map<string, string>();+ for (const ids of chunkArray([...new Set(groupIds)], 200)) {+ const { data, error } = await serviceRole+ .from("companyGroup")+ .select("id, ownerId")+ .in("id", ids);+ if (error)+ throw new Error(`Failed to load company groups: ${error.message}`);+ for (const group of data) {+ if (group.ownerId) ownerIds.set(group.id, group.ownerId);+ }+ }++ const users = new Map<string, GroupOwner>();+ for (const ids of chunkArray([...new Set(ownerIds.values())], 200)) {+ const { data, error } = await serviceRole+ .from("user")+ .select("id, email, firstName")+ .in("id", ids);+ if (error) throw new Error(`Failed to load group owners: ${error.message}`);+ for (const user of data) users.set(user.id, user);+ }++ const owners = new Map<string, GroupOwner>();+ for (const [groupId, ownerId] of ownerIds) {+ const owner = users.get(ownerId);+ if (owner) owners.set(groupId, owner);+ }+ return owners;+}++/**+ * Whether a planned delete still holds, read in the purge's own transaction: the+ * company exists and is not bypassed, no company in its group has a plan, the+ * group's current owner is the person who was warned (and is not internal or a+ * bypass user), and that warning is due. The plan step ran minutes (or retries)+ * earlier.+ */+async function isStillDueForDeletion(+ trx: Kysely<KyselyDatabase>,+ companyId: string+): Promise<boolean> {+ if (splitIds(STRIPE_BYPASS_COMPANY_IDS).includes(companyId)) return false;+ const company = await trx+ .selectFrom("company")+ .select("companyGroupId")+ .where("id", "=", companyId)+ .executeTakeFirst();+ if (!company) return false;++ const groupId = company.companyGroupId;+ const paying = await trx+ .selectFrom("companyPlan")+ .select("id")+ .where((eb) =>+ groupId === null+ ? eb("id", "=", companyId)+ : eb(+ "id",+ "in",+ eb+ .selectFrom("company")+ .select("company.id")+ .where("company.companyGroupId", "=", groupId)+ )+ )+ .limit(1)+ .executeTakeFirst();+ if (paying) return false;++ // Only an owned group is ever warned. The owner may have changed, or become+ // internal, since the plan step, and a new owner has not been warned.+ if (groupId === null) return false;+ const owner = await trx+ .selectFrom("companyGroup")+ .innerJoin("user", "user.id", "companyGroup.ownerId")+ .select(["user.id", "user.email"])+ .where("companyGroup.id", "=", groupId)+ .executeTakeFirst();+ if (+ !owner ||+ splitIds(STRIPE_BYPASS_USER_IDS).includes(owner.id) ||+ isInternalEmail(owner.email)+ ) {+ return false;+ }++ const marker = await trx+ .selectFrom("externalIntegrationMapping")+ .select("metadata")+ .where("integration", "=", WARNING_INTEGRATION)+ .where("companyId", "=", companyId)+ .executeTakeFirst();+ const warning = marker?.metadata as Warning | undefined;+ if (warning?.ownerId !== owner.id) return false;+ return isDueForDeletion(warning, {+ now: Date.now(),+ today: datetime.today("UTC").toString()+ });+}export const weeklyFunction = inngest.createFunction({ id: "weekly", retries: 2 },{ cron: "0 21 * * 0" },async ({ step, logger }) => {const serviceRole = getCarbonServiceRole();- await step.run("cloud-cleanup", async () => {- logger.info("Starting weekly tasks");-- try {- if (process.env.CARBON_EDITION === Edition.Cloud) {- const bypassList = (STRIPE_BYPASS_COMPANY_IDS ?? "")- .split(",")- .map((id) => id.trim())- .filter(Boolean);-- logger.info("Bypass list", { bypassList });-- // Get all companies- const { data: companies, error: companiesError } = await serviceRole- .from("company")- .select("id, name, createdAt");-- if (companiesError) {- logger.error("Failed to fetch companies", {- error: companiesError- });- return;- }- logger.info("Found companies", { count: companies?.length || 0 });+ /** Email each company's group owner and record the warning (or the failure). */+ async function warnInactiveCompanies(+ batch: CleanupTarget[],+ plannedAt: number+ ) {+ const owners = await getGroupOwners(+ batch.flatMap((c) => (c.companyGroupId ? [c.companyGroupId] : []))+ );+ // A retried step skips companies an earlier attempt of this run warned.+ const { data: markers, error: markersError } = await serviceRole+ .from("externalIntegrationMapping")+ .select("companyId, metadata")+ .eq("integration", WARNING_INTEGRATION)+ .in(+ "companyId",+ batch.map((c) => c.id)+ );+ if (markersError) throw new Error(markersError.message);+ const warnedThisRun = new Set(+ markers+ .filter((m) => {+ const warnedAt = (m.metadata as Warning | null)?.warnedAt;+ return warnedAt !== undefined && Date.parse(warnedAt) >= plannedAt;+ })+ .map((m) => m.companyId)+ );- // Get all company plans- const { data: companyPlans, error: plansError } = await serviceRole- .from("companyPlan")- .select("id, stripeSubscriptionStatus");+ // The email names this date, and the delete waits for it (isDueForDeletion).+ const deleteAfter = datetime.today("UTC").add({ days: 7 }).toString();+ const deletionDate = formatDate(+ deleteAfter,+ { dateStyle: "long" },+ "en-US"+ );+ const billingUrl = `${getAppUrl()}/x/settings/billing`;++ for (const company of batch) {+ if (warnedThisRun.has(company.id)) continue;+ const owner = company.companyGroupId+ ? owners.get(company.companyGroupId)+ : undefined;+ if (!owner) {+ // Never warned means never deleted, so a company nobody owns is kept.+ logger.warn("No owner to warn; company kept", company);+ continue;+ }- if (plansError) {- logger.error("Failed to fetch company plans", {- error: plansError- });- return;- }+ const { error } = await sendEmail({+ to: owner.email,+ subject: `${company.name} will be deleted on or after ${deletionDate}`,+ html: await render(+ CompanyDeletionWarningEmail({+ recipientName: owner.firstName ?? undefined,+ companyName: company.name,+ deletionDate,+ billingUrl+ })+ )+ });+ if (error) {+ logger.error("Failed to send company deletion warning", {+ ...company,+ error+ });+ }- // Create a map of company plans for quick lookup- const planMap = new Map(- companyPlans?.map((plan) => [- plan.id,- plan.stripeSubscriptionStatus- ]) || []+ // A failed send is recorded too, so next week it queues behind the rest.+ const warning: Warning = error+ ? { failedAt: datetime.timestamp() }+ : { warnedAt: datetime.timestamp(), deleteAfter };+ const { error: markerError } = await serviceRole+ .from("externalIntegrationMapping")+ .upsert(+ {+ entityType: "company",+ entityId: company.id,+ integration: WARNING_INTEGRATION,+ externalId: "",+ metadata: { ...warning, ownerId: owner.id, to: owner.email },+ companyId: company.id+ },+ { onConflict: "entityType,entityId,integration,companyId" });+ if (markerError) {+ logger.error("Failed to record company deletion warning", {+ ...company,+ error: markerError+ });+ }+ }+ }- // Filter companies to delete- const oneWeekAgo = new Date();- oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);-- const companiesToDelete =- companies?.filter((company) => {- if (planMap.get(company.id) === "Canceled") {- return true;- }-- if (bypassList.includes(company.id)) {- return false;- }+ // Cloud only. A canceled subscription keeps its plan row until Stripe ends it+ // (customer.subscription.deleted removes the row), so a company is never+ // deleted inside a period it paid for.+ const plan = await step.run("plan-inactive-company-cleanup", async () => {+ if (process.env.CARBON_EDITION !== Edition.Cloud) return NOTHING_TO_DO;++ // Paged: PostgREST caps a plain select at 1000 rows, and a plan row cut+ // off there would make a paying company look planless.+ const [companies, plans] = await Promise.all([+ fetchAllFromTable<CompanyCandidate>(+ serviceRole,+ "company",+ "id, name, createdAt, companyGroupId"+ ),+ fetchAllFromTable<{ id: string }>(serviceRole, "companyPlan", "id")+ ]);+ if (companies.error || plans.error) {+ logger.error("Failed to load companies for cleanup", {+ error: companies.error ?? plans.error+ });+ return NOTHING_TO_DO;+ }- if (planMap.get(company.id)) {- return false;- }+ const selection = {+ companies: companies.data,+ planCompanyIds: new Set(plans.data.map((plan) => plan.id)),+ protectedCompanyIds: new Set(splitIds(STRIPE_BYPASS_COMPANY_IDS)),+ now: Date.now(),+ limit: Number.POSITIVE_INFINITY+ };+ // Owners are looked up only for the candidates' groups: a group owned by+ // a Carbon or bypass user has plan access without a plan row.+ const candidates = selectInactiveCompanies({+ ...selection,+ protectedGroupIds: new Set()+ });+ let owners: Map<string, GroupOwner>;+ try {+ owners = await getGroupOwners(+ candidates.flatMap((c) =>+ c.companyGroupId ? [c.companyGroupId] : []+ )+ );+ } catch (error) {+ logger.error("Failed to load company group owners", { error });+ return NOTHING_TO_DO;+ }+ // A group owned by a Carbon or bypass user has plan access without a row.+ const bypassUsers = new Set(splitIds(STRIPE_BYPASS_USER_IDS));+ const protectedGroupIds = new Set(+ [...owners]+ .filter(+ ([, owner]) =>+ bypassUsers.has(owner.id) || isInternalEmail(owner.email)+ )+ .map(([groupId]) => groupId)+ );+ const inactive = selectInactiveCompanies({+ ...selection,+ protectedGroupIds+ });+ // Never warned means never deleted, so an ownerless company is kept. It is+ // left out of the capped lists, where it would hold a slot every week.+ const warnable = inactive.filter(+ (c) => c.companyGroupId !== null && owners.has(c.companyGroupId)+ );- // Keep companies created in the last week- const createdAt = new Date(company.createdAt);- if (createdAt > oneWeekAgo) {- return false;- }+ const markers = await fetchAllFromTable<{+ id: string;+ companyId: string;+ metadata: Warning | null;+ }>(+ serviceRole,+ "externalIntegrationMapping",+ "id, companyId, metadata",+ (query) => query.eq("integration", WARNING_INTEGRATION)+ );+ if (markers.error) {+ logger.error("Failed to load company deletion warnings", {+ error: markers.error+ });+ return NOTHING_TO_DO;+ }- // Delete this company- return true;- }) || [];+ // A warning only counts while the company is still inactive: one that+ // regained a plan is cleared, and is warned afresh if it lapses again.+ const inactiveIds = new Set(inactive.map((c) => c.id));+ const warnings = new Map<string, Warning>();+ const staleWarningIds: string[] = [];+ for (const marker of markers.data) {+ if (!inactiveIds.has(marker.companyId)) staleWarningIds.push(marker.id);+ else if (marker.metadata)+ warnings.set(marker.companyId, marker.metadata);+ }- logger.info("Companies to delete", {- count: companiesToDelete.length- });+ const { toWarn, toDelete } = splitByWarning({+ inactive: warnable,+ warnings,+ clock: {+ now: selection.now,+ today: datetime.today("UTC").toString()+ },+ limit: MAX_COMPANY_DELETIONS_PER_RUN+ });+ const slim = (list: CompanyCandidate[]) =>+ list.map(({ id, name, companyGroupId }) => ({+ id,+ name,+ companyGroupId+ }));++ logger.info("Inactive companies", {+ inactive: inactive.length,+ protectedByOwner: candidates.length - inactive.length,+ withoutOwner: inactive.length - warnable.length,+ toWarn: slim(toWarn),+ toDelete: slim(toDelete),+ staleWarnings: staleWarningIds.length+ });+ return {+ plannedAt: selection.now,+ toWarn: slim(toWarn),+ toDelete: slim(toDelete),+ staleWarningIds+ };+ });- const { error: deletedCompaniesError } = await serviceRole- .from("company")+ if (plan.staleWarningIds.length > 0) {+ await step.run("clear-stale-deletion-warnings", async () => {+ for (const ids of chunkArray(plan.staleWarningIds, 200)) {+ const { error } = await serviceRole+ .from("externalIntegrationMapping").delete()- .in(- "id",- companiesToDelete.map((company) => company.id)- );+ .in("id", ids);+ if (error) {+ logger.error("Failed to clear stale deletion warnings", { error });+ }+ }+ });+ }- if (deletedCompaniesError) {- logger.error("Failed to delete companies", {- error: deletedCompaniesError- });- return;- } else {- logger.info("Deleted companies", {- count: companiesToDelete.length- });- for (const company of companiesToDelete) {- logger.info("Deleted company", { company: company.name });+ // A batch that still fails after its retries is logged and skipped, so it+ // cannot end the run before the remaining batches and the reminders.+ const warnBatches = chunkArray(plan.toWarn, 10);+ for (let i = 0; i < warnBatches.length; i++) {+ try {+ await step.run(`warn-inactive-companies-${i}`, () =>+ warnInactiveCompanies(warnBatches[i]!, plan.plannedAt)+ );+ } catch (error) {+ logger.error("Failed to warn a batch of inactive companies", {+ batch: i,+ error+ });+ }+ }++ // Ten companies per step: a company that cannot be deleted is logged and+ // skipped, and the table catalog is read once per step, not per company.+ // Only companies warned at least six days ago reach this list.+ const batches = chunkArray(plan.toDelete, 10);+ for (let i = 0; i < batches.length; i++) {+ try {+ await step.run(`delete-inactive-companies-${i}`, async () => {+ const db = getJobDatabaseClient();+ const replica = await canSetReplicationRole(db);+ const catalog = await getCompanyTableCatalog(db);+ const results: { id: string; deleted: boolean }[] = [];++ for (const company of batches[i]!) {+ try {+ // Re-checked in the purge's own transaction: a plan bought, or a+ // warning cleared, since the plan step ran must stop the delete.+ const purged = await db.transaction().execute(async (trx) => {+ if (!(await isStillDueForDeletion(trx, company.id)))+ return false;+ await purgeCompany(trx, catalog, company.id, { replica });+ // Before the commit: when any of it fails the delete rolls back,+ // and the company, still warned and due, is retried next week.+ const failures = await removeCompanyLeftovers(+ trx,+ serviceRole,+ company.id+ );+ if (failures.length > 0) {+ for (const failure of failures) {+ logger.error(`Failed to remove company ${failure.part}`, {+ ...company,+ error: failure.error+ });+ }+ throw new Error(+ `Company cleanup incomplete: ${failures.map((f) => f.part).join(", ")}`+ );+ }+ return true;+ });+ if (!purged) {+ logger.info(+ "Company no longer due for deletion; kept",+ company+ );+ results.push({ id: company.id, deleted: false });+ continue;+ }+ } catch (error) {+ logger.error("Failed to delete company", {+ ...company,+ replica,+ error+ });+ results.push({ id: company.id, deleted: false });+ continue;}- }- // Drop search index tables for companies being deleted- for (const company of companiesToDelete) {- const { error: dropSearchError } = await serviceRole.rpc(+ const { error: searchError } = await serviceRole.rpc("drop_company_search_index",{ p_company_id: company.id });- if (dropSearchError) {+ if (searchError) {logger.error("Failed to drop search index for company", {- company: company.name,- error: dropSearchError- });- } else {- logger.info("Dropped search index for company", {- company: company.name+ ...company,+ error: searchError});}+ logger.info("Deleted company", company);+ results.push({ id: company.id, deleted: true });}- }+ return results;+ });} catch (error) {- logger.error("Unexpected error in cloud cleanup", { error });+ logger.error("Failed to delete a batch of inactive companies", {+ batch: i,+ error+ });}- });+ }// Build inside a memoized step, send via step.sendEvent — sending// mid-step would double-deliver on a retry after a partial send.diff --git a/packages/jobs/src/inngest/functions/tasks/company-export.ts b/packages/jobs/src/inngest/functions/tasks/company-export.tsindex d87e7f3c42..c4a5134d33 100644--- a/packages/jobs/src/inngest/functions/tasks/company-export.ts+++ b/packages/jobs/src/inngest/functions/tasks/company-export.ts@@ -4,6 +4,7 @@ import { getCompanyPrivateBucket } from "@carbon/files";import { getLogger } from "@carbon/logger";import { NonRetriableError } from "inngest";import { sql } from "kysely";+import { listBucketFilesRecursive } from "../../../backups/storage";import { getJobDatabaseClient, type JobDatabase } from "../../../db";import { inngest } from "../../client";import type { Manifest } from "./company-backup";@@ -471,29 +472,3 @@ export const companyExportFunction = inngest.createFunction(});});--async function listBucketFilesRecursive(- client: ServiceRole,- bucket: string,- prefix = ""-): Promise<Array<{ path: string; size: number }>> {- const files: Array<{ path: string; size: number }> = [];- const { data, error } = await client.storage.from(bucket).list(prefix, {- limit: 1000- });- if (error || !data) return files;-- for (const entry of data) {- const path = prefix ? `${prefix}/${entry.name}` : entry.name;- if (entry.id === null) {- // folder- files.push(...(await listBucketFilesRecursive(client, bucket, path)));- } else {- files.push({- path,- size: (entry.metadata as { size?: number } | null)?.size ?? 0- });- }- }- return files;-}