* fix(security): gate replace_sie_import behind owner/admin membership The RPC was SECURITY DEFINER with EXECUTE granted to PUBLIC and anon, no company_members lookup, no auth.uid() reference and no unauthorized raise, while setting gnubok.allow_delete to disarm the BFL immutability and retention triggers. Any caller holding a company_id and an import id could hard delete another tenant's verifikationer. Confirmed live in production. Applies the same fail closed owner/admin guard that undo_sie_import already carries (migration 20260624120000), resolving the actor from COALESCE(p_user_id, auth.uid()) so it denies when the role is NULL, then revokes EXECUTE from PUBLIC and anon. search_path and the raised statement_timeout are restated, since CREATE OR REPLACE drops settings that are not repeated. userId is a required parameter on replaceSIEImport: the service client has a NULL auth.uid(), so a caller without an explicit actor now fails to compile rather than hitting the closed gate at runtime. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(security): validate arcim OAuth callback state server side The callback route is skipAuth and decoded the state parameter as plain base64url JSON, trusting consentId and provider from it. A one time code was minted at flow start and never read. An unauthenticated attacker who learned a consent id could run an OAuth flow on their own provider account and post the callback with a forged state, landing their tokens on another tenant's consent, so the victim's next migration imported the attacker's ledger. State is now an opaque randomBytes(32) pointer to a provider_otc row, consumed by a single atomic UPDATE guarded on used_at IS NULL and expires_at, so a replay loses the row lock race and updates nothing. provider is read from provider_consents rather than trusted from the client. provider_otc already existed for exactly this purpose and was never wired up. Also scopes getConsent to an owning company, closing a cross tenant status oracle where the preview and migrate paths echoed a consent's status before the scoped check ran. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(security): scope documents storage to company_id (phase A) The documents bucket policies matched on auth.uid(), and upload keys were documents/{userId}/..., so company membership was never consulted. Removing a member revoked nothing: their session still authenticated and they kept direct Storage read access to every receipt, supplier invoice and bank statement they had uploaded. The same bug was fixed for sie-files in 20260416120000; this bucket was left behind. Phase A is additive. Company scoped policies are added alongside the uploader scoped ones, uploads move to documents/{companyId}/{userId}/..., and reads accept either layout so nothing breaks mid migration. Phase C, which drops the old policies, is gated on the backfill reporting zero remaining legacy prefix objects. The policy compares the company segment as text rather than casting to uuid the way sie-files does: this bucket holds keys whose second segment is not a uuid (MCP audit packages), and Postgres does not guarantee the bucket prefix qual runs before the cast, so a planner reordering would raise 22P02 and fail the whole query instead of filtering the row out. deleteDocument now removes both candidate keys. Removing only the stored pointer would leave a readable orphan copy of a document the user asked to erase. The backfill script is included but has never been run. It defaults to dry run, refuses .env.local by name, and verifies each copy is readable and SHA-256 identical before repointing the row. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(security): enforce events:read scope and membership on /api/events This was the only one of the three validateApiKey call sites with no downstream guard: v1 and the MCP server both check scope and re-verify company membership, this route did neither. An events:read scope existed and was documented as gating the endpoint but was never called, so a legacy key falling back to DEFAULT_SCOPES read the full log. The bound company id went straight from the api_keys row into a service role query, so a key whose user had been removed from the company kept reading. Adds the scope check before any database access, re-verifies company_members with archived_at IS NULL, honours test mode by stamping X-Gnubok-Mode instead of ignoring it, applies minimisePayload so the pull surface can never return a wider payload than the push surface, and replaces the three flat error strings with the canonical envelope. Test key reads are served rather than blocked: TEST_KEY_WRITE_BLOCKED is gated on mutations in with-api-v1, so a read gets the same treatment as every other v1 read endpoint. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * perf(bookkeeping): sweep remaining journal_entries!inner embeds A previous refactor removed this pattern from lib/reports and introduced fetchEntryLines, but the class was never swept. Seventeen sites remained and had become the top application consumer of production database time: measured across the resulting query shapes, 32,694 calls and 25,848 seconds of execution, mean 790ms, with shapes averaging 2.6s and 3.0s and maxing at 7,962ms against the 8s statement_timeout, which surfaced to users as 500s on the booking path. PostgREST compiles an embed with filters on the embedded side into a correlated INNER JOIN LATERAL with a parameterized LIMIT, which stops Postgres reordering the join, so each query walked the whole journal_entry_lines table across all tenants. Driving from the entries side instead turns that into two indexed round trips. Converted sites keep their existing shape: the helper reattaches the parent entry under the same key the embed produced. Several conversions also remove a latent silent truncation where an unpaginated query was capped at PostgREST's 1000 row ceiling. Two deliberate exceptions. The free text ilike legs of the MCP display query stay on the embed, because each is capped at legLimit and that cap drives the truncation contract the tool reports, while the helper is unbounded. The accounts route moves to the existing get_account_usage_counts RPC instead, since its embed was a head count and the helper returns rows. commitEntry's write path is untouched: the change there is confined to the read query of the pre-commit dimension rule check. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(api): anchor v1 list cursors on created_at Page two returned page one, forever, while still advertising a fresh next_cursor. The three routes sorted by and encoded a Postgres date column, which serializes as YYYY-MM-DD, but decodeDefaultCursor validates the cursor timestamp as full ISO-8601 and returned null, so the keyset filter was never applied and has_more never went false. An integrator syncing verifikat looped on the newest rows indefinitely. The transactions route already solved this and its comment names the trap; the fix was never ported. All three now order and encode on created_at with an id tie break, matching the transactions keyset predicate exactly. ISO_TIMESTAMP is deliberately left alone: relaxing it would silently change sort semantics on the route that currently works. Default ordering therefore moves from business date to insert order. Every business date is still on the row, and the invoices list gains date_from and date_to filters so a date range is still reachable; the other two already had them. The tests use an in-memory PostgREST that actually evaluates the filters, because the repo's pass-through mock cannot catch this class of bug: the bug is that the filter is never sent. They walk to exhaustion with a hard iteration cap, so an unterminated walk fails instead of hanging. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(api): separate dry run from commit in the idempotency hash The request hash was built from url.pathname, which excludes the query string, so a dry run and its commit hashed identically. Following the flow documented in dry-run.ts, re-issuing the request with the same Idempotency-Key returned the cached preview with Idempotent-Replayed set and wrote nothing, while reporting 200. An agent or integrator saw success for a write that never happened. dry_run is folded into the hash only when true, not as an unconditional boolean. Including it as false would change the hash of every ordinary write, and with a 24h idempotency TTL any key in flight across the deploy would fail the request_hash comparison and 409 on a legitimate retry. Both hash call sites now go through one shared helper so they cannot drift into a permanent cache miss, and dry run responses are no longer stored at all. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * ci: install the Bedrock SDK out of tree in the compliance review The Swedish accounting compliance gate had failed ten consecutive runs and so was posting nothing. With --no-package-lock npm discarded the lockfile and re-resolved the whole tree from package.json, floating @hookform/resolvers to 5.4.3, whose valibot ^1 peer conflicts with the pinned valibot 0.39.0. Installing into the parent of the checkout resolves only that one package, so an unrelated peer conflict can never take the gate down again. Node still finds it because ESM bare specifiers walk up parent node_modules; NODE_PATH would not have worked, as it is CommonJS only. --legacy-peer-deps was rejected because it masks future genuine peer conflicts and still reifies the full tree. The same step's SDK version is aligned from 0.31.0 back to the 0.29.1 that package.json and check:guards enforce after the streaming outage. That drift went unnoticed because the pin guard only inspects package.json and the lockfile, never workflow files. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * build(docker): generate crontabs from vercel.json vercel.json defines 16 cron jobs; both Docker crontabs carried 9, and were byte identical to each other. Self hosted deployments therefore never sent recurring invoices, never dispatched webhooks and never cleaned up idempotency keys. tax-deadlines also ran once a year on 2 January instead of daily, and documents/verify weekly instead of daily. Extension crons are included rather than excluded. The Dockerfile copies the whole tree before building, so every extension cron route is compiled into the image regardless of the enabled preset, and each returns 200 when its extension is unconfigured, so curl -sf logs no failure. Two such entries were already present in the crontab for extensions absent from the preset, which settles the intent. documents/verify is treated as drift rather than a self hosted concession: the weekly cadence was present in the hosted crontab too, and the run is capped at 200 documents walking a nulls-first queue, so weekly drains the integrity queue seven times slower on a check that exists for BFL retention. webhooks/dispatch keeps its per minute cadence, adding 1,440 requests a day on self hosted. A gentler tick would silently stretch the first retry, since the retry ladder opens at 60 seconds. SCHEDULE_OVERRIDES is the one line place to change that. A parity test asserts the path sets match minus a documented exclusion list, and ratchets three cron routes that are currently scheduled nowhere so they are named rather than silently rotting. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * chore(observability): add a provider agnostic error sink There is no error tracking in this codebase: logs go to console and Vercel retention and nowhere else, nothing alerts on the 16 cron jobs, and seven code comments across lib, app, components and extensions asserted that Sentry captures errors when Sentry is not a dependency. The two most recent bug fixes on this repo were both discovered by customer email. This adds the sink, not a vendor. No dependency is taken: the interface has a no-op default and a registration point, so behaviour is unchanged until an adapter is registered. Releases are tagged from the build id already inlined by next.config.ts. Redaction moved out of lib/logger.ts into a leaf module that both the logger and the sink import, so there is one denylist and no path from application data to a third party can skip the personnummer regex, including direct sink calls that bypass the logger. That matters here because these logs carry personnummer and financial data. verifyCronSecret now reports its own 401s, which covers all 16 jobs without touching a route file and catches the case where CRON_SECRET is rotated without updating the scheduler and every job silently 401s forever. The threshold is one failure rather than the backup alert's three: suppressing the first occurrence is precisely how an outage stays invisible. The seven misleading comments are corrected to describe what the code actually does, including the two cases that still are not covered: the client side one, since the sink is server side, and a warn level call that is not forwarded. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix: remediate the 2026-07-26 similar-sweep findings across all surfaces Resolves the ~150-finding sweep (dev_docs/similar-sweep-2026-07-26.md) with one agent per finding; every behavioural fix carries a regression test proven to fail at HEAD. Full status, corrections to the sweep, refusals and open decisions in dev_docs/similar-sweep-2026-07-26-remediation-status.md. Structural roots closed: - resolveSekAmountOrNull(): honest SEK resolution refuses instead of booking 1:1; four duplicated toSek closures now refuse via INVOICE_FX_RATE_MISSING - ledger-line-amount.ts: journal_entry_lines.currency labels the document, not the amount; SQL pre-filter decoy proven and fixed - sparse-patch.ts: .partial() does not strip .default() in Zod 4.4.3; the exploitable salary payslip-line PATCH and KPI preferences sinks fixed - tests/schema: migration-replay phantom-column guard (13k+ refs, closed CHECK sets, onConflict targets); found 28 real defects, all fixed, all four baselines now empty - three new ratchet guards: sek-labelled-amount, cross-extension-import, ungated-extension-route Highlights: lawful VAT-rate set on all seven invoice surfaces (ML 6 kap), RC input VAT mismatch wired on web + both MCP callers, missing-underlag resource delegates to the shared RPC predicate, push-notifications consent polarity fail-closed, deadlines undo honours requested state, silent-failure and read-side-fabrication classes fixed across settings/KPI/inbox/Stripe/ Arcim/kassaflodesanalys, error-envelope stringification fixed at 10+ sites with isSwedishUserMessage extended. Also includes the parallel session's MCP invoice tools (update_invoice, recurring schedules, invoice deliveries) which share files with the sweep work and are verified green together. 13 new migrations are NOT applied anywhere; they apply via branch merge. 20260726120000 backfills 1247 supplier-invoice rows. pg tests for new DDL are written but unrun (no local Postgres). Verified: 11088 tests / 881 files green, tsc 0 non-test errors, lint 0 errors, check:guards passing, MCP payload 57475/57500. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(migrations): rename replace_sie_import migration off main's 20260726090000 version origin/main shipped 20260726090000_agent_quota_rpc_caller_guard.sql; keeping our replace_sie_import migration on the same version would abort the Supabase apply with a schema_migrations_pkey duplicate at merge time. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(review): remediate pre-publish deep-review findings across all slices A 13-agent review of the full branch diff surfaced 1 critical, 5 high and ~45 further findings; this commit resolves them in one pass: - replace_sie_import / undo_sie_import: p_user_id honored only for service_role callers; any other caller is pinned to auth.uid() (impersonation gate bypass), authz raise errcode 42501 mapped to a Swedish 403 in the route, new caller-guard migration for undo - bulk_book_transactions refuses homogeneous non-SEK batches instead of writing foreign magnitudes into SEK ledger columns - credit-note cap trigger: company-match on credited_invoice_id, no cross-tenant figures in exception text - link_voucher RPCs resolve NULL invoice currency as SEK end to end - personal-number ciphertext CHECK split into NOT VALID + VALIDATE - same-currency foreign settlements clear 1510 at booking rate and book realized diff to 3960/7960; rate-less foreign write paths refuse - receivables revaluation covers partially_paid and outstanding amounts - period lock guard paginates candidates past the PostgREST 1000 cap - documents: service-client storage removals after authz, dual-layout reads in integrity cron and archive export, backfill delete-source sweep actually deletes with hash verification and shared-key grouping - invoice matching normalizes NULL/lowercase currencies (regression), duplicate candidates stop claiming amount matches they never ran - match-invoice aborts on any booking failure (no paid-without-verifikat) - refresh-exchange-rate reverts on concurrent booking (TOCTOU window) - KPI preferences upsert arbiter aligned to the company-scoped constraint - personnummer_last4 stripped from all salary responses incl. MCP tools - worked-hours batch restores destroyed rows on conflict and error paths - MCP: shared duplicate-claim builder (no more 'null kr'), short-circuit on tag_journal_lines overflow, auto_send schedules stage as high risk - observability sink redacts emails/IBANs/API keys and keeps redacted stacks in prod; assorted small guards (safe-return-to /@, dry_run=True, cursor helper off-by-one, OAuth state TTL 10 min, arcim saveMappings call removed) Full dispositions, deferred items and hand-verified accounting numbers are documented in the PR body and DECISIONS.md. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(personnummer): implement masking and encryption for personal numbers with tests * fix(review): address CI and compliance-bot findings for PR #1215 pg-real: the CI image's auth shim reads the legacy request.jwt.claim.role GUC, so both service-role simulations (runAsServiceRole and the invoice-delivery test's local helper) never satisfied auth.role() = 'service_role' and every legitimate p_user_id path failed closed; the shared helper now sets both GUC shapes plus SET LOCAL ROLE with a fail-loud sanity check, and the delivery test reuses it. The link-voucher migration had recreated both RPCs from pre-rewrite file text, reintroducing the NULL-unsafe membership pattern the null-safe-tenant-guards ratchet bans; both guards now use public.caller_is_company_member() with all currency changes preserved. Compliance bots: the customers export now emits the standard masked form instead of raw AES-256-GCM ciphertext in the Org-/personnummer column, and maskCustomerRow returns a non-round-trippable placeholder on decrypt failure instead of 500ing the list. MCP parity: gnubok_lock_period's staging pre-check now runs the exact countUnbookedInPeriod the commit path enforces (exported from period-service; local mirror deleted), and gnubok_agi_status resolves AGI state run-scoped so a correction run no longer renders as already filed. Declined with evidence: PR-Agent's opening-balances null-zeroing concern (all mergeable columns are NOT NULL with defaults per 20260713101000). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(review): address codex review findings on PR #1215 - restore 20260726140000 to its preview-recorded content and restate the NULL-safe tenant guard under 20260727130000: a recorded migration version never re-runs, so the in-place edit could not reach the preview branch - replace toFixed() with sv-SE two-decimal formatting in the ROT/RUT cap warning texts and update the pinned test expectations - drop the em dash in the fiscal-periods route comment - strip trailing whitespace in import-existing.test.ts Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * test(reports): raise timeout on real PDF render tests renderToBuffer does real @react-pdf layout work and exceeds the 5s default when the full suite saturates the CPU; tests pass in isolation. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1288 lines
52 KiB
TypeScript
1288 lines
52 KiB
TypeScript
import type { Extension, ExtensionContext } from '@/lib/extensions/types'
|
|
import { NextResponse } from 'next/server'
|
|
import {
|
|
createConsent,
|
|
getConsent,
|
|
listConsents,
|
|
generateOtc,
|
|
consumeOAuthState,
|
|
getAuthUrl,
|
|
exchangeAuthToken,
|
|
submitProviderToken,
|
|
acceptConsent,
|
|
deleteConsent,
|
|
resolveConsent,
|
|
fetchCompanyInfoDirect,
|
|
ProviderTokenInvalidError,
|
|
ConsentNotFoundError,
|
|
} from './lib/provider-client'
|
|
import { providerSupportsSie, fetchProviderSieFiles, getAllowedFiscalYears } from './lib/sie-fetcher'
|
|
import { mapCompanyInfo } from './lib/entity-mapper'
|
|
import { executeMigration } from './lib/migration-orchestrator'
|
|
import { importProviderDocuments } from './lib/import-documents'
|
|
import { reconcileSupplierInvoiceVouchers } from '@/lib/invoices/bulk-reconcile-supplier-vouchers'
|
|
import type { ArcimProvider } from './types'
|
|
import { ARCIM_PROVIDERS } from './types'
|
|
import { parseSIEFile, validateSIEFile } from '@/lib/import/sie-parser'
|
|
import { suggestMappings, getMappingStats, isSystemAccount } from '@/lib/import/account-mapper'
|
|
import { loadMappings, generateImportPreview, executeSIEImport } from '@/lib/import/sie-import'
|
|
import { BAS_REFERENCE } from '@/lib/bookkeeping/bas-reference'
|
|
import type { ProviderName } from '@/lib/providers/types'
|
|
import { errorResponseFromCode } from '@/lib/errors/get-structured-error'
|
|
import { classifyProviderError } from '@/lib/providers/with-provider-call'
|
|
import { createLogger } from '@/lib/logger'
|
|
|
|
const moduleLog = createLogger('extensions/arcim-migration')
|
|
|
|
/**
|
|
* The one answer the unauthenticated OAuth callback gives for every state
|
|
* failure: forged, unknown, expired, replayed, or pointing at a consent that no
|
|
* longer exists. Distinguishing them would turn the callback into a probe for
|
|
* other tenants' consent ids.
|
|
*/
|
|
const STATE_REJECTED_MESSAGE =
|
|
'Ingen giltig migrationssession hittades. Starta om anslutningen.'
|
|
|
|
/**
|
|
* Map known OAuth error codes from providers (Fortnox, Visma) to actionable
|
|
* Swedish guidance. Falls back to the raw provider message so we never hide
|
|
* unknown errors from the user.
|
|
*/
|
|
function translateOAuthError(error: string, description: string | null): string {
|
|
const haystack = `${error} ${description ?? ''}`.toLowerCase()
|
|
|
|
if (haystack.includes('missing license') || haystack.includes('not have enough licenses')) {
|
|
return 'Du behöver aktivera tilläggstjänsten "Fortnox Integration" (~149 kr/mån) på ditt Fortnox-konto innan du kan ansluta. Aktivera den under Inställningar → Tilläggstjänster i Fortnox och försök igen.'
|
|
}
|
|
|
|
if (error === 'access_denied') {
|
|
return 'Du avbröt anslutningen i leverantörens inloggning. Försök igen om du vill koppla kontot.'
|
|
}
|
|
|
|
if (error === 'invalid_scope') {
|
|
return 'Tredjepartsappen har inte rätt behörigheter för ditt konto. Kontakta supporten.'
|
|
}
|
|
|
|
return description ? `${error}: ${description}` : error
|
|
}
|
|
|
|
/**
|
|
* Build a provider OAuth authorization URL bound to an EXISTING consent id.
|
|
* Used by both first-time connect and reconnect (token revival): the callback
|
|
* runs exchangeAuthToken(consentId, …) which upserts the fresh tokens keyed by
|
|
* consent_id, so re-running OAuth against the same consent overwrites a dead
|
|
* refresh-token pair in place: no disconnect/recreate needed.
|
|
*/
|
|
async function buildArcimOAuthUrl(consentId: string, provider: ArcimProvider): Promise<string> {
|
|
// Server-side state row: consent id, provider (via the consent), expiry and a
|
|
// consumed marker all live in provider_otc. The `state` handed to the provider
|
|
// is that row's opaque random primary key, nothing more.
|
|
const otc = await generateOtc(consentId)
|
|
|
|
// Prefer a provider-specific redirect override (e.g. VISMA_REDIRECT_URI) when
|
|
// set: lets dev environments route through a single registered URI rather
|
|
// than registering every ngrok URL on the OAuth client. Falls back to
|
|
// NEXT_PUBLIC_APP_URL + the canonical callback path.
|
|
const appUrl = process.env.NEXT_PUBLIC_APP_URL || ''
|
|
const providerRedirectEnv =
|
|
provider === 'visma'
|
|
? process.env.VISMA_REDIRECT_URI
|
|
: provider === 'fortnox'
|
|
? process.env.FORTNOX_REDIRECT_URI
|
|
: undefined
|
|
const callbackUrl =
|
|
providerRedirectEnv && providerRedirectEnv.trim().length > 0
|
|
? providerRedirectEnv
|
|
: `${appUrl}/api/extensions/ext/arcim-migration/callback`
|
|
|
|
// The state is the one-time code itself: an unguessable pointer to the row
|
|
// above. It deliberately encodes NOTHING. The previous base64url JSON payload
|
|
// was attacker-authored input the callback trusted, so anyone who learned a
|
|
// consent id could redirect their own provider tokens onto that consent.
|
|
const { url } = await getAuthUrl(provider, otc.code, callbackUrl)
|
|
return url
|
|
}
|
|
|
|
/**
|
|
* Provider Migration extension
|
|
*
|
|
* Migrates bookkeeping data from external Swedish accounting systems
|
|
* (Fortnox, Visma, Bokio, Björn Lundén, Briox) into Accounted by talking
|
|
* directly to each provider's API.
|
|
*
|
|
* Bookkeeping data (accounts, balances, vouchers) is imported via SIE
|
|
* files fetched from providers. Entity data (customers, suppliers,
|
|
* invoices) is imported via the provider REST APIs.
|
|
*/
|
|
export const arcimMigrationExtension: Extension = {
|
|
id: 'arcim-migration',
|
|
name: 'Systemmigration',
|
|
version: '2.0.0',
|
|
|
|
apiRoutes: [
|
|
// ── List available providers ───────────────────────────────────
|
|
{
|
|
method: 'GET',
|
|
path: '/providers',
|
|
handler: async () => {
|
|
return NextResponse.json({ providers: ARCIM_PROVIDERS })
|
|
},
|
|
},
|
|
|
|
// ── Check existing connections and import history ──────────────
|
|
{
|
|
method: 'GET',
|
|
path: '/status',
|
|
handler: async (request: Request, ctx?: ExtensionContext) => {
|
|
const supabase = ctx?.supabase ?? await (await import('@/lib/supabase/server')).createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const companyId = ctx?.companyId ?? user.id
|
|
|
|
try {
|
|
// Get accepted consents only (status 1): not abandoned/created ones
|
|
const allConsents = await listConsents(companyId)
|
|
const consents = allConsents.filter(c => c.status === 1)
|
|
|
|
// Get SIE import history
|
|
const { data: sieImports } = await supabase
|
|
.from('sie_imports')
|
|
.select('id, filename, status, accounts_count, transactions_count, company_name, fiscal_year_start, fiscal_year_end, imported_at, created_at')
|
|
.eq('company_id', companyId)
|
|
.order('created_at', { ascending: false })
|
|
.limit(10)
|
|
|
|
// Get entity counts (to show what's already been imported)
|
|
const [
|
|
{ count: customerCount },
|
|
{ count: supplierCount },
|
|
{ count: invoiceCount },
|
|
] = await Promise.all([
|
|
supabase.from('customers').select('*', { count: 'exact', head: true }).eq('company_id', companyId),
|
|
supabase.from('suppliers').select('*', { count: 'exact', head: true }).eq('company_id', companyId),
|
|
supabase.from('invoices').select('*', { count: 'exact', head: true }).eq('company_id', companyId),
|
|
])
|
|
|
|
return NextResponse.json({
|
|
consents: consents.map(c => ({
|
|
id: c.id,
|
|
provider: c.provider,
|
|
status: c.status,
|
|
companyName: c.companyName,
|
|
createdAt: c.createdAt,
|
|
})),
|
|
sieImports: sieImports ?? [],
|
|
entityCounts: {
|
|
customers: customerCount ?? 0,
|
|
suppliers: supplierCount ?? 0,
|
|
invoices: invoiceCount ?? 0,
|
|
},
|
|
})
|
|
} catch (error) {
|
|
moduleLog.error('arcim status failed', error as Error, { companyId })
|
|
return errorResponseFromCode('PROVIDER_STATUS_FAILED', moduleLog, {
|
|
details: { reason: error instanceof Error ? error.message : 'unknown' },
|
|
})
|
|
}
|
|
},
|
|
},
|
|
|
|
// ── Start consent flow (create consent + OTC) ─────────────────
|
|
{
|
|
method: 'POST',
|
|
path: '/connect',
|
|
handler: async (request: Request, ctx?: ExtensionContext) => {
|
|
const log = ctx?.log ?? console
|
|
const supabase = ctx?.supabase ?? await (await import('@/lib/supabase/server')).createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const companyId = ctx?.companyId ?? user.id
|
|
|
|
const { provider, companyName, orgNumber, reconnect } = await request.json() as {
|
|
provider: ArcimProvider
|
|
companyName?: string
|
|
orgNumber?: string
|
|
reconnect?: boolean
|
|
}
|
|
|
|
if (!provider) {
|
|
return errorResponseFromCode('VALIDATION_ERROR', moduleLog, {
|
|
details: { field: 'provider', reason: 'required' },
|
|
})
|
|
}
|
|
|
|
const providerInfo = ARCIM_PROVIDERS.find(p => p.id === provider)
|
|
if (!providerInfo) {
|
|
return errorResponseFromCode('PROVIDER_INVALID', moduleLog, {
|
|
details: { provider },
|
|
})
|
|
}
|
|
|
|
try {
|
|
const { createServiceClient: createSvc } = await import('@/lib/supabase/server')
|
|
|
|
const existingConsents = await listConsents(companyId)
|
|
|
|
// Reconnect: an existing connection's stored tokens are dead (refresh
|
|
// failed → PROVIDER_AUTH_EXPIRED). Re-run auth against the SAME consent
|
|
// so fresh tokens overwrite the dead pair in place: no disconnect, no
|
|
// duplicate consent, import history preserved. Bypasses the
|
|
// alreadyConnected short-circuit below (which would otherwise skip the
|
|
// auth that's the whole point here).
|
|
if (reconnect) {
|
|
const stale = existingConsents.find(
|
|
c => c.provider === provider && (c.status === 0 || c.status === 1),
|
|
)
|
|
if (stale) {
|
|
if (ctx?.settings) {
|
|
await ctx.settings.set('consent_id', stale.id)
|
|
await ctx.settings.set('provider', provider)
|
|
}
|
|
if (providerInfo.authType === 'oauth') {
|
|
const authUrl = await buildArcimOAuthUrl(stale.id, provider)
|
|
return NextResponse.json({
|
|
consentId: stale.id,
|
|
authType: 'oauth',
|
|
authUrl,
|
|
reconnect: true,
|
|
})
|
|
}
|
|
// Token-based providers re-authorize by re-entering credentials
|
|
return NextResponse.json({
|
|
consentId: stale.id,
|
|
authType: 'token',
|
|
reconnect: true,
|
|
})
|
|
}
|
|
// No existing consent to revive: fall through to a normal connect.
|
|
}
|
|
|
|
// Reuse existing accepted consent if one exists for this provider
|
|
const accepted = existingConsents.find(c => c.provider === provider && c.status === 1)
|
|
|
|
if (accepted) {
|
|
// Already connected: skip OAuth, go straight to preview
|
|
if (ctx?.settings) {
|
|
await ctx.settings.set('consent_id', accepted.id)
|
|
await ctx.settings.set('provider', provider)
|
|
}
|
|
|
|
return NextResponse.json({
|
|
consentId: accepted.id,
|
|
authType: providerInfo.authType,
|
|
alreadyConnected: true,
|
|
})
|
|
}
|
|
|
|
// Check for status 0 consents that already have tokens stored (credentials submitted but migration not completed)
|
|
const pending = existingConsents.filter(c => c.provider === provider && c.status === 0)
|
|
if (pending.length > 0) {
|
|
const svc = createSvc()
|
|
for (const p of pending) {
|
|
const { data: tokens } = await svc
|
|
.from('provider_consent_tokens')
|
|
// consent_id is the PK: there is no `id` column. Selecting `id`
|
|
// errors silently (only `data` is read), so `tokens` was always
|
|
// null and the reuse branch below never fired, deleting valid
|
|
// status-0 consents as "abandoned".
|
|
.select('consent_id')
|
|
.eq('consent_id', p.id)
|
|
.limit(1)
|
|
if (tokens && tokens.length > 0) {
|
|
// Tokens exist: reuse this consent, skip credential entry
|
|
if (ctx?.settings) {
|
|
await ctx.settings.set('consent_id', p.id)
|
|
await ctx.settings.set('provider', provider)
|
|
}
|
|
return NextResponse.json({
|
|
consentId: p.id,
|
|
authType: providerInfo.authType,
|
|
alreadyConnected: true,
|
|
})
|
|
}
|
|
}
|
|
// No tokens found: clean up abandoned consents
|
|
for (const p of pending) {
|
|
await deleteConsent(p.id)
|
|
}
|
|
}
|
|
|
|
// Create new consent
|
|
const consent = await createConsent(
|
|
companyId,
|
|
provider,
|
|
`gnubok-migration-${user.id}`,
|
|
orgNumber,
|
|
companyName
|
|
)
|
|
|
|
if (ctx?.settings) {
|
|
await ctx.settings.set('consent_id', consent.id)
|
|
await ctx.settings.set('provider', provider)
|
|
}
|
|
|
|
if (providerInfo.authType === 'oauth') {
|
|
const authUrl = await buildArcimOAuthUrl(consent.id, provider)
|
|
|
|
return NextResponse.json({
|
|
consentId: consent.id,
|
|
authType: 'oauth',
|
|
authUrl,
|
|
})
|
|
} else {
|
|
// Token-based providers: consent is ready for direct use
|
|
return NextResponse.json({
|
|
consentId: consent.id,
|
|
authType: 'token',
|
|
})
|
|
}
|
|
} catch (error) {
|
|
log.error('arcim connect failed', error as Error, { provider })
|
|
return errorResponseFromCode('PROVIDER_CONNECT_FAILED', moduleLog, {
|
|
details: { reason: error instanceof Error ? error.message : 'unknown' },
|
|
})
|
|
}
|
|
},
|
|
},
|
|
|
|
// ── Submit API token for token-based providers (Bokio, etc.) ──
|
|
{
|
|
method: 'POST',
|
|
path: '/submit-token',
|
|
handler: async (request: Request, ctx?: ExtensionContext) => {
|
|
const log = ctx?.log ?? console
|
|
const supabase = ctx?.supabase ?? await (await import('@/lib/supabase/server')).createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
// The caller's tenant: NOT the provider-side company id below.
|
|
const ownerCompanyId = ctx?.companyId ?? user.id
|
|
|
|
// `companyId` in the body is the PROVIDER-side company identifier
|
|
// (BL User-Key / Briox account ID / Bokio company GUID).
|
|
const { consentId, provider, apiToken, companyId: providerCompanyId } = await request.json() as {
|
|
consentId: string
|
|
provider: ArcimProvider
|
|
apiToken: string
|
|
companyId?: string
|
|
}
|
|
|
|
if (!consentId || !provider) {
|
|
return errorResponseFromCode('VALIDATION_ERROR', moduleLog, {
|
|
details: { fields: ['consentId', 'provider'], reason: 'required' },
|
|
})
|
|
}
|
|
|
|
// BL uses server-side client credentials: only needs companyId
|
|
if (provider !== 'bjornlunden' && !apiToken) {
|
|
return errorResponseFromCode('PROVIDER_TOKEN_REQUIRED', moduleLog, {
|
|
details: { provider },
|
|
})
|
|
}
|
|
|
|
// Briox needs the account ID (the /token clientid param) alongside
|
|
// the application token; Bokio/BL need their company GUID.
|
|
if ((provider === 'bokio' || provider === 'bjornlunden' || provider === 'briox') && !providerCompanyId) {
|
|
return errorResponseFromCode('PROVIDER_COMPANY_ID_REQUIRED', moduleLog, {
|
|
details: { provider },
|
|
})
|
|
}
|
|
|
|
try {
|
|
await submitProviderToken(
|
|
consentId,
|
|
provider,
|
|
apiToken || 'client_credentials',
|
|
providerCompanyId,
|
|
ownerCompanyId,
|
|
)
|
|
return NextResponse.json({ success: true, consentId })
|
|
} catch (error) {
|
|
log.error('arcim submit-token failed', error as Error, { provider })
|
|
// Consent missing or owned by another company: same 404 either way.
|
|
if (error instanceof ConsentNotFoundError) {
|
|
return errorResponseFromCode('PROVIDER_CONSENT_NOT_FOUND', moduleLog, {
|
|
details: { consentId },
|
|
})
|
|
}
|
|
// Wrong credentials (provider actively rejected them): tell the
|
|
// user to re-check the pasted values instead of a generic 500.
|
|
if (error instanceof ProviderTokenInvalidError) {
|
|
return errorResponseFromCode('PROVIDER_TOKEN_INVALID', moduleLog, {
|
|
details: { provider, reason: error.message },
|
|
})
|
|
}
|
|
return errorResponseFromCode('PROVIDER_TOKEN_SUBMIT_FAILED', moduleLog, {
|
|
details: { reason: error instanceof Error ? error.message : 'unknown' },
|
|
})
|
|
}
|
|
},
|
|
},
|
|
|
|
// ── OAuth callback ────────────────────────────────────────────
|
|
{
|
|
method: 'GET',
|
|
path: '/callback',
|
|
skipAuth: true,
|
|
handler: async (request: Request, ctx?: ExtensionContext) => {
|
|
const log = ctx?.log ?? console
|
|
const url = new URL(request.url)
|
|
const code = url.searchParams.get('code')
|
|
const stateRaw = url.searchParams.get('state')
|
|
const oauthError = url.searchParams.get('error')
|
|
const oauthErrorDescription = url.searchParams.get('error_description')
|
|
const appUrl = process.env.NEXT_PUBLIC_APP_URL || ''
|
|
|
|
// JSON-encode for safe embedding inside <script>. Escapes quotes/unicode
|
|
// and `</` so the value can't break out of the script tag.
|
|
const jsLiteral = (value: unknown) =>
|
|
JSON.stringify(value ?? '').replace(/</g, '\\u003c')
|
|
|
|
const respondWithError = (reason: string) => {
|
|
const fallbackUrl = new URL(`${appUrl}/import`)
|
|
fallbackUrl.searchParams.set('migration', 'error')
|
|
fallbackUrl.searchParams.set('reason', reason)
|
|
|
|
const escapedReason = reason
|
|
.replace(/&/g, '&')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>')
|
|
|
|
const html = `<!DOCTYPE html><html><body><script>
|
|
if (window.opener) {
|
|
window.opener.postMessage({ type: 'arcim-oauth-error', reason: ${jsLiteral(reason)} }, ${jsLiteral(appUrl)});
|
|
window.close();
|
|
} else {
|
|
window.location.href = ${jsLiteral(fallbackUrl.toString())};
|
|
}
|
|
</script><p>Anslutningen misslyckades: ${escapedReason}</p></body></html>`
|
|
|
|
return new Response(html, {
|
|
status: 200,
|
|
headers: { 'Content-Type': 'text/html' },
|
|
})
|
|
}
|
|
|
|
// Provider returned an OAuth error (user cancelled, missing API
|
|
// subscription on the Fortnox side, invalid scope, etc.)
|
|
if (oauthError) {
|
|
log.error('OAuth callback returned provider error', {
|
|
error: oauthError,
|
|
errorDescription: oauthErrorDescription,
|
|
hasCode: !!code,
|
|
hasState: !!stateRaw,
|
|
})
|
|
return respondWithError(translateOAuthError(oauthError, oauthErrorDescription))
|
|
}
|
|
|
|
if (!code || !stateRaw) {
|
|
log.error('OAuth callback missing code or state', {
|
|
hasCode: !!code,
|
|
hasState: !!stateRaw,
|
|
queryKeys: Array.from(url.searchParams.keys()),
|
|
})
|
|
return respondWithError('Återanropet saknade code eller state. Försök igen.')
|
|
}
|
|
|
|
try {
|
|
// Single source of truth for who this callback belongs to: the
|
|
// server-written provider_otc row, consumed atomically here. The row
|
|
// carries the consent; the consent carries the provider. Nothing is
|
|
// read from the query string except the opaque state token itself, so
|
|
// an attacker who knows a victim's consent id still cannot steer their
|
|
// own provider tokens onto it.
|
|
const resolvedState = await consumeOAuthState(stateRaw)
|
|
|
|
if (!resolvedState) {
|
|
// Unknown/forged state, expired state, replayed state and deleted
|
|
// consent all end here with the SAME message: this route is
|
|
// unauthenticated, so telling the caller which one it was would
|
|
// disclose whether a given consent exists.
|
|
log.error('OAuth callback state rejected', {
|
|
hasCode: !!code,
|
|
stateLength: stateRaw.length,
|
|
})
|
|
return respondWithError(STATE_REJECTED_MESSAGE)
|
|
}
|
|
|
|
const { consentId, provider } = resolvedState
|
|
|
|
const redirectUri = `${appUrl}/api/extensions/ext/arcim-migration/callback`
|
|
|
|
// Exchange OAuth code directly with the provider
|
|
await exchangeAuthToken(consentId, provider, code, redirectUri)
|
|
|
|
// Return an HTML page that notifies the opener tab and closes itself
|
|
const successUrl = `${appUrl}/import?migration=connected&consentId=${encodeURIComponent(consentId)}`
|
|
const html = `<!DOCTYPE html><html><body><script>
|
|
if (window.opener) {
|
|
window.opener.postMessage({ type: 'arcim-oauth-success', consentId: ${jsLiteral(consentId)} }, ${jsLiteral(appUrl)});
|
|
window.close();
|
|
} else {
|
|
window.location.href = ${jsLiteral(successUrl)};
|
|
}
|
|
</script><p>Anslutningen lyckades. Du kan stänga denna flik.</p></body></html>`
|
|
|
|
return new Response(html, {
|
|
status: 200,
|
|
headers: { 'Content-Type': 'text/html' },
|
|
})
|
|
} catch (error) {
|
|
log.error('OAuth callback exchange failed', error)
|
|
const reason = error instanceof Error ? error.message : 'Okänt fel vid tokenutbyte.'
|
|
return respondWithError(reason)
|
|
}
|
|
},
|
|
},
|
|
|
|
// ── Preview: fetch company info + SIE stats before migration ──
|
|
{
|
|
method: 'GET',
|
|
path: '/preview',
|
|
handler: async (request: Request, ctx?: ExtensionContext) => {
|
|
const log = ctx?.log ?? console
|
|
const supabase = ctx?.supabase ?? await (await import('@/lib/supabase/server')).createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const companyId = ctx?.companyId ?? user.id
|
|
|
|
const url = new URL(request.url)
|
|
const consentId = url.searchParams.get('consentId')
|
|
|
|
if (!consentId) {
|
|
return errorResponseFromCode('VALIDATION_ERROR', moduleLog, {
|
|
details: { field: 'consentId', reason: 'required' },
|
|
})
|
|
}
|
|
|
|
try {
|
|
// Company-scoped read: the status below is returned to the caller, so
|
|
// an unscoped lookup would let any authenticated user probe another
|
|
// tenant's consent id for existence and connection state. A foreign
|
|
// consent throws ConsentNotFoundError, same as a nonexistent one.
|
|
const consent = await getConsent(consentId, companyId)
|
|
if (consent.status !== 0 && consent.status !== 1) {
|
|
return errorResponseFromCode('PROVIDER_CONSENT_NOT_READY', moduleLog, {
|
|
details: { consentId, status: consent.status },
|
|
})
|
|
}
|
|
|
|
// Resolve consent to get access token
|
|
const resolved = await resolveConsent(companyId, consentId)
|
|
const provider = resolved.consent.provider as ProviderName
|
|
|
|
// Fetch company info directly from provider
|
|
let mapped = null
|
|
try {
|
|
const companyInfo = await fetchCompanyInfoDirect(provider, resolved.accessToken, resolved.providerCompanyId)
|
|
mapped = companyInfo ? mapCompanyInfo(companyInfo) : null
|
|
} catch (err) {
|
|
log.info('Company info fetch failed:', err instanceof Error ? err.message : String(err))
|
|
}
|
|
|
|
// Try to fetch SIE data (Fortnox and Briox serve SIE over the API)
|
|
let sieAvailable = false
|
|
let sieStats: { accountCount: number; transactionCount: number; fiscalYears: number[] } | null = null
|
|
|
|
if (providerSupportsSie(provider)) {
|
|
try {
|
|
log.info(`Fetching SIE export from ${provider} for consent ${consentId}...`)
|
|
// Fetch SIE type 4 for the most recent allowed year to get stats
|
|
const { files, availableYears } = await fetchProviderSieFiles(
|
|
provider,
|
|
resolved.accessToken,
|
|
resolved.providerCompanyId,
|
|
{ latestOnly: true },
|
|
)
|
|
if (files.length > 0) {
|
|
const parsed = parseSIEFile(files[files.length - 1].rawContent)
|
|
sieAvailable = true
|
|
sieStats = {
|
|
accountCount: parsed.accounts.length,
|
|
transactionCount: parsed.vouchers.length,
|
|
fiscalYears: availableYears,
|
|
}
|
|
}
|
|
} catch (err) {
|
|
log.info('SIE export failed:', err instanceof Error ? err.message : String(err))
|
|
}
|
|
}
|
|
|
|
// Check if the company already has completed SIE imports (from manual upload)
|
|
const { count: sieImportCount } = await supabase
|
|
.from('sie_imports')
|
|
.select('*', { count: 'exact', head: true })
|
|
.eq('company_id', companyId)
|
|
.eq('status', 'completed')
|
|
|
|
return NextResponse.json({
|
|
consent: {
|
|
id: consent.id,
|
|
provider: consent.provider,
|
|
status: consent.status,
|
|
companyName: consent.companyName,
|
|
},
|
|
companyInfo: mapped,
|
|
sieAvailable,
|
|
sieStats,
|
|
hasSieData: (sieImportCount ?? 0) > 0,
|
|
})
|
|
} catch (error) {
|
|
log.error('arcim preview failed', error as Error)
|
|
// Consent missing or owned by another company: same 404 either way.
|
|
if (error instanceof ConsentNotFoundError) {
|
|
return errorResponseFromCode('PROVIDER_CONSENT_NOT_FOUND', moduleLog, {
|
|
details: { consentId },
|
|
})
|
|
}
|
|
// Classify HTTP failures into typed codes so the toast can suggest
|
|
// reconnect / retry instead of a generic "preview failed".
|
|
const classified = classifyProviderError(error)
|
|
return errorResponseFromCode(classified ?? 'PROVIDER_PREVIEW_FAILED', moduleLog, {
|
|
details: {
|
|
reason: error instanceof Error ? error.message : 'unknown',
|
|
classified: classified ?? 'unclassified',
|
|
},
|
|
})
|
|
}
|
|
},
|
|
},
|
|
|
|
// ── Fetch + parse SIE data for mapping step ───────────────────
|
|
{
|
|
method: 'GET',
|
|
path: '/sie-data',
|
|
handler: async (request: Request, ctx?: ExtensionContext) => {
|
|
const log = ctx?.log ?? console
|
|
const supabase = ctx?.supabase ?? await (await import('@/lib/supabase/server')).createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const companyId = ctx?.companyId ?? user.id
|
|
|
|
const url = new URL(request.url)
|
|
const consentId = url.searchParams.get('consentId')
|
|
|
|
if (!consentId) {
|
|
return NextResponse.json({ error: 'consentId is required' }, { status: 400 })
|
|
}
|
|
|
|
try {
|
|
// Resolve consent
|
|
const resolved = await resolveConsent(companyId, consentId)
|
|
const provider = resolved.consent.provider as ProviderName
|
|
|
|
if (!providerSupportsSie(provider)) {
|
|
return errorResponseFromCode('PROVIDER_SIE_NOT_SUPPORTED', moduleLog, {
|
|
details: { provider },
|
|
})
|
|
}
|
|
|
|
// Fetch SIE type 4 for each allowed fiscal year
|
|
const { files: sieFiles, failedYears } = await fetchProviderSieFiles(
|
|
provider,
|
|
resolved.accessToken,
|
|
resolved.providerCompanyId,
|
|
)
|
|
|
|
if (sieFiles.length === 0) {
|
|
// The allowed window is rolling (current year and the two before
|
|
// it): interpolate the actual range instead of the static
|
|
// registry message so the text never goes stale.
|
|
const allowedYears = [...getAllowedFiscalYears()].sort((a, b) => a - b)
|
|
const range = `${allowedYears[0]}-${allowedYears[allowedYears.length - 1]}`
|
|
return errorResponseFromCode('PROVIDER_SIE_NO_YEARS', moduleLog, {
|
|
messageSv: `Inga räkenskapsår ${range} hittades hos leverantören.`,
|
|
messageEn: `No fiscal years available for ${range}.`,
|
|
...(failedYears.length > 0 ? { details: { failedYears } } : {}),
|
|
})
|
|
}
|
|
|
|
// Parse most recent file for preview/validation
|
|
const sieFile = sieFiles[sieFiles.length - 1]
|
|
const parsed = parseSIEFile(sieFile.rawContent)
|
|
const validation = validateSIEFile(parsed)
|
|
|
|
if (!validation.valid) {
|
|
log.warn(
|
|
`arcim sie-data validation failed for ${provider} fiscal year ${sieFile.fiscalYear}: ` +
|
|
`${validation.errors.length} error(s): ${validation.errors.slice(0, 3).join(' | ')}`,
|
|
)
|
|
return NextResponse.json({
|
|
error: 'validation',
|
|
message: 'SIE file validation failed',
|
|
validation,
|
|
}, { status: 400 })
|
|
}
|
|
|
|
// Collect ALL unique accounts across ALL fiscal year files
|
|
const allAccountsMap = new Map<string, { number: string; name: string }>()
|
|
for (const file of sieFiles) {
|
|
const fileParsed = parseSIEFile(file.rawContent)
|
|
for (const acc of fileParsed.accounts) {
|
|
if (!allAccountsMap.has(acc.number)) {
|
|
allAccountsMap.set(acc.number, { number: acc.number, name: acc.name })
|
|
}
|
|
}
|
|
}
|
|
const allAccounts = [...allAccountsMap.values()]
|
|
.filter(a => !isSystemAccount(a.number))
|
|
.map(a => ({ number: a.number, name: a.name }))
|
|
|
|
// Load existing user mappings
|
|
const existingMappings = await loadMappings(supabase, companyId)
|
|
const existingRecords = [...existingMappings.values()].map(m => ({
|
|
id: '',
|
|
user_id: user.id,
|
|
source_account: m.sourceAccount,
|
|
source_name: m.sourceName,
|
|
target_account: m.targetAccount,
|
|
confidence: m.confidence,
|
|
match_type: m.matchType,
|
|
created_at: '',
|
|
updated_at: '',
|
|
}))
|
|
|
|
// Suggest mappings
|
|
const basAccounts = BAS_REFERENCE.map(b => ({
|
|
account_number: b.account_number,
|
|
account_name: b.account_name,
|
|
}))
|
|
const mappings = suggestMappings(allAccounts, basAccounts, existingRecords)
|
|
const mappingStats = getMappingStats(mappings)
|
|
|
|
log.info(`Account mapping: ${allAccounts.length} unique accounts across ${sieFiles.length} files, ${mappingStats.unmapped} unmapped`)
|
|
|
|
const preview = generateImportPreview(parsed, mappings)
|
|
|
|
// Detect prior imports by *fiscal period overlap*, not file hash.
|
|
// Providers embed the export-time #GEN date in every SIE export so
|
|
// the hash always changes between syncs; only the period stays
|
|
// stable. A re-sync replaces the prior import for the same period.
|
|
const fileStatuses: {
|
|
fiscalYear: number
|
|
rawContent: string
|
|
previousImport: {
|
|
importedAt: string | null
|
|
fiscalYearStart: string | null
|
|
fiscalYearEnd: string | null
|
|
} | null
|
|
}[] = []
|
|
for (const file of sieFiles) {
|
|
const fileParsed = parseSIEFile(file.rawContent)
|
|
const fyStart = fileParsed.stats.fiscalYearStart
|
|
const fyEnd = fileParsed.stats.fiscalYearEnd
|
|
|
|
let priorImport: {
|
|
imported_at: string | null
|
|
fiscal_year_start: string | null
|
|
fiscal_year_end: string | null
|
|
} | null = null
|
|
|
|
if (fyStart && fyEnd) {
|
|
const { data } = await supabase
|
|
.from('sie_imports')
|
|
.select('imported_at, fiscal_year_start, fiscal_year_end')
|
|
.eq('company_id', companyId)
|
|
.eq('status', 'completed')
|
|
.lte('fiscal_year_start', fyEnd)
|
|
.gte('fiscal_year_end', fyStart)
|
|
.limit(1)
|
|
.maybeSingle()
|
|
priorImport = data
|
|
}
|
|
|
|
fileStatuses.push({
|
|
fiscalYear: file.fiscalYear,
|
|
rawContent: file.rawContent,
|
|
previousImport: priorImport
|
|
? {
|
|
importedAt: priorImport.imported_at,
|
|
fiscalYearStart: priorImport.fiscal_year_start,
|
|
fiscalYearEnd: priorImport.fiscal_year_end,
|
|
}
|
|
: null,
|
|
})
|
|
}
|
|
|
|
const replacedFileCount = fileStatuses.filter(f => f.previousImport).length
|
|
|
|
return NextResponse.json({
|
|
parsed,
|
|
mappings,
|
|
mappingStats,
|
|
preview,
|
|
validation,
|
|
rawContent: fileStatuses.map(f => f.rawContent),
|
|
fileStatuses: fileStatuses.map(f => ({
|
|
fiscalYear: f.fiscalYear,
|
|
previousImport: f.previousImport,
|
|
// Back-compat for older wizard builds: an `alreadyImported`
|
|
// boolean. The new wizard reads `previousImport` directly.
|
|
alreadyImported: !!f.previousImport,
|
|
importedAt: f.previousImport?.importedAt ?? null,
|
|
})),
|
|
allImported: false,
|
|
newFileCount: fileStatuses.length - replacedFileCount,
|
|
replacedFileCount,
|
|
// Allowed years whose provider export failed: the wizard warns
|
|
// the user before proceeding so an IB/UB gap cannot slip through.
|
|
failedYears,
|
|
basAccounts: BAS_REFERENCE,
|
|
})
|
|
} catch (error) {
|
|
log.error('arcim sie-data fetch failed', error as Error)
|
|
const classified = classifyProviderError(error)
|
|
return errorResponseFromCode(classified ?? 'PROVIDER_SIE_FETCH_FAILED', moduleLog, {
|
|
details: {
|
|
reason: error instanceof Error ? error.message : 'unknown',
|
|
classified: classified ?? 'unclassified',
|
|
},
|
|
})
|
|
}
|
|
},
|
|
},
|
|
|
|
// ── Import SIE data (accounts, balances, vouchers) ────────────
|
|
{
|
|
method: 'POST',
|
|
path: '/import-sie',
|
|
handler: async (request: Request, ctx?: ExtensionContext) => {
|
|
const log = ctx?.log ?? console
|
|
const supabase = ctx?.supabase ?? await (await import('@/lib/supabase/server')).createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const companyId = ctx?.companyId ?? user.id
|
|
|
|
const { rawContent, mappings, options } = await request.json() as {
|
|
rawContent: string
|
|
mappings: import('@/lib/import/types').AccountMapping[]
|
|
options: {
|
|
createFiscalPeriod: boolean
|
|
importOpeningBalances: boolean
|
|
importTransactions: boolean
|
|
voucherSeries?: string
|
|
updateAccountNames?: boolean
|
|
}
|
|
}
|
|
|
|
if (!rawContent || !mappings) {
|
|
return NextResponse.json({ error: 'rawContent and mappings are required' }, { status: 400 })
|
|
}
|
|
|
|
try {
|
|
const parsed = parseSIEFile(rawContent)
|
|
|
|
// Validate all accounts are mapped (same as manual upload)
|
|
const unmapped = mappings.filter((m: import('@/lib/import/types').AccountMapping) => !m.targetAccount)
|
|
if (unmapped.length > 0) {
|
|
return NextResponse.json({
|
|
error: 'validation',
|
|
message: `${unmapped.length} account(s) are not mapped`,
|
|
unmappedAccounts: unmapped.map((m: import('@/lib/import/types').AccountMapping) => ({
|
|
account: m.sourceAccount,
|
|
name: m.sourceName,
|
|
})),
|
|
}, { status: 400 })
|
|
}
|
|
|
|
// Account creation (and #KONTO renames) happen inside
|
|
// executeSIEImport via syncMappedAccounts: the auto-activate block
|
|
// that used to live here was a duplicate of that logic. Mapping
|
|
// persistence ALSO happens inside executeSIEImport, with the
|
|
// correct companyId + userId and non-fatal warning handling: the
|
|
// direct saveMappings call that used to sit here passed user.id in
|
|
// the companyId slot, which throws on RLS/FK now that the helper
|
|
// surfaces upsert failures, 500ing every provider-migration import
|
|
// before a single voucher was written. Do not re-add it.
|
|
const result = await executeSIEImport(supabase, companyId, user.id, parsed, mappings, {
|
|
filename: `migration-sie-${Date.now()}.se`,
|
|
fileContent: rawContent,
|
|
createFiscalPeriod: options.createFiscalPeriod,
|
|
importOpeningBalances: options.importOpeningBalances,
|
|
importTransactions: options.importTransactions,
|
|
voucherSeries: options.voucherSeries,
|
|
// Default ON: re-syncs keep account names current with the source
|
|
// system (idempotent: equal names are a no-op in the rename pass).
|
|
updateAccountNames: options.updateAccountNames ?? true,
|
|
// Provider re-sync semantics: a prior completed import for the
|
|
// same fiscal year is automatically replaced (its imported
|
|
// entries are cancelled) so the user can pull updated data
|
|
// without manual cleanup. Manual SIE upload keeps default
|
|
// 'block' behavior.
|
|
onExistingPeriod: 'replace',
|
|
})
|
|
|
|
log.info('SIE import completed:', {
|
|
success: result.success,
|
|
journalEntriesCreated: result.journalEntriesCreated,
|
|
errors: result.errors.length,
|
|
errorDetails: result.errors.slice(0, 10),
|
|
})
|
|
|
|
return NextResponse.json(result)
|
|
} catch (error) {
|
|
log.error('arcim sie import failed', error as Error)
|
|
const classified = classifyProviderError(error)
|
|
return errorResponseFromCode(classified ?? 'SIE_IMPORT_UNEXPECTED', moduleLog, {
|
|
details: {
|
|
reason: error instanceof Error ? error.message : 'unknown',
|
|
classified: classified ?? 'unclassified',
|
|
},
|
|
})
|
|
}
|
|
},
|
|
},
|
|
|
|
// ── Execute entity migration (customers, suppliers, invoices) ──
|
|
{
|
|
method: 'POST',
|
|
path: '/migrate',
|
|
handler: async (request: Request, ctx?: ExtensionContext) => {
|
|
const log = ctx?.log ?? console
|
|
const supabase = ctx?.supabase ?? await (await import('@/lib/supabase/server')).createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const companyId = ctx?.companyId ?? user.id
|
|
|
|
const {
|
|
consentId,
|
|
importCompanyInfo = true,
|
|
importCustomers = true,
|
|
importSuppliers = true,
|
|
importSalesInvoices = true,
|
|
importSupplierInvoices = true,
|
|
reconcileVouchers = true,
|
|
} = await request.json() as {
|
|
consentId: string
|
|
importCompanyInfo?: boolean
|
|
importCustomers?: boolean
|
|
importSuppliers?: boolean
|
|
importSalesInvoices?: boolean
|
|
importSupplierInvoices?: boolean
|
|
reconcileVouchers?: boolean
|
|
}
|
|
|
|
if (!consentId) {
|
|
return NextResponse.json({ error: 'consentId is required' }, { status: 400 })
|
|
}
|
|
|
|
try {
|
|
// Company-scoped read: the status below is returned to the caller, so
|
|
// an unscoped lookup would let any authenticated user probe another
|
|
// tenant's consent id for existence and connection state. A foreign
|
|
// consent throws ConsentNotFoundError, same as a nonexistent one.
|
|
const consent = await getConsent(consentId, companyId)
|
|
if (consent.status !== 0 && consent.status !== 1) {
|
|
return errorResponseFromCode('PROVIDER_CONSENT_NOT_READY', moduleLog, {
|
|
details: { consentId, status: consent.status },
|
|
})
|
|
}
|
|
|
|
// ── Guard: a completed SIE import is required before entity import ──
|
|
// Most providers expose ONLY entity data (customers, suppliers,
|
|
// invoices) via API: never the general ledger. Fortnox pulls the GL
|
|
// itself via SIE-over-API and is exempt. Briox and Björn Lundén also
|
|
// serve SIE over the API, but the wizard runs /import-sie before
|
|
// /migrate, so this guard stays satisfied, and keeps protecting
|
|
// against a skipped SIE step. Importing entities without the
|
|
// SIE-derived ledger (kontoplan,
|
|
// ingående balanser, verifikationer) would leave an incomplete
|
|
// bokföring under BFL: a subledger with no chart of accounts and no
|
|
// opening balances, so every subsequent posting and balance is wrong.
|
|
// The wizard surfaces this as an advisory banner, but it must be
|
|
// enforced here so the rule cannot be bypassed by a direct API call,
|
|
// a skipped wizard step, or a stale client.
|
|
if (consent.provider !== 'fortnox') {
|
|
const { count: completedSieImports } = await supabase
|
|
.from('sie_imports')
|
|
.select('id', { count: 'exact', head: true })
|
|
.eq('company_id', companyId)
|
|
.eq('status', 'completed')
|
|
|
|
if (!completedSieImports || completedSieImports < 1) {
|
|
return errorResponseFromCode('PROVIDER_SIE_IMPORT_REQUIRED', moduleLog, {
|
|
details: { provider: consent.provider },
|
|
})
|
|
}
|
|
}
|
|
|
|
log.info(`Starting migration for user ${user.id} from ${consent.provider}`)
|
|
|
|
const results = await executeMigration({
|
|
consentId,
|
|
companyId,
|
|
userId: user.id,
|
|
supabase,
|
|
importCompanyInfo,
|
|
importCustomers,
|
|
importSuppliers,
|
|
importSalesInvoices,
|
|
importSupplierInvoices,
|
|
reconcileVouchers,
|
|
})
|
|
|
|
log.info('Migration completed:', results)
|
|
|
|
// Mark consent as fully accepted now that data has been imported
|
|
await acceptConsent(consentId)
|
|
|
|
return NextResponse.json({ success: true, results })
|
|
} catch (error) {
|
|
log.error('arcim migration failed', error as Error)
|
|
// Consent missing or owned by another company: same 404 either way.
|
|
if (error instanceof ConsentNotFoundError) {
|
|
return errorResponseFromCode('PROVIDER_CONSENT_NOT_FOUND', moduleLog, {
|
|
details: { consentId },
|
|
})
|
|
}
|
|
const classified = classifyProviderError(error)
|
|
return errorResponseFromCode(classified ?? 'PROVIDER_MIGRATE_FAILED', moduleLog, {
|
|
details: {
|
|
reason: error instanceof Error ? error.message : 'unknown',
|
|
classified: classified ?? 'unclassified',
|
|
},
|
|
})
|
|
}
|
|
},
|
|
},
|
|
|
|
// ── Reconcile supplier invoices to GL payment vouchers ────────
|
|
// Re-runnable maintenance endpoint. The migration runs this automatically as
|
|
// its final step, but SIE (the GL) and entity import are two separate HTTP
|
|
// requests whose order is UI-driven: so if the GL lands after the entity
|
|
// import, or a company was migrated before this feature existed, call this to
|
|
// auto-link settled supplier invoices to their existing vouchers. Pass
|
|
// { dryRun: true } to preview the plan (incl. items needing manual review)
|
|
// without writing.
|
|
{
|
|
method: 'POST',
|
|
path: '/reconcile',
|
|
handler: async (request: Request, ctx?: ExtensionContext) => {
|
|
const log = ctx?.log ?? console
|
|
const supabase = ctx?.supabase ?? await (await import('@/lib/supabase/server')).createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const companyId = ctx?.companyId ?? user.id
|
|
|
|
let dryRun = false
|
|
try {
|
|
const body = (await request.json()) as { dryRun?: boolean }
|
|
dryRun = body?.dryRun === true
|
|
} catch {
|
|
// empty body is fine: default to a real run
|
|
}
|
|
|
|
try {
|
|
const result = await reconcileSupplierInvoiceVouchers({
|
|
supabase,
|
|
companyId,
|
|
userId: user.id,
|
|
dryRun,
|
|
})
|
|
log.info('arcim reconcile completed', {
|
|
companyId,
|
|
dryRun,
|
|
autoLinked: result.autoLinked,
|
|
ambiguous: result.ambiguous,
|
|
unmatched: result.unmatched,
|
|
})
|
|
return NextResponse.json({ success: true, dryRun, result })
|
|
} catch (error) {
|
|
log.error('arcim reconcile failed', error as Error)
|
|
return errorResponseFromCode('PROVIDER_MIGRATE_FAILED', moduleLog, {
|
|
details: { reason: error instanceof Error ? error.message : 'unknown' },
|
|
})
|
|
}
|
|
},
|
|
},
|
|
|
|
// ── Import provider underlag (receipts) and link to verifikat ──
|
|
// Best-effort, re-runnable. Kept off the migration's critical path: the
|
|
// Bokio document API is rate-limited (200 req/60s) and a full receipt
|
|
// sweep issues hundreds of download calls, which would blow the 300s
|
|
// migration window. Pages /uploads, resolves each receipt's verifikat via
|
|
// the SIE-preserved Bokio voucher number, and archives it idempotently
|
|
// (skips content already stored for the company). Pass { dryRun: true } to
|
|
// preview the match plan without downloading or writing.
|
|
{
|
|
method: 'POST',
|
|
path: '/import-documents',
|
|
handler: async (request: Request, ctx?: ExtensionContext) => {
|
|
const log = ctx?.log ?? console
|
|
const supabase = ctx?.supabase ?? await (await import('@/lib/supabase/server')).createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const companyId = ctx?.companyId ?? user.id
|
|
|
|
let consentId: string | undefined
|
|
let dryRun = false
|
|
try {
|
|
const body = (await request.json()) as { consentId?: string; dryRun?: boolean }
|
|
consentId = body?.consentId
|
|
dryRun = body?.dryRun === true
|
|
} catch {
|
|
// empty/invalid body: consentId check below rejects it
|
|
}
|
|
|
|
if (!consentId) {
|
|
return NextResponse.json({ error: 'consentId is required' }, { status: 400 })
|
|
}
|
|
|
|
try {
|
|
const result = await importProviderDocuments({
|
|
supabase,
|
|
companyId,
|
|
userId: user.id,
|
|
consentId,
|
|
dryRun,
|
|
})
|
|
log.info('arcim import-documents completed', {
|
|
companyId,
|
|
dryRun,
|
|
scanned: result.scanned,
|
|
linked: result.linked,
|
|
skipped: result.skipped,
|
|
unmatched: result.unmatched,
|
|
failed: result.failed,
|
|
})
|
|
return NextResponse.json({ success: true, dryRun, result })
|
|
} catch (error) {
|
|
log.error('arcim import-documents failed', error as Error)
|
|
return errorResponseFromCode('PROVIDER_IMPORT_DOCUMENTS_FAILED', moduleLog, {
|
|
details: { reason: error instanceof Error ? error.message : 'unknown' },
|
|
})
|
|
}
|
|
},
|
|
},
|
|
|
|
// ── Accept consent (mark as fully connected after import) ─────
|
|
{
|
|
method: 'POST',
|
|
path: '/accept',
|
|
handler: async (request: Request, ctx?: ExtensionContext) => {
|
|
const supabase = ctx?.supabase ?? await (await import('@/lib/supabase/server')).createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const companyId = ctx?.companyId ?? user.id
|
|
const { consentId } = await request.json() as { consentId: string }
|
|
if (!consentId) {
|
|
return NextResponse.json({ error: 'consentId is required' }, { status: 400 })
|
|
}
|
|
|
|
// Verify consent belongs to this company before mutating
|
|
const { data: consent } = await supabase
|
|
.from('provider_consents')
|
|
.select('id')
|
|
.eq('id', consentId)
|
|
.eq('company_id', companyId)
|
|
.single()
|
|
|
|
if (!consent) {
|
|
return errorResponseFromCode('PROVIDER_CONSENT_NOT_FOUND', moduleLog)
|
|
}
|
|
|
|
try {
|
|
await acceptConsent(consentId)
|
|
return NextResponse.json({ success: true })
|
|
} catch (error) {
|
|
moduleLog.error('arcim accept failed', error as Error, { consentId })
|
|
return errorResponseFromCode('PROVIDER_ACCEPT_FAILED', moduleLog, {
|
|
details: { reason: error instanceof Error ? error.message : 'unknown' },
|
|
})
|
|
}
|
|
},
|
|
},
|
|
|
|
// ── Disconnect / revoke consent ───────────────────────────────
|
|
{
|
|
method: 'DELETE',
|
|
path: '/disconnect',
|
|
handler: async (request: Request, ctx?: ExtensionContext) => {
|
|
const log = ctx?.log ?? console
|
|
const supabase = ctx?.supabase ?? await (await import('@/lib/supabase/server')).createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const companyId = ctx?.companyId ?? user.id
|
|
const { consentId } = await request.json() as { consentId: string }
|
|
|
|
if (!consentId) {
|
|
return NextResponse.json({ error: 'consentId is required' }, { status: 400 })
|
|
}
|
|
|
|
// Verify consent belongs to this company before mutating
|
|
const { data: consent } = await supabase
|
|
.from('provider_consents')
|
|
.select('id')
|
|
.eq('id', consentId)
|
|
.eq('company_id', companyId)
|
|
.single()
|
|
|
|
if (!consent) {
|
|
return errorResponseFromCode('PROVIDER_CONSENT_NOT_FOUND', moduleLog)
|
|
}
|
|
|
|
try {
|
|
await deleteConsent(consentId)
|
|
|
|
if (ctx?.settings) {
|
|
await ctx.settings.clear('consent_id')
|
|
await ctx.settings.clear('provider')
|
|
}
|
|
|
|
return NextResponse.json({ success: true })
|
|
} catch (error) {
|
|
log.error('arcim disconnect failed', error as Error, { consentId })
|
|
return errorResponseFromCode('PROVIDER_DISCONNECT_FAILED', moduleLog, {
|
|
details: { reason: error instanceof Error ? error.message : 'unknown' },
|
|
})
|
|
}
|
|
},
|
|
},
|
|
],
|
|
|
|
eventHandlers: [],
|
|
}
|