* feat: add option to exclude year-end closing entries in SIE export and related reports * delete docs * fix: allow Chrome's PDF viewer in verifikat document preview The /api/documents/:id/inline route shipped with `object-src 'none'` in its CSP, which blocked Chrome's built-in PDF viewer (it renders inline PDFs via an internal <embed>). Users on Chrome saw "Det här innehållet har blockerats" when expanding a PDF attachment in the bookkeeping view; Firefox (PDF.js) and Edge (own viewer) were unaffected, and JPGs worked because <img> isn't subject to object-src. Drops the CSP for this route to the minimum needed for embeddability: `frame-ancestors 'self'`. X-Content-Type-Options: nosniff plus the fixed Content-Type from the handler already block MIME confusion; X-Frame-Options: SAMEORIGIN + frame-ancestors still block clickjacking. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * feat(auth): add webmail deep link to email confirmation screens Mirrors Stripe's signup UX: after asking the user to verify their email, detect their webmail provider from the domain and show a button that opens the inbox in a new tab. Gmail gets a from:<sender> search pre-populated; Outlook/Yahoo/iCloud/Proton open the inbox directly. Unknown / custom domains fall back to the existing copy. Sender address is configurable via NEXT_PUBLIC_BRANDING_AUTH_EMAIL_FROM (default noreply@gnubok.se) so white-label installs can match their Supabase Auth SMTP config. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(auth): unblock first-time password set for BankID users with MFA Supabase rejects updateUser({password}) and mfa.unenroll with "AAL2 session is required" whenever a TOTP factor is enrolled. BankID magic-link logins produce AAL1, and middleware skips MFA enforcement for bankid_linked users, so they had no path to AAL2 — leaving them unable to set a backup password or disable MFA without going through the email-recovery escape hatch. - /api/account/password: branch on app_metadata.has_password. First-time set writes via service.auth.admin.updateUserById (no existing credential to protect, AAL2 guard does not apply). Change-password keeps the user-session updateUser so AAL2 still fires for credential rotation. - /mfa/verify: accept a safeReturnTo query param and route there after successful verify, so step-up flows can land back where they came from. - SecuritySettings: detect the AAL2 error from both change-password and mfa.unenroll and redirect through /mfa/verify?returnTo=/settings/account instead of toasting a dead-end error. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * Add tests and rounding utility for öre precision in bokslut calculations - Implemented `roundOre` function for rounding SEK amounts to two decimal places, ensuring consistent monetary calculations. - Introduced `ORE_TOLERANCE` constant for comparing rounded amounts, facilitating invariant checks in financial entries. - Created comprehensive tests for `roundOre`, covering typical cases, edge cases, and idempotency. - Added year-end invariants tests to verify database-level guarantees for closing entries, ensuring they balance to the öre and reject discrepancies. - Developed end-to-end tests for the dispositions chain, validating the correctness of calculations across various scenarios. * fix: update PDF rendering to remove Swish QR code generation and set default to disable Swish visibility * fix: enhance security by rejecting data URIs in safeReturnTo function tests * fix: improve rounding logic in roundOre function and add customer_type migration * fix: add customer_type column to customers and enforce CHECK constraint --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
192 lines
6.6 KiB
TypeScript
192 lines
6.6 KiB
TypeScript
import { createClient } from '@/lib/supabase/server'
|
|
import { getActiveCompanyId, requireCompanyId } from '@/lib/company/context'
|
|
import { requireWritePermission } from '@/lib/auth/require-write'
|
|
import { validateBody } from '@/lib/api/validate'
|
|
import { AccountingFrameworkSchema } from '@/lib/api/schemas'
|
|
import { getBASReference } from '@/lib/bookkeeping/bas-reference'
|
|
import { createLogger } from '@/lib/logger'
|
|
import { NextResponse } from 'next/server'
|
|
import { z } from 'zod'
|
|
|
|
const log = createLogger('api/company/current')
|
|
|
|
// BAS 2026 accounts required for K3's uppskjuten skatt (latent tax) entries.
|
|
// Both rows carry k2_excluded=true in lib/bookkeeping/bas-data so they are
|
|
// NOT seeded by seed_chart_of_accounts() for K2 companies. When a company
|
|
// opts into K3 we backfill them here so the engine can resolve them by
|
|
// account_number when the first latent-tax entry is posted.
|
|
const K3_LATENT_TAX_ACCOUNTS = ['2240', '8940'] as const
|
|
|
|
/**
|
|
* GET /api/company/current
|
|
*
|
|
* Returns the active company id for the authenticated user. Used by the
|
|
* client-side CompanyTabSync listener to detect cross-tab divergence (e.g.
|
|
* when a tab was hidden/backgrounded during a switch in another tab) and
|
|
* force a hard reload on mismatch.
|
|
*
|
|
* Never cached — the whole point is that the response reflects the current
|
|
* authoritative value in user_preferences.
|
|
*/
|
|
export async function GET() {
|
|
const supabase = await createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
return NextResponse.json(
|
|
{ error: 'Unauthorized' },
|
|
{
|
|
status: 401,
|
|
headers: { 'Cache-Control': 'private, no-store' },
|
|
},
|
|
)
|
|
}
|
|
|
|
const companyId = await getActiveCompanyId(supabase, user.id)
|
|
|
|
return NextResponse.json(
|
|
{ companyId },
|
|
{ headers: { 'Cache-Control': 'private, no-store' } },
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Body shape for PATCH /api/company/current.
|
|
*
|
|
* Currently only carries `accounting_framework` (K2 / K3). Adding more
|
|
* companies-level fields here is fine but anything that belongs on
|
|
* company_settings should go to /api/settings instead.
|
|
*/
|
|
const PatchBodySchema = z.object({
|
|
accounting_framework: AccountingFrameworkSchema.optional(),
|
|
})
|
|
|
|
/**
|
|
* PATCH /api/company/current
|
|
*
|
|
* Updates company-level fields (in the `companies` table) for the active
|
|
* company. Separate from /api/settings (which writes to `company_settings`)
|
|
* because the columns live on different tables.
|
|
*
|
|
* Currently scoped to `accounting_framework` (K2 / K3) — only meaningful for
|
|
* entity_type='aktiebolag'. The handler rejects K3 for non-AB to prevent
|
|
* impossible chart-of-accounts states downstream.
|
|
*/
|
|
export async function PATCH(request: Request) {
|
|
const supabase = await createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const writeCheck = await requireWritePermission(supabase, user.id)
|
|
if (!writeCheck.ok) return writeCheck.response
|
|
|
|
const companyId = await requireCompanyId(supabase, user.id)
|
|
|
|
const validation = await validateBody(request, PatchBodySchema)
|
|
if (!validation.success) return validation.response
|
|
|
|
const updates: Record<string, unknown> = {}
|
|
|
|
if (validation.data.accounting_framework !== undefined) {
|
|
// Only AB can opt in to K3 — EF stays on the simpler EF rules and never
|
|
// touches K2/K3. Fetch the entity_type before applying.
|
|
const { data: company } = await supabase
|
|
.from('companies')
|
|
.select('entity_type')
|
|
.eq('id', companyId)
|
|
.single()
|
|
if (!company) {
|
|
return NextResponse.json(
|
|
{ error: 'Företaget kunde inte hittas' },
|
|
{ status: 404 },
|
|
)
|
|
}
|
|
if (
|
|
validation.data.accounting_framework === 'k3'
|
|
&& company.entity_type !== 'aktiebolag'
|
|
) {
|
|
return NextResponse.json(
|
|
{ error: 'K3 (BFNAR 2012:1) gäller endast aktiebolag.' },
|
|
{ status: 400 },
|
|
)
|
|
}
|
|
updates.accounting_framework = validation.data.accounting_framework
|
|
}
|
|
|
|
if (Object.keys(updates).length === 0) {
|
|
// Nothing to write — surface the current row so the client can refresh
|
|
// its local state without a no-op write.
|
|
const { data } = await supabase
|
|
.from('companies')
|
|
.select('id, accounting_framework, entity_type')
|
|
.eq('id', companyId)
|
|
.single()
|
|
return NextResponse.json({ data })
|
|
}
|
|
|
|
const { data, error } = await supabase
|
|
.from('companies')
|
|
.update(updates)
|
|
.eq('id', companyId)
|
|
.select('id, accounting_framework, entity_type')
|
|
.single()
|
|
|
|
if (error) {
|
|
return NextResponse.json({ error: error.message }, { status: 500 })
|
|
}
|
|
|
|
// When opting in to K3, ensure the two latent-tax (uppskjuten skatt)
|
|
// accounts exist in the company's chart of accounts. The base seed skips
|
|
// them for K2 companies via k2_excluded=true, so without this backfill
|
|
// the engine cannot resolve account_id for the first latent-tax post.
|
|
// Wrapped in try/catch so a CoA insert failure does not block the
|
|
// framework update — the user can still re-trigger the seed later.
|
|
// The reverse switch (K3 → K2) intentionally keeps the rows for audit
|
|
// history; the legal record of past K3 postings must remain intact.
|
|
if (data.accounting_framework === 'k3') {
|
|
try {
|
|
const rows = K3_LATENT_TAX_ACCOUNTS.map(accountNumber => {
|
|
const basRef = getBASReference(accountNumber)
|
|
if (!basRef) return null
|
|
return {
|
|
user_id: user.id,
|
|
company_id: companyId,
|
|
account_number: basRef.account_number,
|
|
account_name: basRef.account_name,
|
|
account_class: basRef.account_class,
|
|
account_group: basRef.account_group,
|
|
account_type: basRef.account_type,
|
|
normal_balance: basRef.normal_balance,
|
|
sru_code: basRef.sru_code,
|
|
k2_excluded: basRef.k2_excluded,
|
|
plan_type: 'full_bas',
|
|
is_active: true,
|
|
is_system_account: true,
|
|
description: basRef.description,
|
|
}
|
|
}).filter((row): row is NonNullable<typeof row> => row !== null)
|
|
|
|
if (rows.length > 0) {
|
|
const { error: seedError } = await supabase
|
|
.from('chart_of_accounts')
|
|
.upsert(rows, { onConflict: 'company_id,account_number', ignoreDuplicates: true })
|
|
if (seedError) {
|
|
log.error('Failed to seed K3 latent-tax accounts', {
|
|
companyId,
|
|
error: seedError.message,
|
|
})
|
|
}
|
|
}
|
|
} catch (err) {
|
|
log.error('Unexpected error seeding K3 latent-tax accounts', {
|
|
companyId,
|
|
error: err instanceof Error ? err.message : String(err),
|
|
})
|
|
}
|
|
}
|
|
|
|
return NextResponse.json({ data })
|
|
}
|