Files
accounted/extensions/general/push-notifications/notification-scheduler.ts
T
MattssonandClaude Opus 4.8 53e343ee92 Bug/invalid imports (#1146)
* feat: add Accounted MCP namespace

* fix(bookkeeping): stop flagging verifikat whose underlag lives on a referenced supplier invoice

The missing-underlag surfaces only accepted a document directly linked to
the entry, so payment verifikat for supplier invoices (doc on the
registration entry per design) and entries whose doc was pinned to the
bank transaction before matching were falsely flagged; opening the entry
showed the referenced doc and cleared the warning client-side, and it
came back on reload.

- verifikat_without_documents + transactions_without_documents now treat
  an entry as covered when a supplier invoice referencing it (registration
  or payment FK, or a supplier_invoice_payments row) carries a document
  anchored to a journal entry (BFL 5 kap 7 paragraf hänvisning till
  underlag; anchoring required because the WORM deletion guards key on
  document_attachments.journal_entry_id)
- match-supplier-invoice routes (dashboard + v1) propagate the
  transaction's pinned document onto the payment verifikat, mirroring the
  categorize route; migration backfills rows already written (open
  unlocked periods, company-guarded, never steals a linked doc)
- /api/documents/counts, the transactions-page badges, the bulk "Inget
  underlag krävs" count and the push-notification scheduler share the
  same reference-aware predicate, so every surface agrees with the RPC
- counts route validates journal_entry_ids as UUIDs (they are
  interpolated into a PostgREST or-filter)

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

* fix(transactions): align table columns flush with page edges

Collapse the checkbox gutter column to zero width and hang the
hover-revealed checkbox/expand chevron in the page margins, drop the
outer padding so DATUM sits flush left and STATUS flush right, and
tuck the overflow-menu dots under the middle of the STATUS header.
Applied to both the inbox and history tables so they stay identical.

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

* fix(arsredovisning): tie anlaggningstillgangar note to booked depreciation

The ARL 5:8 roll-forward note recomputed depreciation from its own
day-based linear formula (365.25/12 month length, non-inclusive day
count, linear only), drifting ~20 kr per year per asset from the
ledger-driven resultat- and balansrakning and misstating non-linear
methods entirely. Note figures now come from posted
depreciation_schedules rows (the same source disposeAsset reverses),
falling back to the engine's computeAnnualDepreciation when nothing is
posted; pre-onboarding opening balances iterate prior years through
the engine. Adds a note-vs-trial-balance tie-out warning (accounts
1000-1299, over 1 kr) surfaced before download.

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

* refactor(stripe): move connect and sync surface from settings to import page

Stripe's transaction feed is a continuous import source in the same
category as the PSD2 bank connection, so its connect/sync surface now
lives on the import page as a source card (mode=stripe), gated
"kommer snart" on hosted like before; self-hosted keeps the full panel.

- Import page: Stripe card after Koppla bank, renders the existing
  StripeSettingsPanel via the settings-panel registry
- OAuth callback and panel cleanup return to /import?mode=stripe
- Settings > Betalningar retired: nav item removed, route redirects,
  PaymentsSettingsContent deleted, legacy ?tab=payments mapped
- New import.stripe_* strings in sv+en; dead settings_nav.payments removed

Crons and sync logic unchanged; payment-link settings stay in the
invoicing section.

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

* fix(underlag): paginate missing-underlag cron and harden doc-surface queries

Resolve PR review findings on bug/invalid-imports:
- notification-scheduler: fetchAllRows on all 5 global reads; past 1000 rows
  the capped reads produced false "saknade underlag" notifications
- bulk-missing: LOOKUP_CHUNK 300->150 so the twice-embedded .or() id list
  stays under the PostgREST URL limit
- bulk-missing + transactions page: UUID-guard the .or()-interpolated id
  lists, matching documents/counts
- match-supplier-invoice (dashboard + v1): log documentId/journalEntryId on
  the non-fatal doc-link warning
- well-known/oauth-protected-resource: document the tool_namespace allow-list
- messages/en: reword stripe_description
- DECISIONS.md: record the asset ibAck tie-out and Tailwind !important calls

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(tic): convert registrationDate from Unix seconds to millisecond epoch in lookup and profile tests

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-24 15:03:50 +02:00

389 lines
11 KiB
TypeScript

/**
* Cron-based notification scheduling.
*
* Handles time-dependent checks that cannot be event-driven:
* - Tax deadlines approaching (7 days, 1 day, today)
* - Invoice due/overdue reminders (3 days before, on due date, 3/7 days overdue)
*
* Both functions call `sendNotificationToUser()` from the sender module
* instead of duplicating the send pipeline.
*/
import type { SupabaseClient } from '@supabase/supabase-js'
import type { NotificationType } from '@/types'
import { fetchAllRows } from '@/lib/supabase/fetch-all'
import { sendNotificationToUser } from './notification-sender'
import {
createTaxDeadlinePayload,
createInvoiceOverduePayload,
createInvoiceDuePayload,
createMissingUnderlagPayload,
} from './payload-builders'
/**
* Send tax deadline notifications.
* Checks for deadlines due in 7 days, 1 day, or today.
*/
export async function sendTaxDeadlineNotifications(
supabase: SupabaseClient
): Promise<{ sent: number; skipped: number }> {
let sent = 0
let skipped = 0
const today = new Date()
today.setHours(0, 0, 0, 0)
const todayStr = today.toISOString().split('T')[0]
const in7Days = new Date(today)
in7Days.setDate(in7Days.getDate() + 7)
const in7DaysStr = in7Days.toISOString().split('T')[0]
const tomorrow = new Date(today)
tomorrow.setDate(tomorrow.getDate() + 1)
const tomorrowStr = tomorrow.toISOString().split('T')[0]
const { data: deadlines } = await supabase
.from('deadlines')
.select('id, user_id, title, due_date')
.eq('deadline_type', 'tax')
.eq('is_completed', false)
.in('status', ['upcoming', 'action_needed'])
.in('due_date', [in7DaysStr, tomorrowStr, todayStr])
if (!deadlines || deadlines.length === 0) {
return { sent: 0, skipped: 0 }
}
// Group by user
const userDeadlines = new Map<string, typeof deadlines>()
for (const deadline of deadlines) {
const list = userDeadlines.get(deadline.user_id) || []
list.push(deadline)
userDeadlines.set(deadline.user_id, list)
}
for (const [userId, userDls] of userDeadlines) {
// Check user-level tax_deadlines_enabled
const { data: settings } = await supabase
.from('notification_settings')
.select('tax_deadlines_enabled')
.eq('company_id', userId)
.single()
if (settings && !settings.tax_deadlines_enabled) {
skipped += userDls.length
continue
}
for (const deadline of userDls) {
const daysUntil = Math.ceil(
(new Date(deadline.due_date).getTime() - today.getTime()) / (1000 * 60 * 60 * 24)
)
const payload = createTaxDeadlinePayload(
deadline.title,
deadline.due_date,
daysUntil,
deadline.id
)
const result = await sendNotificationToUser(
supabase,
userId,
payload,
'tax_deadline',
deadline.id,
daysUntil
)
if (result.sent) {
sent++
} else {
skipped++
}
}
}
return { sent, skipped }
}
/**
* Send invoice reminder notifications.
* Checks for invoices due in 3 days, today, or overdue by 3/7 days.
*/
export async function sendInvoiceNotifications(
supabase: SupabaseClient
): Promise<{ sent: number; skipped: number }> {
let sent = 0
let skipped = 0
const today = new Date()
today.setHours(0, 0, 0, 0)
const todayStr = today.toISOString().split('T')[0]
const in3Days = new Date(today)
in3Days.setDate(in3Days.getDate() + 3)
const in3DaysStr = in3Days.toISOString().split('T')[0]
const daysAgo3 = new Date(today)
daysAgo3.setDate(daysAgo3.getDate() - 3)
const daysAgo3Str = daysAgo3.toISOString().split('T')[0]
const daysAgo7 = new Date(today)
daysAgo7.setDate(daysAgo7.getDate() - 7)
const daysAgo7Str = daysAgo7.toISOString().split('T')[0]
const { data: invoices } = await supabase
.from('invoices')
.select('id, user_id, invoice_number, total, due_date, customer:customers(name)')
.in('status', ['sent', 'overdue'])
.in('due_date', [in3DaysStr, todayStr, daysAgo3Str, daysAgo7Str])
if (!invoices || invoices.length === 0) {
return { sent: 0, skipped: 0 }
}
// Group by user
const userInvoices = new Map<string, typeof invoices>()
for (const invoice of invoices) {
const list = userInvoices.get(invoice.user_id) || []
list.push(invoice)
userInvoices.set(invoice.user_id, list)
}
for (const [userId, userInvs] of userInvoices) {
// Check user-level invoice_reminders_enabled
const { data: settings } = await supabase
.from('notification_settings')
.select('invoice_reminders_enabled')
.eq('company_id', userId)
.single()
if (settings && !settings.invoice_reminders_enabled) {
skipped += userInvs.length
continue
}
for (const invoice of userInvs) {
const dueDate = new Date(invoice.due_date)
const daysUntil = Math.ceil(
(dueDate.getTime() - today.getTime()) / (1000 * 60 * 60 * 24)
)
const isOverdue = daysUntil < 0
const notificationType: NotificationType = isOverdue ? 'invoice_overdue' : 'invoice_due'
const customer = invoice.customer as unknown as { name: string } | null
const customerName = customer?.name || 'Okänd kund'
const payload = isOverdue
? createInvoiceOverduePayload(
invoice.invoice_number,
customerName,
invoice.total,
invoice.due_date,
invoice.id
)
: createInvoiceDuePayload(
invoice.invoice_number,
customerName,
invoice.total,
invoice.due_date,
invoice.id
)
const result = await sendNotificationToUser(
supabase,
userId,
payload,
notificationType,
invoice.id,
Math.abs(daysUntil)
)
if (result.sent) {
sent++
} else {
skipped++
}
}
}
return { sent, skipped }
}
/**
* Source types that require supporting documents (underlag).
*/
const NEEDS_ATTACHMENT_SOURCE_TYPES = [
'manual',
'bank_transaction',
'supplier_invoice_registered',
'supplier_invoice_paid',
'supplier_invoice_cash_payment',
'import',
]
/**
* Send missing underlag notifications.
* Checks all users for posted journal entries without attached documents.
* Deduplicates via the 'missing-underlag-weekly' tag on the notification payload.
*/
export async function sendMissingUnderlagNotifications(
supabase: SupabaseClient
): Promise<{ sent: number; skipped: number }> {
let sent = 0
let skipped = 0
// Get all users who have posted entries with source types that need docs.
// This is a GLOBAL cron over every company, so each read below must page
// past PostgREST's 1000-row cap: a truncated read here would under-count
// candidates, and a truncated docs/reference read would over-count missing
// underlag, producing false "saknade underlag" notifications.
const entries = await fetchAllRows<{ id: string; user_id: string }>(({ from, to }) =>
supabase
.from('journal_entries')
.select('id, user_id')
.eq('status', 'posted')
.in('source_type', NEEDS_ATTACHMENT_SOURCE_TYPES)
.order('id')
.range(from, to)
)
if (entries.length === 0) {
return { sent: 0, skipped: 0 }
}
// Get all document_attachments linked to journal entries
const attachments = await fetchAllRows<{ journal_entry_id: string | null }>(({ from, to }) =>
supabase
.from('document_attachments')
.select('journal_entry_id')
.eq('is_current_version', true)
.not('journal_entry_id', 'is', null)
.order('id')
.range(from, to)
)
const entriesWithDocs = new Set(attachments.map((a) => a.journal_entry_id))
// BFL 5 kap 7 § hänvisning: entries referenced by a supplier invoice whose
// document is retained and anchored to a journal entry count as covered
// (mirrors the verifikat_without_documents RPC): typically the payment
// verifikat, whose invoice document hangs on the registration verifikat.
const siRefs = await fetchAllRows<{
registration_journal_entry_id: string | null
payment_journal_entry_id: string | null
document: { journal_entry_id: string | null } | null
}>(({ from, to }) =>
supabase
.from('supplier_invoices')
.select(
'registration_journal_entry_id, payment_journal_entry_id, document:document_attachments(journal_entry_id)'
)
.not('document_id', 'is', null)
.order('id')
.range(from, to) as unknown as PromiseLike<{
data: {
registration_journal_entry_id: string | null
payment_journal_entry_id: string | null
document: { journal_entry_id: string | null } | null
}[] | null
error: { message: string } | null
}>
)
for (const si of siRefs) {
if (!si.document?.journal_entry_id) continue // unanchored: not underlag
if (si.registration_journal_entry_id) entriesWithDocs.add(si.registration_journal_entry_id)
if (si.payment_journal_entry_id) entriesWithDocs.add(si.payment_journal_entry_id)
}
const sipRefs = await fetchAllRows<{
journal_entry_id: string | null
supplier_invoice: {
document_id: string | null
document: { journal_entry_id: string | null } | null
} | null
}>(({ from, to }) =>
supabase
.from('supplier_invoice_payments')
.select(
'journal_entry_id, supplier_invoice:supplier_invoices(document_id, document:document_attachments(journal_entry_id))'
)
.not('journal_entry_id', 'is', null)
.order('id')
.range(from, to) as unknown as PromiseLike<{
data: {
journal_entry_id: string | null
supplier_invoice: {
document_id: string | null
document: { journal_entry_id: string | null } | null
} | null
}[] | null
error: { message: string } | null
}>
)
for (const sip of sipRefs) {
if (sip.journal_entry_id && sip.supplier_invoice?.document?.journal_entry_id) {
entriesWithDocs.add(sip.journal_entry_id)
}
}
// Entries the user has explicitly flagged as "no underlag required" (bank
// fees, interest, internal transfers, salary, tax payments). Treated as
// satisfied so we don't nag the user about them.
const exempted = await fetchAllRows<{ journal_entry_id: string }>(({ from, to }) =>
supabase
.from('journal_entry_no_doc_required')
.select('journal_entry_id')
.order('journal_entry_id')
.range(from, to)
)
const exemptedEntries = new Set(exempted.map((e) => e.journal_entry_id))
// Group missing counts by user
const userMissingCounts = new Map<string, number>()
for (const entry of entries) {
if (!entriesWithDocs.has(entry.id) && !exemptedEntries.has(entry.id)) {
userMissingCounts.set(
entry.user_id,
(userMissingCounts.get(entry.user_id) || 0) + 1
)
}
}
for (const [userId, count] of userMissingCounts) {
// Check user setting
const { data: settings } = await supabase
.from('notification_settings')
.select('missing_underlag_enabled')
.eq('company_id', userId)
.single()
if (settings && settings.missing_underlag_enabled === false) {
skipped++
continue
}
const payload = createMissingUnderlagPayload(count)
const result = await sendNotificationToUser(
supabase,
userId,
payload,
'missing_underlag',
'weekly-check'
)
if (result.sent) {
sent++
} else {
skipped++
}
}
return { sent, skipped }
}