Files
accounted/lib/invoices/delivery-recipient-statuses.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

52 lines
1.5 KiB
TypeScript

import type {
InvoiceDeliveryProviderStatus,
InvoiceDeliveryRecipientStatuses,
} from '@/types'
const PROVIDER_STATUSES = new Set<InvoiceDeliveryProviderStatus>([
'delayed',
'delivered',
'complained',
'bounced',
'failed',
'suppressed',
])
/**
* Keeps the public recipient-outcome shape PII-free even if an upstream RPC
* is widened accidentally. Only stable To/CC positions and known outcomes
* survive; raw addresses and BCC references are discarded.
*/
export function sanitizeDeliveryRecipientStatuses(
value: unknown,
): InvoiceDeliveryRecipientStatuses {
if (!value || typeof value !== 'object' || Array.isArray(value)) return {}
const sanitized: Record<
string,
{ status: InvoiceDeliveryProviderStatus; status_at: string }
> = {}
for (const [reference, outcome] of Object.entries(value)) {
if (!/^(to|cc):[1-9][0-9]*$/.test(reference)) continue
if (!outcome || typeof outcome !== 'object' || Array.isArray(outcome)) continue
const candidate = outcome as { status?: unknown; status_at?: unknown }
if (
typeof candidate.status !== 'string'
|| !PROVIDER_STATUSES.has(candidate.status as InvoiceDeliveryProviderStatus)
|| typeof candidate.status_at !== 'string'
|| Number.isNaN(new Date(candidate.status_at).getTime())
) {
continue
}
sanitized[reference] = {
status: candidate.status as InvoiceDeliveryProviderStatus,
status_at: candidate.status_at,
}
}
return sanitized as InvoiceDeliveryRecipientStatuses
}