Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/db/src/lead-status-contract.ts
T

36 lines
1.3 KiB
TypeScript

export type LeadStatus = "new" | "qualified" | "converted" | "lost" | "spam";
export const ALLOWED_LEAD_STATUS_TRANSITIONS: Readonly<Record<LeadStatus, readonly LeadStatus[]>> = {
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<LeadStatus> = 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,
}
: {}),
};
}