Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

refactor events list

Kiran Ka654e1db99ce
diff --git a/apps/web/lib/analytics/get-events.ts b/apps/web/lib/analytics/get-events.ts
index 0471263ee7..b15c8f58ac 100644
--- a/apps/web/lib/analytics/get-events.ts
+++ b/apps/web/lib/analytics/get-events.ts
@@ -1,6 +1,12 @@
+import { prisma } from "@/lib/prisma";
import { tb } from "@/lib/tinybird";
-import { z } from "zod";
+import { Link } from "@prisma/client";
import { tbDemo } from "../tinybird/demo-client";
+import {
+ transformClickEventData,
+ transformLeadEventData,
+ transformSaleEventData,
+} from "../webhook/transform";
import { eventsFilterTB } from "../zod/schemas/analytics";
import { clickEventEnrichedSchema } from "../zod/schemas/clicks";
import { leadEventEnrichedSchema } from "../zod/schemas/leads";
@@ -8,14 +14,14 @@ import { saleEventEnrichedSchema } from "../zod/schemas/sales";
import { INTERVAL_DATA } from "./constants";
import { EventsFilters } from "./types";
+// Promise<
+// | z.infer<typeof clickEventEnrichedSchema>[]
+// | z.infer<typeof leadEventEnrichedSchema>[]
+// | z.infer<typeof saleEventEnrichedSchema>[]
+// >
+
// Fetch data for /api/events
-export const getEvents = async (
- params: EventsFilters,
-): Promise<
- | z.infer<typeof clickEventEnrichedSchema>[]
- | z.infer<typeof leadEventEnrichedSchema>[]
- | z.infer<typeof saleEventEnrichedSchema>[]
-> => {
+export const getEvents = async (params: EventsFilters) => {
let { event, workspaceId, interval, start, end, isDemo } = params;
if (start) {
@@ -52,6 +58,75 @@ export const getEvents = async (
end: end.toISOString().replace("T", " ").replace("Z", ""),
});
+ const linkIds = response.data.map((d) => d.link_id);
+
+ const links = await prisma.link.findMany({
+ where: {
+ id: {
+ in: linkIds,
+ },
+ },
+ include: {
+ tags: true,
+ },
+ });
+
+ const linksMap = links.reduce(
+ (acc, link) => {
+ acc[link.id] = link;
+ return acc;
+ },
+ {} as Record<string, Link>,
+ );
+
+ const events = response.data
+ .map((event) => (linksMap[event.link_id] ? event : null))
+ .filter((d) => d !== null);
+
+ // Click events
+ if (event === "clicks") {
+ return events.map((d) =>
+ transformClickEventData({
+ ...d,
+ link: linksMap[d.link_id],
+ bot: d.bot === 1,
+ qr: d.qr === 1,
+ }),
+ );
+ }
+
+ // Lead events
+ if (event === "leads") {
+ return response.data.map((d) =>
+ transformLeadEventData({
+ ...d,
+ bot: d.bot === 1,
+ qr: d.qr === 1,
+ link: linksMap[d.link_id],
+ customerId: "d.customer_id", // TODO: fix it,
+ customerName: d.customer_name,
+ customerEmail: d.customer_email,
+ customerAvatar: d.customer_avatar,
+ }),
+ );
+ }
+
+ // Sale events
+ if (event === "sales") {
+ return response.data.map((d) =>
+ transformSaleEventData({
+ ...d,
+ bot: d.bot === 1,
+ qr: d.qr === 1,
+ link: linksMap[d.link_id],
+ customerId: "d.customer_id",
+ customerName: d.customer_name,
+ customerEmail: d.customer_email,
+ customerAvatar: d.customer_avatar,
+ }),
+ );
+ }
+
return response.data.map((d) => ({
...d,
// timestamp is always in UTC
diff --git a/apps/web/lib/webhook/schemas.ts b/apps/web/lib/webhook/schemas.ts
index 316054783f..58e3bdad57 100644
--- a/apps/web/lib/webhook/schemas.ts
+++ b/apps/web/lib/webhook/schemas.ts
@@ -43,13 +43,11 @@ export const clickEventSchema = z.object({
export const leadEventSchema = z.object({
eventName: z.string(),
customer: customerSchema,
- click: clickEventSchema
- .omit({ link: true, timestamp: true, clickId: true })
- .and(
- z.object({
- id: z.string(),
- }),
- ),
+ click: z
+ .object({
+ id: z.string(),
+ })
+ .and(clickEventSchema.omit({ link: true, timestamp: true, clickId: true })),
link: linkEventSchema,
});
@@ -57,13 +55,11 @@ export const saleEventSchema = z.object({
eventName: z.string(),
customer: customerSchema,
sale: saleSchema,
- click: clickEventSchema
- .omit({ link: true, timestamp: true, clickId: true })
- .and(
- z.object({
- id: z.string(),
- }),
- ),
+ click: z
+ .object({
+ id: z.string(),
+ })
+ .and(clickEventSchema.omit({ link: true, timestamp: true, clickId: true })),
link: linkEventSchema,
});
diff --git a/apps/web/lib/webhook/transform.ts b/apps/web/lib/webhook/transform.ts
index a3b9f25a5e..4195e04a0a 100644
--- a/apps/web/lib/webhook/transform.ts
+++ b/apps/web/lib/webhook/transform.ts
@@ -63,7 +63,9 @@ export const transformLeadEventData = (data: any) => {
id: lead.customerId,
name: lead.customerName,
email: lead.customerEmail,
- avatar: lead.customerAvatar,
+ avatar:
+ lead.customerAvatar ||
+ `https://api.dicebear.com/7.x/micah/svg?seed=${lead.customerId}`,
},
click: {
...lead,