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.
Supplier & Customer Documents and Bank Details — implementation plan
Spec / source: .ai/specs/2026-09-16-supplier-customer-documents-and-bank-details.md
Branch: feat/supplier-customer-docs
Worktree: /Users/aashu/work/carbon/carbon-feat-supplier-customer-docs — run every command from here. Do NOT touch /Users/aashu/work/carbon/carbon.
Critical context for the executor
- This is a pnpm workspace. Never use
npm. - The ERP app's package name is
erp(not@carbon/erp) —pnpm exec turbo run typecheck --filter=erp. - Whole-repo typecheck OOMs. Always scope with
--filter. - After any migration:
pnpm db:migratethenpnpm run generate:types, BEFORE any typecheck. - Never run
git commit. The user commits. Leave changes in the working tree. - Every new user-facing string must be Lingui-wrapped (
<Trans>for JSX,t`...`fromuseLingui()for attributes/strings). No bare English. - Local Postgres for this worktree is on port 58641 (
PGPASSWORD=postgres psql -h 127.0.0.1 -p 58641 -U postgres -d postgres).
Progress
- Task 1: Migration — documentSourceType enum values
- Task 2: Migration — supplierBankAccount + customerBankAccount tables
- Task 3: Apply migrations and regenerate types
- Task 4: Register bank tables in audit config
- Task 5: Add documentSourceTypes TS mirror values + document location links
- Task 6: Add path entries for documents and bank accounts
- Task 7: Extract shared RecordDocuments component
- Task 8: Supplier + customer Documents routes
- Task 9: Add permission-gating to sidebar hooks
- Task 10: Sidebar entries for Documents and Bank Accounts
- Task 11: Bank account validators and services
- Task 12: Bank account form + list UI components
- Task 13: Bank account routes (list / new / edit / delete)
- Task 14: Full verification
Dependencies
- Task 1, 2 independent of each other; both must precede Task 3.
- Task 3 must precede Tasks 4, 11 (needs regenerated DB types).
- Task 5, 6 independent, can run any time after Task 3.
- Task 7 must precede Task 8.
- Task 9 must precede Task 10.
- Task 11 must precede Tasks 12, 13.
- Task 12 must precede Task 13.
- Task 14 last.
Task 1: Migration — add Supplier and Customer to documentSourceType
Depends on: none
Files:
- Create:
packages/database/supabase/migrations/<timestamp>_supplier-customer-document-source.sql - Copy from (precedent):
packages/database/supabase/migrations/20260202000000_supplier_quote_document_type.sql
Steps:
- Create the migration file:
pnpm db:migrate:new supplier-customer-document-source - The generated filename has a timestamp. Verify its HHMMSS portion is NOT
000000— if it is, rename the file with a randomized HHMMSS (e.g.20260916142853_supplier-customer-document-source.sql). The timestamp is the migration's primary key and000000collides across branches. - Write exactly this content into that file:
-- Master records (supplier, customer) can now own documents. Every existing -- documentSourceType value is a transaction or an item; these are the first -- two master-record sources. ALTER TYPE "documentSourceType" ADD VALUE IF NOT EXISTS 'Supplier'; ALTER TYPE "documentSourceType" ADD VALUE IF NOT EXISTS 'Customer';
Verify:
ls packages/database/supabase/migrations/ | grep supplier-customer-document-source
# Expected: exactly one filename, timestamp not ending in 000000
Out of scope: Do not modify the document table itself — it needs no schema change. Do not touch any other enum.
Task 2: Migration — supplierBankAccount and customerBankAccount tables
Depends on: none
Files:
- Create:
packages/database/supabase/migrations/<timestamp>_supplier-customer-bank-accounts.sql - Copy from (precedent):
.claude/rules/conventions-database.mdtable + RLS template;packages/database/supabase/migrations/20260905132037_job-operation-batching.sqlfor the exact RLS policy idiom.
Steps:
Create the migration file:
pnpm db:migrate:new supplier-customer-bank-accountsCheck the HHMMSS is not
000000; rename with a randomized one if it is.Write this content. Note
countryCodeisCHAR(2)referencingcountry("alpha2")— verified against the live DB;countryhas no integer id column.-- Counterparty bank accounts: the bank we pay a supplier into, and the bank we -- refund a customer to. NOT the company's own bank accounts, and unrelated to -- "payment"."bankAccount", which is a GL chart-of-accounts reference. -- -- Gated on accounting_* rather than purchasing_*/sales_* on purpose: payments are -- run by finance, and purchasing_view is very widely held. CREATE TABLE "supplierBankAccount" ( "id" TEXT NOT NULL DEFAULT id('sba'), "companyId" TEXT NOT NULL, "supplierId" TEXT NOT NULL, "name" TEXT NOT NULL, "accountHolderName" TEXT, "bankName" TEXT, "countryCode" CHAR(2), "currencyCode" TEXT, "accountNumber" TEXT, "routingNumber" TEXT, "iban" TEXT, "swiftBic" TEXT, "isPrimary" BOOLEAN NOT NULL DEFAULT FALSE, "active" BOOLEAN NOT NULL DEFAULT TRUE, "notes" TEXT, "createdBy" TEXT NOT NULL REFERENCES "user"("id"), "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), "updatedBy" TEXT REFERENCES "user"("id"), "updatedAt" TIMESTAMP WITH TIME ZONE, "customFields" JSONB, "tags" TEXT[], PRIMARY KEY ("id", "companyId"), FOREIGN KEY ("companyId") REFERENCES "company"("id") ON DELETE CASCADE, FOREIGN KEY ("supplierId", "companyId") REFERENCES "supplier"("id", "companyId") ON UPDATE CASCADE ON DELETE CASCADE, FOREIGN KEY ("countryCode") REFERENCES "country"("alpha2") ON UPDATE CASCADE ON DELETE SET NULL ); CREATE INDEX "supplierBankAccount_companyId_idx" ON "supplierBankAccount" ("companyId"); CREATE INDEX "supplierBankAccount_supplierId_idx" ON "supplierBankAccount" ("supplierId"); CREATE INDEX "supplierBankAccount_createdBy_idx" ON "supplierBankAccount" ("createdBy"); CREATE INDEX "supplierBankAccount_updatedBy_idx" ON "supplierBankAccount" ("updatedBy"); CREATE INDEX "supplierBankAccount_countryCode_idx" ON "supplierBankAccount" ("countryCode"); CREATE UNIQUE INDEX "supplierBankAccount_primary_idx" ON "supplierBankAccount" ("supplierId", "companyId") WHERE "isPrimary" AND "active"; ALTER TABLE "public"."supplierBankAccount" ENABLE ROW LEVEL SECURITY; CREATE POLICY "SELECT" ON "public"."supplierBankAccount" FOR SELECT USING ( "companyId" = ANY ((SELECT get_companies_with_employee_permission('accounting_view'))::text[]) ); CREATE POLICY "INSERT" ON "public"."supplierBankAccount" FOR INSERT WITH CHECK ( "companyId" = ANY ((SELECT get_companies_with_employee_permission('accounting_create'))::text[]) ); CREATE POLICY "UPDATE" ON "public"."supplierBankAccount" FOR UPDATE USING ( "companyId" = ANY ((SELECT get_companies_with_employee_permission('accounting_update'))::text[]) ); CREATE POLICY "DELETE" ON "public"."supplierBankAccount" FOR DELETE USING ( "companyId" = ANY ((SELECT get_companies_with_employee_permission('accounting_delete'))::text[]) ); CREATE TABLE "customerBankAccount" ( "id" TEXT NOT NULL DEFAULT id('cba'), "companyId" TEXT NOT NULL, "customerId" TEXT NOT NULL, "name" TEXT NOT NULL, "accountHolderName" TEXT, "bankName" TEXT, "countryCode" CHAR(2), "currencyCode" TEXT, "accountNumber" TEXT, "routingNumber" TEXT, "iban" TEXT, "swiftBic" TEXT, "isPrimary" BOOLEAN NOT NULL DEFAULT FALSE, "active" BOOLEAN NOT NULL DEFAULT TRUE, "notes" TEXT, "createdBy" TEXT NOT NULL REFERENCES "user"("id"), "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), "updatedBy" TEXT REFERENCES "user"("id"), "updatedAt" TIMESTAMP WITH TIME ZONE, "customFields" JSONB, "tags" TEXT[], PRIMARY KEY ("id", "companyId"), FOREIGN KEY ("companyId") REFERENCES "company"("id") ON DELETE CASCADE, FOREIGN KEY ("customerId", "companyId") REFERENCES "customer"("id", "companyId") ON UPDATE CASCADE ON DELETE CASCADE, FOREIGN KEY ("countryCode") REFERENCES "country"("alpha2") ON UPDATE CASCADE ON DELETE SET NULL ); CREATE INDEX "customerBankAccount_companyId_idx" ON "customerBankAccount" ("companyId"); CREATE INDEX "customerBankAccount_customerId_idx" ON "customerBankAccount" ("customerId"); CREATE INDEX "customerBankAccount_createdBy_idx" ON "customerBankAccount" ("createdBy"); CREATE INDEX "customerBankAccount_updatedBy_idx" ON "customerBankAccount" ("updatedBy"); CREATE INDEX "customerBankAccount_countryCode_idx" ON "customerBankAccount" ("countryCode"); CREATE UNIQUE INDEX "customerBankAccount_primary_idx" ON "customerBankAccount" ("customerId", "companyId") WHERE "isPrimary" AND "active"; ALTER TABLE "public"."customerBankAccount" ENABLE ROW LEVEL SECURITY; CREATE POLICY "SELECT" ON "public"."customerBankAccount" FOR SELECT USING ( "companyId" = ANY ((SELECT get_companies_with_employee_permission('accounting_view'))::text[]) ); CREATE POLICY "INSERT" ON "public"."customerBankAccount" FOR INSERT WITH CHECK ( "companyId" = ANY ((SELECT get_companies_with_employee_permission('accounting_create'))::text[]) ); CREATE POLICY "UPDATE" ON "public"."customerBankAccount" FOR UPDATE USING ( "companyId" = ANY ((SELECT get_companies_with_employee_permission('accounting_update'))::text[]) ); CREATE POLICY "DELETE" ON "public"."customerBankAccount" FOR DELETE USING ( "companyId" = ANY ((SELECT get_companies_with_employee_permission('accounting_delete'))::text[]) );Verify the FK target shape before trusting it:
PGPASSWORD=postgres psql -h 127.0.0.1 -p 58641 -U postgres -d postgres -c '\d country' | head -8Expected: a
alpha2 | character(2)column andcountry_pkey PRIMARY KEY, btree (alpha2). Ifcountry's PK is notalpha2, STOP and report — do not improvise a different FK.
Verify:
grep -c "CREATE TABLE" packages/database/supabase/migrations/*supplier-customer-bank-accounts.sql
# Expected: 2
grep -c "get_companies_with_employee_permission('accounting_" packages/database/supabase/migrations/*supplier-customer-bank-accounts.sql
# Expected: 8
Out of scope: Do NOT add bank columns to supplierPayment or customerPayment. Do NOT add encryption, vault functions, or audit-redaction — the spec explicitly accepts plaintext (see the spec's Accepted Risks section). Do NOT create a bankAccount table for the company's own banks.
Task 3: Apply migrations and regenerate types
Depends on: Task 1, Task 2
Files:
- Modify (generated, do not hand-edit):
packages/database/src/types.ts,packages/database/supabase/functions/lib/types.ts,packages/database/src/swagger-docs-schema.ts
Steps:
- Apply:
pnpm db:migrate - Regenerate DB types:
pnpm run generate:types - Confirm the new tables and enum values exist in the live DB:
PGPASSWORD=postgres psql -h 127.0.0.1 -p 58641 -U postgres -d postgres -c "SELECT unnest(enum_range(NULL::\"documentSourceType\"))::text" | grep -E "Supplier$|Customer$" PGPASSWORD=postgres psql -h 127.0.0.1 -p 58641 -U postgres -d postgres -c '\d "supplierBankAccount"' | head -20
Verify:
grep -c "supplierBankAccount" packages/database/src/types.ts
# Expected: a number greater than 0
grep -c "customerBankAccount" packages/database/src/types.ts
# Expected: a number greater than 0
Out of scope: Do not hand-edit any generated types file. If the generator fails, STOP and report — do not patch types by hand.
Task 4: Register bank tables in the audit config
Depends on: Task 3
Files:
- Modify:
packages/database/src/audit.config.ts— add both tables to the existingsupplierandcustomerentities, and add display labels.
Steps:
- In the
customerentity'stablesobject (around line 141–155, which already containscustomerPayment: { role: "extension" }), add:customerBankAccount: { entityIdColumn: "customerId" }, - In the
supplierentity'stablesobject (around line 174–190, which already containssupplierPayment: { role: "extension" }), add:supplierBankAccount: { entityIdColumn: "supplierId" }, - In the table-label map (around line 615–625, which contains
customerPayment: "Payment"), add:customerBankAccount: "Bank Account", supplierBankAccount: "Bank Account",
Use the { entityIdColumn } role — documented at audit.config.ts:17-18 as "a child table with its own surrogate PK; the named column contains the parent entity ID". Do NOT use role: "extension", which is only for 1:1 tables whose PK equals the parent FK.
Verify:
pnpm exec turbo run typecheck --filter=@carbon/database
# Expected: exits 0, no type errors
Out of scope: Do NOT add per-field redaction or touch skipFields. The spec's Accepted Risks section documents that bank numbers appear in audit diffs in plaintext — that is the user's explicit decision, not a bug to fix here.
Task 5: Add TS enum mirror values and document location links
Depends on: Task 3
Files:
- Modify:
apps/erp/app/modules/documents/documents.models.ts— add two values todocumentSourceTypes(line 9). - Modify:
apps/erp/app/modules/documents/ui/Documents/DocumentsTable.tsx— add two cases togetDocumentLocation()(around line 634).
Steps:
- In
documents.models.ts, thedocumentSourceTypesarray starts at line 9 and ends with...itemType. Add"Supplier"and"Customer"as entries before...itemType:
This array is hand-maintained (not generated) and drives the source-type filter in the documents table."Shipment", "Supplier", "Customer", ...itemType - In
DocumentsTable.tsx, insidegetDocumentLocation(sourceDocument, sourceDocumentId), add these two cases beforedefault::case "Supplier": return path.to.supplierDetails(sourceDocumentId); case "Customer": return path.to.customerDetails(sourceDocumentId);path.to.supplierDetailsandpath.to.customerDetailsalready exist (apps/erp/app/utils/path.ts:2188and:557).
Verify:
grep -A2 '"Shipment",' apps/erp/app/modules/documents/documents.models.ts
# Expected: shows "Supplier", and "Customer",
grep -c 'case "Supplier":' apps/erp/app/modules/documents/ui/Documents/DocumentsTable.tsx
# Expected: 1
Out of scope: Do not reorder or remove existing enum values — the array order feeds a UI filter list but the values must all remain.
Task 6: Add path entries
Depends on: Task 3
Files:
- Modify:
apps/erp/app/utils/path.ts— add four route builders, keeping alphabetical order within thetoobject.
Steps:
- Near the existing
customerDetails(line 557) andcustomerPayment(line 569) entries, add:customerBankAccount: (customerId: string, id: string) => generatePath(`${x}/customer/${customerId}/bank-accounts/${id}`), customerBankAccounts: (id: string) => generatePath(`${x}/customer/${id}/bank-accounts`), customerDocuments: (id: string) => generatePath(`${x}/customer/${id}/documents`), deleteCustomerBankAccount: (customerId: string, id: string) => generatePath(`${x}/customer/${customerId}/bank-accounts/delete/${id}`), newCustomerBankAccount: (customerId: string) => generatePath(`${x}/customer/${customerId}/bank-accounts/new`), - Near the existing
supplierDetails(line 2190) entry, add:
Place each in its correct alphabetical position within the object.supplierBankAccount: (supplierId: string, id: string) => generatePath(`${x}/supplier/${supplierId}/bank-accounts/${id}`), supplierBankAccounts: (id: string) => generatePath(`${x}/supplier/${id}/bank-accounts`), supplierDocuments: (id: string) => generatePath(`${x}/supplier/${id}/documents`), deleteSupplierBankAccount: (supplierId: string, id: string) => generatePath(`${x}/supplier/${supplierId}/bank-accounts/delete/${id}`), newSupplierBankAccount: (supplierId: string) => generatePath(`${x}/supplier/${supplierId}/bank-accounts/new`),
Verify:
grep -cE "supplierDocuments|customerDocuments|supplierBankAccounts|customerBankAccounts" apps/erp/app/utils/path.ts
# Expected: 4
Out of scope: Do not modify the existing supplierDefaultAttachments entry — default attachments are a different feature (outbound PO email attachments).
Task 7: Extract shared RecordDocuments component
Depends on: none (but do after Task 6 so path.to.* exists)
Files:
- Create:
apps/erp/app/components/RecordDocuments.tsx - Copy from (precedent):
apps/erp/app/modules/sales/ui/Opportunity/OpportunityDocuments.tsx— the whole upload/download/delete/list card, minus the drag-and-drop and Opportunity-specific pieces.
Steps:
- Read
apps/erp/app/modules/sales/ui/Opportunity/OpportunityDocuments.tsxin full first. It is ~490 lines and contains the complete reference implementation. - Create
RecordDocuments.tsxexporting a default component with this exact prop shape:type RecordDocumentsProps = { files: FileObject[]; /** The record's id — becomes document.sourceDocumentId */ id: string; /** Storage prefix segment under ${companyId}/, e.g. "supplier" */ bucketPrefix: string; /** documentSourceType value, e.g. "Supplier" */ sourceDocument: "Supplier" | "Customer"; /** Permission module gating upload/delete, e.g. "purchasing" */ module: "purchasing" | "sales"; isReadOnly?: boolean; }; - Carry over from the precedent, adapting names:
- The
Card/CardHeader/CardTitle/CardAction/CardContentlayout with aTableof files. FileDropzone+ a hidden file input with an uploadIconButton(LuUpload).upload(): for each file,stripSpecialCharacters(file.name), build the path as`${companyId}/${bucketPrefix}/${id}/${safeName}`, callcarbon.storage.from("private").upload(fileName, file, { cacheControl:${12 * 60 * 60}, upsert: true }), then on success callcreateDocumentRecord({ path, name, size })which submits aFormDatawithpath,name,size(KB,Math.round(size / 1024)),sourceDocument,sourceDocumentIdtopath.to.newDocumentviasubmit(formData, { method: "post", action: path.to.newDocument, navigate: false, fetcherKey: ... }).download(),deleteAttachment()(calls.remove([path])), optimistic rows viauseFetchers,revalidator.revalidate().getDocumentType(file.name)for the icon,convertKbToStringfor the size column,DateTimeforupdated_at.- Permission checks: upload/delete gated on
permissions.can("update", module)/permissions.can("delete", module).
- The
- Wrap every user-facing string in Lingui:
<Trans>in JSX,t`...`fromuseLingui()for toasts and aria labels. The precedent already does this — follow it exactly. - Do NOT carry over:
useDndContext/useDraggable/useOptimisticDocumentDrag(drag-to-line is Opportunity-specific), theLuRadioTower/LuShoppingCartbadges, theOutlet, or theopportunityprop.
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exits 0
grep -c "Trans\|useLingui" apps/erp/app/components/RecordDocuments.tsx
# Expected: greater than 0 (proves Lingui wrapping is present)
Out of scope: Do NOT modify OpportunityDocuments.tsx to use this new component. Migrating it is unrelated refactoring of working code and is explicitly excluded by spec decision D9.
Task 8: Supplier and customer Documents routes
Depends on: Task 5, Task 6, Task 7
Files:
- Create:
apps/erp/app/routes/x+/supplier+/$supplierId.documents.tsx - Create:
apps/erp/app/routes/x+/customer+/$customerId.documents.tsx - Copy from (precedent):
apps/erp/app/routes/x+/supplier+/$supplierId.default-attachments.tsx— same loader shape, same storage.list()call.
Steps:
- Create
$supplierId.documents.tsx:import { requirePermissions } from "@carbon/auth/auth.server"; import type { LoaderFunctionArgs } from "react-router"; import { useLoaderData } from "react-router"; import RecordDocuments from "~/components/RecordDocuments"; export async function loader({ request, params }: LoaderFunctionArgs) { const { client, companyId } = await requirePermissions(request, { view: "purchasing" }); const { supplierId } = params; if (!supplierId) throw new Error("Missing supplierId"); const result = await client.storage .from("private") .list(`${companyId}/supplier/${supplierId}`); return { supplierId, files: result.data ?? [] }; } export default function SupplierDocumentsRoute() { const { supplierId, files } = useLoaderData<typeof loader>(); return ( <RecordDocuments files={files} id={supplierId} bucketPrefix="supplier" sourceDocument="Supplier" module="purchasing" /> ); } - Create
$customerId.documents.tsx— identical but withcustomerId,view: "sales", prefix`${companyId}/customer/${customerId}`,bucketPrefix="customer",sourceDocument="Customer",module="sales".
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exits 0
ls apps/erp/app/routes/x+/supplier+/\$supplierId.documents.tsx apps/erp/app/routes/x+/customer+/\$customerId.documents.tsx
# Expected: both paths listed, no "No such file"
Out of scope: Do not add an Outlet — these routes have no child routes.
Task 9: Add permission-gating to the sidebar hooks
Depends on: none
Files:
- Modify:
apps/erp/app/modules/purchasing/ui/Supplier/SupplierSidebar/useSupplierSidebar.tsx— extend the filter at lines 125–129. - Modify:
apps/erp/app/modules/sales/ui/Customer/CustomerSidebar/useCustomerSidebar.tsx— same change.
Steps:
- The current filter is:
].filter( (item) => item.role === undefined || item.role.some((role) => permissions.is(role as Role)) );permissions.is(role)tests the user's ROLE (employee / supplier / customer) — verified atapps/erp/app/hooks/usePermissions.tsx:40-45. It cannot express a module permission likeaccounting_view.permissions.can(action, feature)is the separate function for that (usePermissions.tsx:26-31). - Change the filter in BOTH files to also honour an optional
permissionfield:].filter((item) => { const roleOk = item.role === undefined || item.role.some((role) => permissions.is(role as Role)); const permissionOk = !("permission" in item) || item.permission === undefined || permissions.can(item.permission.action, item.permission.module); return roleOk && permissionOk; }); - If TypeScript complains that
permissiondoes not exist on the array's inferred element type, add the field as optional to the object literal type. The array is built inline, so the simplest fix is to annotate the returned array's element type explicitly. If the inline-array inference makes this awkward, STOP and report rather than restructuring the whole hook.
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exits 0
Out of scope: Do NOT change the role field on any existing sidebar entry. Existing entries must keep behaving exactly as they do now.
Task 10: Sidebar entries for Documents and Bank Accounts
Depends on: Task 6, Task 9
Files:
- Modify:
apps/erp/app/modules/purchasing/ui/Supplier/SupplierSidebar/useSupplierSidebar.tsx - Modify:
apps/erp/app/modules/sales/ui/Customer/CustomerSidebar/useCustomerSidebar.tsx
Steps:
- In
useSupplierSidebar.tsx, add two entries. Place Documents immediately after the existingDefault Attachmentsentry (which usesicon: <LuFiles />at line ~87), and Bank Accounts afterPayment:{ name: t`Documents`, to: path.to.supplierDocuments(supplierId), role: ["employee"], icon: <LuFiles /> }, { name: t`Bank Accounts`, to: path.to.supplierBankAccounts(supplierId), role: ["employee"], permission: { action: "view", module: "accounting" }, icon: <LuLandmark /> }, LuFilesis already imported in that file (line 6). AddLuLandmarkto thereact-icons/luimport list — note it already appears in a commented-out block at line ~122, so the name is correct but the import may need adding.- Do the same in
useCustomerSidebar.tsxwithpath.to.customerDocuments/path.to.customerBankAccounts. Check whichLu*icons that file already imports and add what is missing. - Both
namevalues use thet`...`template tag fromuseLingui(), matching every existing entry in these files.
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exits 0
grep -c "supplierDocuments\|supplierBankAccounts" apps/erp/app/modules/purchasing/ui/Supplier/SupplierSidebar/useSupplierSidebar.tsx
# Expected: 2
Out of scope: Do not remove the commented-out Shipping/Accounting blocks at the end of the supplier sidebar — they are pre-existing and unrelated.
Task 11: Bank account validators and services
Depends on: Task 3
Files:
- Modify:
apps/erp/app/modules/purchasing/purchasing.models.ts— addsupplierBankAccountValidator. - Modify:
apps/erp/app/modules/purchasing/purchasing.service.ts— add four functions. - Modify:
apps/erp/app/modules/sales/sales.models.ts— addcustomerBankAccountValidator. - Modify:
apps/erp/app/modules/sales/sales.service.ts— add four functions. - Modify:
apps/erp/app/modules/purchasing/types.tsandapps/erp/app/modules/sales/types.ts— export the row types. - Copy from (precedent):
supplierProcessValidatoratpurchasing.models.ts:414;upsertSupplierProcessatpurchasing.service.ts:2064;deleteSupplierProcessatpurchasing.service.ts:275.
Steps:
- Add to
purchasing.models.ts(nearsupplierLocationValidator, line ~400):export const supplierBankAccountValidator = z .object({ id: zfd.text(z.string().optional()), supplierId: z.string().min(1, { message: "Supplier is required" }), name: zfd.text(z.string().min(1, { message: "Name is required" })), accountHolderName: zfd.text(z.string().optional()), bankName: zfd.text(z.string().optional()), countryCode: zfd.text(z.string().optional()), currencyCode: zfd.text(z.string().optional()), accountNumber: zfd.text(z.string().optional()), routingNumber: zfd.text(z.string().optional()), iban: zfd.text(z.string().optional()), swiftBic: zfd.text(z.string().optional()), isPrimary: zfd.checkbox(), active: zfd.checkbox(), notes: zfd.text(z.string().optional()) }) .refine((data) => data.accountNumber || data.iban, { message: "Either an account number or an IBAN is required", path: ["accountNumber"] }) .refine((data) => !data.iban || isValidIban(data.iban), { message: "Invalid IBAN", path: ["iban"] }) .refine((data) => !data.routingNumber || isValidAbaRouting(data.routingNumber), { message: "Invalid routing number", path: ["routingNumber"] }) .refine((data) => !data.swiftBic || /^[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?$/.test(data.swiftBic.toUpperCase()), { message: "Invalid SWIFT/BIC code", path: ["swiftBic"] }); - Create the two validation helpers in
packages/utils/src/bank.tsand export them frompackages/utils/src/index.ts:
Import them into the models file from/** ISO 13616 IBAN check: rearrange, letters->digits, mod 97 must equal 1. */ export function isValidIban(raw: string): boolean { const iban = raw.replace(/\s+/g, "").toUpperCase(); if (!/^[A-Z]{2}[0-9]{2}[A-Z0-9]{1,30}$/.test(iban)) return false; const rearranged = iban.slice(4) + iban.slice(0, 4); const expanded = rearranged.replace(/[A-Z]/g, (c) => (c.charCodeAt(0) - 55).toString() ); // mod-97 over a string, digit by digit (the number exceeds Number.MAX_SAFE_INTEGER) let remainder = 0; for (const digit of expanded) { remainder = (remainder * 10 + Number(digit)) % 97; } return remainder === 1; } /** ABA routing checksum: 9 digits, weighted 3-7-1 sum mod 10 must equal 0. */ export function isValidAbaRouting(raw: string): boolean { const digits = raw.replace(/\s+/g, ""); if (!/^[0-9]{9}$/.test(digits)) return false; const w = [3, 7, 1, 3, 7, 1, 3, 7, 1]; const sum = digits .split("") .reduce((acc, d, i) => acc + Number(d) * w[i], 0); return sum % 10 === 0; }@carbon/utils. Check first whetherzfd.checkbox()is the idiom used elsewhere in these models files (grep -n "zfd.checkbox" apps/erp/app/modules/*/*.models.ts). If the repo uses a different boolean coercion, match it. Ifzfd.checkboxdoes not exist, STOP and report — do not invent a coercion. - Add to
purchasing.service.ts, matching theupsertSupplierProcessshape exactly (two-arm union oncreatedByvsupdatedBy,sanitize()on update):
Inexport async function getSupplierBankAccounts( client: SupabaseClient<Database>, supplierId: string ) { return client .from("supplierBankAccount") .select("*") .eq("supplierId", supplierId) .eq("active", true) .order("isPrimary", { ascending: false }) .order("name"); } export async function getSupplierBankAccount( client: SupabaseClient<Database>, id: string ) { return client.from("supplierBankAccount").select("*").eq("id", id).single(); } export async function upsertSupplierBankAccount(...) // mirror upsertSupplierProcess export async function deleteSupplierBankAccount(...) // mirror deleteSupplierProcessupsertSupplierBankAccount, whenisPrimaryis true, first clear it on the party's other rows:
Do this before the insert/update so the partial unique index never sees two primaries.await client .from("supplierBankAccount") .update({ isPrimary: false }) .eq("supplierId", bankAccount.supplierId) .neq("id", idBeingSaved ?? ""); - Mirror all of the above in the sales module for
customerBankAccount. - Export the new functions and validators from each module's
index.tsbarrel, and add row types totypes.tsfollowing howSupplierProcess/SupplierLocationare declared there.
Verify:
pnpm exec turbo run typecheck --filter=erp --filter=@carbon/utils
# Expected: exits 0
Out of scope: Do not build a Kysely transaction — these are simple supabase-client calls, matching every sibling service function. Do not add encryption or masking in the service layer; masking is presentation-only (Task 12).
Task 12: Bank account form and list UI components
Depends on: Task 11
Files:
- Create:
apps/erp/app/modules/purchasing/ui/Supplier/SupplierBankAccountForm.tsx - Create:
apps/erp/app/modules/purchasing/ui/Supplier/SupplierBankAccounts.tsx - Create:
apps/erp/app/modules/sales/ui/Customer/CustomerBankAccountForm.tsx - Create:
apps/erp/app/modules/sales/ui/Customer/CustomerBankAccounts.tsx - Modify: the
index.tsin eachui/Supplier/ui/Customerfolder to export them. - Copy from (precedent):
apps/erp/app/modules/purchasing/ui/Supplier/SupplierLocations.tsx(list card with actions + ConfirmDelete) andapps/erp/app/modules/purchasing/ui/Supplier/SupplierProcessForm.tsx(drawer form with ValidatedForm).
Steps:
Read both precedent files fully before writing.
SupplierBankAccountForm.tsx— a drawerValidatedFormwithvalidator={supplierBankAccountValidator}, containing, in this order:<Hidden name="id" />,<Hidden name="supplierId" />Input name="name"labelt`Name`Input name="accountHolderName"labelt`Account Holder`Input name="bankName"labelt`Bank Name`Country name="countryCode"labelt`Country`Currency name="currencyCode"labelt`Currency`Input name="accountNumber"labelt`Account Number`Input name="routingNumber"labelt`Routing Number`Input name="iban"labelt`IBAN`Input name="swiftBic"labelt`SWIFT / BIC`Boolean name="isPrimary"labelt`Primary Account`Boolean name="active"labelt`Active`TextArea name="notes"labelt`Notes`<CustomFormFields table="supplierBankAccount" /><Submit isDisabled={isDisabled}>whereisDisabled = !permissions.can(isEditing ? "update" : "create", "accounting")
All of
Input,TextArea,Boolean,Country,Currency,Hidden,Submit,CustomFormFieldsare exported from~/components/Form(verified present inapps/erp/app/components/Form/index.ts).SupplierBankAccounts.tsx— aCardlisting accounts, followingSupplierLocations.tsx:CardActionwith aNewbutton →path.to.newSupplierBankAccount(supplierId), gatedpermissions.can("create", "accounting").- One row per account showing:
name, aBadgereadingt`Primary`whenisPrimary, the bank name, and the masked number. - Masking helper, defined in the component file:
Showfunction maskAccountNumber(value: string | null): string { if (!value) return ""; const trimmed = value.replace(/\s+/g, ""); if (trimmed.length <= 4) return "••••"; return `••••${trimmed.slice(-4)}`; }maskAccountNumber(account.iban ?? account.accountNumber). - A per-row reveal toggle (
useState<Set<string>>of revealed ids,LuEye/LuEyeOfficon button) that swaps the masked string for the raw value. Add a comment above it stating this is presentation-only and the loader already returns the full value. - Row actions (Edit / Delete) gated on
permissions.can("update"/"delete", "accounting"), plusConfirmDeletewired topath.to.deleteSupplierBankAccount(...), exactly asSupplierLocations.tsxdoes. - An
<Outlet />at the end so the new/edit/delete child routes render.
Mirror both files for customer.
Every visible string Lingui-wrapped.
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exits 0
grep -c "maskAccountNumber" apps/erp/app/modules/purchasing/ui/Supplier/SupplierBankAccounts.tsx
# Expected: 2 or more (definition plus at least one use)
Out of scope: Do not build an approval/pending-change workflow — spec decision D6 defers it. Do not add a "verify with supplier" step.
Task 13: Bank account routes
Depends on: Task 11, Task 12
Files:
- Create:
apps/erp/app/routes/x+/supplier+/$supplierId.bank-accounts.tsx - Create:
apps/erp/app/routes/x+/supplier+/$supplierId.bank-accounts.new.tsx - Create:
apps/erp/app/routes/x+/supplier+/$supplierId.bank-accounts.$id.tsx - Create:
apps/erp/app/routes/x+/supplier+/$supplierId.bank-accounts.delete.$id.tsx - Create: the four mirrored
$customerId.bank-accounts*.tsxfiles. - Copy from (precedent):
$supplierId.locations.tsx,$supplierId.locations.new.tsx,$supplierId.processes.$id.tsx,$supplierId.processes.delete.$id.tsx.
Steps:
- List route (
$supplierId.bank-accounts.tsx) — mirror$supplierId.locations.tsxexactly, but:requirePermissions(request, { view: "accounting" })- call
getSupplierBankAccounts(client, supplierId) - on error, redirect to
path.to.supplier(supplierId)with a flash message"Failed to fetch supplier bank accounts" - render
<SupplierBankAccounts bankAccounts={bankAccounts} />
- New route — mirror
$supplierId.locations.new.tsx:requirePermissions(request, { create: "accounting" })- validate with
supplierBankAccountValidator - call
upsertSupplierBankAccount(client, { ...data, supplierId, companyId, createdBy: userId, customFields: setCustomFields(formData) }) - redirect to
path.to.supplierBankAccounts(supplierId)with success flash - the default export renders
SupplierBankAccountFormwithinitialValuesof empty strings,isPrimary: false,active: true, andcountryCode: company?.countryCode ?? "" userIdcomes fromrequirePermissions— confirm the exact destructured name against the precedent file (grep -n "userId" apps/erp/app/routes/x+/supplier+/\$supplierId.processes.new.tsx). IfrequirePermissionsdoes not returnuserId, STOP and report.
- Edit route (
$supplierId.bank-accounts.$id.tsx) — mirror$supplierId.processes.$id.tsx: loadergetSupplierBankAccount, action withupdate: "accounting"callingupsertSupplierBankAccountwithupdatedBy. - Delete route — mirror
$supplierId.processes.delete.$id.tsx:delete: "accounting",deleteSupplierBankAccount,ConfirmDeletereading the row fromuseRouteDataonpath.to.supplierBankAccounts(supplierId). - Mirror all four for customer with
sales→ stillaccountingpermissions (the permission module is accounting on BOTH sides — that is the point of spec decision D2).
Verify:
pnpm exec turbo run typecheck --filter=erp
# Expected: exits 0
ls apps/erp/app/routes/x+/supplier+/ | grep -c bank-accounts
# Expected: 4
ls apps/erp/app/routes/x+/customer+/ | grep -c bank-accounts
# Expected: 4
Out of scope: Do not gate these routes on purchasing or sales — they are accounting-gated by design.
Task 14: Full verification
Depends on: all previous tasks
Steps:
- Typecheck the touched packages:
pnpm exec turbo run typecheck --filter=erp --filter=@carbon/database --filter=@carbon/utils - Lint:
pnpm run lint - Unit tests:
pnpm run test - Dataset / backup pre-commit gates (these read the live schema and are run by
.husky/pre-commit; the migrations are already applied by Task 3):pnpm db:check:datasets pnpm db:check:backups - Confirm no English-only strings were introduced in the new UI files:
grep -rn "label=\"" apps/erp/app/modules/purchasing/ui/Supplier/SupplierBankAccount*.tsx apps/erp/app/modules/sales/ui/Customer/CustomerBankAccount*.tsx apps/erp/app/components/RecordDocuments.tsx # Expected: no output. A raw label="..." string is untranslated; it must be label={t`...`}. - Report results honestly — including any failures — and list what was NOT verified (the app was not launched; no browser testing was done).
Verify: all commands above exit 0, except where a pre-existing failure is present on the branch — in that case report it as pre-existing rather than fixing unrelated code.
Out of scope: Do NOT run git commit. Do not launch the dev server unless the user asks.