* feat(company): ideell förening as a third legal form, behind a flag (#2072 step 1) Why the problem occurred: the legal form was modelled as a binary flag in ~300 files. `EntityType` was a two-member union, but nothing dispatched on it exhaustively: 28 sites defaulted `?? 'enskild_firma'` (invoice, categorize, match, stripe, invoice-inbox) or `?? 'aktiebolag'` (year-end, bokslut, MCP), and every form-dependent choice was an `=== 'aktiebolag' ? A : B` ternary. Widening the union compiled everywhere and changed nothing, so a förening would have booked as an enskild firma in the app and as an aktiebolag in bokslut and MCP, with no error anywhere. The lookup refused föreningar at the door (mapEntityType returned null), which is what the tester hit. What was removed or simplified: the silent defaults. One module, lib/company/entity-type.ts, now holds the list (ENTITY_TYPES), the parser (never defaults), the resolver (settings hint, then companies.entity_type, then throw) and `byEntityType`, whose Record arms make the compiler refuse the next widening until each site has an answer. The form-dependent facts (closing account, owner settlement account, calendar-year lock, default method, K1/K2 label, personnummer vs 16-prefix) live there once instead of in the ternaries. On the SQL side supported_entity_types() replaces four copies of the literal list in the create RPCs. Why this shape and not the proposed one: the tracker asked for the enum widening plus a chart; that alone was the dangerous version (compiles, books wrong). Bundling stiftelse was considered and dropped: identical plumbing but no chart block. Creation sits behind NEXT_PUBLIC_IDEELL_FORENING_ENABLED so the CHECK, RPCs and seed can ship now and the first partner is switched on without a migration; the flag goes when Phase 2 (packs, INK3, årsbokslut, Swish) lands on the tracker. Domain choices (DECISIONS.md 2026-09-08, verify with an accountant before Phase 2): result closes to 2069 with 2068 as prior-year carry; no owner accounts, member settlement on 2890; accrual default; brutet räkenskapsår allowed; K1 label for the 5 000 kr accrual threshold (BFNAR 2010:1); org number gets the 16 prefix. Migration 20260908110835 widens the three CHECK constraints, adds supported_entity_types(), re-creates the three create RPCs with the widened guard and adds the förening block to seed_chart_of_accounts. Applied to staging and covered by ideell-forening-entity-type.pg.test.ts. Part of #2072 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PdGafpUA7jVV1oYjkwfQCh * fix(company): close the förening paths the skeptic refuted (#2072) Five refutations from the /skeptic pass on 7a05c54d2, each fixed at the shared definition rather than the reported site: 1. Privately paid supplier invoices and the utlägg dialog resolved the owner account in lib/expenses/payer.ts with its own AB/EF ternary, so a förening member's invoice was built on 2893 and then refused by the expense-claim service (which already said 2890), burning an ankomstnummer. The helper now uses ownerSettlementAccount. 2. Booking templates substitute their `_ab` accounts only for an aktiebolag; the `private_expense` template kept its base 2013 for a förening. Template accounts now resolve through templateAccountForForm: EF base, AB override, förening base with owner accounts translated to 2890 (booking-templates.ts and proposal-lines.ts share it). 3. A VAT-registered förening with helårsmoms got no momsdeklaration deadline: the annual VAT rule bailed on anything but AB/EF. A förening is a juridisk person and follows the räkenskapsår schedule (SFL 26 kap 33 §), so the rule now keys on fiscalYearLockedToCalendar instead of the two literals; same in the MCP VAT report. 4. 2069 would have accumulated across years: the year-open omföring was AB-only with 2099/2098 hard-coded. planResultAppropriation now takes the pair from resultClosingAccounts (AB 2099 -> 2098, förening 2069 -> 2068) and skips forms with no carry (EF). 5. With the flag off, a registry lookup that returned "Ideell förening" was prefilled into the onboarding journey, the form picker was skipped and the create step answered "Ogiltig företagsform" with no way back. The journey, the BankID picker, the onboarding page and the MCP lookup now use mapSetupEntityType, which maps only creatable forms, so a flagged-off form falls through to the picker as before. Also: form picker keeps its AB-first order; tests for each fix. Part of #2072 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PdGafpUA7jVV1oYjkwfQCh * chore(migrations): move ideell förening migration after main's latest version (20260908143051) Two migrations landed on main after the branch forked; a lower version would be skipped by the merge-time apply. Staging history row renamed to match. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PdGafpUA7jVV1oYjkwfQCh * chore(skills): regenerate accounted-api reference for the widened entity_type enum Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PdGafpUA7jVV1oYjkwfQCh --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
293 lines
11 KiB
TypeScript
293 lines
11 KiB
TypeScript
import { createClient } from '@/lib/supabase/server'
|
|
import { ENTITY_TYPE_LABELS_SV, isEntityType } from '@/lib/company/entity-type'
|
|
import { redirect } from 'next/navigation'
|
|
import { headers } from 'next/headers'
|
|
import { getActiveCompanyId } from '@/lib/company/context'
|
|
import { hasCapability } from '@/lib/entitlements/has-capability'
|
|
import { CAPABILITY } from '@/lib/entitlements/keys'
|
|
import { ensureTicSnapshot } from '@/lib/agent/composer/tic-fetch'
|
|
import { employeeFieldValue, loadActiveEmployeeCount } from '@/lib/agent/composer/employee-facts'
|
|
import AgentOnboarding from '@/components/onboarding/agent/AgentOnboarding'
|
|
|
|
export const dynamic = 'force-dynamic'
|
|
|
|
// /onboarding/agent: Phase A (real-timed build) and Phase B (review) of the
|
|
// specialized accountant agent build sequence.
|
|
//
|
|
// Plan refs: dev_docs/specialized-agent-plan.md §7 (Build-sequence UX),
|
|
// §15 Phase 2 (Build-sequence UX).
|
|
export default async function AgentOnboardingPage() {
|
|
const supabase = await createClient()
|
|
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
if (!user) redirect('/login')
|
|
|
|
const companyId = await getActiveCompanyId(supabase, user.id)
|
|
if (!companyId) redirect('/onboarding')
|
|
|
|
// Paywall: the build flow drives the gated composer stream (ai capability).
|
|
// Send non-payers to billing where the assistant is pitched, instead of a
|
|
// build sequence that 403s mid-stream.
|
|
if (!(await hasCapability(supabase, companyId, CAPABILITY.ai))) {
|
|
redirect('/settings/billing')
|
|
}
|
|
|
|
// Everything that doesn't depend on the TIC snapshot loads in one batch:
|
|
// the settings row (carrying the is_sandbox gate + the onboarding-form
|
|
// data, moms_period, fiscal_year_start_month, f_skatt, city, …, that
|
|
// never makes it onto `companies` proper), the greeting profile, any
|
|
// existing agent profile, and the atom registry titles ("Konsult It"-style
|
|
// slug labels look ugly; the registry has them as authored).
|
|
const [
|
|
{ data: settings },
|
|
{ data: profile },
|
|
{ data: existingProfile },
|
|
{ data: atomRows },
|
|
activeEmployees,
|
|
hdrs,
|
|
] = await Promise.all([
|
|
supabase
|
|
.from('company_settings')
|
|
.select(
|
|
'is_sandbox, city, address_line1, postal_code, f_skatt, vat_registered, moms_period, fiscal_year_start_month, employer_registered, pays_salaries',
|
|
)
|
|
.eq('company_id', companyId)
|
|
.maybeSingle(),
|
|
supabase.from('profiles').select('full_name').eq('id', user.id).single(),
|
|
supabase
|
|
.from('agent_profiles')
|
|
.select('company_id, profile_summary, verified_at')
|
|
.eq('company_id', companyId)
|
|
.maybeSingle(),
|
|
supabase
|
|
.from('agent_atom_registry')
|
|
.select('id, title')
|
|
.eq('is_active', true)
|
|
.is('parent_atom_id', null), // skill titles only; reference children never appear as profile chips
|
|
// The only source that actually knows the headcount. Cheap: a head-only
|
|
// count served by idx_employees_active, and it rides the same batch.
|
|
loadActiveEmployeeCount(supabase, companyId).catch(() => null),
|
|
headers(),
|
|
])
|
|
|
|
// Sandbox companies ship with a pre-built verified agent_profile: the
|
|
// build flow on this page would call TIC and the gated composer stream,
|
|
// both of which 403. Send them back to the dashboard where the demo
|
|
// assistant is already visible via the sheet preview.
|
|
if (settings?.is_sandbox) redirect('/')
|
|
|
|
// Trigger the TIC live-fetch + cache before the field-resolving query
|
|
// below. ensureTicSnapshot is fast on cache-hit (single SELECT) and
|
|
// best-effort on miss: it never throws. Phase A still runs through the
|
|
// streaming endpoint; this just lets the initial Phase B render show the
|
|
// SNI/verksamhetsbeskrivning when the user returns to the page after
|
|
// stream completion.
|
|
const cookieHeader = hdrs.get('cookie') ?? ''
|
|
const host = hdrs.get('host') ?? 'localhost:3000'
|
|
const proto = hdrs.get('x-forwarded-proto') ?? (host.startsWith('localhost') ? 'http' : 'https')
|
|
const origin = `${proto}://${host}`
|
|
// upgradeV1: this is the one place the v2-only sections (statuses,
|
|
// beneficialOwners, payrolls, …) materially drive the composer, and it's
|
|
// a deliberate once-per-company action: safe to spend the TIC calls to
|
|
// bring a pre-v2 snapshot up to date.
|
|
await ensureTicSnapshot({ supabase, companyId, cookieHeader, origin, upgradeV1: true })
|
|
|
|
// Fetch the small handful of fields we render directly into Phase B so the
|
|
// user sees real values (not "Laddar…") the moment the stream finishes.
|
|
// Must run AFTER ensureTicSnapshot, it reads the tic_snapshot that call
|
|
// may have just written.
|
|
const { data: company } = await supabase
|
|
.from('companies')
|
|
.select('name, entity_type, org_number, tic_snapshot')
|
|
.eq('id', companyId)
|
|
.single()
|
|
|
|
if (!company) redirect('/onboarding')
|
|
|
|
const firstName = profile?.full_name?.split(' ')[0] ?? null
|
|
// Pre-render-friendly snapshot of company info: used to seed Phase B fields
|
|
// before the stream completes so the layout doesn't jump.
|
|
const initialFields = buildInitialFields(company, settings, activeEmployees)
|
|
|
|
const atomTitles: Record<string, string> = {}
|
|
for (const row of (atomRows ?? []) as { id: string; title: string }[]) {
|
|
atomTitles[row.id] = row.title
|
|
}
|
|
|
|
return (
|
|
<AgentOnboarding
|
|
companyId={companyId}
|
|
companyName={company.name}
|
|
firstName={firstName}
|
|
initialFields={initialFields}
|
|
atomTitles={atomTitles}
|
|
alreadyVerified={Boolean(existingProfile?.verified_at)}
|
|
existingSummary={existingProfile?.profile_summary ?? null}
|
|
/>
|
|
)
|
|
}
|
|
|
|
interface InitialFields {
|
|
entity_type_label: string
|
|
// Multiple SNI codes: most companies have one but some are
|
|
// multi-vertical (e.g. konsult + lagerförsäljning).
|
|
sni_codes: { code: string; name: string }[]
|
|
// Verksamhetsbeskrivning (purpose) from Bolagsverket via TIC.
|
|
purpose: string | null
|
|
city: string | null
|
|
fiscal_period: string | null
|
|
vat_period: string | null
|
|
f_skatt: string | null
|
|
employees: string | null
|
|
}
|
|
|
|
interface CompanySettingsForPhaseB {
|
|
city: string | null
|
|
address_line1: string | null
|
|
postal_code: string | null
|
|
f_skatt: boolean | null
|
|
vat_registered: boolean | null
|
|
moms_period: string | null
|
|
fiscal_year_start_month: number | null
|
|
// The employer facts that exist on company_settings. This select used to name
|
|
// `employee_count` and `has_employees`, neither of which is a column on this
|
|
// table, so PostgREST rejected the select whole (42703) and every field above
|
|
// arrived null as well.
|
|
employer_registered: boolean | null
|
|
pays_salaries: boolean | null
|
|
}
|
|
|
|
function buildInitialFields(
|
|
company: {
|
|
name: string
|
|
entity_type: string
|
|
org_number: string | null
|
|
tic_snapshot: Record<string, unknown> | null
|
|
},
|
|
settings: CompanySettingsForPhaseB | null,
|
|
activeEmployees: number | null,
|
|
): InitialFields {
|
|
const tic = (company.tic_snapshot ?? null) as Record<string, unknown> | null
|
|
const entityLabel =
|
|
company.entity_type === 'aktiebolag'
|
|
? 'AB'
|
|
: isEntityType(company.entity_type)
|
|
? ENTITY_TYPE_LABELS_SV[company.entity_type]
|
|
: company.entity_type
|
|
|
|
// Tier the resolution: TIC snapshot (if cached) wins because it's the
|
|
// authoritative Bolagsverket data; company_settings is the user-entered
|
|
// fallback from onboarding. Either can be missing.
|
|
let sniCodes: { code: string; name: string }[] = []
|
|
let purpose: string | null = null
|
|
let city: string | null = null
|
|
let fSkatt: string | null = null
|
|
let vatPeriod: string | null = null
|
|
let fiscalPeriod: string | null = null
|
|
let ticEmployeeRange: string | null = null
|
|
|
|
if (tic) {
|
|
const sni = (tic.sniCodes as { code: string; name: string }[] | undefined) ?? []
|
|
if (Array.isArray(sni)) sniCodes = sni
|
|
const tp = tic.purpose
|
|
if (typeof tp === 'string' && tp.trim().length > 0) purpose = tp.trim()
|
|
const addr = tic.address as { city: string | null } | null
|
|
if (addr?.city) city = addr.city
|
|
const reg = tic.registration as { fTax?: boolean } | undefined
|
|
if (reg) fSkatt = reg.fTax ? 'Aktivt' : 'Saknas'
|
|
if (tic.employeeRange) ticEmployeeRange = tic.employeeRange as string
|
|
// v2 caches `fiscalYear` (current fiscal-year configuration) on the
|
|
// snapshot. Prefer it over the user-entered value below so onboarding
|
|
// can show the registered fiscal period from Bolagsverket without
|
|
// making the user re-enter it.
|
|
const ticFiscal = tic.fiscalYear as { startMonthDay?: string | null } | null
|
|
const startMonth = parseTicStartMonth(ticFiscal?.startMonthDay)
|
|
if (startMonth != null) fiscalPeriod = fiscalYearLabel(startMonth)
|
|
}
|
|
|
|
if (settings) {
|
|
if (!city && settings.city) city = settings.city
|
|
if (!fSkatt && settings.f_skatt != null) {
|
|
fSkatt = settings.f_skatt ? 'Aktivt' : 'Saknas'
|
|
}
|
|
if (settings.moms_period) {
|
|
vatPeriod = momsPeriodLabel(settings.moms_period)
|
|
}
|
|
// settings.fiscal_year_start_month is the user-confirmed value: only
|
|
// overwrite the TIC-derived label if the user has explicitly set it
|
|
// (i.e. when there was no TIC fiscalYear AND they entered it manually).
|
|
if (!fiscalPeriod && settings.fiscal_year_start_month != null) {
|
|
fiscalPeriod = fiscalYearLabel(settings.fiscal_year_start_month)
|
|
}
|
|
}
|
|
|
|
// "Anställda": the live headcount if there is one, else Bolagsverket's
|
|
// interval, else the attested employer facts, else null so the row shows its
|
|
// placeholder instead of a number nobody entered. See employee-facts.ts for
|
|
// why pays_salaries === false is not treated as "Nej".
|
|
const employees = employeeFieldValue({
|
|
activeEmployees,
|
|
ticEmployeeRange,
|
|
employerRegistered: settings?.employer_registered ?? null,
|
|
paysSalaries: settings?.pays_salaries ?? null,
|
|
})
|
|
|
|
return {
|
|
entity_type_label: entityLabel,
|
|
sni_codes: sniCodes,
|
|
purpose,
|
|
city,
|
|
fiscal_period: fiscalPeriod,
|
|
vat_period: vatPeriod,
|
|
f_skatt: fSkatt,
|
|
employees,
|
|
}
|
|
}
|
|
|
|
// Parse TIC v2's `startMonthDay` ("MM-DD") into a month number 1-12. Returns
|
|
// null when the field is missing or malformed so the caller falls back to
|
|
// company_settings.fiscal_year_start_month.
|
|
function parseTicStartMonth(value: string | null | undefined): number | null {
|
|
if (!value) return null
|
|
const match = /^(\d{1,2})-\d{1,2}$/.exec(value)
|
|
if (!match) return null
|
|
const month = Number(match[1])
|
|
if (!Number.isInteger(month) || month < 1 || month > 12) return null
|
|
return month
|
|
}
|
|
|
|
function momsPeriodLabel(period: string): string {
|
|
switch (period) {
|
|
case 'monthly':
|
|
return 'Månadsmoms'
|
|
case 'quarterly':
|
|
return 'Kvartalsmoms'
|
|
case 'yearly':
|
|
return 'Årsmoms'
|
|
default:
|
|
return period
|
|
}
|
|
}
|
|
|
|
// "fiscal_year_start_month=1" → "januari-december".
|
|
function fiscalYearLabel(startMonth: number): string {
|
|
const months = [
|
|
'januari',
|
|
'februari',
|
|
'mars',
|
|
'april',
|
|
'maj',
|
|
'juni',
|
|
'juli',
|
|
'augusti',
|
|
'september',
|
|
'oktober',
|
|
'november',
|
|
'december',
|
|
]
|
|
if (startMonth < 1 || startMonth > 12) return ''
|
|
const startIdx = startMonth - 1
|
|
const endIdx = (startIdx + 11) % 12
|
|
return `${months[startIdx]}-${months[endIdx]}`
|
|
}
|