subscriptions.dunning-schedule@1.0.0
impl/typescript.ts
1,062 bytes · the TypeScript implementation · view raw
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
import { addDays } from "./dates_add_days.ts"; ← from dates.add-days ^1.0.0 · built alongside by fune
import { addBusinessDays } from "./dates_add_business_days.ts"; ← from dates.add-business-days ^1.0.0 · built alongside by fune
/**
* The retry dates for a payment that failed on `failedOn`. Each offset counts
* from the failure date, not from the previous retry, in calendar days or in
* working days.
*/
export function dunningSchedule(failedOn: string, retryOffsetsDays: readonly number[], businessDays: boolean, holidays: readonly string[]): readonly string[] {
let previous = 0;
for (const offset of retryOffsetsDays) {
if (!Number.isInteger(offset) || offset < 1) {
throw new RangeError(`retry offsets must be whole days of 1 or more, received ${offset}`);
}
if (offset <= previous) {
throw new RangeError(`retry offsets must be strictly increasing, received ${offset} after ${previous}`);
}
previous = offset;
}
// Validates the failure date even for an empty policy.
addDays(failedOn, 0);
return retryOffsetsDays.map((offset) => (businessDays ? addBusinessDays(failedOn, offset, holidays) : addDays(failedOn, offset)));
}