Files
accounted/lib/customers/default-payment-terms.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

30 lines
1.0 KiB
TypeScript

import type { SupabaseClient } from '@supabase/supabase-js'
/**
* Resolve the payment terms for a new customer.
*
* Order: the caller-provided value, then the company's own default
* (company_settings.invoice_default_days, the same setting the invoice
* flow reads), then 30 as the last resort. Best-effort on the settings
* read: a missing row or query error falls back to 30 rather than
* failing the create.
*/
export async function resolveDefaultPaymentTerms(
supabase: SupabaseClient,
companyId: string,
provided: number | null | undefined,
): Promise<number> {
if (typeof provided === 'number' && Number.isFinite(provided) && provided > 0) {
return provided
}
const { data } = await supabase
.from('company_settings')
.select('invoice_default_days')
.eq('company_id', companyId)
.maybeSingle()
const days = (data as { invoice_default_days?: number | null } | null)?.invoice_default_days
return typeof days === 'number' && Number.isInteger(days) && days > 0 ? days : 30
}