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.

use customer.saleAmount for LTV + add backfill script

Steven Tey7499ec9e13a1
diff --git a/apps/web/app/api/customers/[id]/activity/route.ts b/apps/web/app/api/customers/[id]/activity/route.ts
index 5398b9bc44..3fc3b2c422 100644
--- a/apps/web/app/api/customers/[id]/activity/route.ts
+++ b/apps/web/app/api/customers/[id]/activity/route.ts
@@ -46,16 +46,6 @@ export const GET = withWorkspace(async ({ workspace, params }) => {
link = decodeLinkIfCaseSensitive(link);
}
- // Find the LTV of the customer
- // TODO: Calculate this from all events, not limited
- const ltv = events.reduce((acc, event) => {
- if (event.event === "sale" && event.saleAmount) {
- acc += Number(event.saleAmount);
- }
-
- return acc;
- }, 0);
-
// Find the time to lead of the customer
const timeToLead =
customer.clickedAt && customer.createdAt
@@ -73,7 +63,7 @@ export const GET = withWorkspace(async ({ workspace, params }) => {
return NextResponse.json(
customerActivityResponseSchema.parse({
- ltv,
+ ltv: customer.saleAmount,
timeToLead,
timeToSale,
events,
diff --git a/apps/web/scripts/backfill-customer-sales.ts b/apps/web/scripts/backfill-customer-sales.ts
new file mode 100644
index 0000000000..c8eb8b42ba
--- /dev/null
+++ b/apps/web/scripts/backfill-customer-sales.ts
@@ -0,0 +1,64 @@
+import { prisma } from "@dub/prisma";
+import "dotenv-flow/config";
+import * as fs from "fs";
+import * as Papa from "papaparse";
+
+let customersToBackfill: {
+ customerId: string;
+ sales: number;
+ saleAmount: number;
+}[] = [];
+
+async function main() {
+ Papa.parse(fs.createReadStream("customer_sales.csv", "utf-8"), {
+ header: true,
+ skipEmptyLines: true,
+ step: (result: {
+ data: {
+ customerId: string;
+ sales: string;
+ saleAmount: string;
+ };
+ }) => {
+ customersToBackfill.push({
+ customerId: result.data.customerId,
+ sales: parseInt(result.data.sales),
+ saleAmount: parseInt(result.data.saleAmount),
+ });
+ },
+ complete: async () => {
+ console.table(
+ customersToBackfill
+ .slice(0, 10)
+ .concat(customersToBackfill.slice(90, 100)),
+ );
+
+ // take first 100 customers, backfill, and delete from csv
+ const chunkedCustomers = customersToBackfill.slice(0, 100);
+
+ await Promise.all(
+ chunkedCustomers.map(async (customer) => {
+ try {
+ await prisma.customer.update({
+ where: { id: customer.customerId },
+ data: {
+ sales: customer.sales,
+ saleAmount: customer.saleAmount,
+ },
+ });
+ } catch (error) {
+ console.log(
+ `Can't find customer ${customer.customerId} with sales ${customer.sales} and sale amount ${customer.saleAmount}`,
+ );
+ }
+ }),
+ );
+
+ // delete from csv
+ customersToBackfill = customersToBackfill.slice(100);
+ fs.writeFileSync("customer_sales.csv", Papa.unparse(customersToBackfill));
+ },
+ });
+}
+
+main();
diff --git a/apps/web/ui/shared/markdown-description.tsx b/apps/web/ui/shared/markdown-description.tsx
index 5f925c5005..6691d30709 100644
--- a/apps/web/ui/shared/markdown-description.tsx
+++ b/apps/web/ui/shared/markdown-description.tsx
@@ -1,5 +1,5 @@
import { cn } from "@dub/utils";
-import Markdown from "react-markdown";
+import ReactMarkdown from "react-markdown";
export const MarkdownDescription = ({
children,
@@ -9,7 +9,7 @@ export const MarkdownDescription = ({
className?: string;
}) => {
return (
- <Markdown
+ <ReactMarkdown
className={cn(
"text-sm text-neutral-500",
"[&_a]:cursor-help [&_a]:text-neutral-600 [&_a]:underline [&_a]:decoration-dotted [&_a]:underline-offset-2 hover:[&_a]:text-neutral-800",
@@ -27,6 +27,6 @@ export const MarkdownDescription = ({
}}
>
{children}
- </Markdown>
+ </ReactMarkdown>
);
};