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 scripts

Steven Tey5476a700452a
diff --git a/apps/web/scripts/customers/beehiiv/update-commissions-payouts.ts b/apps/web/scripts/customers/beehiiv/update-commissions-payouts.ts
new file mode 100644
index 0000000000..d859f56a66
--- /dev/null
+++ b/apps/web/scripts/customers/beehiiv/update-commissions-payouts.ts
@@ -0,0 +1,93 @@
+import { prisma } from "@dub/prisma";
+import "dotenv-flow/config";
+
+async function main() {
+ const customerId = "cus_xxx";
+ const newLinkId = "link_xxx";
+
+ const newLink = await prisma.link.findUniqueOrThrow({
+ where: {
+ id: newLinkId,
+ },
+ });
+
+ const updatedCommissions = await prisma.commission.updateMany({
+ where: {
+ customerId,
+ status: {
+ not: "processed",
+ },
+ },
+ data: {
+ linkId: newLink.id,
+ partnerId: newLink.partnerId!,
+ },
+ });
+
+ console.log(
+ `Updated ${updatedCommissions.count} non-processed commissions to link ${newLink.id} and partner ${newLink.partnerId}`,
+ );
+
+ const processedCommissions = await prisma.commission.findMany({
+ where: {
+ customerId,
+ status: "processed",
+ },
+ });
+
+ console.log(
+ `Found ${processedCommissions.length} processed commissions for customer ${customerId}`,
+ );
+
+ const updatedProcessedCommissions = await prisma.commission.updateMany({
+ where: {
+ id: {
+ in: processedCommissions.map((commission) => commission.id),
+ },
+ },
+ data: {
+ linkId: newLink.id,
+ partnerId: newLink.partnerId!,
+ payoutId: null,
+ status: "pending",
+ },
+ });
+
+ console.log(
+ `Updated ${updatedProcessedCommissions.count} processed commissions to link ${newLink.id} and partner ${newLink.partnerId}`,
+ );
+
+ const payoutIdsToRetally = Array.from(
+ new Set(
+ processedCommissions
+ .map((commission) => commission.payoutId)
+ .filter((payoutId): payoutId is string => Boolean(payoutId)),
+ ),
+ );
+
+ for (const payoutId of payoutIdsToRetally) {
+ const commissionsSum = await prisma.commission.aggregate({
+ where: {
+ payoutId,
+ },
+ _sum: {
+ earnings: true,
+ },
+ });
+ const payoutAmount = commissionsSum._sum?.earnings ?? 0;
+ if (payoutAmount > 0) {
+ await prisma.payout.update({
+ where: { id: payoutId },
+ data: { amount: payoutAmount },
+ });
+ console.log(`Updated payout ${payoutId} with amount ${payoutAmount}`);
+ } else {
+ await prisma.payout.delete({
+ where: { id: payoutId },
+ });
+ console.log(`Deleted payout ${payoutId} because it has no earnings`);
+ }
+ }
+}
+
+main();
diff --git a/apps/web/scripts/customers/beehiiv/update-links-stats.ts b/apps/web/scripts/customers/beehiiv/update-links-stats.ts
new file mode 100644
index 0000000000..d18d5b588c
--- /dev/null
+++ b/apps/web/scripts/customers/beehiiv/update-links-stats.ts
@@ -0,0 +1,63 @@
+import { prisma } from "@dub/prisma";
+import "dotenv-flow/config";
+
+async function main() {
+ const customerId = "cus_xxx";
+ const oldLinkId = "link_xxx";
+ const newLinkId = "link_xxx";
+
+ const newLink = await prisma.link.findUniqueOrThrow({
+ where: {
+ id: newLinkId,
+ },
+ });
+
+ await prisma.customer.update({
+ where: {
+ id: customerId,
+ },
+ data: {
+ linkId: newLink.id,
+ partnerId: newLink.partnerId,
+ },
+ });
+ console.log(
+ `Updated customer ${customerId} with linkId ${newLink.id} and partnerId ${newLink.partnerId}`,
+ );
+
+ const aggregateStats = await prisma.customer.groupBy({
+ by: ["linkId"],
+ where: {
+ linkId: {
+ in: [oldLinkId, newLinkId],
+ },
+ },
+ _count: {
+ id: true,
+ },
+ _sum: {
+ sales: true,
+ saleAmount: true,
+ },
+ });
+
+ console.log(aggregateStats);
+
+ for (const stat of aggregateStats) {
+ const { linkId, _count, _sum } = stat;
+ if (!linkId) continue;
+ await prisma.link.update({
+ where: { id: linkId },
+ data: {
+ leads: _count?.id ?? 0,
+ sales: _sum?.sales ?? 0,
+ saleAmount: _sum?.saleAmount ?? 0,
+ },
+ });
+ console.log(
+ `Updated link ${linkId} with leads ${_count?.id ?? 0} and sales ${_sum?.sales ?? 0} and saleAmount ${_sum?.saleAmount ?? 0}`,
+ );
+ }
+}
+
+main();
diff --git a/apps/web/scripts/customers/geoship/update-sale-events-amount.ts b/apps/web/scripts/customers/beehiiv/update-sale-events.ts
similarity index 65%
rename from apps/web/scripts/customers/geoship/update-sale-events-amount.ts
rename to apps/web/scripts/customers/beehiiv/update-sale-events.ts
index 540d5f4ff4..bf8ca5e8b4 100644
--- a/apps/web/scripts/customers/geoship/update-sale-events-amount.ts
+++ b/apps/web/scripts/customers/beehiiv/update-sale-events.ts
@@ -4,37 +4,35 @@ import * as z from "zod/v4";
import { tb } from "../../../lib/tinybird/client";
import { recordSaleWithTimestamp } from "../../../lib/tinybird/record-sale";
-const getSaleEvent = tb.buildPipe({
- pipe: "get_sale_events",
+const getEvents = tb.buildPipe({
+ pipe: "internal_get_events",
parameters: z.object({
- linkId: z.string(),
+ customerId: z.string(),
}),
data: z.any(),
});
// update tinybird sale event
async function main() {
- const linkId = "link_xxx";
- const columnName = "amount";
- const columnValue = 20000;
+ const customerId = "cus_xxx";
+ const oldLinkId = "link_xxx";
+ const newLinkId = "link_xxx";
- const link = await prisma.link.findUniqueOrThrow({
+ const newLink = await prisma.link.findUniqueOrThrow({
where: {
- id: linkId,
+ id: newLinkId,
},
});
- const { data } = await getSaleEvent({ linkId });
+ const { data } = await getEvents({ customerId });
if (data.length === 0) {
console.log("No data found");
return;
}
const updatedData = data.map((item) => ({
...item,
- [columnName]: columnValue,
- domain: link.domain,
- key: link.key,
- workspace_id: link.projectId,
+ link_id: newLink.id,
+ key: newLink.key,
}));
console.log(updatedData);
@@ -45,15 +43,11 @@ async function main() {
const deleteRes = await Promise.allSettled([
deleteData({
dataSource: "dub_sale_events",
- linkId,
- columnName,
- oldValue: 50000,
+ linkId: oldLinkId,
}),
deleteData({
dataSource: "dub_sale_events_mv",
- linkId,
- columnName,
- oldValue: 50000,
+ linkId: oldLinkId,
}),
]);
console.log(deleteRes);
@@ -62,13 +56,9 @@ async function main() {
const deleteData = async ({
dataSource,
linkId,
- columnName,
- oldValue,
}: {
dataSource: string;
linkId: string;
- columnName: string;
- oldValue: number;
}) => {
return fetch(
`https://api.us-east.tinybird.co/v0/datasources/${dataSource}/delete`,
@@ -78,7 +68,7 @@ const deleteData = async ({
Authorization: `Bearer ${process.env.TINYBIRD_API_KEY}`,
"Content-Type": "application/x-www-form-urlencoded",
},
- body: `delete_condition=link_id='${linkId}' and ${columnName}='${oldValue}'`,
+ body: `delete_condition=link_id='${linkId}' and customer_id='cus_1KPRE36329N3YYR6YPG0KXB2J'`,
},
).then((res) => res.json());
};
diff --git a/apps/web/scripts/partners/aggregate-stats-seeding.ts b/apps/web/scripts/partners/aggregate-stats-seeding.ts
index bff458d8a1..13a60e0697 100644
--- a/apps/web/scripts/partners/aggregate-stats-seeding.ts
+++ b/apps/web/scripts/partners/aggregate-stats-seeding.ts
@@ -1,6 +1,6 @@
import { prisma } from "@dub/prisma";
import "dotenv-flow/config";
-import { publishPartnerActivityEvent } from "../../lib/upstash/redis-streams";
+import { publishPartnerActivityEvent } from "../../lib/upstash/redis-streams/partner-activity";
// Seeding script to seed the partner stats with activity
async function main() {
diff --git a/apps/web/scripts/tinybird/update-click-event.ts b/apps/web/scripts/tinybird/update-click-event.ts
index 37e5cd3cfb..f253e41ac5 100644
--- a/apps/web/scripts/tinybird/update-click-event.ts
+++ b/apps/web/scripts/tinybird/update-click-event.ts
@@ -3,9 +3,9 @@ import { getClickEvent } from "../../lib/tinybird/get-click-event";
// update tinybird sale event
async function main() {
- const clickId = "liE74JEvfIBXKNmR";
+ const clickId = "D0aoUca8hLdzmJJE";
const columnName = "link_id";
- const columnValue = "link_1JRVCZWHWACS7R2KZB9ME6CJR";
+ const columnValue = "link_xxx";
const oldData = await getClickEvent({ clickId });
if (!oldData) {
@@ -37,13 +37,13 @@ async function main() {
dataSource: "dub_click_events_mv",
clickId,
columnName,
- oldValue: oldData[columnName],
+ oldValue: "link_1KPRDAYXXE9D2FYM691BK5C59",
}),
deleteData({
dataSource: "dub_click_events_id",
clickId,
columnName,
- oldValue: oldData[columnName],
+ oldValue: "link_1KPRDAYXXE9D2FYM691BK5C59",
}),
]);
console.log(deleteRes);
diff --git a/apps/web/scripts/tinybird/update-lead-event.ts b/apps/web/scripts/tinybird/update-lead-event.ts
index 58063db0c7..85c656b7c9 100644
--- a/apps/web/scripts/tinybird/update-lead-event.ts
+++ b/apps/web/scripts/tinybird/update-lead-event.ts
@@ -1,23 +1,29 @@
+import { prisma } from "@dub/prisma";
import "dotenv-flow/config";
import * as z from "zod/v4";
import { tb } from "../../lib/tinybird/client";
-import { recordLeadWithTimestamp } from "../../lib/tinybird/record-lead";
-const getLeadEvent = tb.buildPipe({
- pipe: "get_lead_event_by_id",
+const getEvents = tb.buildPipe({
+ pipe: "internal_get_events",
parameters: z.object({
- eventId: z.string(),
+ customerId: z.string(),
}),
data: z.any(),
});
// update tinybird lead event
async function main() {
- const eventId = "9Q22Mkh9edsHdePl";
- const columnName = "event_name";
- const columnValue = "Manual commission for QE6IwYufKjVaHFbreyW3";
+ const customerId = "cus_xxx";
+ const oldLinkId = "link_xxx";
+ const newLinkId = "link_xxx";
- const { data } = await getLeadEvent({ eventId });
+ const newLink = await prisma.link.findUniqueOrThrow({
+ where: {
+ id: newLinkId,
+ },
+ });
+
+ const { data } = await getEvents({ customerId });
const oldData = data[0];
if (!oldData) {
console.log("No data found");
@@ -25,26 +31,27 @@ async function main() {
}
const updatedData = {
...oldData,
- [columnName]: columnValue,
+ link_id: newLink.id,
+ key: newLink.key,
};
- console.log(updatedData);
+ // console.log(updatedData);
- const res = await recordLeadWithTimestamp(updatedData);
- console.log(res);
+ // const res = await recordLeadWithTimestamp(updatedData);
+ // console.log(res);
// delete data from tinybird
const deleteRes = await Promise.allSettled([
deleteData({
dataSource: "dub_lead_events",
- eventId,
- columnName,
- oldValue: oldData[columnName],
+ customerId,
+ columnName: "link_id",
+ oldValue: oldLinkId,
}),
deleteData({
dataSource: "dub_lead_events_mv",
- eventId,
- columnName,
- oldValue: oldData[columnName],
+ customerId,
+ columnName: "link_id",
+ oldValue: oldLinkId,
}),
]);
console.log(deleteRes);
@@ -52,12 +59,12 @@ async function main() {
const deleteData = async ({
dataSource,
- eventId,
+ customerId,
columnName,
oldValue,
}: {
dataSource: string;
- eventId: string;
+ customerId: string;
columnName: string;
oldValue: string;
}) => {
@@ -69,7 +76,7 @@ const deleteData = async ({
Authorization: `Bearer ${process.env.TINYBIRD_API_KEY}`,
"Content-Type": "application/x-www-form-urlencoded",
},
- body: `delete_condition=event_id='${eventId}' and ${columnName}='${oldValue}'`,
+ body: `delete_condition=customer_id='${customerId}' and ${columnName}='${oldValue}'`,
},
).then((res) => res.json());
};