Files
accounted/extensions/general/push-notifications/payload-builders.ts
T
Jakob Wennberg 9b3e344796 fix(notifications,mcp): carry the record's currency in amount strings (#1179)
Fixes #1171. Three currency-blind format sites:

- Invoice due/overdue push notifications rendered every total as kr;
  the scheduler query did not even select currency. Builders now take
  the invoice currency ("kr" only for SEK, ISO code otherwise).
- The receipt-matcher MCP widget hardcoded ' kr' although the tool
  handler passes each transaction's currency through.
- The duplicate-booking warning in gnubok_categorize_transaction
  interpolated "N kr" for a transaction whose currency was already
  selected; the string reaches the agent, so a mislabeled currency can
  mislead the model, not just the user.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-25 12:58:55 +02:00

216 lines
5.7 KiB
TypeScript

/**
* Notification payload constructors for all push notification types.
*
* Centralises every notification's title/body/icon/action in one place.
* Both event handlers and the cron scheduler import from here.
*/
import type { NotificationPayload } from './notification-sender'
// ============================================================
// Helpers
// ============================================================
function formatDate(dateStr: string): string {
const date = new Date(dateStr)
return date.toLocaleDateString('sv-SE', {
day: 'numeric',
month: 'short',
})
}
// ============================================================
// Event-driven payloads
// ============================================================
export function createPeriodLockedPayload(
periodName: string,
periodId: string
): NotificationPayload {
return {
title: 'Period låst',
body: `${periodName} har låsts`,
icon: '/icons/icon-192.png',
badge: '/icons/badge-72.png',
tag: `period-locked-${periodId}`,
data: {
url: '/periods',
type: 'period_locked',
id: periodId,
},
}
}
export function createYearClosedPayload(
periodName: string,
periodId: string
): NotificationPayload {
return {
title: 'Årsbokslut klart',
body: `Årsbokslut klart för ${periodName}`,
icon: '/icons/icon-192.png',
badge: '/icons/badge-72.png',
tag: `year-closed-${periodId}`,
data: {
url: '/periods',
type: 'period_year_closed',
id: periodId,
},
}
}
export function createInvoiceSentPayload(
invoiceNumber: string,
invoiceId: string
): NotificationPayload {
return {
title: 'Faktura skickad',
body: `Faktura ${invoiceNumber} skickad`,
icon: '/icons/icon-192.png',
badge: '/icons/badge-72.png',
tag: `invoice-sent-${invoiceId}`,
data: {
url: `/invoices/${invoiceId}`,
type: 'invoice_sent',
id: invoiceId,
},
}
}
export function createReceiptExtractedPayload(
merchantName: string | null,
receiptId: string
): NotificationPayload {
const merchant = merchantName || 'Okänd butik'
return {
title: 'Kvitto analyserat',
body: `Kvitto analyserat: ${merchant}`,
icon: '/icons/icon-192.png',
badge: '/icons/badge-72.png',
tag: `receipt-extracted-${receiptId}`,
data: {
url: '/transactions',
type: 'receipt_extracted',
id: receiptId,
},
}
}
export function createReceiptMatchedPayload(
receiptId: string,
transactionId: string
): NotificationPayload {
return {
title: 'Kvitto matchat',
body: 'Kvitto matchat mot transaktion',
icon: '/icons/icon-192.png',
badge: '/icons/badge-72.png',
tag: `receipt-matched-${receiptId}`,
data: {
url: '/transactions',
type: 'receipt_matched',
id: receiptId,
},
}
}
// ============================================================
// Missing underlag payload
// ============================================================
export function createMissingUnderlagPayload(count: number): NotificationPayload {
return {
title: 'Saknade underlag',
body: `${count} verifikation(er) saknar underlag. Bifoga för att uppfylla bokföringslagen.`,
icon: '/icons/icon-192.png',
badge: '/icons/badge-72.png',
tag: 'missing-underlag-weekly',
data: {
url: '/bookkeeping?missingUnderlag=true',
type: 'missing_underlag',
},
}
}
// ============================================================
// Cron-based payloads (moved from lib/push/web-push.ts)
// ============================================================
export function createTaxDeadlinePayload(
title: string,
dueDate: string,
daysUntil: number,
deadlineId: string
): NotificationPayload {
const urgencyText =
daysUntil === 0 ? 'IDAG!' : daysUntil === 1 ? 'imorgon' : `om ${daysUntil} dagar`
return {
title: `${title}`,
body: `Deadline ${urgencyText} (${formatDate(dueDate)})`,
icon: '/icons/icon-192.png',
badge: '/icons/badge-72.png',
tag: `tax-deadline-${deadlineId}`,
data: {
url: '/deadlines',
type: 'tax_deadline',
id: deadlineId,
},
actions: [
{ action: 'view', title: 'Visa' },
{ action: 'dismiss', title: 'Avfärda' },
],
}
}
// Invoices carry their own currency; "kr" is only correct for SEK.
function formatInvoiceAmount(amount: number, currency: string): string {
return `${amount.toLocaleString('sv-SE')} ${!currency || currency === 'SEK' ? 'kr' : currency}`
}
export function createInvoiceOverduePayload(
invoiceNumber: string,
customerName: string,
amount: number,
currency: string,
dueDate: string,
invoiceId: string
): NotificationPayload {
return {
title: `Obetald faktura #${invoiceNumber}`,
body: `${customerName} - ${formatInvoiceAmount(amount, currency)} (förföll ${formatDate(dueDate)})`,
icon: '/icons/icon-192.png',
badge: '/icons/badge-72.png',
tag: `invoice-${invoiceId}`,
data: {
url: `/invoices/${invoiceId}`,
type: 'invoice_overdue',
id: invoiceId,
},
actions: [{ action: 'view', title: 'Visa faktura' }],
}
}
export function createInvoiceDuePayload(
invoiceNumber: string,
customerName: string,
amount: number,
currency: string,
dueDate: string,
invoiceId: string
): NotificationPayload {
return {
title: `Faktura #${invoiceNumber} förfaller`,
body: `${customerName} - ${formatInvoiceAmount(amount, currency)} (${formatDate(dueDate)})`,
icon: '/icons/icon-192.png',
badge: '/icons/badge-72.png',
tag: `invoice-${invoiceId}`,
data: {
url: `/invoices/${invoiceId}`,
type: 'invoice_due',
id: invoiceId,
},
actions: [{ action: 'view', title: 'Visa faktura' }],
}
}