The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Sync the index from the importers and the account merge
Rafidfff8a609b81
diff --git a/apps/web/app/(ee)/api/cron/partners/merge-accounts/route.ts b/apps/web/app/(ee)/api/cron/partners/merge-accounts/route.tsindex f0d85d0916..516382c231 100644--- a/apps/web/app/(ee)/api/cron/partners/merge-accounts/route.ts+++ b/apps/web/app/(ee)/api/cron/partners/merge-accounts/route.ts@@ -3,6 +3,7 @@ import { resolveFraudGroups } from "@/lib/api/fraud/resolve-fraud-groups";import { linkCache } from "@/lib/api/links/cache";import { includeProgramEnrollment } from "@/lib/api/links/include-program-enrollment";import { includeTags } from "@/lib/api/links/include-tags";+import { queuePartnerSearchSync } from "@/lib/api/partners/queue-partner-search-sync";import { syncTotalCommissions } from "@/lib/api/partners/sync-total-commissions";import { PRISMA_UPDATEMANY_LIMIT } from "@/lib/cron";import { verifyQstashSignature } from "@/lib/cron/verify-qstash";@@ -66,6 +67,9 @@ export async function POST(req: Request) {image: true,programs: {select: {+ // Needed to re-index both sides of the merge afterwards, including+ // the source enrollments that get deleted rather than reassigned.+ id: true,programId: true,tenantId: true,status: true,@@ -323,6 +327,17 @@ export async function POST(req: Request) {}}+ // Both sides, because a merge either reassigns a source enrollment to the+ // target partner, which changes every identity field on the document, or+ // deletes it in favour of the target's. Passing all of them lets the job+ // read each one back and work out which happened.+ await queuePartnerSearchSync({+ enrollmentIds: [+ ...sourcePartnerEnrollments.map(({ id }) => id),+ ...targetPartnerEnrollments.map(({ id }) => id),+ ],+ });+// Remove the user if there are no workspaces left// TODO: we need to handle deleting multiple users when we allow partners to invite their team members in the futureconst sourcePartnerUser = sourcePartnerUsers[0];diff --git a/apps/web/lib/firstpromoter/import-partners.ts b/apps/web/lib/firstpromoter/import-partners.tsindex 025071d336..8d7f19c8ce 100644--- a/apps/web/lib/firstpromoter/import-partners.ts+++ b/apps/web/lib/firstpromoter/import-partners.ts@@ -9,6 +9,7 @@ import {import { createId } from "../api/create-id";import { bulkCreateLinks } from "../api/links";import { upsertPartnerPlatform } from "../api/partner-profile/upsert-partner-platform";+import { queuePartnerSearchSync } from "../api/partners/queue-partner-search-sync";import { DEFAULT_PARTNER_GROUP } from "../zod/schemas/groups";import { FirstPromoterApi } from "./api";import { firstPromoterImporter, MAX_BATCHES } from "./importer";@@ -89,6 +90,14 @@ export async function importPartners(payload: FirstPromoterImportPayload) {);}});++ // Queued per page rather than per partner, so a large import produces a+ // handful of chunked payloads instead of one message each.+ await queuePartnerSearchSync({+ enrollmentIds: results.flatMap((result) =>+ result.status === "fulfilled" && result.value ? [result.value] : [],+ ),+ });}currentPage++;@@ -199,12 +208,12 @@ async function createPartnerAndLinks({if (!program.domain || !program.url) {console.error("Program domain or url not found", program.id);- return;+ return programEnrollment.id;}if (programEnrollment.links.length > 0) {console.log("Partner already has links", partner.id);- return;+ return programEnrollment.id;}const links = affiliate.promoter_campaigns.map((campaign, idx) => ({@@ -224,4 +233,6 @@ async function createPartnerAndLinks({await bulkCreateLinks({links,});++ return programEnrollment.id;}diff --git a/apps/web/lib/partnerstack/import-partners.ts b/apps/web/lib/partnerstack/import-partners.tsindex a6f3eb65de..c07b2b864c 100644--- a/apps/web/lib/partnerstack/import-partners.ts+++ b/apps/web/lib/partnerstack/import-partners.ts@@ -2,6 +2,7 @@ import { prisma } from "@/lib/prisma";import { COUNTRIES, COUNTRY_CODES } from "@dub/utils";import { PartnerGroup, Program } from "@prisma/client";import { createId } from "../api/create-id";+import { queuePartnerSearchSync } from "../api/partners/queue-partner-search-sync";import { logImportError } from "../tinybird/log-import-error";import { redis } from "../upstash";import { DEFAULT_PARTNER_GROUP } from "../zod/schemas/groups";@@ -56,7 +57,7 @@ export async function importPartners(payload: PartnerStackImportPayload) {break;}- await Promise.allSettled(+ const results = await Promise.allSettled(partners.map((partner) =>createPartner({program,@@ -67,6 +68,15 @@ export async function importPartners(payload: PartnerStackImportPayload) {),);+ // Queued per page rather than per partner, so a large import produces a+ // handful of chunked payloads instead of one message each.+ await queuePartnerSearchSync({+ partnerIds: results.flatMap((result) =>+ result.status === "fulfilled" && result.value ? [result.value] : [],+ ),+ programId,+ });+await new Promise((resolve) => setTimeout(resolve, 2000));processedBatches++;@@ -199,4 +209,6 @@ async function createPartner({await redis.hset(`${PARTNER_IDS_KEY_PREFIX}:${program.id}`, {[partner.key]: partnerId,});++ return partnerId;}diff --git a/apps/web/lib/rewardful/import-partners.ts b/apps/web/lib/rewardful/import-partners.tsindex c982377c2a..69d9ea1fc7 100644--- a/apps/web/lib/rewardful/import-partners.ts+++ b/apps/web/lib/rewardful/import-partners.ts@@ -3,6 +3,7 @@ import { nanoid } from "@dub/utils";import { Program } from "@prisma/client";import { createId } from "../api/create-id";import { bulkCreateLinks } from "../api/links";+import { queuePartnerSearchSync } from "../api/partners/queue-partner-search-sync";import { logImportError } from "../tinybird/log-import-error";import { redis } from "../upstash";import { RewardfulApi } from "./api";@@ -124,6 +125,13 @@ export async function importPartners(payload: RewardfulImportPayload) {]),),);++ // Queued per page rather than per partner, so a large import produces a+ // handful of chunked payloads instead of one message each.+ await queuePartnerSearchSync({+ partnerIds: filteredPartners.map((p) => p.dubPartnerId),+ programId: program.id,+ });}}diff --git a/apps/web/lib/tapfiliate/cleanup-partners.ts b/apps/web/lib/tapfiliate/cleanup-partners.tsindex 67a2e332e4..cf2607f337 100644--- a/apps/web/lib/tapfiliate/cleanup-partners.ts+++ b/apps/web/lib/tapfiliate/cleanup-partners.ts@@ -1,4 +1,5 @@import { bulkDeleteLinks } from "@/lib/api/links/bulk-delete-links";+import { queuePartnerSearchSync } from "@/lib/api/partners/queue-partner-search-sync";import { prisma } from "@/lib/prisma";import { sendEmail } from "@dub/email";import ProgramImported from "@dub/email/templates/program-imported";@@ -57,6 +58,21 @@ export async function cleanupPartners(payload: TapfiliateImportPayload) {await bulkDeleteLinks(linksToDelete);+ // Resolved before the delete, because afterwards nothing can map these+ // partners back to the enrollments they had, and a deletion is invisible+ // to the reconciliation sweep.+ const removedEnrollments = await prisma.programEnrollment.findMany({+ where: {+ programId,+ partnerId: {+ in: partnerIdsWithNoLeads,+ },+ },+ select: {+ id: true,+ },+ });+await prisma.programEnrollment.deleteMany({where: {programId,@@ -66,6 +82,10 @@ export async function cleanupPartners(payload: TapfiliateImportPayload) {},});+ await queuePartnerSearchSync({+ enrollmentIds: removedEnrollments.map(({ id }) => id),+ });+// Remove partners that are not enrolled in any other programconst otherProgramEnrollments = await prisma.programEnrollment.findMany({where: {diff --git a/apps/web/lib/tapfiliate/import-partners.ts b/apps/web/lib/tapfiliate/import-partners.tsindex a3ae364e8b..b0880ac803 100644--- a/apps/web/lib/tapfiliate/import-partners.ts+++ b/apps/web/lib/tapfiliate/import-partners.ts@@ -4,6 +4,7 @@ import slugify from "@sindresorhus/slugify";import { createId } from "../api/create-id";import { createLink } from "../api/links";import { generatePartnerLink } from "../api/partners/generate-partner-link";+import { queuePartnerSearchSync } from "../api/partners/queue-partner-search-sync";import { logImportError } from "../tinybird/log-import-error";import { WorkspaceProps } from "../types";import { DEFAULT_PARTNER_GROUP } from "../zod/schemas/groups";@@ -111,6 +112,10 @@ export async function importPartners(payload: TapfiliateImportPayload) {.map((p) => p.value);if (partnerIds.length > 0) {+ // Queued per page rather than per partner, so a large import produces a+ // handful of chunked payloads instead of one message each.+ await queuePartnerSearchSync({ partnerIds, programId });+await tapfiliateImporter.trackImportedPartnerIds({programId,partnerIds,diff --git a/apps/web/lib/tolt/cleanup-partners.ts b/apps/web/lib/tolt/cleanup-partners.tsindex fa771fe62e..103f3ec1ab 100644--- a/apps/web/lib/tolt/cleanup-partners.ts+++ b/apps/web/lib/tolt/cleanup-partners.ts@@ -1,4 +1,5 @@import { bulkDeleteLinks } from "@/lib/api/links/bulk-delete-links";+import { queuePartnerSearchSync } from "@/lib/api/partners/queue-partner-search-sync";import { prisma } from "@/lib/prisma";import { toltImporter } from "./importer";@@ -51,6 +52,21 @@ export async function cleanupPartners({ programId }: { programId: string }) {await bulkDeleteLinks(linksToDelete);+ // Resolved before the delete, because afterwards nothing can map these+ // partners back to the enrollments they had, and a deletion is invisible+ // to the reconciliation sweep.+ const removedEnrollments = await prisma.programEnrollment.findMany({+ where: {+ programId,+ partnerId: {+ in: partnerIdsToRemove,+ },+ },+ select: {+ id: true,+ },+ });+await prisma.programEnrollment.deleteMany({where: {programId,@@ -60,6 +76,10 @@ export async function cleanupPartners({ programId }: { programId: string }) {},});+ await queuePartnerSearchSync({+ enrollmentIds: removedEnrollments.map(({ id }) => id),+ });+// Remove partners that are not enrolled in any other programconst otherProgramEnrollments = await prisma.programEnrollment.findMany({where: {diff --git a/apps/web/lib/tolt/import-partners.ts b/apps/web/lib/tolt/import-partners.tsindex 8055d55fb3..0c0e30007b 100644--- a/apps/web/lib/tolt/import-partners.ts+++ b/apps/web/lib/tolt/import-partners.ts@@ -1,6 +1,7 @@import { prisma } from "@/lib/prisma";import { Partner, Program } from "@prisma/client";import { createId } from "../api/create-id";+import { queuePartnerSearchSync } from "../api/partners/queue-partner-search-sync";import { logImportError } from "../tinybird/log-import-error";import { DEFAULT_PARTNER_GROUP } from "../zod/schemas/groups";import { ToltApi } from "./api";@@ -84,6 +85,13 @@ export async function importPartners(payload: ToltImportPayload) {.map((p) => p.value);if (partners.length > 0) {+ // Queued per page rather than per partner, so a large import produces a+ // handful of chunked payloads instead of one message each.+ await queuePartnerSearchSync({+ partnerIds: partners.map((p) => p.id),+ programId,+ });+await toltImporter.addPartners({programId,partnerIds: partners.map((p) => p.id),