Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/utils/integrations/leads/leadAudit.server.ts
T

67 lines
2.0 KiB
TypeScript

import type { JsonValue } from "@hivegrid/foundation";
import { createAuditEvent } from "@hivegrid/db/audit-events.server";
import { logActivity } from "@hivegrid/activity";
import type { LeadRouteContext } from "./leadAccess.server";
import type { LeadRow } from "./leadService.server";
export type LeadAuditAction =
| "lead.created"
| "lead.updated"
| "lead.status_changed"
| "lead.assigned"
| "lead.bulk_updated"
| "lead.qualified"
| "lead.convert_requested"
| "lead.source_created"
| "lead.source_updated"
| "lead.source_archived"
| "lead.anti_spam_updated";
export async function auditLeadEvent(input: {
ctx: Pick<LeadRouteContext, "tenantId" | "userId">;
action: LeadAuditAction;
targetType: "lead" | "lead_source" | "lead_anti_spam";
targetId: string;
metadata?: Record<string, JsonValue>;
}): Promise<void> {
try {
await createAuditEvent({
tenantId: input.ctx.tenantId,
actorId: input.ctx.userId,
actorType: "user",
action: input.action,
targetType: input.targetType,
targetId: input.targetId,
metadata: input.metadata ?? null,
});
} catch {
// Best-effort: audit failures are reported upstream, not to the user action.
}
}
export async function logLeadActivity(input: {
ctx: Pick<LeadRouteContext, "tenantId" | "userId">;
leadId: string;
activityType: "lead_created" | "lead_updated" | "lead_qualified" | "assignee_changed" | "status_changed";
summaryText: string;
}): Promise<void> {
try {
await logActivity({
tenantId: input.ctx.tenantId,
actorUserId: input.ctx.userId,
actorKind: "user",
activityType: input.activityType,
subjectType: "lead",
subjectId: input.leadId,
summaryText: input.summaryText,
});
} catch {
// Activity is best-effort; never break the mutation.
}
}
export function leadDisplayName(row: Pick<LeadRow, "vorname" | "nachname" | "email" | "telefon" | "quelle">): string {
const name = `${row.vorname ?? ""} ${row.nachname ?? ""}`.trim();
return name || row.email || row.telefon || row.quelle;
}