feat(invoice-inbox): surface WhatsApp chat context in booking flows (#1339)

* feat(invoice-inbox): surface WhatsApp chat context in booking flows

The WhatsApp intake bot writes verified human answers (photo caption,
representation deltagare + syfte, sender note, open-question state) to
invoice_inbox_items.channel_context. This makes the in-app booking flows
READ it:

- New core renderer lib/documents/channel-context-notes.ts: deterministic
  compact Swedish line ("Representation: Anna Berg (Volvo), Jakob W ·
  Syfte: uppföljning av avtal"), capped at 220 chars by dropping whole
  participant names ("… och N till"), never mid-name. Representation
  first, then user_note; caption only when nothing else exists.
- FieldsRail "Från WhatsApp" block in InvoiceInboxWorkspace: caption,
  deltagare, syfte, anteckning rows plus an ochre AttnLine when a chat
  question expired unanswered (pending_question.status = moved_to_app).
- Notes threading: book-direct and convert default their notes to the
  rendered string server-side when the request carries none (a supplied
  value always wins); BookDirectlyDialog prefills its notes input with
  the same string so the user can edit it before it lands. Bulk-book
  (categorize-core) joins the shared batch note with the per-item
  rendered context so the representation trail survives batch booking.
- Inbox list: whatsapp rows get a chat icon and a quiet "Fråga obesvarad"
  badge for moved_to_app items. No worklist count change: unresolved
  whatsapp items are already counted by countInboxDocuments.
- sv/en strings for every new key; renderer + route + bulk tests.

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

* fix(invoice-inbox): honor cleared notes and keep unreviewed captions out of verifikat

Two adversarial-review findings on the WhatsApp surfacing flows, both about
text that lands on an immutable verifikat.

1. A cleared note was silently re-applied. BookDirectlyDialog prefills the
   rendered chat context, and the dialog sent `notes.trim() || undefined`
   while book-direct and convert defaulted from channel_context on any falsy
   value. A user who read the prefill, disagreed and deleted it therefore got
   it written back onto a posted entry, removable only through a formal
   rättelse. Both code comments claimed "an edited value always wins", which
   was false for exactly that edit. Now PRESENCE of the field decides: the
   dialog always submits `notes` (empty string included) and the routes only
   default when the field is absent from the request (MCP, older clients).
   The Zod `.optional()` carrying that distinction is documented at the
   schema so it is not "tidied" into a `.default('')` later.

2. The photo caption was auto-burned into verifikat text with no review.
   renderChannelContextNotes fell back to the raw caption, and bulk-book
   appended the result per item with no per-item notes field at all (the MCP
   approval preview deliberately shows no per-item PII either), so unreviewed
   chat text reached a WORM record nobody had seen. The renderer now takes
   { includeCaption } and leaves the caption out by DEFAULT: representation
   answers and user_note are replies to a question the bot asked, the caption
   is not. Only the Bokför direkt prefill opts in, where the user reads the
   string in an editable field before booking.

Tests: cleared-notes and whitespace-cleared on book-direct, cleared-notes on
convert, caption-never-defaulted on both routes, caption-not-threaded in
bulk-book, and the renderer's opt-in. The book-direct cleared-notes tests
were mutation-checked (restoring the truthiness fallback fails them).

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

* fix(reports): export the chat answers behind a verifikat in the full archive

The verifikat line caps the representation trail at 220 chars and drops whole
participant names ("… och N till"); the complete list (deltagare, syfte,
raw_answer) exists only in invoice_inbox_items.channel_context. That table was
in ARCHIVE_EXCLUDED_TABLES with a rationale predating channel_context ("inbox
workflow state"), so a company leaving Accounted and keeping the full-archive
export as its BFL 7-year record kept an incomplete deltagare documentation for
its representation deductions.

Dumped as a column PROJECTION, not the whole row: the new
MasterDataTableSpec.columns narrows the select to the underlag provenance
(document, matched transaction, created verifikat / leverantörsfaktura) plus
channel_context, so the answers are tied to what was booked from them while
the inbox workflow state (email bodies, OCR output, error messages) stays out
of the archive. The documents themselves remain in dokument/.

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

---------

Co-authored-by: Jakob Wennberg <jakob.wennberg@gmail.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-08-05 14:54:30 +02:00
committed by GitHub
co-authored by Claude Fable 5 Jakob Wennberg
parent 398c734b93
commit d1c411ad6f
15 changed files with 1124 additions and 20 deletions
+6
View File
@@ -1383,6 +1383,12 @@ export const BookInboxItemDirectlySchema = z.object({
fiscal_period_id: uuid,
entry_date: isoDate,
description: z.string().min(1, 'Beskrivning krävs'),
// `.optional()` here carries meaning the route depends on: ABSENT means
// "caller has no opinion", so the route may default the notes from the
// item's chat context, while an explicit '' means the user cleared the
// prefilled note and nothing must be written back onto the verifikat.
// Keep it `.optional()`, never `.default('')` or a min(1): both would
// collapse those two cases into one.
notes: z.string().max(2000).optional(),
lines: z.array(CreateJournalEntryLineSchema).min(2, 'Minst två rader krävs för dubbel bokföring'),
transaction_id: uuid.optional(),
@@ -0,0 +1,210 @@
/**
* renderChannelContextNotes: the one string WhatsApp chat context becomes on
* its way into a verifikat description. Pins precedence (representation >
* user_note > caption), the caption opt-in, the 220-char cap, and whole-name
* participant truncation ("… och N till", never a name cut mid-way).
*/
import { describe, it, expect } from 'vitest'
import {
renderChannelContextNotes,
renderChannelParticipant,
CHANNEL_CONTEXT_NOTES_MAX,
} from '../channel-context-notes'
import type { InboxChannelContext } from '@/types'
function repCtx(
participants: { name: string; company: string | null }[],
purpose: string | null = null,
extra: Partial<InboxChannelContext> = {},
): InboxChannelContext {
return {
channel: 'whatsapp',
representation: {
participants,
purpose,
event_date: null,
raw_answer: 'raw',
answered_at: '2026-08-01T12:00:00Z',
},
...extra,
}
}
describe('renderChannelContextNotes', () => {
it('renders a full representation answer: participants with company, then purpose', () => {
const ctx = repCtx(
[
{ name: 'Anna Berg', company: 'Volvo' },
{ name: 'Jakob W', company: null },
],
'uppföljning av avtal',
)
expect(renderChannelContextNotes(ctx)).toBe(
'Representation: Anna Berg (Volvo), Jakob W · Syfte: uppföljning av avtal',
)
})
it('renders a self participant (no company) as the bare name', () => {
expect(renderChannelParticipant({ name: ' Jakob W ', company: null })).toBe('Jakob W')
expect(renderChannelParticipant({ name: 'Anna', company: ' ' })).toBe('Anna')
const ctx = repCtx([{ name: 'Jakob W', company: null }], 'lunch med kund')
expect(renderChannelContextNotes(ctx)).toBe(
'Representation: Jakob W · Syfte: lunch med kund',
)
})
it('renders purpose alone when the participant list is empty', () => {
const ctx = repCtx([], 'kundmiddag')
expect(renderChannelContextNotes(ctx)).toBe('Syfte: kundmiddag')
})
it('truncates the participant list by whole names with "… och N till"', () => {
const participants = Array.from({ length: 12 }, (_, i) => ({
name: `Deltagare Efternamnsson ${i + 1}`,
company: 'Företagsnamnet Aktiebolag',
}))
const ctx = repCtx(participants, 'branschmässa i Göteborg')
const line = renderChannelContextNotes(ctx)!
expect(line.length).toBeLessThanOrEqual(CHANNEL_CONTEXT_NOTES_MAX)
expect(line).toMatch(/… och \d+ till/)
// Whole names only: every rendered participant appears in full or not at
// all. The first is always kept; the last is always dropped here.
expect(line).toContain('Deltagare Efternamnsson 1 (Företagsnamnet Aktiebolag)')
expect(line).not.toContain('Deltagare Efternamnsson 12')
// The dropped count accounts for every name not printed.
const [, dropped] = line.match(/… och (\d+) till/)!
const printed = participants.filter((p) => line.includes(`${p.name} (${p.company})`)).length
expect(printed + Number(dropped)).toBe(participants.length)
// No name was cut mid-way: the truncation marker follows a complete
// "(company)" closing paren, not a partial name.
expect(line).toMatch(/\(Företagsnamnet Aktiebolag\) … och \d+ till/)
// Purpose survives truncation.
expect(line).toContain('Syfte: branschmässa i Göteborg')
})
it('keeps at least one participant even when the line still overflows, then caps free text', () => {
const ctx = repCtx(
[
{ name: 'Anna Berg', company: 'Volvo' },
{ name: 'Bo Ek', company: null },
],
'x'.repeat(400),
)
const line = renderChannelContextNotes(ctx)!
expect(line.length).toBeLessThanOrEqual(CHANNEL_CONTEXT_NOTES_MAX)
expect(line).toContain('Representation: Anna Berg (Volvo)')
expect(line.endsWith('…')).toBe(true)
})
it('renders user_note alone', () => {
const ctx: InboxChannelContext = {
channel: 'whatsapp',
user_note: 'Parkering vid kundbesök i Uppsala',
}
expect(renderChannelContextNotes(ctx)).toBe('Parkering vid kundbesök i Uppsala')
})
it('appends user_note after the representation answer', () => {
const ctx = repCtx([{ name: 'Anna Berg', company: 'Volvo' }], 'avtalslunch', {
user_note: 'Betald privat',
})
expect(renderChannelContextNotes(ctx)).toBe(
'Representation: Anna Berg (Volvo) · Syfte: avtalslunch · Betald privat',
)
})
it('falls back to the caption only when nothing else exists AND it was asked for', () => {
const captionOnly: InboxChannelContext = {
channel: 'whatsapp',
caption: 'Kvitto taxi till kundmöte',
}
expect(renderChannelContextNotes(captionOnly, { includeCaption: true })).toBe(
'Kvitto taxi till kundmöte',
)
// Caption is ignored the moment an explicit answer exists.
const withNote: InboxChannelContext = {
channel: 'whatsapp',
caption: 'Kvitto taxi till kundmöte',
user_note: 'Resa till Arlanda',
}
expect(renderChannelContextNotes(withNote, { includeCaption: true })).toBe('Resa till Arlanda')
})
// The caption is chat text nobody was asked for and nobody reviewed, while
// every unattended caller (bulk-book, the routes' server-side defaults)
// writes the result into an immutable verifikat. Opt-in, never default.
it('leaves the caption out by default', () => {
const captionOnly: InboxChannelContext = {
channel: 'whatsapp',
caption: 'kvittot från igår, Annas sjukbesök, hon betalade',
}
expect(renderChannelContextNotes(captionOnly)).toBeNull()
expect(renderChannelContextNotes(captionOnly, {})).toBeNull()
expect(renderChannelContextNotes(captionOnly, { includeCaption: false })).toBeNull()
})
it('renders the answered parts and drops the caption when captions are off', () => {
const ctx = repCtx([{ name: 'Anna Berg', company: 'Volvo' }], 'avtalslunch', {
caption: 'privat text som ingen granskat',
user_note: 'Betald privat',
})
const line = renderChannelContextNotes(ctx)!
expect(line).toBe('Representation: Anna Berg (Volvo) · Syfte: avtalslunch · Betald privat')
expect(line).not.toContain('privat text som ingen granskat')
})
it('caps a runaway caption', () => {
const ctx: InboxChannelContext = { channel: 'whatsapp', caption: 'k'.repeat(500) }
const line = renderChannelContextNotes(ctx, { includeCaption: true })!
expect(line.length).toBeLessThanOrEqual(CHANNEL_CONTEXT_NOTES_MAX)
expect(line.endsWith('…')).toBe(true)
})
it('returns null for empty input', () => {
expect(renderChannelContextNotes(null)).toBeNull()
expect(renderChannelContextNotes(undefined)).toBeNull()
expect(renderChannelContextNotes({ channel: 'whatsapp' })).toBeNull()
expect(
renderChannelContextNotes({
channel: 'whatsapp',
caption: 'ignorerad utan opt-in',
user_note: '',
representation: {
participants: [{ name: ' ', company: null }],
purpose: ' ',
event_date: null,
raw_answer: 'nej',
answered_at: '2026-08-01T12:00:00Z',
},
}),
).toBeNull()
expect(
renderChannelContextNotes(
{
channel: 'whatsapp',
caption: ' ',
user_note: '',
representation: {
participants: [{ name: ' ', company: null }],
purpose: ' ',
event_date: null,
raw_answer: 'nej',
answered_at: '2026-08-01T12:00:00Z',
},
},
{ includeCaption: true },
),
).toBeNull()
})
it('is deterministic (same input, same output)', () => {
const ctx = repCtx(
[{ name: 'Anna Berg', company: 'Volvo' }],
'uppföljning av avtal',
{ user_note: 'Kortköp' },
)
expect(renderChannelContextNotes(ctx)).toBe(renderChannelContextNotes(ctx))
})
})
+119
View File
@@ -0,0 +1,119 @@
/**
* Render the verified human answers captured on a chat-sourced inbox item
* (invoice_inbox_items.channel_context, written by the WhatsApp intake bot)
* as ONE compact Swedish line for the booking notes path.
*
* The rendered string travels through the existing `notes` parameters
* (book-direct, convert, bulk-book via categorize-core) and ends up appended
* to the verifikat description, which caps at 500 chars in
* lib/bookkeeping/transaction-entries.ts. This renderer therefore stays well
* under that: at most CHANNEL_CONTEXT_NOTES_MAX chars, truncating the
* participant list by WHOLE names ("… och 3 till"), never mid-name.
*
* Precedence: representation answers first (Skatteverket's dokumentationskrav
* for representation: deltagare + syfte belong on the verifikat), then the
* sender's explicit note. Both are answers a human typed to a question the bot
* asked, having been told they reach the bookkeeping. The photo caption is the
* weakest signal: nobody was asked for it and nobody reviewed it, so it is
* OFF by default and only renders where a human sees the result before it is
* posted (see ChannelContextNotesOptions.includeCaption).
*
* Core lib: must not import from @/extensions. Deliberately Swedish-only
* output: the verifikat description is a regulatory surface (see
* .claude/rules/i18n.md), not UI chrome.
*/
import type { InboxChannelContext } from '@/types'
/**
* Cap on the rendered line. 220 leaves the description's 500-char cap plenty
* of room for the bank text / supplier prefix it is appended to.
*/
export const CHANNEL_CONTEXT_NOTES_MAX = 220
/** "Anna Berg (Volvo)" with a company, bare "Jakob W" without (the sender
* themselves usually answers without naming their own company). */
export function renderChannelParticipant(p: {
name: string
company: string | null
}): string {
const name = (p.name ?? '').trim()
if (!name) return ''
const company = (p.company ?? '').trim()
return company ? `${name} (${company})` : name
}
function buildLine(
names: string[],
droppedCount: number,
purpose: string | null,
userNote: string | null,
): string {
const parts: string[] = []
if (names.length > 0) {
const suffix = droppedCount > 0 ? ` … och ${droppedCount} till` : ''
parts.push(`Representation: ${names.join(', ')}${suffix}`)
}
if (purpose) parts.push(`Syfte: ${purpose}`)
if (userNote) parts.push(userNote)
return parts.join(' · ')
}
/** Last-resort cap for free text (purpose/note/caption): the whole-name rule
* above governs the participant list; a runaway free-text field is cut with
* an ellipsis instead. Result is always <= CHANNEL_CONTEXT_NOTES_MAX. */
function capFreeText(line: string): string {
if (line.length <= CHANNEL_CONTEXT_NOTES_MAX) return line
return `${line.slice(0, CHANNEL_CONTEXT_NOTES_MAX - 1).trimEnd()}…`
}
export interface ChannelContextNotesOptions {
/**
* Render the raw photo caption when there is neither a representation
* answer nor a sender note. Default false.
*
* Off by default on purpose. The representation answer and the note are
* replies to a question the bot asked, so the sender knew they were writing
* bookkeeping text; the caption is whatever happened to be typed next to a
* photo and nobody reviewed it. Every unattended path (bulk-book, the
* server-side note defaults used by MCP and API callers) writes straight
* into a posted verifikat, which BFL 5 kap 5 § only lets you change through
* a formal rättelse. Pass true only where a human sees the string and can
* edit or delete it before booking: today that is the Bokför direkt dialog
* prefill.
*/
includeCaption?: boolean
}
export function renderChannelContextNotes(
ctx: InboxChannelContext | null | undefined,
options: ChannelContextNotesOptions = {},
): string | null {
if (!ctx) return null
const names = (ctx.representation?.participants ?? [])
.map(renderChannelParticipant)
.filter((n) => n.length > 0)
const purpose = ctx.representation?.purpose?.trim() || null
const userNote = ctx.user_note?.trim() || null
const caption = options.includeCaption ? ctx.caption?.trim() || null : null
// Caption (when allowed) only if there is neither a representation answer
// nor a note.
if (names.length === 0 && !purpose && !userNote) {
return caption ? capFreeText(caption) : null
}
// Full participant list first; drop whole names from the end until the
// line fits. Keeps at least one name so the representation trail never
// degrades to a bare count.
let keep = names.length
for (;;) {
const line = buildLine(names.slice(0, keep), names.length - keep, purpose, userNote)
if (line.length <= CHANNEL_CONTEXT_NOTES_MAX) return line
if (keep > 1) {
keep--
continue
}
return capFreeText(line)
}
}
@@ -118,6 +118,7 @@ function buildMasterDataQueue(opts: {
describe('generateFullArchive', () => {
let supabase: ReturnType<typeof createQueuedMockSupabase>['supabase']
let enqueueMany: ReturnType<typeof createQueuedMockSupabase>['enqueueMany']
let findCall: ReturnType<typeof createQueuedMockSupabase>['findCall']
beforeEach(() => {
vi.clearAllMocks()
@@ -125,6 +126,7 @@ describe('generateFullArchive', () => {
const mock = createQueuedMockSupabase()
supabase = mock.supabase
enqueueMany = mock.enqueueMany
findCall = mock.findCall
})
describe('scope: period', () => {
@@ -825,6 +827,72 @@ describe('generateFullArchive', () => {
])
})
// The archive is what a company leaving Accounted keeps as its BFL
// 7-year record. A representation answer given in chat is documented in
// full only in invoice_inbox_items.channel_context: the verifikat line
// caps at 220 chars and drops whole names ("… och N till"), while
// Skatteverket wants every deltagare. So the answers must be exported.
it('exports the chat answers behind a verifikat, in full', async () => {
const participants = Array.from({ length: 10 }, (_, i) => ({
name: `Deltagare Efternamnsson ${i + 1}`,
company: 'Företagsnamnet Aktiebolag',
}))
const inboxRow = {
id: 'inbox-1',
created_at: '2024-06-01T10:00:00Z',
source: 'whatsapp',
status: 'confirmed',
document_id: 'doc-1',
matched_transaction_id: 'tx-1',
created_journal_entry_id: 'je-1',
created_supplier_invoice_id: null,
channel_context: {
channel: 'whatsapp',
representation: {
participants,
purpose: 'avtalsförhandling',
event_date: '2024-06-01',
raw_answer: 'tio personer, avtalsförhandling',
answered_at: '2024-06-01T18:00:00Z',
},
},
}
enqueueMany([
{ data: COMPANY_ROW },
{ data: [PERIOD_2024] },
{ data: [] }, // document_attachments
{ data: [] }, // sie_imports
{ data: [] }, // sie_account_mappings
...buildMasterDataQueue({ direct: { invoice_inbox_items: [inboxRow] } }),
])
const buffer = await generateFullArchive(supabase as any, 'company-1', {
scope: 'all',
})
const zip = await JSZip.loadAsync(buffer)
const rows = JSON.parse(
await zip.file('data/invoice_inbox_items.json')!.async('text')
) as Array<Record<string, any>>
// Every deltagare survives, not the three that fit on the verifikat line.
expect(rows[0].channel_context.representation.participants).toHaveLength(10)
expect(rows[0].channel_context.representation.raw_answer).toBe(
'tio personer, avtalsförhandling'
)
// Tied to what was booked from it, so a revisor can find the verifikat.
expect(rows[0].created_journal_entry_id).toBe('je-1')
// A projection, not the whole row: inbox workflow state (email bodies,
// OCR output, error messages) stays out of the archive.
const select = findCall('invoice_inbox_items', 'select')?.[0] as string
expect(select).toContain('channel_context')
expect(select).toContain('created_journal_entry_id')
expect(select).not.toContain('email_body_text')
expect(select).not.toContain('extracted_data')
expect(select).not.toBe('*')
})
it('skips raw SIE blobs when include_documents is false but keeps metadata', async () => {
enqueueMany([
{ data: COMPANY_ROW },
+44 -3
View File
@@ -779,6 +779,18 @@ export interface MasterDataTableSpec {
* table through `fk IN (...)` chunks.
*/
via?: { parent: string; fk: string }
/**
* PostgREST select list for a narrow projection. Defaults to `*`.
*
* Only for tables where part of the row is räkenskapsinformation and the
* rest is workflow state that has no place in a portable archive (see
* invoice_inbox_items). Must include the page key.
*
* Additive only, like `denormalize`: an archive already handed to a revisor
* must keep every key it shipped with, so append columns and never drop
* one.
*/
columns?: string
/**
* Parent columns copied onto every child row as `<prefix><column>`.
*
@@ -838,6 +850,28 @@ export const MASTER_DATA_DUMP_TABLES: MasterDataTableSpec[] = [
denormalize: { prefix: 'supplier_invoice_', columns: ['currency', 'exchange_rate'] },
},
{ name: 'supplier_invoice_payments', file: 'supplier_invoice_payments.json' },
// Underlag intake: the chat answers behind a verifikat.
//
// A projection, not the whole table. `channel_context` holds the human
// answers the WhatsApp bot collected (representation deltagare + syfte +
// raw_answer), and it is the ONLY complete copy: the verifikat line carries
// a 220-char render that drops whole names ("… och N till"), and Skatte-
// verket's dokumentationskrav wants every deltagare. Without this file a
// company that leaves with its archive keeps an incomplete representation
// trail. The booking columns come along so each answer can be tied to the
// verifikat it belongs to.
//
// Everything else on the row (email bodies, OCR output, error messages,
// retry state) is inbox workflow state and stays out; the documents
// themselves are in dokument/.
{
name: 'invoice_inbox_items',
file: 'invoice_inbox_items.json',
orderBy: 'created_at',
columns:
'id, created_at, source, status, document_id, matched_transaction_id, ' +
'created_journal_entry_id, created_supplier_invoice_id, channel_context',
},
// Receipts
{ name: 'receipts', file: 'receipts.json', orderBy: 'receipt_date' },
// `receipts` has no exchange_rate column, so only the currency is copied:
@@ -960,7 +994,6 @@ export const ARCHIVE_EXCLUDED_TABLES: Record<string, string> = {
graph_transaction_counterparties: 'derived AI context graph, regenerable',
idempotency_keys: 'infrastructure',
inbox_rate_counters: 'infrastructure',
invoice_inbox_items: 'inbox workflow state; the files live in document_attachments',
mcp_tasks: 'MCP task handles: transient tool-call state with a 1-hour TTL',
metered_events: 'billing telemetry',
notification_log: 'notification dedup log',
@@ -1077,7 +1110,7 @@ async function writeMasterData(
: t.via
? await fetchChildTableRows(supabase, companyId, t)
: await fetchAllRows<Record<string, unknown>>(({ from, to }) => {
let q = supabase.from(t.name).select('*').eq('company_id', companyId)
let q = supabase.from(t.name).select(t.columns ?? '*').eq('company_id', companyId)
if (t.orderBy) {
q = q.order(t.orderBy, { ascending: true })
}
@@ -1086,7 +1119,15 @@ async function writeMasterData(
// silently SKIPS/DUPLICATES rows across page boundaries: data loss in
// a statutory 7-year retention archive. dedupeBy is defense-in-depth
// against the duplicate case.
return q.order(pageKey, { ascending: true }).range(from, to)
//
// The select list is built at runtime (spec.columns), so
// PostgREST's literal-string type inference cannot resolve it and
// falls back to an error type; the runtime shape is the declared
// columns, by construction. Same cast as fetchChildTableRows.
return q.order(pageKey, { ascending: true }).range(from, to) as unknown as PromiseLike<{
data: Record<string, unknown>[] | null
error: { message: string } | null
}>
}, { dedupeBy: (r) => String(r[pageKey]) })
data.file(t.file, JSON.stringify(rows, null, 2))
} catch (err) {
@@ -349,6 +349,175 @@ describe('bulkBookMatchedInboxItems: booking', () => {
})
})
describe('bulkBookMatchedInboxItems: WhatsApp channel-context notes threading', () => {
const WA_CONTEXT = {
channel: 'whatsapp',
representation: {
participants: [
{ name: 'Anna Berg', company: 'Volvo' },
{ name: 'Jakob W', company: null },
],
purpose: 'uppföljning av avtal',
event_date: null,
raw_answer: 'raw',
answered_at: '2026-08-01T12:00:00Z',
},
}
const RENDERED = 'Representation: Anna Berg (Volvo), Jakob W · Syfte: uppföljning av avtal'
const bookableWaItem = () => [
{ data: { id: 'i1', matched_transaction_id: 'tx-1', created_journal_entry_id: null, created_supplier_invoice_id: null, channel_context: WA_CONTEXT } },
{ data: { id: 'tx-1', date: '2026-06-01', amount: -700, currency: 'SEK', cash_account_id: null, journal_entry_id: null } },
{ data: { entity_type: 'aktiebolag', fiscal_year_start_month: 1 } },
{ data: [{ id: 'fp-1' }] },
{ error: null },
{ data: [] },
]
it('threads the rendered channel context into the verifikat notes', async () => {
const supabase = queuedSupabase(bookableWaItem())
const { booked, skipped } = await bulkBookMatchedInboxItems(supabase, 'u1', 'c1', {
item_ids: ['i1'],
category: 'expense_software',
})
expect(skipped).toEqual([])
expect(booked).toHaveLength(1)
// createTransactionJournalEntry(supabase, companyId, userId, tx, mapping, notes)
expect(mockCreateJE).toHaveBeenCalledWith(
expect.anything(),
'c1',
'u1',
expect.objectContaining({ id: 'tx-1' }),
expect.anything(),
RENDERED,
)
})
it('keeps the shared batch note AND the per-item channel context', async () => {
const supabase = queuedSupabase(bookableWaItem())
await bulkBookMatchedInboxItems(supabase, 'u1', 'c1', {
item_ids: ['i1'],
category: 'expense_software',
notes: 'Gemensam batchanteckning',
})
expect(mockCreateJE).toHaveBeenCalledWith(
expect.anything(),
'c1',
'u1',
expect.objectContaining({ id: 'tx-1' }),
expect.anything(),
`Gemensam batchanteckning · ${RENDERED}`,
)
})
// Regression (adversarial review): the photo caption is chat text nobody
// was asked for and nobody reviewed, and this loop books every selected item
// with no per-item notes field. Burning it into a posted verifikat
// description would need a formal rättelse to undo, so only the answered
// parts (representation, user_note) are threaded here.
it('does NOT thread an unreviewed photo caption into the verifikat notes', async () => {
const supabase = queuedSupabase([
{
data: {
id: 'i1',
matched_transaction_id: 'tx-1',
created_journal_entry_id: null,
created_supplier_invoice_id: null,
channel_context: {
channel: 'whatsapp',
caption: 'kvittot från igår, Annas sjukbesök, hon betalade',
},
},
},
{ data: { id: 'tx-1', date: '2026-06-01', amount: -700, currency: 'SEK', cash_account_id: null, journal_entry_id: null } },
{ data: { entity_type: 'aktiebolag', fiscal_year_start_month: 1 } },
{ data: [{ id: 'fp-1' }] },
{ error: null },
{ data: [] },
])
await bulkBookMatchedInboxItems(supabase, 'u1', 'c1', {
item_ids: ['i1'],
category: 'expense_software',
notes: 'Gemensam batchanteckning',
})
expect(mockCreateJE).toHaveBeenCalledWith(
expect.anything(),
'c1',
'u1',
expect.objectContaining({ id: 'tx-1' }),
expect.anything(),
'Gemensam batchanteckning',
)
const passedNotes = mockCreateJE.mock.calls[0][5] as string | undefined
expect(passedNotes).not.toContain('sjukbesök')
})
it('threads the answered parts of a captioned item and drops the caption', async () => {
const supabase = queuedSupabase([
{
data: {
id: 'i1',
matched_transaction_id: 'tx-1',
created_journal_entry_id: null,
created_supplier_invoice_id: null,
channel_context: { ...WA_CONTEXT, caption: 'random bildtext' },
},
},
{ data: { id: 'tx-1', date: '2026-06-01', amount: -700, currency: 'SEK', cash_account_id: null, journal_entry_id: null } },
{ data: { entity_type: 'aktiebolag', fiscal_year_start_month: 1 } },
{ data: [{ id: 'fp-1' }] },
{ error: null },
{ data: [] },
])
await bulkBookMatchedInboxItems(supabase, 'u1', 'c1', {
item_ids: ['i1'],
category: 'expense_software',
})
expect(mockCreateJE).toHaveBeenCalledWith(
expect.anything(),
'c1',
'u1',
expect.objectContaining({ id: 'tx-1' }),
expect.anything(),
RENDERED,
)
})
it('passes notes through unchanged for items without channel context', async () => {
const supabase = queuedSupabase([
{ data: { id: 'i1', matched_transaction_id: 'tx-1', created_journal_entry_id: null, created_supplier_invoice_id: null, channel_context: null } },
{ data: { id: 'tx-1', date: '2026-06-01', amount: -700, currency: 'SEK', cash_account_id: null, journal_entry_id: null } },
{ data: { entity_type: 'aktiebolag', fiscal_year_start_month: 1 } },
{ data: [{ id: 'fp-1' }] },
{ error: null },
{ data: [] },
])
await bulkBookMatchedInboxItems(supabase, 'u1', 'c1', {
item_ids: ['i1'],
category: 'expense_software',
notes: 'Bara batchanteckningen',
})
expect(mockCreateJE).toHaveBeenCalledWith(
expect.anything(),
'c1',
'u1',
expect.objectContaining({ id: 'tx-1' }),
expect.anything(),
'Bara batchanteckningen',
)
})
})
describe('bulkBookMatchedInboxItems: intra-batch duplicate handling', () => {
/** Six queued from() results for one successfully-booked item. */
const bookableItem = (itemId: string, txId: string, amount: number) => [
+22 -3
View File
@@ -30,6 +30,7 @@ import { createTransactionJournalEntry } from '@/lib/bookkeeping/transaction-ent
import { upsertCounterpartyTemplate } from '@/lib/bookkeeping/counterparty-templates'
import { isBookkeepingError } from '@/lib/bookkeeping/errors'
import { linkToJournalEntry } from '@/lib/core/documents/document-service'
import { renderChannelContextNotes } from '@/lib/documents/channel-context-notes'
import {
detectBookingDuplicate,
type BookedDuplicateCandidate,
@@ -38,7 +39,7 @@ import {
import { hasLiveJournalEntryLink } from '@/lib/transactions/link-journal-entry'
import { appendProcessingHistory } from '@/lib/processing-history/append'
import { createLogger } from '@/lib/logger'
import type { Transaction, TransactionCategory, EntityType, VatTreatment } from '@/types'
import type { InboxChannelContext, Transaction, TransactionCategory, EntityType, VatTreatment } from '@/types'
const log = createLogger('transactions/categorize-core')
@@ -501,7 +502,7 @@ export async function bulkBookMatchedInboxItems(
for (const itemId of item_ids) {
const { data: item, error: itemError } = await supabase
.from('invoice_inbox_items')
.select('id, matched_transaction_id, created_journal_entry_id, created_supplier_invoice_id')
.select('id, matched_transaction_id, created_journal_entry_id, created_supplier_invoice_id, channel_context')
.eq('id', itemId)
.eq('company_id', companyId)
.maybeSingle()
@@ -523,6 +524,24 @@ export async function bulkBookMatchedInboxItems(
continue
}
// WhatsApp-sourced underlag carry verified human context (representation
// deltagare + syfte, sender note) in channel_context. Thread it into the
// verifikat description ALONGSIDE the caller's shared batch note: bulk
// booking never shows a per-item notes field, so dropping the chat
// answers here would silently lose the Skatteverket representation
// documentation that only exists on this one item.
//
// Answers only, never the photo caption (the renderer leaves it out
// unless asked for it): this loop books without any per-item review and
// the verifikat description is immutable under BFL 5 kap, so unreviewed
// chat text must not land there. Captions only reach a verifikat through
// Bokför direkt, where the user reads them in an editable field first.
const channelNotes = renderChannelContextNotes(
(item as { channel_context?: InboxChannelContext | null }).channel_context,
)
const itemNotes =
[notes?.trim(), channelNotes].filter(Boolean).join(' · ') || undefined
let result: CategorizeCoreResult
try {
result = await categorizeMatchedTransaction(
@@ -530,7 +549,7 @@ export async function bulkBookMatchedInboxItems(
userId,
companyId,
item.matched_transaction_id as string,
{ category, vatTreatment: vat_treatment, vatAmount: vat_amount, notes, allowDuplicate: allow_duplicate, dimensions },
{ category, vatTreatment: vat_treatment, vatAmount: vat_amount, notes: itemNotes, allowDuplicate: allow_duplicate, dimensions },
// Snapshot copies so the guard sees only the prior bookings of this batch.
{ excludeTransactionIds: [...bookedTransactionIds], excludeJournalEntryIds: [...bookedJournalEntryIds] },
)