d1c411ad6f
* 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>
211 lines
7.8 KiB
TypeScript
211 lines
7.8 KiB
TypeScript
/**
|
|
* 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))
|
|
})
|
|
})
|