Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/view-models/src/leads/lead-list.vm.ts
T
2026-07-08 16:15:37 +02:00

131 lines
4.6 KiB
TypeScript

import type { VMState } from "../index.js";
export type LeadQuelle = "form" | "email" | "aircall" | "manual" | "custom";
export type LeadStatus = "new" | "qualified" | "converted" | "lost" | "spam";
export type LeadWeiterverarbeitungFreigabe = "ja" | "nein" | "unklar";
export interface LeadListRowSource {
readonly id: string;
readonly quelle: LeadQuelle;
readonly status: LeadStatus;
readonly weiterverarbeitungFreigabe: LeadWeiterverarbeitungFreigabe;
readonly vorname: string | null;
readonly nachname: string | null;
readonly email: string | null;
readonly telefon: string | null;
readonly message: string | null;
readonly zustaendigerUserId: string | null;
readonly spamScore: number;
readonly spamReason: string | null;
readonly isQuarantined: boolean;
readonly konvertiertProjektId: string | null;
readonly verknuepfterKontaktId: string | null;
readonly createdAt: Date;
}
export interface LeadListRow {
readonly id: string;
readonly displayName: string;
readonly preview: string | null;
readonly quelle: LeadQuelle;
readonly quelleLabelKey: string;
readonly status: LeadStatus;
readonly statusLabelKey: string;
readonly weiterverarbeitungFreigabe: LeadWeiterverarbeitungFreigabe;
readonly freigabeLabelKey: string;
readonly email: string | null;
readonly telefon: string | null;
readonly zustaendigerLabel: string | null;
readonly spamScore: number;
readonly markers: readonly ("duplicate" | "spam" | "converted")[];
readonly detailHref: string;
readonly createdAtIso: string;
}
export interface LeadListData {
readonly rows: readonly LeadListRow[];
readonly total: number;
readonly nextCursor: string | null;
readonly canWrite: boolean;
readonly canDelete: boolean;
readonly canReadAll: boolean;
readonly createHref: string;
readonly statisticsHref: string;
readonly emptyHeadlineKey: string;
readonly emptyBodyKey: string;
}
export type LeadListVM = VMState<LeadListData>;
export interface BuildLeadListVMInput {
readonly rows: readonly LeadListRowSource[];
readonly userLabels: ReadonlyMap<string, string>;
readonly duplicateLeadIds?: ReadonlySet<string>;
readonly total: number;
readonly nextCursor: string | null;
readonly perms: { readonly canWrite: boolean; readonly canDelete: boolean; readonly canReadAll: boolean };
readonly tenantSlug: string;
readonly createHref: string;
readonly statisticsHref: string;
}
export function leadQuelleLabelKey(quelle: LeadQuelle): string {
return `leads.quelle.${quelle}`;
}
export function leadStatusLabelKey(status: LeadStatus): string {
return `leads.status.${status}`;
}
export function leadFreigabeLabelKey(value: LeadWeiterverarbeitungFreigabe): string {
return `leads.freigabe.${value}`;
}
export function buildLeadDisplayName(row: Pick<LeadListRowSource, "vorname" | "nachname" | "email" | "telefon" | "quelle">): string {
const name = `${row.vorname ?? ""} ${row.nachname ?? ""}`.trim();
return name || row.email || row.telefon || row.quelle;
}
export function buildLeadListVM(input: BuildLeadListVMInput): LeadListVM {
const rows = input.rows.map((row) => {
const markers: ("duplicate" | "spam" | "converted")[] = [];
if (input.duplicateLeadIds?.has(row.id)) markers.push("duplicate");
if (row.isQuarantined || row.status === "spam" || row.spamScore >= 50) markers.push("spam");
if (row.konvertiertProjektId || row.status === "converted") markers.push("converted");
return {
id: row.id,
displayName: buildLeadDisplayName(row),
preview: row.message ? row.message.slice(0, 80) : null,
quelle: row.quelle,
quelleLabelKey: leadQuelleLabelKey(row.quelle),
status: row.status,
statusLabelKey: leadStatusLabelKey(row.status),
weiterverarbeitungFreigabe: row.weiterverarbeitungFreigabe,
freigabeLabelKey: leadFreigabeLabelKey(row.weiterverarbeitungFreigabe),
email: row.email,
telefon: row.telefon,
zustaendigerLabel: row.zustaendigerUserId ? (input.userLabels.get(row.zustaendigerUserId) ?? null) : null,
spamScore: row.spamScore,
markers,
detailHref: `/${input.tenantSlug}/leads/${row.id}`,
createdAtIso: row.createdAt.toISOString(),
} satisfies LeadListRow;
});
return {
status: "ready",
data: {
rows,
total: input.total,
nextCursor: input.nextCursor,
canWrite: input.perms.canWrite,
canDelete: input.perms.canDelete,
canReadAll: input.perms.canReadAll,
createHref: input.createHref,
statisticsHref: input.statisticsHref,
emptyHeadlineKey: "leads.list.empty.headline",
emptyBodyKey: "leads.list.empty.body",
},
};
}