* feat(onboarding): company setup from the conversation and POST /api/v1/companies Third PR of agent-first onboarding (#1814). Once connected, the agent can now set up a company end to end without the web wizard, and partner platforms can provision companies over REST. - create_company_for_user: service-role-only SECURITY DEFINER twin of create_company_with_owner taking the owner explicitly (service clients have no auth.uid()). pg-real test covers creation, role gating, unknown owner and foreign team. - lib/company/create-company.ts: the wizard's creation sequence (org number, TIC snapshot, BAS chart, settings, first fiscal period, tax deadlines, rollback) extracted into createCompanyCore; the Server Action delegates to it, behaviour unchanged. - lib/company/onboarding-input.ts: one Zod schema + planner for the agent/API paths; a VAT-registered company without moms_period is refused (a missing period silently yields zero VAT deadlines). - MCP: gnubok_create_company (two-phase: preview, then confirm=true; companies:write, company-independent), gnubok_connect_bank and gnubok_connect_skatteverket (status + the browser link, gated on bank_sync / skatteverket, search-only in the catalog), the "onboarding" skill, and initialize instructions pointing at it. - Consent page pre-ticks companies:write for an account with no company yet, so the setup does not dead-end on insufficient scope after signup. - POST /api/v1/companies (companies:write, dry-run aware) on the same core; scope map, registry, spec snapshot and the generated API skill updated. - tools/list payload ceiling raised 59.95K -> 60.4K for the one new default-catalog tool (documented in the guard). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018wCdzRTatKiDByKB8hCNT6 * fix(onboarding): explicit f_skatt, org number when VAT-registered, EF first year ends 31 Dec Review findings on #1864 (Swedish compliance review): - f_skatt is required, never defaulted to approved (SE-R-005 risk). - org_number is required when vat_registered: the invoice momsregistreringsnummer derives from it (ML 17 kap 24 §). - An enskild firma's first fiscal year must end on 31 December and its start month is forced to 1 even with first_fiscal_year set, mirroring the wizard's own rule text (BFL 3 kap. 1 §). - POST /api/v1/companies no longer claims Idempotency-Key support (the wrapper only honours it on company-scoped routes). - pg-real: createCompanyCore's chart seed runs under the real service_role, which the unit tests could not prove. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018wCdzRTatKiDByKB8hCNT6 * test(pg): starter chart has 41 accounts, assert non-empty The service_role chart-seed proof passed the part that mattered (no 42501 from seed_chart_of_accounts) and failed on a wrong row-count guess: the seeded chart is a curated starter set, not the full BAS list. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018wCdzRTatKiDByKB8hCNT6 * fix(migrations): move create_company_for_user to 20260825120000 main gained 20260824170000_bulk_book_transactions_service_actor.sql with the same version while this branch was open; two files on one version abort every Supabase branch apply and the prod auto-apply. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018wCdzRTatKiDByKB8hCNT6 * chore(api): refresh spec snapshot and generated skill after rebasing onto main Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018wCdzRTatKiDByKB8hCNT6 * fix(mcp): flat create_company result, refuse localhost connect links, test hygiene CodeRabbit on #1864: the confirmed-create result was wrapped in the { data, next } envelope while its outputSchema promised top-level fields; it now returns the fields with next as a sibling. The two connect-link tools refuse to build a link when NEXT_PUBLIC_APP_URL is unset instead of handing a remote user a localhost URL. Tests clear mocks and the event bus in beforeEach. Not changed: the rollback already survives user_preferences.active_company_id (that FK is ON DELETE SET NULL since 20260331010000), and v1 error details stay in the surface's English developer convention. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018wCdzRTatKiDByKB8hCNT6 --------- Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
166 lines
6.0 KiB
TypeScript
166 lines
6.0 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { CompanySetupSchema, planCompanySetup } from '../onboarding-input'
|
|
|
|
const base = {
|
|
name: 'Testbolaget AB',
|
|
entity_type: 'aktiebolag' as const,
|
|
org_number: '5560000001',
|
|
vat_registered: true,
|
|
moms_period: 'quarterly' as const,
|
|
accounting_method: 'accrual' as const,
|
|
f_skatt: true,
|
|
}
|
|
|
|
describe('CompanySetupSchema', () => {
|
|
it('accepts a complete aktiebolag setup', () => {
|
|
expect(CompanySetupSchema.safeParse(base).success).toBe(true)
|
|
})
|
|
|
|
it('refuses a VAT-registered company without a moms_period (silent zero-deadline trap)', () => {
|
|
const result = CompanySetupSchema.safeParse({ ...base, moms_period: undefined })
|
|
expect(result.success).toBe(false)
|
|
if (!result.success) {
|
|
expect(result.error.issues.some((i) => i.path.join('.') === 'moms_period')).toBe(true)
|
|
}
|
|
})
|
|
|
|
it('refuses a moms_period on a company that is not VAT-registered', () => {
|
|
const result = CompanySetupSchema.safeParse({ ...base, vat_registered: false })
|
|
expect(result.success).toBe(false)
|
|
})
|
|
|
|
it('accepts a non-VAT company with no moms_period', () => {
|
|
const result = CompanySetupSchema.safeParse({ ...base, vat_registered: false, moms_period: undefined })
|
|
expect(result.success).toBe(true)
|
|
})
|
|
|
|
it('refuses a VAT-registered company without an org number (invoice momsregistreringsnummer)', () => {
|
|
const result = CompanySetupSchema.safeParse({ ...base, org_number: undefined })
|
|
expect(result.success).toBe(false)
|
|
if (!result.success) {
|
|
expect(result.error.issues.some((i) => i.path.join('.') === 'org_number')).toBe(true)
|
|
}
|
|
})
|
|
|
|
it('accepts a non-VAT company without an org number', () => {
|
|
const result = CompanySetupSchema.safeParse({
|
|
...base,
|
|
org_number: undefined,
|
|
vat_registered: false,
|
|
moms_period: undefined,
|
|
})
|
|
expect(result.success).toBe(true)
|
|
})
|
|
|
|
it('refuses an omitted f_skatt: F-skatt approval is never assumed', () => {
|
|
const result = CompanySetupSchema.safeParse({ ...base, f_skatt: undefined })
|
|
expect(result.success).toBe(false)
|
|
})
|
|
|
|
it('refuses an enskild firma first fiscal year that does not end on 31 December', () => {
|
|
const result = CompanySetupSchema.safeParse({
|
|
...base,
|
|
entity_type: 'enskild_firma',
|
|
first_fiscal_year: { start: '2026-03-15', end: '2027-06-30' },
|
|
})
|
|
expect(result.success).toBe(false)
|
|
if (!result.success) {
|
|
expect(result.error.issues.some((i) => i.path.join('.') === 'first_fiscal_year.end')).toBe(true)
|
|
}
|
|
})
|
|
|
|
it('refuses a malformed organisationsnummer', () => {
|
|
const result = CompanySetupSchema.safeParse({ ...base, org_number: '1234' })
|
|
expect(result.success).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('planCompanySetup', () => {
|
|
it('produces the wizard-shaped settings and a calendar-year period by default', () => {
|
|
const plan = planCompanySetup(CompanySetupSchema.parse(base))
|
|
expect(plan.ok).toBe(true)
|
|
if (!plan.ok) return
|
|
const year = new Date().getFullYear()
|
|
expect(plan.fiscalPeriod.startDate).toBe(`${year}-01-01`)
|
|
expect(plan.fiscalPeriod.endDate).toBe(`${year}-12-31`)
|
|
expect(plan.input.settings).toMatchObject({
|
|
entity_type: 'aktiebolag',
|
|
company_name: 'Testbolaget AB',
|
|
org_number: '5560000001',
|
|
vat_registered: true,
|
|
vat_number: 'SE556000000101',
|
|
moms_period: 'quarterly',
|
|
accounting_method: 'accrual',
|
|
f_skatt: true,
|
|
fiscal_year_start_month: 1,
|
|
is_first_fiscal_year: false,
|
|
})
|
|
})
|
|
|
|
it('forces enskild firma onto the calendar year regardless of the requested start month', () => {
|
|
const plan = planCompanySetup(
|
|
CompanySetupSchema.parse({ ...base, entity_type: 'enskild_firma', fiscal_year_start_month: 7 })
|
|
)
|
|
expect(plan.ok).toBe(true)
|
|
if (!plan.ok) return
|
|
expect(plan.input.settings.fiscal_year_start_month).toBe(1)
|
|
expect(plan.fiscalPeriod.startDate.endsWith('-01-01')).toBe(true)
|
|
})
|
|
|
|
it('uses the first fiscal year dates and derives the start month from its end', () => {
|
|
const plan = planCompanySetup(
|
|
CompanySetupSchema.parse({
|
|
...base,
|
|
first_fiscal_year: { start: '2026-03-15', end: '2027-06-30' },
|
|
})
|
|
)
|
|
expect(plan.ok).toBe(true)
|
|
if (!plan.ok) return
|
|
expect(plan.fiscalPeriod).toMatchObject({ startDate: '2026-03-15', endDate: '2027-06-30' })
|
|
expect(plan.fiscalPeriod.name).toContain('Första räkenskapsåret')
|
|
expect(plan.input.settings.fiscal_year_start_month).toBe(7)
|
|
expect(plan.input.settings.is_first_fiscal_year).toBe(true)
|
|
})
|
|
|
|
it('keeps an enskild firma on the calendar year through a first fiscal year ending 31 December', () => {
|
|
const plan = planCompanySetup(
|
|
CompanySetupSchema.parse({
|
|
...base,
|
|
entity_type: 'enskild_firma',
|
|
first_fiscal_year: { start: '2026-03-15', end: '2026-12-31' },
|
|
})
|
|
)
|
|
expect(plan.ok).toBe(true)
|
|
if (!plan.ok) return
|
|
expect(plan.fiscalPeriod).toMatchObject({ startDate: '2026-03-15', endDate: '2026-12-31' })
|
|
expect(plan.input.settings.fiscal_year_start_month).toBe(1)
|
|
})
|
|
|
|
it('carries f_skatt=false through instead of defaulting to approved', () => {
|
|
const plan = planCompanySetup(CompanySetupSchema.parse({ ...base, f_skatt: false }))
|
|
expect(plan.ok).toBe(true)
|
|
if (!plan.ok) return
|
|
expect(plan.input.settings.f_skatt).toBe(false)
|
|
})
|
|
|
|
it('rejects a first fiscal year longer than 18 months', () => {
|
|
const plan = planCompanySetup(
|
|
CompanySetupSchema.parse({
|
|
...base,
|
|
first_fiscal_year: { start: '2026-01-01', end: '2027-12-31' },
|
|
})
|
|
)
|
|
expect(plan.ok).toBe(false)
|
|
})
|
|
|
|
it('leaves vat_number null and moms_period null for a non-VAT company', () => {
|
|
const plan = planCompanySetup(
|
|
CompanySetupSchema.parse({ ...base, vat_registered: false, moms_period: undefined })
|
|
)
|
|
expect(plan.ok).toBe(true)
|
|
if (!plan.ok) return
|
|
expect(plan.input.settings.vat_number).toBeNull()
|
|
expect(plan.input.settings.moms_period).toBeNull()
|
|
})
|
|
})
|