Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions apps/web/app/(ee)/api/cron/framer/backfill-leads-batch/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { generateRandomName } from "@/lib/names";
import {
recordLeadWithTimestamp,
recordSaleWithTimestamp,
tb,
} from "@/lib/tinybird";
import { redis } from "@/lib/upstash";
import z from "@/lib/zod";
Expand Down Expand Up @@ -38,6 +39,16 @@ const FRAMER_WORKSPACE_ID = "clsvopiw0000ejy0grp821me0";
const CACHE_KEY = "framerMigratedExternalIdEventNames";
const DOMAIN = "framer.link";

const getFramerLeadEvents = tb.buildPipe({
pipe: "get_framer_lead_events",
parameters: z.object({
linkIds: z
.union([z.string(), z.array(z.string())])
.transform((v) => (Array.isArray(v) ? v : v.split(","))),
}),
data: z.any(),
});

// POST /api/cron/framer/backfill-leads-batch
export const POST = withWorkspace(async ({ req, workspace }) => {
try {
Expand Down Expand Up @@ -80,22 +91,34 @@ export const POST = withWorkspace(async ({ req, workspace }) => {
}),
]);

const { data: leadEventsForLinks } = await getFramerLeadEvents({
linkIds: links.map((l) => l.id),
});

let validEntries: PayloadItem[] = [];
let invalidEntries: (PayloadItem & { error: string })[] = [];
let invalidEntries: (PayloadItem & { error: string; clickId?: string })[] =
[];

originalPayload.map((p, index) => {
if (existsResults[index]) {
const linkData = links.find((l) => l.key === p.via);

if (!linkData) {
invalidEntries.push({
...p,
error: "Already backfilled.",
error: `Link for via tag ${p.via} not found.`,
});
return;
}

if (!links.some((l) => l.key === p.via)) {
if (existsResults[index]) {
const clickId = leadEventsForLinks.find(
(e) => e.link_id === linkData.id && e.event_name === p.eventName,
)?.click_id;

invalidEntries.push({
...p,
error: `Link for via tag ${p.via} not found.`,
error: "Already backfilled.",
clickId,
});
return;
}
Expand Down
6 changes: 4 additions & 2 deletions apps/web/scripts/framer/backfill-remainder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import z from "../../lib/zod";
const getFramerLeadEvents = tb.buildPipe({
pipe: "get_framer_lead_events",
parameters: z.object({
linkId: z.string(),
linkIds: z
.union([z.string(), z.array(z.string())])
.transform((v) => (Array.isArray(v) ? v : v.split(","))),
}),
data: z.any(),
});
Expand Down Expand Up @@ -69,7 +71,7 @@ async function processFramerData(linkToBackfill: {
);

const { data: leadEvents } = await getFramerLeadEvents({
linkId: linkToBackfill.linkId,
linkIds: linkToBackfill.linkId,
});

const customerIdsSet = new Set(
Expand Down