Files
accounted/lib/invoices/clear-settled-batch-allocations.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

60 lines
2.3 KiB
TypeScript

import type { SupabaseClient } from '@supabase/supabase-js'
import { clearSettledInvoiceSuggestions } from './clear-settled-invoice-suggestions'
/**
* One allocation result as returned by the `match_batch_allocate` RPC
* (supabase/migrations/20260601122000_match_batch_allocate_round3_fixes.sql).
* Only the fields this cleanup needs are declared; both callers pass richer
* rows.
*/
export interface BatchAllocationResult {
kind: 'customer_invoice' | 'supplier_invoice' | string
invoice_id?: string | null
supplier_invoice_id?: string | null
status: 'paid' | 'partially_paid' | string
}
/**
* Retire the suggestion pointers at every invoice a samlingsbetalning settled
* in full (issue #1259).
*
* The RPC nulls potential_invoice_id / potential_supplier_invoice_id only on
* the source transaction (WHERE id = p_tx_id), so every OTHER transaction of
* the company keeps a pointer at an invoice the batch just closed. That row is
* excluded here via exceptTransactionId: the RPC already linked it.
*
* Shared by the two callers of `match_batch_allocate`, the HTTP route
* (app/api/transactions/[id]/match-batch/route.ts) and the MCP staged-operation
* executor (commitMatchBatchAllocate in lib/pending-operations/commit.ts), so
* they cannot drift.
*
* Partially paid allocations are deliberately left alone: such an invoice is
* still matchable, so its suggestions must survive.
*
* Best effort like the helper it wraps: the batch verifikat is already
* committed, so a failed cleanup must never fail the settle.
*/
export async function clearSettledBatchAllocationSuggestions(
supabase: SupabaseClient,
companyId: string,
allocations: readonly BatchAllocationResult[],
transactionId: string,
): Promise<void> {
for (const alloc of allocations) {
if (alloc.status !== 'paid') continue
if (alloc.kind === 'customer_invoice' && alloc.invoice_id) {
await clearSettledInvoiceSuggestions(supabase, companyId, 'invoice', alloc.invoice_id, {
exceptTransactionId: transactionId,
})
} else if (alloc.kind === 'supplier_invoice' && alloc.supplier_invoice_id) {
await clearSettledInvoiceSuggestions(
supabase,
companyId,
'supplier_invoice',
alloc.supplier_invoice_id,
{ exceptTransactionId: transactionId },
)
}
}
}