Files
accounted/extensions/general/mcp-server/resources/recent-activity.ts
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

69 lines
2.8 KiB
TypeScript

import type { McpResource } from './types'
import { fetchJunctionLinkedTxIds } from '@/lib/reconciliation/bank-reconciliation'
export const recentActivityResource: McpResource = {
uri: 'Accounted://recent-activity',
name: 'Recent Activity',
description: 'Most recent journal entries, invoices, and bank transactions for the current company. Optional ?limit=N (default 20, max 100). Use to orient on the latest state without burning tool calls.',
mimeType: 'application/json',
read: async ({ supabase, companyId, query }) => {
const limit = Math.min(Math.max(Number(query?.get('limit') ?? 20), 1), 100)
const [journalEntries, invoices, transactions] = await Promise.all([
supabase
.from('journal_entries')
.select('id, voucher_number, voucher_series, entry_date, description, status, created_at')
.eq('company_id', companyId)
.order('created_at', { ascending: false })
.limit(limit),
supabase
.from('invoices')
.select('id, invoice_number, customer_id, invoice_date, due_date, total, currency, status, created_at')
.eq('company_id', companyId)
.order('created_at', { ascending: false })
.limit(limit),
supabase
.from('transactions')
.select('id, date, description, amount, currency, journal_entry_id, category, merchant_name')
.eq('company_id', companyId)
.order('date', { ascending: false })
.limit(limit),
])
// Never report an empty list when the query failed: an agent that is told
// "zero invoices" reasons from false state, which is worse than an error.
if (journalEntries.error) {
throw new Error(`Failed to read recent journal entries: ${journalEntries.error.message}`)
}
if (invoices.error) {
throw new Error(`Failed to read recent invoices: ${invoices.error.message}`)
}
if (transactions.error) {
throw new Error(`Failed to read recent transactions: ${transactions.error.message}`)
}
// A NULL pointer is not "unbooked" on its own: rows bulk-booked into a
// samlingsverifikat or split over several verifikat (1:N) are anchored
// through transaction_voucher_links (lib/transactions/is-booked.ts).
const recentTx = transactions.data ?? []
const pointerUnbooked = recentTx.filter((t) => !t.journal_entry_id)
const junctionLinked =
pointerUnbooked.length > 0
? await fetchJunctionLinkedTxIds(
supabase,
companyId,
pointerUnbooked.map((t) => t.id as string),
)
: new Set<string>()
return {
limit,
journal_entries: journalEntries.data ?? [],
invoices: invoices.data ?? [],
transactions: recentTx,
uncategorized_transaction_count: pointerUnbooked.filter((t) => !junctionLinked.has(t.id as string))
.length,
}
},
}