Skip to content

Commit

Permalink
update scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed Nov 18, 2024
1 parent 98c0b04 commit 40ca8c2
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 13 deletions.
91 changes: 80 additions & 11 deletions apps/web/scripts/backfill-missing-sales.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import { getEvents } from "@/lib/analytics/get-events";
import { prisma } from "@/lib/prisma";
import { getLeadEvent, recordSale } from "@/lib/tinybird";
import { clickEventSchemaTB } from "@/lib/zod/schemas/clicks";
import { nanoid } from "@dub/utils";
import "dotenv-flow/config";
import { SaleEvent } from "dub/models/components";
import * as fs from "fs";
import * as Papa from "papaparse";

const projectId = "xxx";
const workspaceId = "xxx";
let customerSpend: {
email: string;
spend: number;
amount: number;
date: string;
}[] = [];

async function main() {
const customers = await prisma.customer.findMany({
where: {
projectId,
projectId: workspaceId,
},
select: {
id: true,
email: true,
externalId: true,
},
Expand All @@ -30,23 +37,85 @@ async function main() {
data: {
email: string;
net_volume: string;
created: string;
};
}) => {
customerSpend.push({
email: result.data.email,
spend: parseFloat(result.data.net_volume),
amount: parseFloat(result.data.net_volume),
date: result.data.created,
});
},
complete: () => {
console.table(customerSpend);
complete: async () => {
const alreadyLogged = (await getEvents({
workspaceId,
event: "sales",
interval: "all",
page: 1,
limit: 1000,
order: "desc",
sortBy: "timestamp",
})) as unknown as SaleEvent[];

// overlap
const toBackfill = await Promise.all(
customerSpend.map(async (cs) => {
const customer = customers.find((c) => c.email === cs.email);
if (!customer) return null;
// if the sale has already been logged, skip
if (
alreadyLogged.some(
(e) => e.customer["externalId"] === customer.externalId,
)
)
return null;

const overlap = customers.filter((c) =>
customerSpend.some((cs) => cs.email === c.email),
);
const eventId = nanoid(16);

console.table(overlap);
const leadEvent = await getLeadEvent({
customerId: customer.id,
});

const clickData = clickEventSchemaTB
.omit({ timestamp: true })
.parse(leadEvent.data[0]);

const saleAmount = cs.amount * 100;

const data = {
...clickData,
timestamp: new Date(cs.date).toISOString(),
event_id: eventId,
event_name: "Subscription creation",
customer_id: customer.id,
payment_processor: "stripe",
amount: saleAmount,
currency: "usd",
invoice_id: "",
metadata: "",
};

const tbRes = await recordSale(data);
console.log(tbRes);
const prismaRes = prisma.link.update({
where: {
id: clickData.link_id,
},
data: {
sales: {
increment: 1,
},
saleAmount: {
increment: saleAmount,
},
},
});
console.log(prismaRes);

return data;
}),
).then((res) => res.filter(Boolean));

console.log(JSON.stringify(toBackfill, null, 2));
},
});
}
Expand Down
3 changes: 2 additions & 1 deletion apps/web/scripts/backfill-programId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const enrollmentIds = [
// "cm3ghlajq00002tvj5b72vvgj",
// "cm3ghqh1300012tvjbeyeo5ec",
// "cm3gofygs000011simcw9kcqd",
"cm3goiy8q0000rcpha96y0vhj",
// "cm3goiy8q0000rcpha96y0vhj",
"cm2v7e3780000d1efwh8b63y5",
];

async function main() {
Expand Down
2 changes: 1 addition & 1 deletion apps/web/scripts/backfill-sales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import z from "@/lib/zod";
import { saleEventResponseSchema } from "@/lib/zod/schemas/sales";
import "dotenv-flow/config";

const enrollmentId = "cm3goiy8q0000rcpha96y0vhj";
const enrollmentId = "cm2v7e3780000d1efwh8b63y5";

async function main() {
const programEnrollment = await prisma.programEnrollment.findUnique({
Expand Down

0 comments on commit 40ca8c2

Please sign in to comment.