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 group moves, enrollment creation, and the API status paths

Rafi2cd20ad12d2d
diff --git a/apps/web/lib/api/groups/move-partners-to-group.ts b/apps/web/lib/api/groups/move-partners-to-group.ts
index bded179de3..90bb5b386c 100644
--- a/apps/web/lib/api/groups/move-partners-to-group.ts
+++ b/apps/web/lib/api/groups/move-partners-to-group.ts
@@ -14,6 +14,7 @@ import { getWorkspaceUsers } from "../get-workspace-users";
import { includeProgramEnrollment } from "../links/include-program-enrollment";
import { includeTags } from "../links/include-tags";
import { notifyPartnerGroupChange } from "../partners/notify-partner-group-change";
+import { queuePartnerSearchSync } from "../partners/queue-partner-search-sync";
interface MovePartnersToGroupParams {
workspaceId: string;
@@ -189,6 +190,10 @@ export async function movePartnersToGroup({
recordLink(partnerLinks),
+ queuePartnerSearchSync({
+ enrollmentIds: updatedProgramEnrollments.map(({ id }) => id),
+ }),
+
notifyPartnerGroupChange({
programId,
groupId: group.id,
diff --git a/apps/web/lib/api/partners/applications/approve-partner.ts b/apps/web/lib/api/partners/applications/approve-partner.ts
index 2509e3fc1d..900397cc00 100644
--- a/apps/web/lib/api/partners/applications/approve-partner.ts
+++ b/apps/web/lib/api/partners/applications/approve-partner.ts
@@ -9,6 +9,7 @@ import { approvePartnerSchema } from "../../../zod/schemas/partners";
import { trackActivityLog } from "../../activity-log/track-activity-log";
import { DubApiError } from "../../errors";
import { getGroupOrThrow } from "../../groups/get-group-or-throw";
+import { queuePartnerSearchSync } from "../queue-partner-search-sync";
type ApprovePartnerInput = z.infer<typeof approvePartnerSchema> & {
programId: string;
@@ -131,6 +132,8 @@ export async function approvePartner({
waitUntil(
Promise.allSettled([
+ queuePartnerSearchSync({ partnerIds: [partnerId], programId }),
+
trackActivityLog({
workspaceId: program.workspace.id,
programId,
diff --git a/apps/web/lib/api/partners/applications/reject-partner.ts b/apps/web/lib/api/partners/applications/reject-partner.ts
index 2d4cfbdabe..4768eebf35 100644
--- a/apps/web/lib/api/partners/applications/reject-partner.ts
+++ b/apps/web/lib/api/partners/applications/reject-partner.ts
@@ -12,6 +12,7 @@ import { trackActivityLog } from "../../activity-log/track-activity-log";
import { DubApiError } from "../../errors";
import { resolveFraudGroups } from "../../fraud/resolve-fraud-groups";
import { getDefaultProgramIdOrThrow } from "../../programs/get-default-program-id-or-throw";
+import { queuePartnerSearchSync } from "../queue-partner-search-sync";
type RejectPartnerInput = z.infer<typeof rejectPartnerSchema> & {
userId: string;
@@ -146,6 +147,12 @@ export async function rejectPartner({
waitUntil(
Promise.allSettled([
+ // Covers both outcomes above: an instant re-application timeframe deletes
+ // the enrollment, anything else rejects it. The job reads the ID back and
+ // upserts or deletes accordingly, so this hook does not need to know
+ // which branch ran.
+ queuePartnerSearchSync({ enrollmentIds: [programEnrollment.id] }),
+
trackActivityLog({
workspaceId: workspace.id,
programId,
diff --git a/apps/web/lib/api/partners/bulk-reactivate-partners.ts b/apps/web/lib/api/partners/bulk-reactivate-partners.ts
index 39a0a6f159..97d16367f3 100644
--- a/apps/web/lib/api/partners/bulk-reactivate-partners.ts
+++ b/apps/web/lib/api/partners/bulk-reactivate-partners.ts
@@ -6,6 +6,7 @@ import { Partner, Program, ProgramEnrollment } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import { trackActivityLog } from "../activity-log/track-activity-log";
import { linkCache } from "../links/cache";
+import { queuePartnerSearchSync } from "./queue-partner-search-sync";
type ProgramEnrollmentWithPartner = Pick<
ProgramEnrollment,
@@ -137,21 +138,27 @@ export async function bulkReactivatePartners({
}
waitUntil(
- trackActivityLog(
- programEnrollments.map(({ partnerId }) => ({
- workspaceId: program.workspaceId,
- programId: program.id,
- resourceType: "partner",
- resourceId: partnerId,
- action: "partner.reactivated",
- changeSet: {
- status: {
- old: "deactivated",
- new: "approved",
+ Promise.allSettled([
+ trackActivityLog(
+ programEnrollments.map(({ partnerId }) => ({
+ workspaceId: program.workspaceId,
+ programId: program.id,
+ resourceType: "partner",
+ resourceId: partnerId,
+ action: "partner.reactivated",
+ changeSet: {
+ status: {
+ old: "deactivated",
+ new: "approved",
+ },
},
- },
- })),
- ),
+ })),
+ ),
+
+ queuePartnerSearchSync({
+ enrollmentIds: programEnrollments.map(({ id }) => id),
+ }),
+ ]),
);
// Send email notifications
diff --git a/apps/web/lib/api/partners/create-and-enroll-partner.ts b/apps/web/lib/api/partners/create-and-enroll-partner.ts
index 377cfd1558..ed817a69f0 100644
--- a/apps/web/lib/api/partners/create-and-enroll-partner.ts
+++ b/apps/web/lib/api/partners/create-and-enroll-partner.ts
@@ -15,6 +15,7 @@ import { getGroupOrThrow } from "../groups/get-group-or-throw";
import { createOrGetProgramEnrollment } from "./create-or-get-program-enrollment";
import { createPartnerDefaultLinks } from "./create-partner-default-links";
import { getOrCreatePartner } from "./get-or-create-partner";
+import { queuePartnerSearchSync } from "./queue-partner-search-sync";
import { throwIfExistingTenantEnrollmentExists } from "./throw-if-existing-tenant-id-exists";
interface CreateAndEnrollPartnerInput {
@@ -252,6 +253,10 @@ export const createAndEnrollPartner = async ({
trigger: "partner.enrolled",
data: enrolledPartner,
}),
+
+ // Queued after the default links exist, so the first document already
+ // carries them and the partner is findable by short link right away.
+ queuePartnerSearchSync({ enrollmentIds: [programEnrollment.id] }),
]),
);
diff --git a/apps/web/lib/api/partners/process-partner-deactivation.ts b/apps/web/lib/api/partners/process-partner-deactivation.ts
index 87473a8b3b..acc19c4c0c 100644
--- a/apps/web/lib/api/partners/process-partner-deactivation.ts
+++ b/apps/web/lib/api/partners/process-partner-deactivation.ts
@@ -5,6 +5,7 @@ import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
import { Partner, ProgramEnrollmentStatus } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import { trackActivityLog } from "../activity-log/track-activity-log";
+import { queuePartnerSearchSync } from "./queue-partner-search-sync";
interface ProcessPartnerDeactivationParams {
workspaceId: string;
@@ -90,6 +91,10 @@ export async function processPartnerDeactivation({
},
);
+ // Queued outside the `user` branch below, which only runs for operator-driven
+ // deactivations. The index has to follow the status either way.
+ await queuePartnerSearchSync({ partnerIds, programId });
+
if (user) {
waitUntil(
trackActivityLog(