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.

update api/customers/[id]/activities

Kiran K8c7b6ac647d1
diff --git a/apps/web/app/api/customers/[id]/activities/route.ts b/apps/web/app/api/customers/[id]/activities/route.ts
index 0c1abca6c1..a63d8f20e5 100644
--- a/apps/web/app/api/customers/[id]/activities/route.ts
+++ b/apps/web/app/api/customers/[id]/activities/route.ts
@@ -1,29 +1,23 @@
+import { getEvents } from "@/lib/analytics/get-events";
import { getCustomerOrThrow } from "@/lib/api/customers/get-customer-or-throw";
import { withWorkspace } from "@/lib/auth";
-import { getCustomerEvents } from "@/lib/tinybird/get-customer-events";
-import { CustomerSchema } from "@/lib/zod/schemas/customers";
+import {
+ customerActivitySchema,
+ CustomerSchema,
+} from "@/lib/zod/schemas/customers";
+import { saleEventResponseSchema } from "@/lib/zod/schemas/sales";
import { NextResponse } from "next/server";
import { z } from "zod";
-const CustomerActivitySchema = z.object({
- timestamp: z.date(),
- event: z.enum(["click", "lead", "sale"]),
- event_name: z.string(),
- metadata: z.union([
- z.null(),
- z.object({
- payment_processor: z.string(),
- amount: z.string(),
- }),
- ]),
-});
+type CustomerActivity = z.infer<typeof customerActivitySchema>;
+type SaleEvent = z.infer<typeof saleEventResponseSchema>;
-const schema = z.object({
+const responseSchema = z.object({
ltv: z.number(),
timeToLead: z.number(),
timeToSale: z.number(),
customer: CustomerSchema,
- activities: z.array(CustomerActivitySchema),
+ activities: z.array(customerActivitySchema),
});
// GET /api/customers/[id]/activities - get a customer's activity
@@ -35,21 +29,44 @@ export const GET = withWorkspace(async ({ workspace, params }) => {
id: customerId,
});
- const events = await getCustomerEvents({
- customerId,
+ // TODO:
+ // We need to add a new filter clickId/customerId to `getEvents` or add a new pipe to get sales by clickId/customerId
+ const events = await getEvents({
+ event: "sales",
+ order: "desc",
+ sortBy: "timestamp",
+ linkId: customer.linkId!, // FIX ME
+ workspaceId: workspace.id,
+ interval: "1y",
+ page: 1,
+ limit: 50,
+ });
+
+ const activities: CustomerActivity[] = events.map((event: SaleEvent) => {
+ return {
+ timestamp: new Date(event.timestamp),
+ event: "sale",
+ event_name: event.eventName,
+ metadata: {
+ amount: event.sale.amount,
+ payment_processor: event.sale.paymentProcessor,
+ },
+ };
});
- const activities = events.data.map((event) => ({
- ...event,
- metadata: event.metadata ? JSON.parse(event.metadata) : null,
- timestamp: new Date(event.timestamp),
- }));
+ // Add lead event to activities
+ activities.push({
+ timestamp: customer.leadCreatedAt!,
+ event: "lead",
+ event_name: "Account created",
+ metadata: null,
+ });
// Add click event to activities
activities.push({
timestamp: customer.clickedAt!,
event: "click",
- event_name: "Link click",
+ event_name: "Link clicked",
metadata: null,
});
@@ -78,7 +95,7 @@ export const GET = withWorkspace(async ({ workspace, params }) => {
: 0;
return NextResponse.json(
- schema.parse({
+ responseSchema.parse({
ltv,
timeToLead,
timeToSale,
diff --git a/apps/web/lib/tinybird/get-customer-events.ts b/apps/web/lib/tinybird/get-customer-events.ts
deleted file mode 100644
index feb9cab459..0000000000
--- a/apps/web/lib/tinybird/get-customer-events.ts
+++ /dev/null
@@ -1,12 +0,0 @@
-import z from "../zod";
-import { customerEventsSchemaTB } from "../zod/schemas/customers";
-import { tb } from "./client";
-
-// We only fetch lead and click events from TB
-export const getCustomerEvents = tb.buildPipe({
- pipe: "get_customer_events",
- parameters: z.object({
- customerId: z.string(),
- }),
- data: customerEventsSchemaTB,
-});
diff --git a/apps/web/lib/zod/schemas/customers.ts b/apps/web/lib/zod/schemas/customers.ts
index 5cc4163294..95fee7404e 100644
--- a/apps/web/lib/zod/schemas/customers.ts
+++ b/apps/web/lib/zod/schemas/customers.ts
@@ -74,16 +74,6 @@ export const trackCustomerResponseSchema = z.object({
customerAvatar: z.string().nullable(),
});
-// simple schema returned by /events API
-// TODO: Remove this schema if it is not used
-export const customerSchema = z.object({
- id: z.string(),
- name: z.string(),
- email: z.string(),
- avatar: z.string(),
- externalId: z.string().optional(),
-});
-
export const CUSTOMERS_MAX_PAGE_SIZE = 100;
export const customersQuerySchema = z
@@ -103,3 +93,16 @@ export const customerEventsSchemaTB = z.object({
event_name: z.string(),
metadata: z.string().default(""),
});
+
+export const customerActivitySchema = z.object({
+ timestamp: z.date(),
+ event: z.enum(["click", "lead", "sale"]),
+ event_name: z.string(),
+ metadata: z.union([
+ z.null(),
+ z.object({
+ payment_processor: z.string(),
+ amount: z.number(),
+ }),
+ ]),
+});