Files
accounted/lib/skatteverket/sync-order.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

22 lines
896 B
TypeScript

/**
* Fair ordering for the skattekonto sync cron: with more connected companies
* than one run can process (MAX_COMPANIES_PER_RUN), a fixed order would
* starve the tail forever. Never-synced companies go first, then the ones
* synced longest ago; ties keep the incoming order (stable sort).
*/
export function orderByStalestSync<T extends { companyId: string }>(
work: readonly T[],
lastSyncedAtByCompany: ReadonlyMap<string, string | null | undefined>,
): T[] {
const rank = (item: T): number => {
const iso = lastSyncedAtByCompany.get(item.companyId)
if (!iso) return Number.NEGATIVE_INFINITY
const ms = Date.parse(iso)
return Number.isFinite(ms) ? ms : Number.NEGATIVE_INFINITY
}
return work
.map((item, index) => ({ item, index, rank: rank(item) }))
.sort((a, b) => (a.rank === b.rank ? a.index - b.index : a.rank - b.rank))
.map((x) => x.item)
}