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 Tey0834e7f63009
diff --git a/apps/web/lib/tinybird/get-sale-event.ts b/apps/web/lib/tinybird/get-sale-event.ts
new file mode 100644
index 0000000000..fe2bbd550d
--- /dev/null
+++ b/apps/web/lib/tinybird/get-sale-event.ts
@@ -0,0 +1,10 @@
+import * as z from "zod/v4";
+import { tb } from "./client";
+
+export const getSaleEvent = tb.buildPipe({
+ pipe: "get_sale_event",
+ parameters: z.object({
+ eventId: z.string(),
+ }),
+ data: z.any(),
+});
diff --git a/apps/web/scripts/tinybird/delete-sale-event.ts b/apps/web/scripts/tinybird/delete-sale-event.ts
index 0c2d857bcf..fb08c12189 100644
--- a/apps/web/scripts/tinybird/delete-sale-event.ts
+++ b/apps/web/scripts/tinybird/delete-sale-event.ts
@@ -1,8 +1,32 @@
+import { prisma } from "@dub/prisma";
import "dotenv-flow/config";
+import { syncPartnerLinksStats } from "../../lib/api/partners/sync-partner-links-stats";
+import { getSaleEvent } from "../../lib/tinybird/get-sale-event";
// update tinybird sale event
async function main() {
- const deleteCondition = "link_id = 'link_xxx' AND amount = 0";
+ const eventId = "xxx";
+ const deleteCondition = `event_id = '${eventId}'`;
+ const { data } = await getSaleEvent({ eventId });
+ const oldData = data[0];
+ if (!oldData) {
+ console.log("No data found");
+ return;
+ }
+
+ const { link_id, customer_id } = oldData;
+ const [link, customer] = await Promise.all([
+ prisma.link.findUniqueOrThrow({
+ where: {
+ id: link_id,
+ },
+ }),
+ prisma.customer.findUniqueOrThrow({
+ where: {
+ id: customer_id,
+ },
+ }),
+ ]);
// delete data from tinybird
const deleteRes = await Promise.allSettled([
@@ -16,6 +40,40 @@ async function main() {
}),
]);
console.log(deleteRes);
+
+ await prisma.link.update({
+ where: {
+ id: link.id,
+ },
+ data: {
+ sales: {
+ decrement: 1,
+ },
+ saleAmount: {
+ decrement: oldData.amount,
+ },
+ },
+ });
+
+ await prisma.customer.update({
+ where: {
+ id: customer.id,
+ },
+ data: {
+ sales: {
+ decrement: 1,
+ },
+ saleAmount: {
+ decrement: oldData.amount,
+ },
+ },
+ });
+
+ await syncPartnerLinksStats({
+ partnerId: link.partnerId!,
+ programId: link.programId!,
+ eventType: "sale",
+ });
}
const deleteData = async ({
diff --git a/apps/web/scripts/tinybird/update-sale-event.ts b/apps/web/scripts/tinybird/update-sale-event.ts
index e08eb6cb38..42e4237282 100644
--- a/apps/web/scripts/tinybird/update-sale-event.ts
+++ b/apps/web/scripts/tinybird/update-sale-event.ts
@@ -1,16 +1,7 @@
import "dotenv-flow/config";
-import * as z from "zod/v4";
-import { tb } from "../../lib/tinybird/client";
+import { getSaleEvent } from "../../lib/tinybird/get-sale-event";
import { recordSaleWithTimestamp } from "../../lib/tinybird/record-sale";
-const getSaleEvent = tb.buildPipe({
- pipe: "get_sale_event",
- parameters: z.object({
- eventId: z.string(),
- }),
- data: z.any(),
-});
-
// update tinybird sale event
async function main() {
const eventId = "nBxldg6VxQNUCCwZ";