31e0cd6e05
* 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>
154 lines
6.3 KiB
TypeScript
154 lines
6.3 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import type { PoolClient } from 'pg'
|
|
import { randomUUID } from 'node:crypto'
|
|
import { getClient, getPool, runAsServiceRole } from './setup'
|
|
import { insertAuthUser } from './fixtures'
|
|
|
|
/**
|
|
* create_company_for_user (migration 20260825120000): the service-role twin
|
|
* of create_company_with_owner used by the MCP tool gnubok_create_company and
|
|
* POST /api/v1/companies (issue #1814 PR 3).
|
|
*
|
|
* Locks in:
|
|
* - service role creates the company, owner membership, 1930 cash account
|
|
* and active-company preference for the explicit owner, and the trial
|
|
* grant trigger fires for it like for every other creation path
|
|
* - authenticated and anon callers are refused outright (42501): the
|
|
* function takes the owner as a plain argument, so exposing it to
|
|
* PostgREST roles would let anyone create companies for anyone
|
|
* - an unknown owner is refused (23503) and a foreign team is refused (42501)
|
|
*/
|
|
|
|
async function asRole<T>(
|
|
role: 'authenticated' | 'anon',
|
|
userId: string | null,
|
|
fn: (client: PoolClient) => Promise<T>,
|
|
): Promise<T> {
|
|
const client = await getClient()
|
|
try {
|
|
await client.query('BEGIN')
|
|
await client.query(`SELECT set_config('request.jwt.claims', $1, true)`, [
|
|
JSON.stringify(userId ? { sub: userId, role } : { role }),
|
|
])
|
|
await client.query(`SELECT set_config('request.jwt.claim.sub', $1, true)`, [userId ?? ''])
|
|
await client.query(`SELECT set_config('request.jwt.claim.role', $1, true)`, [role])
|
|
await client.query(`SET LOCAL ROLE ${role}`)
|
|
const result = await fn(client)
|
|
await client.query('COMMIT')
|
|
return result
|
|
} catch (error) {
|
|
await client.query('ROLLBACK').catch(() => {})
|
|
throw error
|
|
} finally {
|
|
client.release()
|
|
}
|
|
}
|
|
|
|
const CREATE = `SELECT public.create_company_for_user($1::uuid, $2, $3, $4::uuid) AS id`
|
|
|
|
describe('create_company_for_user.pg', () => {
|
|
it('creates company, owner membership, cash account and preference for the explicit owner', async () => {
|
|
const userId = await insertAuthUser()
|
|
const name = `Provisioned AB ${randomUUID().slice(0, 8)}`
|
|
|
|
const created = await getPool().query<{ id: string }>(CREATE, [userId, name, 'aktiebolag', null])
|
|
const companyId = created.rows[0]!.id
|
|
expect(companyId).toMatch(/^[0-9a-f-]{36}$/)
|
|
|
|
const company = await getPool().query(
|
|
`SELECT name, entity_type, created_by, team_id FROM public.companies WHERE id = $1`,
|
|
[companyId],
|
|
)
|
|
expect(company.rows[0]).toMatchObject({ name, entity_type: 'aktiebolag', created_by: userId, team_id: null })
|
|
|
|
const member = await getPool().query(
|
|
`SELECT role FROM public.company_members WHERE company_id = $1 AND user_id = $2`,
|
|
[companyId, userId],
|
|
)
|
|
expect(member.rows[0]).toMatchObject({ role: 'owner' })
|
|
|
|
const cash = await getPool().query(
|
|
`SELECT ledger_account, is_primary FROM public.cash_accounts WHERE company_id = $1`,
|
|
[companyId],
|
|
)
|
|
expect(cash.rows).toEqual([{ ledger_account: '1930', is_primary: true }])
|
|
|
|
const prefs = await getPool().query(
|
|
`SELECT active_company_id FROM public.user_preferences WHERE user_id = $1`,
|
|
[userId],
|
|
)
|
|
expect(prefs.rows[0]).toMatchObject({ active_company_id: companyId })
|
|
|
|
// The trial trigger on companies covers this path like every other one.
|
|
const grants = await getPool().query(
|
|
`SELECT count(*)::int AS n FROM public.capability_grants WHERE company_id = $1 AND source = 'trial'`,
|
|
[companyId],
|
|
)
|
|
expect(grants.rows[0]!.n).toBeGreaterThan(0)
|
|
})
|
|
|
|
it('runs the whole creation core under the real service_role, including the BAS chart seed', async () => {
|
|
// The MCP tool and POST /api/v1/companies run createCompanyCore with a
|
|
// service-role client. seed_chart_of_accounts is SECURITY DEFINER with a
|
|
// grant to `authenticated` only; this pins that service_role (PUBLIC
|
|
// execute, no REVOKE) can still call it, which unit tests cannot see.
|
|
const userId = await insertAuthUser()
|
|
const companyId = await runAsServiceRole(async (client) => {
|
|
const created = await client.query<{ id: string }>(CREATE, [userId, 'Service AB', 'aktiebolag', null])
|
|
const id = created.rows[0]!.id
|
|
await client.query(`SELECT public.seed_chart_of_accounts($1::uuid, 'aktiebolag')`, [id])
|
|
return id
|
|
})
|
|
const chart = await getPool().query<{ n: number }>(
|
|
`SELECT count(*)::int AS n FROM public.chart_of_accounts WHERE company_id = $1`,
|
|
[companyId],
|
|
)
|
|
// The starter chart is a curated subset (41 accounts on CI), not the full BAS list.
|
|
expect(chart.rows[0]!.n).toBeGreaterThan(0)
|
|
})
|
|
|
|
it('refuses an authenticated caller even for their own user id', async () => {
|
|
const userId = await insertAuthUser()
|
|
await expect(
|
|
asRole('authenticated', userId, async (client) => {
|
|
await client.query(CREATE, [userId, 'Sneaky AB', 'aktiebolag', null])
|
|
}),
|
|
).rejects.toMatchObject({ code: '42501' })
|
|
})
|
|
|
|
it('refuses an anon caller', async () => {
|
|
const userId = await insertAuthUser()
|
|
await expect(
|
|
asRole('anon', null, async (client) => {
|
|
await client.query(CREATE, [userId, 'Sneaky AB', 'aktiebolag', null])
|
|
}),
|
|
).rejects.toMatchObject({ code: '42501' })
|
|
})
|
|
|
|
it('refuses an unknown owner', async () => {
|
|
await expect(
|
|
getPool().query(CREATE, [randomUUID(), 'Ghost AB', 'aktiebolag', null]),
|
|
).rejects.toMatchObject({ code: '23503' })
|
|
})
|
|
|
|
it('refuses a team the owner is not a member of', async () => {
|
|
const owner = await insertAuthUser()
|
|
const other = await insertAuthUser()
|
|
const teamId = randomUUID()
|
|
await getPool().query(`INSERT INTO public.teams (id, name, created_by) VALUES ($1, 'Other', $2)`, [teamId, other])
|
|
await getPool().query(
|
|
`INSERT INTO public.team_members (team_id, user_id, role) VALUES ($1, $2, 'owner')`,
|
|
[teamId, other],
|
|
)
|
|
await expect(
|
|
getPool().query(CREATE, [owner, 'Wrong Team AB', 'aktiebolag', teamId]),
|
|
).rejects.toMatchObject({ code: '42501' })
|
|
})
|
|
|
|
it('rejects an unsupported entity type and an empty name', async () => {
|
|
const userId = await insertAuthUser()
|
|
await expect(getPool().query(CREATE, [userId, 'X HB', 'handelsbolag', null])).rejects.toThrow(/Invalid entity_type/)
|
|
await expect(getPool().query(CREATE, [userId, ' ', 'aktiebolag', null])).rejects.toThrow(/p_name is required/)
|
|
})
|
|
})
|