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.

Sync the index on the remaining enrollment and profile paths

Rafi3ac2d2135f06
diff --git a/apps/web/app/(ee)/api/cron/partners/auto-reject/route.ts b/apps/web/app/(ee)/api/cron/partners/auto-reject/route.ts
index 8dda919d85..3a8aea2bf6 100644
--- a/apps/web/app/(ee)/api/cron/partners/auto-reject/route.ts
+++ b/apps/web/app/(ee)/api/cron/partners/auto-reject/route.ts
@@ -1,4 +1,5 @@
import { resolveFraudGroups } from "@/lib/api/fraud/resolve-fraud-groups";
+import { queuePartnerSearchSync } from "@/lib/api/partners/queue-partner-search-sync";
import { trackApplicationEvents } from "@/lib/application-events/update-application-event";
import { withCron } from "@/lib/cron/with-cron";
import { evaluateApplicationRequirements } from "@/lib/partners/evaluate-application-requirements";
@@ -143,6 +144,8 @@ export const POST = withCron(async ({ rawBody }) => {
partnerIds: [partnerId],
}),
+ queuePartnerSearchSync({ enrollmentIds: [programEnrollment.id] }),
+
partner.email &&
sendEmail({
to: partner.email,
diff --git a/apps/web/lib/actions/confirm-email-change.ts b/apps/web/lib/actions/confirm-email-change.ts
index 48fecef3e7..00b464551e 100644
--- a/apps/web/lib/actions/confirm-email-change.ts
+++ b/apps/web/lib/actions/confirm-email-change.ts
@@ -5,6 +5,7 @@ import {
assertCanConfirmEmailChange,
EmailChangeRequestData,
} from "@/lib/auth/confirm-email-change";
+import { queuePartnerSearchSync } from "@/lib/api/partners/queue-partner-search-sync";
import { syncPlainCustomerEmail } from "@/lib/plain/upsert-plain-customer";
import { prisma } from "@/lib/prisma";
import { redis } from "@/lib/upstash";
@@ -129,6 +130,14 @@ export const confirmEmailChangeAction = authUserActionClient
const shouldSyncPlainCustomerEmail =
!!data.syncIdentity || !data.isPartnerProfile;
+ // Whichever of the two partner branches above ran, if either. The email-only
+ // branch touches no partner and so has nothing to re-index.
+ const changedPartnerId = data.syncIdentity
+ ? data.partnerId
+ : data.isPartnerProfile
+ ? tokenIdentifier
+ : undefined;
+
waitUntil(
Promise.allSettled([
sendEmail({
@@ -154,6 +163,12 @@ export const confirmEmailChangeAction = authUserActionClient
: []),
redis.del(`email-change-request:token:${tokenFound.token}`),
+
+ // Email is both a searchable field and the n-gram source, so a stale
+ // one leaves the partner findable under an address they no longer have.
+ ...(changedPartnerId
+ ? [queuePartnerSearchSync({ partnerIds: [changedPartnerId] })]
+ : []),
]),
);
diff --git a/apps/web/lib/actions/partners/accept-program-invite.ts b/apps/web/lib/actions/partners/accept-program-invite.ts
index 447806269d..84ca9cc358 100644
--- a/apps/web/lib/actions/partners/accept-program-invite.ts
+++ b/apps/web/lib/actions/partners/accept-program-invite.ts
@@ -1,5 +1,6 @@
"use server";
+import { queuePartnerSearchSync } from "@/lib/api/partners/queue-partner-search-sync";
import { executeWorkflows } from "@/lib/api/workflows/execute-workflows";
import { triggerDraftBountySubmissionCreation } from "@/lib/bounty/api/trigger-draft-bounty-submissions";
import { generateDiscountCodeForPartner } from "@/lib/discounts/generate-discount-code-for-partner";
@@ -95,6 +96,10 @@ export const acceptProgramInviteAction = authPartnerActionClient
partnerId: enrolledPartner.id,
},
}),
+ // 5. Reflect the accepted status in the partner search index
+ queuePartnerSearchSync({
+ enrollmentIds: [enrollment.id],
+ }),
]);
})(),
);
diff --git a/apps/web/lib/actions/partners/bulk-invite-partners.ts b/apps/web/lib/actions/partners/bulk-invite-partners.ts
index 496b531a3b..66dedf2010 100644
--- a/apps/web/lib/actions/partners/bulk-invite-partners.ts
+++ b/apps/web/lib/actions/partners/bulk-invite-partners.ts
@@ -4,6 +4,7 @@ import { recordAuditLog } from "@/lib/api/audit-logs/record-audit-log";
import { createId } from "@/lib/api/create-id";
import { bulkCreateLinks } from "@/lib/api/links";
import { getGroupRewardsAndBounties } from "@/lib/api/partners/get-group-rewards-and-bounties";
+import { queuePartnerSearchSync } from "@/lib/api/partners/queue-partner-search-sync";
import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
import { extractUtmParams } from "@/lib/api/utm/extract-utm-params";
import { throwIfPartnersLimitExceeded } from "@/lib/partners/throw-if-partners-limit-exceeded";
@@ -199,6 +200,16 @@ export const bulkInvitePartnersAction = authActionClient
);
}
+ // createMany returns no IDs, so this passes partners scoped by program
+ // and lets the job resolve the enrollments. Queued after the default
+ // links exist so the first document already carries them. Partners that
+ // skipDuplicates left untouched are re-indexed unchanged, which is
+ // cheaper than working out which ones were new.
+ await queuePartnerSearchSync({
+ partnerIds: partners.map(({ id }) => id),
+ programId,
+ });
+
const { rewards, bounties } = await getGroupRewardsAndBounties({
programId,
groupId: groupId || program.defaultGroupId,
diff --git a/apps/web/lib/actions/partners/create-program-application.ts b/apps/web/lib/actions/partners/create-program-application.ts
index 9a6b4d834d..e5415651ce 100644
--- a/apps/web/lib/actions/partners/create-program-application.ts
+++ b/apps/web/lib/actions/partners/create-program-application.ts
@@ -4,6 +4,7 @@ import { createId } from "@/lib/api/create-id";
import { isCI, isLocalDev } from "@/lib/api/environment";
import { detectAndRecordFraudApplication } from "@/lib/api/fraud/detect-record-fraud-application";
import { notifyPartnerApplication } from "@/lib/api/partners/notify-partner-application";
+import { queuePartnerSearchSync } from "@/lib/api/partners/queue-partner-search-sync";
import { getIP } from "@/lib/api/utils/get-ip";
import { markApplicationEventSubmitted } from "@/lib/application-events/update-application-event";
import { getApplicationEventCookieName } from "@/lib/application-events/utils";
@@ -382,6 +383,8 @@ async function createApplicationAndEnrollment({
}),
markApplicationEventSubmitted(programEnrollment),
+
+ queuePartnerSearchSync({ enrollmentIds: [programEnrollment.id] }),
]);
})(),
);
diff --git a/apps/web/lib/actions/partners/decline-program-invite.ts b/apps/web/lib/actions/partners/decline-program-invite.ts
index fd87430150..9663a883d4 100644
--- a/apps/web/lib/actions/partners/decline-program-invite.ts
+++ b/apps/web/lib/actions/partners/decline-program-invite.ts
@@ -1,6 +1,8 @@
"use server";
+import { queuePartnerSearchSync } from "@/lib/api/partners/queue-partner-search-sync";
import { prisma } from "@/lib/prisma";
+import { waitUntil } from "@vercel/functions";
import * as z from "zod/v4";
import { authPartnerActionClient } from "../safe-action";
@@ -14,7 +16,7 @@ export const declineProgramInviteAction = authPartnerActionClient
const { partner } = ctx;
const { programId } = parsedInput;
- await prisma.programEnrollment.update({
+ const enrollment = await prisma.programEnrollment.update({
where: {
partnerId_programId: {
partnerId: partner.id,
@@ -26,4 +28,6 @@ export const declineProgramInviteAction = authPartnerActionClient
status: "declined",
},
});
+
+ waitUntil(queuePartnerSearchSync({ enrollmentIds: [enrollment.id] }));
});
diff --git a/apps/web/lib/actions/partners/onboard-partner.ts b/apps/web/lib/actions/partners/onboard-partner.ts
index a7441e302d..ddf2e683c7 100644
--- a/apps/web/lib/actions/partners/onboard-partner.ts
+++ b/apps/web/lib/actions/partners/onboard-partner.ts
@@ -3,6 +3,7 @@
import { createId } from "@/lib/api/create-id";
import { isCI, isLocalDev } from "@/lib/api/environment";
import { generatePartnerUsername } from "@/lib/api/partners/generate-partner-username";
+import { queuePartnerSearchSync } from "@/lib/api/partners/queue-partner-search-sync";
import { markApplicationEventSubmittedNetwork } from "@/lib/application-events/mark-application-event-submitted-network";
import { completeProgramApplications } from "@/lib/partners/complete-program-applications";
import { prisma } from "@/lib/prisma";
@@ -124,6 +125,12 @@ export const onboardPartnerAction = authUserActionClient
// Mark the application event as submitted for the `network` program
markApplicationEventSubmittedNetwork(updatedPartner),
+
+ // Onboarding sets name, country, and description, all of which the
+ // document carries. Runs after completeProgramApplications above, which
+ // queues its own sync for the enrollments it creates; both collapse on
+ // the same partner, and re-indexing twice is harmless either way.
+ queuePartnerSearchSync({ partnerIds: [updatedPartner.id] }),
]),
);
diff --git a/apps/web/lib/actions/partners/withdraw-partner-application.ts b/apps/web/lib/actions/partners/withdraw-partner-application.ts
index e15b845abd..d7f78641cd 100644
--- a/apps/web/lib/actions/partners/withdraw-partner-application.ts
+++ b/apps/web/lib/actions/partners/withdraw-partner-application.ts
@@ -1,6 +1,8 @@
"use server";
+import { queuePartnerSearchSync } from "@/lib/api/partners/queue-partner-search-sync";
import { prisma } from "@/lib/prisma";
+import { waitUntil } from "@vercel/functions";
import * as z from "zod/v4";
import { authPartnerActionClient } from "../safe-action";
@@ -46,4 +48,9 @@ export const withdrawPartnerApplicationAction = authPartnerActionClient
return deletedProgramEnrollment;
});
+
+ // A withdrawal deletes the enrollment, which no sweep can notice.
+ waitUntil(
+ queuePartnerSearchSync({ enrollmentIds: [programEnrollment.id] }),
+ );
});
diff --git a/apps/web/lib/partners/complete-program-applications.ts b/apps/web/lib/partners/complete-program-applications.ts
index 3cbd44efb3..48f9872cb1 100644
--- a/apps/web/lib/partners/complete-program-applications.ts
+++ b/apps/web/lib/partners/complete-program-applications.ts
@@ -4,6 +4,7 @@ import { PlatformType, Prisma } from "@prisma/client";
import { createId } from "../api/create-id";
import { detectAndRecordFraudApplication } from "../api/fraud/detect-record-fraud-application";
import { notifyPartnerApplication } from "../api/partners/notify-partner-application";
+import { queuePartnerSearchSync } from "../api/partners/queue-partner-search-sync";
import { markApplicationEventSubmitted } from "../application-events/update-application-event";
import { qstash } from "../cron";
import { buildSocialPlatformLookup } from "../social-utils";
@@ -270,6 +271,13 @@ export async function completeProgramApplications(userEmail: string) {
markApplicationEventSubmitted(programEnrollment),
),
);
+
+ // One queue at the end covers both the new enrollments and the platforms
+ // backfilled from the application above, since the job re-reads the whole
+ // document rather than the fields any one write touched.
+ await queuePartnerSearchSync({
+ enrollmentIds: programEnrollments.map(({ id }) => id!),
+ });
} catch (error) {
console.error("Failed to complete program applications", error);
}
diff --git a/apps/web/lib/partners/sync-partner-identity.ts b/apps/web/lib/partners/sync-partner-identity.ts
index 4467f93440..3816df2578 100644
--- a/apps/web/lib/partners/sync-partner-identity.ts
+++ b/apps/web/lib/partners/sync-partner-identity.ts
@@ -1,4 +1,5 @@
import { DubApiError } from "@/lib/api/errors";
+import { queuePartnerSearchSync } from "@/lib/api/partners/queue-partner-search-sync";
import { requestEmailChange } from "@/lib/auth/request-email-change";
import { prisma } from "@/lib/prisma";
import { storage } from "@/lib/storage";
@@ -101,6 +102,12 @@ export async function syncNameAndImageToPartner({
...(hasImageUpdate && { image: partnerImage ?? null }),
},
});
+
+ // Only the name reaches the document; an image-only sync changes nothing the
+ // index holds, so it is not worth a queue.
+ if (hasNameUpdate && name) {
+ await queuePartnerSearchSync({ partnerIds: [partnerId] });
+ }
}
export async function syncNameAndImageToUser({