fix(skattekonto): deep-link Skapa verifikat manuellt to a prefilled, auto-linked verifikat (#1621)

* fix(skattekonto): deep-link Skapa verifikat manuellt to a prefilled, auto-linked verifikat

"Skapa verifikat manuellt" in the SkattekontoBookDialog routed to plain
/bookkeeping: the user landed on the list with no form, no prefill and no
link to the row (reported by a user for a Slutlig skatt event, which has
no booking rule by design).

The CTA now deep-links to /bookkeeping?skv_tx=... carrying the row's id,
date, text and amount. The bookkeeping page opens the Nytt verifikat
dialog prefilled (1630 on the correct side per the booking sign
convention, balanced counter line with the motkonto left to pick, date
and description set) and, once the verifikat is saved (posted or draft),
links it back to the skattekonto row via the existing match endpoint. A
failed link degrades to a destructive toast pointing at the manual
"Matcha mot verifikat" path.

The URL params are prefill convenience only: the match route re-validates
ownership, ALREADY_BOOKED and ENTRY_ALREADY_LINKED server-side. The
parse/build/line-shaping contract lives in core lib
(lib/skatteverket/manual-verifikat-prefill.ts, unit-tested) because the
bookkeeping page cannot import from the extension.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(skattekonto): keep deep-link payload out of the URL + share the 1630 constant

Resolves the PR #1621 review findings in one pass:

- Compliance swarm (GDPR Art.5(1)(f), ISO A.8.12): the deep link no longer
  carries date, text and amount as query params, where they would persist
  in browser history, access logs and Referer headers. The row payload is
  staged in sessionStorage, consumed single-use and validated against the
  opaque skv_tx id, which is all the URL exposes. A missing or mismatched
  payload degrades to the plain /bookkeeping list; the auto-link itself is
  still validated server-side by the match route.
- Swedish accounting review note: SKATTEKONTO_ACCOUNT ('1630') is now
  imported by the extension's booking and match libs from the core prefill
  lib instead of being duplicated, so prefill and server-side booking
  cannot drift.
- CodeRabbit docstring warning: the new lib exports carry docstrings.

Storage is injectable (PrefillStorage) so the node-env tests cover the
round-trip, single-use semantics, id mismatch, malformed payloads and a
throwing privacy-mode storage.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* docs(skattekonto): record the sessionStorage staging window as accepted residual risk

The compliance swarm's remaining LOW finding (ISO A.8.12) offers
documentation as its remediation path: an XSS attacker already reads the
full ledger via the session's authenticated APIs, so the sub-second
sessionStorage staging window adds no capability worth a server-issued
token roundtrip. Recorded in the lib header and DECISIONS.md.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Mattsson
2026-08-15 15:10:06 +02:00
committed by GitHub
co-authored by Claude Fable 5
parent 6404591b89
commit 4362bffc0c
10 changed files with 480 additions and 12 deletions
@@ -1,7 +1,7 @@
'use client'
import { useTranslations } from 'next-intl'
import { Copy, Loader2 } from 'lucide-react'
import { Copy, Link2, Loader2 } from 'lucide-react'
import {
Dialog,
DialogContent,
@@ -18,13 +18,28 @@ export interface CopyPrefill {
notes: string
}
/** Prefill for the skattekonto "Skapa verifikat manuellt" deep link. */
export interface SkvLinkPrefill {
transactionId: string
/** Row summary for the banner: date, text and amount, built by the caller. */
bannerLabel: string
lines: FormLine[]
description: string
date: string
}
interface Props {
open: boolean
onOpenChange: (open: boolean) => void
/** Fired after a verifikat is created/saved as draft. */
onCreated: () => void
/** Fired with the created entry's id (both posted and draft saves). */
onEntryCreated?: (entryId: string) => void
/** When set, the form is pre-filled from a copied verifikat. */
copyPrefill?: CopyPrefill | null
/** When set, the form is pre-filled from a skattekonto row and the caller
* links the created entry back to it. copyPrefill wins if both are set. */
skvPrefill?: SkvLinkPrefill | null
/** True while the copy source is being fetched. */
isLoading?: boolean
}
@@ -38,10 +53,13 @@ export default function NewJournalEntryDialog({
open,
onOpenChange,
onCreated,
onEntryCreated,
copyPrefill,
skvPrefill,
isLoading,
}: Props) {
const t = useTranslations('bookkeeping')
const activeSkvPrefill = copyPrefill ? null : (skvPrefill ?? null)
return (
<Dialog open={open} onOpenChange={onOpenChange}>
@@ -81,12 +99,25 @@ export default function NewJournalEntryDialog({
</div>
</div>
)}
{activeSkvPrefill && (
<div className="flex items-start gap-3 rounded-lg border border-border bg-muted/30 p-3 text-sm">
<Link2 className="h-4 w-4 mt-0.5 shrink-0 text-muted-foreground" />
<div className="flex-1">
<p className="font-medium">
{t('skv_link_banner_title', { label: activeSkvPrefill.bannerLabel })}
</p>
<p className="text-muted-foreground mt-0.5">{t('skv_link_banner_body')}</p>
</div>
</div>
)}
<JournalEntryForm
key={copyPrefill?.sourceId ?? 'fresh'}
key={copyPrefill?.sourceId ?? activeSkvPrefill?.transactionId ?? 'fresh'}
bare
onCreated={onCreated}
initialLines={copyPrefill?.lines}
initialDescription={copyPrefill?.description}
onEntryCreated={onEntryCreated}
initialLines={copyPrefill?.lines ?? activeSkvPrefill?.lines}
initialDate={activeSkvPrefill?.date}
initialDescription={copyPrefill?.description ?? activeSkvPrefill?.description}
initialNotes={copyPrefill?.notes}
/>
</>
@@ -7,6 +7,7 @@ import { Button } from '@/components/ui/button'
import { ConfirmationDialog } from '@/components/ui/confirmation-dialog'
import { useToast } from '@/components/ui/use-toast'
import { getErrorMessage } from '@/lib/errors/get-error-message'
import { stageSkvManualPrefill } from '@/lib/skatteverket/manual-verifikat-prefill'
import { cn, formatCurrency, formatDate } from '@/lib/utils'
import { Loader2 } from 'lucide-react'
import type {
@@ -159,10 +160,15 @@ export default function SkattekontoBookDialog({
}
function handleManualCreate() {
// /bookkeeping owns manual verifikat creation ("Nytt verifikat" in the
// page header); there is no dedicated create route to deep-link.
if (!row) return
// Deep-link into /bookkeeping's Nytt verifikat dialog: the row payload is
// staged in sessionStorage (only the opaque id rides in the URL) so the
// form opens prefilled (1630 + counter line) and the created verifikat is
// auto-linked back to this skattekonto row via the match endpoint. Plain
// '/bookkeeping' here was a dead end: the user landed on the list with no
// form, no prefill and no link.
onOpenChange(false)
router.push('/bookkeeping')
router.push(stageSkvManualPrefill(row))
}
return (