export type LeadStatus = "new" | "qualified" | "converted" | "lost" | "spam"; export const ALLOWED_LEAD_STATUS_TRANSITIONS: Readonly> = { new: ["qualified", "lost", "spam"], qualified: ["converted", "lost", "spam"], converted: [], lost: [], spam: [], }; export function canTransitionLeadStatus(from: LeadStatus, to: LeadStatus): boolean { return from === to || ALLOWED_LEAD_STATUS_TRANSITIONS[from].includes(to); } const SPAM_AUTO_DELETE_DAYS = 30; const TERMINAL_LEAD_STATUSES: ReadonlySet = new Set(["converted", "lost", "spam"]); export function leadStatusTimestampData(status: LeadStatus, now: Date, lostAutoDeleteDays: number) { return { ...(status === "qualified" ? { weiterverarbeitungFreigabe: "ja" as const } : {}), ...(status === "converted" ? { convertedAt: now } : {}), ...(status === "lost" ? { lostAt: now } : {}), ...(status === "spam" ? { spamMarkedAt: now } : {}), ...(TERMINAL_LEAD_STATUSES.has(status) ? { autoDeleteAt: status === "spam" ? new Date(now.getTime() + SPAM_AUTO_DELETE_DAYS * 24 * 60 * 60 * 1000) : status === "lost" && lostAutoDeleteDays > 0 ? new Date(now.getTime() + lostAutoDeleteDays * 24 * 60 * 60 * 1000) : null, } : {}), }; }