The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Address CodeRabbit findings from the partner search PRs
Rafi9ce0e5426081
diff --git a/apps/web/lib/actions/partners/accept-program-invite.ts b/apps/web/lib/actions/partners/accept-program-invite.tsindex 28cf4842f5..1be682c170 100644--- a/apps/web/lib/actions/partners/accept-program-invite.ts+++ b/apps/web/lib/actions/partners/accept-program-invite.ts@@ -46,6 +46,9 @@ export const acceptProgramInviteAction = authPartnerActionClient},});+ // Queue an index update because the enrollment status moved to approved.+ waitUntil(queuePartnerSearchSync({ enrollmentIds: [enrollment.id] }));+waitUntil((async () => {const workspace = await prisma.project.findUnique({@@ -98,10 +101,6 @@ export const acceptProgramInviteAction = authPartnerActionClientpartnerId: enrolledPartner.id,},}),- // Queue an index update because the enrollment status moved to approved- queuePartnerSearchSync({- enrollmentIds: [enrollment.id],- }),]);})(),);diff --git a/apps/web/lib/actions/partners/update-partner-profile.ts b/apps/web/lib/actions/partners/update-partner-profile.tsindex 0d6c2a00fd..f4d1142e6e 100644--- a/apps/web/lib/actions/partners/update-partner-profile.ts+++ b/apps/web/lib/actions/partners/update-partner-profile.ts@@ -187,6 +187,10 @@ export const updatePartnerProfileAction = authPartnerActionClient},});+ // Queue an index update because the partner profile changed. Not scoped+ // by program, since a profile is shared across them.+ waitUntil(queuePartnerSearchSync({ partnerIds: [partner.id] }));+// If the email is being changed, we need to verify the new email addressif (emailChanged) {if (syncIdentity) {@@ -256,10 +260,6 @@ export const updatePartnerProfileAction = authPartnerActionClientwaitUntil(Promise.allSettled([- // Queue an index update because the partner profile changed. Not- // scoped by program, since a profile is shared across them.- queuePartnerSearchSync({ partnerIds: [partner.id] }),-(async () => {const shouldExpireCache = !deepEqual({diff --git a/apps/web/lib/api/groups/move-partners-to-group.ts b/apps/web/lib/api/groups/move-partners-to-group.tsindex 494c253b79..b1285eaaee 100644--- a/apps/web/lib/api/groups/move-partners-to-group.ts+++ b/apps/web/lib/api/groups/move-partners-to-group.ts@@ -96,6 +96,9 @@ export async function movePartnersToGroup({return 0;}+ // Queue an index update because the enrollments moved group (filterable field)+ waitUntil(queuePartnerSearchSync({ partnerIds, programId }));+waitUntil((async () => {const partnerLinks = await prisma.link.findMany({@@ -190,11 +193,6 @@ export async function movePartnersToGroup({recordLink(partnerLinks),- // Queue an index update because the enrollments moved group (filterable field)- queuePartnerSearchSync({- enrollmentIds: updatedProgramEnrollments.map(({ id }) => id),- }),-notifyPartnerGroupChange({programId,groupId: group.id,diff --git a/apps/web/lib/api/partners/create-and-enroll-partner.ts b/apps/web/lib/api/partners/create-and-enroll-partner.tsindex 01d4a49fe0..c398e3263a 100644--- a/apps/web/lib/api/partners/create-and-enroll-partner.ts+++ b/apps/web/lib/api/partners/create-and-enroll-partner.ts@@ -180,6 +180,9 @@ export const createAndEnrollPartner = async ({});}+ // Queue an index update because a new enrollment was created.+ waitUntil(queuePartnerSearchSync({ enrollmentIds: [programEnrollment.id] }));+// Create the partner links based on group defaultsconst links = await createPartnerDefaultLinks({workspace: {@@ -253,9 +256,6 @@ export const createAndEnrollPartner = async ({trigger: "partner.enrolled",data: enrolledPartner,}),-- // Queue an index update because a new enrollment was created.- queuePartnerSearchSync({ enrollmentIds: [programEnrollment.id] }),]),);diff --git a/apps/web/lib/api/partners/queue-partner-search-sync.ts b/apps/web/lib/api/partners/queue-partner-search-sync.tsindex 6488498c31..9512eb208f 100644--- a/apps/web/lib/api/partners/queue-partner-search-sync.ts+++ b/apps/web/lib/api/partners/queue-partner-search-sync.ts@@ -41,8 +41,9 @@ function unique(values: string[] | undefined): string[] {/*** Queues an index sync for whatever the caller just changed.*- * Never throws, so a queue error cannot fail the source mutation. The sweep- * repairs a missed upsert, but not a missed delete.+ * Never throws, so a queue error cannot fail the source mutation. A failed+ * QStash publish falls back to the jobs outbox, so this catch only fires when+ * even that persist fails, and the sync is lost.*/export async function queuePartnerSearchSync({enrollmentIds,@@ -79,7 +80,7 @@ export async function queuePartnerSearchSync({await partnerSearchSyncJob.dispatchBatch(payloads, () => ({ delay }));} catch (error) {console.error(- "[Partner Search] Failed to queue an index sync. The sweep repairs a missed upsert, but not a missed delete.",+ "[Partner Search] Failed to queue an index sync and the outbox could not persist it. The affected documents stay stale until their next sync.",error,);}diff --git a/apps/web/lib/firstpromoter/import-partners.ts b/apps/web/lib/firstpromoter/import-partners.tsindex dfdabdce90..2a137b0738 100644--- a/apps/web/lib/firstpromoter/import-partners.ts+++ b/apps/web/lib/firstpromoter/import-partners.ts@@ -230,9 +230,18 @@ async function createPartnerAndLinks({idx === 0 ? group.partnerGroupDefaultLinks[0]?.id ?? null : null,}));- await bulkCreateLinks({- links,- });+ try {+ await bulkCreateLinks({+ links,+ });+ } catch (error) {+ // The enrollment is already committed, so its ID must still reach the+ // page-level search sync even when link creation fails.+ console.error(+ `Failed to create links for imported affiliate ${affiliate.id}`,+ error,+ );+ }return programEnrollment.id;}diff --git a/apps/web/lib/partnerstack/import-partners.ts b/apps/web/lib/partnerstack/import-partners.tsindex 5b617b6dd6..7856db7c05 100644--- a/apps/web/lib/partnerstack/import-partners.ts+++ b/apps/web/lib/partnerstack/import-partners.ts@@ -206,9 +206,15 @@ async function createPartner({// PS doesn't return the partner email address in the customers response// so we need to keep a map of partner_key (PS) -> partner_id (Dub)// and use it to identify the partner in the customers response- await redis.hset(`${PARTNER_IDS_KEY_PREFIX}:${program.id}`, {- [partner.key]: partnerId,- });+ try {+ await redis.hset(`${PARTNER_IDS_KEY_PREFIX}:${program.id}`, {+ [partner.key]: partnerId,+ });+ } catch (error) {+ // The enrollment is already committed, so its ID must still reach the+ // page-level search sync even when the mapping write fails.+ console.error("Failed to map imported partner key", error, partner.key);+ }return partnerId;}diff --git a/apps/web/scripts/dev/seed-100k-partners.ts b/apps/web/scripts/dev/seed-100k-partners.tsindex df84471be4..0383bdb34e 100644--- a/apps/web/scripts/dev/seed-100k-partners.ts+++ b/apps/web/scripts/dev/seed-100k-partners.ts@@ -625,6 +625,36 @@ async function main() {// Tags and groups are created once per run and reused, so filters have a// small stable set of values to select on rather than one value per partner.const tagIds = TAG_NAMES.map((_, index) => createId({ prefix: "ptag_" }));+ const seededGroupIds = SEEDED_GROUP_NAMES.map(() =>+ createId({ prefix: "grp_" }),+ );++ // The program's own default group, the seeded ones, and null, so "no group"+ // is represented too, which is what a negated filter has to include.+ const groupIds: (string | null)[] = [+ program.defaultGroupId,+ ...seededGroupIds,+ null,+ ];++ // The duplicate-seed check runs before anything is written; running it+ // after the tag and group writes would leave their rows behind on a rerun.+ await assertSeedNotAlreadyApplied(+ generatePartnerChunk({+ start: 0,+ end: Math.min(CHUNK_SIZE, totalCount),+ seedFingerprint,+ passwordHash,+ runStartedAt,+ programId: program.id,+ programDomain: program.domain,+ workspaceId: workspace.id,+ groupIds,+ tagIds,+ }),+ seed,+ );+await prisma.partnerTag.createMany({data: TAG_NAMES.map((name, index) => ({id: tagIds[index],@@ -633,9 +663,6 @@ async function main() {})),});- const seededGroupIds = SEEDED_GROUP_NAMES.map(() =>- createId({ prefix: "grp_" }),- );await prisma.partnerGroup.createMany({data: SEEDED_GROUP_NAMES.map((name, index) => ({id: seededGroupIds[index],@@ -645,14 +672,6 @@ async function main() {})),});- // The program's own default group, the seeded ones, and null, so "no group"- // is represented too, which is what a negated filter has to include.- const groupIds: (string | null)[] = [- program.defaultGroupId,- ...seededGroupIds,- null,- ];-console.log(`Created ${tagIds.length} tags and ${seededGroupIds.length} groups for filtering\n`,);@@ -677,10 +696,6 @@ async function main() {tagIds,});- if (chunk === 0) {- await assertSeedNotAlreadyApplied(partnerChunk, seed);- }-const insertedInChunk = await insertPartnerChunk(partnerChunk);insertedPartners += insertedInChunk;