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 tag changes and enrollment deletions

Rafiaf0be28b66e5
diff --git a/apps/web/app/(ee)/api/cron/cleanup/declined-invites/route.ts b/apps/web/app/(ee)/api/cron/cleanup/declined-invites/route.ts
index 6c54a798c0..aab64e340e 100644
--- a/apps/web/app/(ee)/api/cron/cleanup/declined-invites/route.ts
+++ b/apps/web/app/(ee)/api/cron/cleanup/declined-invites/route.ts
@@ -1,4 +1,5 @@
import { handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { queuePartnerSearchSync } from "@/lib/api/partners/queue-partner-search-sync";
import { PRISMA_UPDATEMANY_LIMIT } from "@/lib/cron";
import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
import { prisma } from "@/lib/prisma";
@@ -51,6 +52,12 @@ export async function POST(req: Request) {
console.log(
`Deleted ${deletedRes.count} declined programEnrollments that are older than 90 days`,
);
+
+ // Deletions are invisible to the reconciliation sweep, so the index only
+ // loses these documents if this hook queues them.
+ await queuePartnerSearchSync({
+ enrollmentIds: declinedProgramEnrollments.map(({ id }) => id),
+ });
}
return NextResponse.json({ status: "OK" });
diff --git a/apps/web/app/(ee)/api/cron/cleanup/rejected-applications/route.ts b/apps/web/app/(ee)/api/cron/cleanup/rejected-applications/route.ts
index 172cf75a15..8c68ace83b 100644
--- a/apps/web/app/(ee)/api/cron/cleanup/rejected-applications/route.ts
+++ b/apps/web/app/(ee)/api/cron/cleanup/rejected-applications/route.ts
@@ -1,4 +1,5 @@
import { handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { queuePartnerSearchSync } from "@/lib/api/partners/queue-partner-search-sync";
import { PRISMA_UPDATEMANY_LIMIT } from "@/lib/cron";
import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
import { prisma } from "@/lib/prisma";
@@ -59,6 +60,12 @@ export async function POST(req: Request) {
`Deleted ${deletedProgramEnrollments.count} rejected programEnrollments that are older than 30 days`,
);
+ // Deletions are invisible to the reconciliation sweep, so the index only
+ // loses these documents if this hook queues them.
+ await queuePartnerSearchSync({
+ enrollmentIds: rejectedProgramEnrollments.map(({ id }) => id),
+ });
+
totalDeletedProgramEnrollments += deletedProgramEnrollments.count;
}
diff --git a/apps/web/lib/actions/partners/delete-program-invite.ts b/apps/web/lib/actions/partners/delete-program-invite.ts
index efc0923569..08aa9aa24b 100644
--- a/apps/web/lib/actions/partners/delete-program-invite.ts
+++ b/apps/web/lib/actions/partners/delete-program-invite.ts
@@ -2,6 +2,7 @@
import { recordAuditLog } from "@/lib/api/audit-logs/record-audit-log";
import { bulkDeleteLinks } from "@/lib/api/links/bulk-delete-links";
+import { queuePartnerSearchSync } from "@/lib/api/partners/queue-partner-search-sync";
import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
import { prisma } from "@/lib/prisma";
import { waitUntil } from "@vercel/functions";
@@ -100,19 +101,25 @@ export const deleteProgramInviteAction = authActionClient
]);
waitUntil(
- recordAuditLog({
- workspaceId: workspace.id,
- programId,
- action: "partner.invite_deleted",
- description: `Partner ${partner.id} invite deleted`,
- actor: user,
- targets: [
- {
- type: "partner",
- id: partner.id,
- metadata: partner,
- },
- ],
- }),
+ Promise.allSettled([
+ recordAuditLog({
+ workspaceId: workspace.id,
+ programId,
+ action: "partner.invite_deleted",
+ description: `Partner ${partner.id} invite deleted`,
+ actor: user,
+ targets: [
+ {
+ type: "partner",
+ id: partner.id,
+ metadata: partner,
+ },
+ ],
+ }),
+
+ // Deletions are invisible to the reconciliation sweep, so this hook is
+ // the only thing that clears the document.
+ queuePartnerSearchSync({ enrollmentIds: [programEnrollment.id] }),
+ ]),
);
});
diff --git a/apps/web/lib/actions/partners/tags/update-program-partner-tags.ts b/apps/web/lib/actions/partners/tags/update-program-partner-tags.ts
index 4d9f1287b8..771cf06c7c 100644
--- a/apps/web/lib/actions/partners/tags/update-program-partner-tags.ts
+++ b/apps/web/lib/actions/partners/tags/update-program-partner-tags.ts
@@ -2,6 +2,7 @@
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 { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
import { prisma } from "@/lib/prisma";
import { recordLink } from "@/lib/tinybird";
@@ -94,6 +95,11 @@ export const updateProgramPartnerTagsAction = authActionClient
]);
});
+ // Tags carry no timestamps, so the reconciliation sweep cannot notice this
+ // change on its own. This hook is the only thing keeping tag filters in the
+ // index honest between full rebuilds.
+ waitUntil(queuePartnerSearchSync({ partnerIds, programId }));
+
// Sync updated partner tags to Tinybird for analytics (top_partner_tags)
waitUntil(
(async () => {
diff --git a/apps/web/lib/api/partners/bulk-delete-partners.ts b/apps/web/lib/api/partners/bulk-delete-partners.ts
index 1b25b0c147..cdd26bf549 100644
--- a/apps/web/lib/api/partners/bulk-delete-partners.ts
+++ b/apps/web/lib/api/partners/bulk-delete-partners.ts
@@ -2,6 +2,7 @@ import { conn } from "@/lib/planetscale";
import { prisma } from "@/lib/prisma";
import { ACME_PROGRAM_ID } from "@dub/utils";
import { bulkDeleteLinks } from "../links/bulk-delete-links";
+import { queuePartnerSearchSync } from "./queue-partner-search-sync";
const BATCH_SIZE = 250;
@@ -160,6 +161,11 @@ export async function bulkDeletePartners({
`Deleted ${deletedProgramEnrollments.count} program enrollments`,
);
+ // Deletions are invisible to the reconciliation sweep, which only sees rows
+ // that still exist. The job removes what it cannot read back, so passing
+ // the now-gone IDs is what clears them from the index.
+ await queuePartnerSearchSync({ enrollmentIds: programEnrollmentIds });
+
if (deletedProgramEnrollments.count > 0) {
await prisma.project.updateMany({
where: {
diff --git a/apps/web/lib/jobs/handlers/partner-tag-deleted-job.ts b/apps/web/lib/jobs/handlers/partner-tag-deleted-job.ts
index 2f86106036..48c3e17083 100644
--- a/apps/web/lib/jobs/handlers/partner-tag-deleted-job.ts
+++ b/apps/web/lib/jobs/handlers/partner-tag-deleted-job.ts
@@ -1,6 +1,7 @@
import * as z from "zod/v4";
import { includeProgramEnrollment } from "../../api/links/include-program-enrollment";
import { includeTags } from "../../api/links/include-tags";
+import { queuePartnerSearchSync } from "../../api/partners/queue-partner-search-sync";
import { CRON_BATCH_SIZE } from "../../cron";
import { prisma } from "../../prisma";
import { recordLink } from "../../tinybird";
@@ -69,6 +70,13 @@ export const partnerTagDeletedJob = defineJob({
`[partnerTagDeletedJob] Deleted ${count} program–partner tag associations.`,
);
+ // Tags carry no timestamps, so the reconciliation sweep cannot see this.
+ await queuePartnerSearchSync({
+ enrollmentIds: programPartnerTags.map(
+ ({ programEnrollment }) => programEnrollment.id,
+ ),
+ });
+
// Update the links to remove the partner tag from TB
const linksToUpdate = await prisma.link.findMany({
where: {