Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+24
-91
@@ -16,8 +16,7 @@
|
||||
*/
|
||||
|
||||
import { createHash } from "node:crypto";
|
||||
import type { Prisma } from "@hivegrid/db/prisma-browser";
|
||||
import { db } from "~/utils/db.server";
|
||||
import { captureMarketingLeadCapability } from "@hivegrid/db/pretenant-security.server";
|
||||
|
||||
export interface CaptureLeadInput {
|
||||
email: string;
|
||||
@@ -60,74 +59,29 @@ export async function captureLead(input: CaptureLeadInput): Promise<CaptureLeadR
|
||||
const ua = input.userAgent ? input.userAgent.slice(0, 500) : null;
|
||||
const attr = input.attribution ?? {};
|
||||
|
||||
const existing = await db.lead.findUnique({ where: { email }, select: { id: true } });
|
||||
|
||||
if (!existing) {
|
||||
const lead = await db.lead.create({
|
||||
data: {
|
||||
email,
|
||||
firstName: trim(input.firstName),
|
||||
lastName: trim(input.lastName),
|
||||
phone: trim(input.phone),
|
||||
company: trim(input.company),
|
||||
source: trim(input.source),
|
||||
utmSource: trim(attr.utmSource),
|
||||
utmMedium: trim(attr.utmMedium),
|
||||
utmCampaign: trim(attr.utmCampaign),
|
||||
utmTerm: trim(attr.utmTerm),
|
||||
utmContent: trim(attr.utmContent),
|
||||
gclid: trim(attr.gclid),
|
||||
fbclid: trim(attr.fbclid),
|
||||
referrer: trim(attr.referrer),
|
||||
landingPage: trim(attr.landingPage),
|
||||
lastIpHash: ipHash,
|
||||
userAgent: ua,
|
||||
events: {
|
||||
create: {
|
||||
type: "captured",
|
||||
payload: serialisePayload({
|
||||
source: trim(input.source),
|
||||
attribution: cleanAttribution(attr),
|
||||
}),
|
||||
},
|
||||
},
|
||||
},
|
||||
select: { id: true },
|
||||
});
|
||||
return { leadId: lead.id, created: true };
|
||||
}
|
||||
|
||||
// Existing — only fill blanks, never overwrite. First-touch attribution wins.
|
||||
const updateData: Prisma.LeadUpdateInput = {
|
||||
lastTouchAt: new Date(),
|
||||
return captureMarketingLeadCapability({
|
||||
email,
|
||||
firstName: trim(input.firstName),
|
||||
lastName: trim(input.lastName),
|
||||
phone: trim(input.phone),
|
||||
company: trim(input.company),
|
||||
source: trim(input.source),
|
||||
utmSource: trim(attr.utmSource),
|
||||
utmMedium: trim(attr.utmMedium),
|
||||
utmCampaign: trim(attr.utmCampaign),
|
||||
utmTerm: trim(attr.utmTerm),
|
||||
utmContent: trim(attr.utmContent),
|
||||
gclid: trim(attr.gclid),
|
||||
fbclid: trim(attr.fbclid),
|
||||
referrer: trim(attr.referrer),
|
||||
landingPage: trim(attr.landingPage),
|
||||
lastIpHash: ipHash,
|
||||
userAgent: ua,
|
||||
firstName: nonNullSetIfMissing(input.firstName),
|
||||
lastName: nonNullSetIfMissing(input.lastName),
|
||||
phone: nonNullSetIfMissing(input.phone),
|
||||
company: nonNullSetIfMissing(input.company),
|
||||
events: {
|
||||
create: {
|
||||
type: "re-captured",
|
||||
payload: serialisePayload({
|
||||
source: trim(input.source),
|
||||
attribution: cleanAttribution(attr),
|
||||
}),
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
// Only conditionally fill blanks — Prisma update treats `undefined` as
|
||||
// "leave alone", and `null` as "set to NULL" which we explicitly do NOT want.
|
||||
// We re-fetch the row only for fields that need conditional patching to
|
||||
// avoid extra reads; the merge logic happens in the next query batch.
|
||||
await db.lead.update({ where: { id: existing.id }, data: updateData });
|
||||
// Best-effort fill-blanks: only touches columns where the existing value is
|
||||
// NULL and the incoming value is set. This is two queries in the dup-case
|
||||
// which keeps the common path simple.
|
||||
await fillBlankProfileFields(existing.id, input);
|
||||
|
||||
return { leadId: existing.id, created: false };
|
||||
eventPayload: serialisePayload({
|
||||
source: trim(input.source),
|
||||
attribution: cleanAttribution(attr),
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
function trim(v: string | null | undefined): string | null {
|
||||
@@ -136,27 +90,6 @@ function trim(v: string | null | undefined): string | null {
|
||||
return t === "" ? null : t;
|
||||
}
|
||||
|
||||
function nonNullSetIfMissing(_v: string | null | undefined): undefined {
|
||||
// Returning `undefined` so Prisma keeps the existing value untouched.
|
||||
// The fill-blanks step (`fillBlankProfileFields`) handles actual patches.
|
||||
return undefined;
|
||||
}
|
||||
|
||||
async function fillBlankProfileFields(leadId: string, input: CaptureLeadInput): Promise<void> {
|
||||
const row = await db.lead.findUnique({
|
||||
where: { id: leadId },
|
||||
select: { firstName: true, lastName: true, phone: true, company: true },
|
||||
});
|
||||
if (!row) return;
|
||||
const patch: Prisma.LeadUpdateInput = {};
|
||||
if (row.firstName == null && trim(input.firstName)) patch.firstName = trim(input.firstName);
|
||||
if (row.lastName == null && trim(input.lastName)) patch.lastName = trim(input.lastName);
|
||||
if (row.phone == null && trim(input.phone)) patch.phone = trim(input.phone);
|
||||
if (row.company == null && trim(input.company)) patch.company = trim(input.company);
|
||||
if (Object.keys(patch).length === 0) return;
|
||||
await db.lead.update({ where: { id: leadId }, data: patch });
|
||||
}
|
||||
|
||||
function cleanAttribution(attr: NonNullable<CaptureLeadInput["attribution"]>): Record<string, string> {
|
||||
const out: Record<string, string> = {};
|
||||
for (const [k, v] of Object.entries(attr)) {
|
||||
@@ -166,7 +99,7 @@ function cleanAttribution(attr: NonNullable<CaptureLeadInput["attribution"]>): R
|
||||
return out;
|
||||
}
|
||||
|
||||
function serialisePayload(obj: Record<string, unknown>): Prisma.InputJsonValue {
|
||||
function serialisePayload(obj: Record<string, unknown>): Record<string, unknown> {
|
||||
// Drop top-level keys with empty / undefined values for compact JSON storage.
|
||||
const out: Record<string, unknown> = {};
|
||||
for (const [k, v] of Object.entries(obj)) {
|
||||
@@ -174,5 +107,5 @@ function serialisePayload(obj: Record<string, unknown>): Prisma.InputJsonValue {
|
||||
if (typeof v === "object" && Object.keys(v as Record<string, unknown>).length === 0) continue;
|
||||
out[k] = v;
|
||||
}
|
||||
return out as Prisma.InputJsonValue;
|
||||
return out;
|
||||
}
|
||||
Reference in new issue
Block a user