feat: multi-tenant company refactor (GNU-19) (#153)

* feat: multi-tenant company refactor (GNU-19)

Introduce companies table, company_members, and user_preferences to
support multiple companies per user. All data scoping changes from
user_id to company_id across the entire codebase.

Key changes:
- Database migration: new tables, company_id on 40+ tables, backfill,
  RLS rewrite from user_id to company-member-based, updated RPCs
- Types: Company, CompanyMember, CompanyRole, UserPreferences types;
  company_id added to all entity interfaces; companyId on all events
- Engine: all 7 core functions take companyId; storno, period, year-end
  services updated; 16 report generators updated
- Middleware: company context resolution (cookie → prefs → first company)
- API routes: ~120 routes updated with requireCompanyId()
- Frontend: CompanyProvider context, layout/dashboard/onboarding updated
- Extensions: context factory, 9 extensions, all lib files updated
- Tests: 1880 tests passing, all helpers updated with company_id defaults

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* feat: add database migrations for multi-tenant company and team system (GNU-19)

Adds company_invitations, company creation RPC, team_members, account
deletion RPC, and teams table refactor migrations. Updates base
multi-tenant migration with cascading FKs and onboarding_step column.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* feat: add team types and update core infrastructure for multi-tenancy (GNU-19)

Adds TeamRole, MemberSource, and Team types. Refactors Supabase service
client to be stateless, updates middleware for team-aware routing, extends
CompanyContext with team/role fields, and updates extension service types
to accept companyId.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* refactor: thread company_id through business logic functions (GNU-19)

Replaces user_id scoping with company_id across all lib modules:
bookkeeping, documents, transactions, invoices, reconciliation, tax,
deadlines, and import. Updates corresponding tests.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* refactor: thread company_id through API routes and extensions (GNU-19)

Updates all existing API routes to extract and pass companyId. Updates
enable-banking and arcim-migration extensions for company-scoped
transaction ingestion and sync.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* feat: add company and team management API routes (GNU-19)

Adds CRUD endpoints for company members, company invitations, team
members, and team invitations. Includes invite token utilities, email
templates, and company switch server action.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* feat: add team/company UI components, pages, and dashboard updates (GNU-19)

Adds CompanySwitcher, ConsultantEmptyState, Step0RoleChoice, company
members and team management panels. Updates dashboard layout for
team-aware routing, onboarding for multi-step role choice, and auth
callback for team invite acceptance. Ignores supabase/.branches/.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: add null guards for company in import page (GNU-19)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: move appUrl declaration to outer scope in invite route (GNU-19)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: add optional chaining for company.name in members section (GNU-19)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: add optional chaining for second company.name in members section (GNU-19)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: add null guards for company in extension components (GNU-19)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: pass companyId to executeSIEImport in arcim-migration extension (GNU-19)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: update tests to use companyId instead of userId and improve type handling

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Mattsson
2026-03-31 16:41:52 +02:00
committed by GitHub
co-authored by Claude Opus 4.6
parent 1756af6c5e
commit 0dd1f5ebc1
290 changed files with 7690 additions and 1339 deletions
+45
View File
@@ -18,6 +18,8 @@ import type {
CompanySettings,
InvoiceInboxItem,
CategorizationTemplate,
Company,
CompanyMember,
} from '@/types'
import type { SIEVoucher, SIETransactionLine } from '@/lib/import/types'
@@ -99,10 +101,41 @@ export function createMockSupabase() {
let _counter = 0
const nextId = () => `test-${++_counter}`
export function makeCompany(overrides: Partial<Company> = {}): Company {
const { team_id = null, ...rest } = overrides
return {
id: 'company-1',
name: 'Test Company',
org_number: null,
entity_type: 'enskild_firma',
created_by: 'user-1',
team_id,
archived_at: null,
created_at: '2024-01-01T00:00:00Z',
updated_at: '2024-01-01T00:00:00Z',
...rest,
}
}
export function makeCompanyMember(overrides: Partial<CompanyMember> = {}): CompanyMember {
return {
id: 'member-1',
company_id: 'company-1',
user_id: 'user-1',
role: 'owner',
invited_by: null,
joined_at: '2024-01-01T00:00:00Z',
created_at: '2024-01-01T00:00:00Z',
updated_at: '2024-01-01T00:00:00Z',
...overrides,
}
}
export function makeReceipt(overrides: Partial<Receipt> = {}): Receipt {
return {
id: nextId(),
user_id: 'user-1',
company_id: 'company-1',
image_url: 'https://example.com/receipt.jpg',
image_thumbnail_url: null,
status: 'confirmed',
@@ -136,6 +169,7 @@ export function makeTransaction(overrides: Partial<Transaction> = {}): Transacti
return {
id: nextId(),
user_id: 'user-1',
company_id: 'company-1',
bank_connection_id: null,
external_id: null,
date: '2024-06-15',
@@ -169,6 +203,7 @@ export function makeFiscalPeriod(overrides: Partial<FiscalPeriod> = {}): FiscalP
return {
id: nextId(),
user_id: 'user-1',
company_id: 'company-1',
name: 'FY 2024',
period_start: '2024-01-01',
period_end: '2024-12-31',
@@ -190,6 +225,7 @@ export function makeJournalEntry(overrides: Partial<JournalEntry> = {}): Journal
return {
id: nextId(),
user_id: 'user-1',
company_id: 'company-1',
fiscal_period_id: 'period-1',
voucher_number: 1,
voucher_series: 'A',
@@ -238,6 +274,7 @@ export function makeDocumentAttachment(
return {
id: nextId(),
user_id: 'user-1',
company_id: 'company-1',
storage_path: 'documents/user-1/file.pdf',
file_name: 'file.pdf',
file_size_bytes: 1024,
@@ -286,6 +323,7 @@ export function makeInvoice(overrides: Partial<Invoice> = {}): Invoice {
return {
id: nextId(),
user_id: 'user-1',
company_id: 'company-1',
customer_id: 'customer-1',
invoice_number: 'F-2024001',
invoice_date: '2024-06-15',
@@ -325,6 +363,7 @@ export function makeInvoicePayment(
return {
id: nextId(),
user_id: 'user-1',
company_id: 'company-1',
invoice_id: 'invoice-1',
payment_date: '2024-07-01',
amount: 12500,
@@ -343,6 +382,7 @@ export function makeCustomer(overrides: Partial<Customer> = {}): Customer {
return {
id: nextId(),
user_id: 'user-1',
company_id: 'company-1',
name: 'Test AB',
customer_type: 'swedish_business',
email: 'kontakt@test.se',
@@ -368,6 +408,7 @@ export function makeSupplier(overrides: Partial<Supplier> = {}): Supplier {
return {
id: nextId(),
user_id: 'user-1',
company_id: 'company-1',
name: 'Leverantör AB',
supplier_type: 'swedish_business',
email: 'info@leverantor.se',
@@ -400,6 +441,7 @@ export function makeSupplierInvoice(
return {
id: nextId(),
user_id: 'user-1',
company_id: 'company-1',
supplier_id: 'supplier-1',
arrival_number: 1,
supplier_invoice_number: 'LF-001',
@@ -442,6 +484,7 @@ export function makeCompanySettings(
return {
id: nextId(),
user_id: 'user-1',
company_id: 'company-1',
entity_type: 'enskild_firma',
company_name: 'Test Firma',
org_number: '199001011234',
@@ -486,6 +529,7 @@ export function makeInvoiceInboxItem(
return {
id: nextId(),
user_id: 'user-1',
company_id: 'company-1',
status: 'pending',
source: 'upload',
email_from: null,
@@ -654,6 +698,7 @@ export function makeCategorizationTemplate(
return {
id: nextId(),
user_id: 'user-1',
company_id: 'company-1',
counterparty_name: 'telia',
counterparty_aliases: ['telia sverige ab'],
debit_account: '6200',