Files
accounted/lib/bookkeeping/no-doc-required.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

51 lines
1.7 KiB
TypeScript

import type { SupabaseClient } from '@supabase/supabase-js'
const CHUNK_SIZE = 500
/**
* Bulk-mark posted journal entries as "Inget underlag krävs" (no supporting
* document required) by inserting rows into journal_entry_no_doc_required.
*
* The flag lives in a sidecar table so the verifikation itself stays immutable
* per BFL: same write the single-entry route performs, just batched. Inserts
* are chunked (Postgres/PostgREST payload safety) and idempotent: rows that
* already exist are left untouched (`ignoreDuplicates`).
*
* The caller is responsible for passing only entry IDs that belong to
* `companyId` and are eligible (posted, document-requiring source type); RLS on
* the table is the security backstop. Used by the SIE-import opt-in auto-exempt
* flow and the batch-mark endpoint.
*
* @returns the number of entry IDs processed (deduped), not the number of new rows.
*/
export async function markEntriesNoDocRequired(
supabase: SupabaseClient,
companyId: string,
userId: string,
entryIds: string[],
reason: string | null,
): Promise<number> {
if (entryIds.length === 0) return 0
// De-dupe so a chunk can never carry the same id twice (ON CONFLICT target).
const uniqueIds = Array.from(new Set(entryIds))
for (let i = 0; i < uniqueIds.length; i += CHUNK_SIZE) {
const chunk = uniqueIds.slice(i, i + CHUNK_SIZE)
const rows = chunk.map((journal_entry_id) => ({
journal_entry_id,
company_id: companyId,
user_id: userId,
reason: reason ?? null,
}))
const { error } = await supabase
.from('journal_entry_no_doc_required')
.upsert(rows, { onConflict: 'journal_entry_id', ignoreDuplicates: true })
if (error) throw new Error(error.message)
}
return uniqueIds.length
}