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.

cleanup partners

Kiran Kc0ba1ba96dac
diff --git a/apps/web/app/(ee)/api/cron/import/tolt/route.ts b/apps/web/app/(ee)/api/cron/import/tolt/route.ts
index 73f9b5c097..9dea3a7d8b 100644
--- a/apps/web/app/(ee)/api/cron/import/tolt/route.ts
+++ b/apps/web/app/(ee)/api/cron/import/tolt/route.ts
@@ -1,5 +1,6 @@
import { handleAndReturnErrorResponse } from "@/lib/api/errors";
import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
+import { cleanupPartners } from "@/lib/tolt/cleanup-partners";
import { importAffiliates } from "@/lib/tolt/import-affiliates";
import { importCommissions } from "@/lib/tolt/import-commissions";
import { importLinks } from "@/lib/tolt/import-links";
@@ -44,6 +45,9 @@ export async function POST(req: Request) {
case "update-stripe-customers":
await updateStripeCustomers(payload);
break;
+ case "cleanup-partners":
+ await cleanupPartners(payload);
+ break;
}
return NextResponse.json("OK");
diff --git a/apps/web/lib/tolt/cleanup-partners.ts b/apps/web/lib/tolt/cleanup-partners.ts
new file mode 100644
index 0000000000..05a3259c41
--- /dev/null
+++ b/apps/web/lib/tolt/cleanup-partners.ts
@@ -0,0 +1,72 @@
+import { prisma } from "@dub/prisma";
+import { toltImporter } from "./importer";
+
+const PARTNER_IDS_PER_BATCH = 100;
+
+export async function cleanupPartners({ programId }: { programId: string }) {
+ let hasMore = true;
+ let start = 0;
+
+ while (hasMore) {
+ const partnerIds = await toltImporter.scanPartnerIds({
+ programId,
+ start,
+ end: start + PARTNER_IDS_PER_BATCH - 1,
+ });
+
+ if (partnerIds.length === 0) {
+ hasMore = false;
+ break;
+ }
+
+ const links = await prisma.link.groupBy({
+ by: ["programId", "partnerId"],
+ where: {
+ programId,
+ partnerId: {
+ in: partnerIds,
+ },
+ },
+ _sum: {
+ leads: true,
+ },
+ });
+
+ const partnersWithNoLeads = links.filter((link) => link._sum.leads === 0);
+ const partnerIdsToRemove = partnersWithNoLeads
+ .map((link) => link.partnerId)
+ .filter((partnerId) => partnerId !== null);
+
+ if (partnerIdsToRemove.length > 0) {
+ await prisma.link.deleteMany({
+ where: {
+ programId,
+ partnerId: {
+ in: partnerIdsToRemove,
+ },
+ },
+ });
+
+ await prisma.programEnrollment.deleteMany({
+ where: {
+ programId,
+ partnerId: {
+ in: partnerIdsToRemove,
+ },
+ },
+ });
+
+ await prisma.partner.deleteMany({
+ where: {
+ id: {
+ in: partnerIdsToRemove,
+ },
+ },
+ });
+ }
+
+ start += PARTNER_IDS_PER_BATCH;
+ }
+
+ await toltImporter.deletePartnerIds(programId);
+}
diff --git a/apps/web/lib/tolt/import-affiliates.ts b/apps/web/lib/tolt/import-affiliates.ts
index f9cfd82752..1c283422d3 100644
--- a/apps/web/lib/tolt/import-affiliates.ts
+++ b/apps/web/lib/tolt/import-affiliates.ts
@@ -52,7 +52,7 @@ export async function importAffiliates({
);
if (activeAffiliates.length > 0) {
- await Promise.all(
+ const partners = await Promise.all(
activeAffiliates.map((affiliate) =>
createPartner({
program,
@@ -61,6 +61,13 @@ export async function importAffiliates({
}),
),
);
+
+ if (partners.length > 0) {
+ await toltImporter.addPartners({
+ programId,
+ partnerIds: partners.map((p) => p.id),
+ });
+ }
}
await new Promise((resolve) => setTimeout(resolve, 2000));
@@ -119,4 +126,6 @@ async function createPartner({
status: "approved",
},
});
+
+ return partner;
}
diff --git a/apps/web/lib/tolt/importer.ts b/apps/web/lib/tolt/importer.ts
index b2f6238cd7..05d64a2dce 100644
--- a/apps/web/lib/tolt/importer.ts
+++ b/apps/web/lib/tolt/importer.ts
@@ -7,13 +7,15 @@ import { ToltConfig } from "./types";
export const MAX_BATCHES = 5;
export const CACHE_EXPIRY = 60 * 60 * 24;
export const CACHE_KEY_PREFIX = "tolt:import";
+export const PARTNER_IDS_KEY_PREFIX = "tolt:import:partnerIds";
export const importSteps = z.enum([
"import-affiliates",
"import-links",
"import-referrals",
"import-commissions",
- "update-stripe-customers",
+ "update-stripe-customers", // update the customers with their stripe customer ID
+ "cleanup-partners", // remove partners with 0 leads
]);
class ToltImporter {
@@ -49,6 +51,40 @@ class ToltImporter {
body,
});
}
+
+ async addPartners({
+ programId,
+ partnerIds,
+ }: {
+ programId: string;
+ partnerIds: string[];
+ }) {
+ if (!partnerIds || partnerIds.length === 0) {
+ return;
+ }
+
+ await redis.lpush(`${PARTNER_IDS_KEY_PREFIX}:${programId}`, ...partnerIds);
+ }
+
+ async scanPartnerIds({
+ programId,
+ start,
+ end,
+ }: {
+ programId: string;
+ start: number;
+ end: number;
+ }) {
+ return await redis.lrange(
+ `${PARTNER_IDS_KEY_PREFIX}:${programId}`,
+ start,
+ end,
+ );
+ }
+
+ async deletePartnerIds(programId: string) {
+ return await redis.del(`${PARTNER_IDS_KEY_PREFIX}:${programId}`);
+ }
}
export const toltImporter = new ToltImporter();
diff --git a/apps/web/scripts/tolt.ts b/apps/web/scripts/tolt.ts
new file mode 100644
index 0000000000..1a946a27ff
--- /dev/null
+++ b/apps/web/scripts/tolt.ts
@@ -0,0 +1,16 @@
+import "dotenv-flow/config";
+import { qstash } from "@/lib/cron";
+import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
+
+async function main() {
+ return await qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/import/tolt`,
+ body: {
+ action: "update-stripe-customers",
+ programId: "prog_CYCu7IMAapjkRpTnr8F1azjN",
+ // page: 0,
+ },
+ });
+}
+
+main();