feat(domain): recurrence dates and occurrence planning

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
YugonitoandClaude Opus 5.5 committed 2026-10-27 18:00:51 +01:00
1 parent 9fb4aab887
commit d722c0034c
2 files changed
+193

No files matched your search

+97
View File
@@ -0,0 +1,97 @@
import { addMonths, dateInMonth, monthOf, monthsBetween } from "@/lib/dates";
import { fullSplit } from "./money";
import type { AllocationMode, Cents, Frequency, Op, OpKind, Split, UserId } from "./types";
export interface RecurringDef {
id: string;
kind: OpKind;
amount: Cents;
label: string;
categoryId: string;
accountUserId: UserId;
split: Split;
frequency: Frequency;
dayOfMonth: number;
startDate: string;
endDate: string | null;
paused: boolean;
allocatedToRecurringId: string | null;
allocationMode: AllocationMode | null;
}
export type PlannedOccurrence = Omit<Op, "id" | "status"> & { status: "PLANNED"; occurrenceDate: string };
const STEP: Record<Frequency, number> = { MONTHLY: 1, QUARTERLY: 3, YEARLY: 12 };
export const occurrenceKey = (recurringId: string, occurrenceDate: string) => `${recurringId}|${occurrenceDate}`;
/** Dates d'occurrence entre deux mois inclus (spec §9). */
export function occurrenceDates(r: RecurringDef, fromMonth: string, toMonth: string): string[] {
const startMonth = monthOf(r.startDate);
const dates: string[] = [];
for (let m = fromMonth < startMonth ? startMonth : fromMonth; m <= toMonth; m = addMonths(m, 1)) {
if (monthsBetween(startMonth, m) % STEP[r.frequency] !== 0) continue;
const date = dateInMonth(m, r.dayOfMonth);
if (date < r.startDate) continue;
if (r.endDate !== null && date > r.endDate) break;
dates.push(date);
}
return dates;
}
export function splitForAllocation(
mode: AllocationMode,
accountUserId: UserId,
targetSplit: Split | null,
custom: Split,
userIds: UserId[],
): Split {
if (mode === "REDUCE_SHARED" && targetSplit) return { ...targetSplit };
if (mode === "CUSTOM") return { ...custom };
return fullSplit(accountUserId, userIds);
}
/** Occurrences manquantes sur l'horizon glissant, à partir du mois courant. */
export function planOccurrences(
recs: RecurringDef[],
existingKeys: Set<string>,
today: string,
userIds: UserId[],
horizonMonths = 12,
): PlannedOccurrence[] {
const fromMonth = monthOf(today);
const toMonth = addMonths(fromMonth, horizonMonths - 1);
const byId = new Map(recs.map((r) => [r.id, r]));
const planned: PlannedOccurrence[] = [];
for (const r of recs) {
if (r.paused) continue;
const target = r.allocatedToRecurringId ? byId.get(r.allocatedToRecurringId) : undefined;
const allocated = r.kind === "INCOME" && target !== undefined && r.allocationMode !== null;
let split: Split;
if (r.kind === "EXPENSE") split = r.split;
else if (allocated) split = splitForAllocation(r.allocationMode!, r.accountUserId, target!.split, r.split, userIds);
else split = fullSplit(r.accountUserId, userIds);
for (const date of occurrenceDates(r, fromMonth, toMonth)) {
if (existingKeys.has(occurrenceKey(r.id, date))) continue;
planned.push({
kind: r.kind,
amount: r.amount,
date,
occurrenceDate: date,
label: r.label,
categoryId: r.categoryId,
accountUserId: r.accountUserId,
status: "PLANNED",
split,
recurringId: r.id,
allocatedToRecurringId: allocated ? target!.id : null,
allocatedToOperationId: null,
allocationMode: allocated ? r.allocationMode : null,
});
}
}
return planned;
}