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 on partner link changes
Rafi80b488694640
diff --git a/apps/web/lib/api/links/bulk-delete-links.ts b/apps/web/lib/api/links/bulk-delete-links.tsindex 1a16a84e5c..e23b75a8a8 100644--- a/apps/web/lib/api/links/bulk-delete-links.ts+++ b/apps/web/lib/api/links/bulk-delete-links.ts@@ -1,3 +1,7 @@+import {+ PARTNER_SEARCH_LINK_SYNC_DELAY_SECONDS,+ queuePartnerSearchSync,+} from "@/lib/api/partners/queue-partner-search-sync";import { deleteDiscountCodes } from "@/lib/discounts/delete-discount-code";import { prisma } from "@/lib/prisma";import { storage } from "@/lib/storage";@@ -60,6 +64,27 @@ export async function bulkDeleteLinks(.map((link) =>storage.delete({ key: link.image!.replace(`${R2_URL}/`, "") }),),++ // Grouped by program so each queued payload carries the scope the job+ // needs to resolve enrollments, rather than one payload per link.+ ...Array.from(+ links.reduce((byProgram, { programId, partnerId }) => {+ if (!programId || !partnerId) {+ return byProgram;+ }++ const partnerIds = byProgram.get(programId) ?? new Set<string>();+ partnerIds.add(partnerId);++ return byProgram.set(programId, partnerIds);+ }, new Map<string, Set<string>>()),+ ([programId, partnerIds]) =>+ queuePartnerSearchSync({+ partnerIds: [...partnerIds],+ programId,+ delay: PARTNER_SEARCH_LINK_SYNC_DELAY_SECONDS,+ }),+ ),]),);}diff --git a/apps/web/lib/api/links/create-link.ts b/apps/web/lib/api/links/create-link.tsindex f5b2e9ec57..def7ff56b5 100644--- a/apps/web/lib/api/links/create-link.ts+++ b/apps/web/lib/api/links/create-link.ts@@ -1,3 +1,4 @@+import { queuePartnerSearchSync } from "@/lib/api/partners/queue-partner-search-sync";import { qstash } from "@/lib/cron";import { getPartnerEnrollmentInfo } from "@/lib/planetscale/get-partner-enrollment-info";import { prisma } from "@/lib/prisma";@@ -153,6 +154,15 @@ export async function createLink(link: ProcessedLinkProps) {...(discount && { discount }),}),+ // A new partner link is how people search for that partner, so this+ // takes the default delay rather than the longer link one.+ response.programId &&+ response.partnerId &&+ queuePartnerSearchSync({+ partnerIds: [response.partnerId],+ programId: response.programId,+ }),+// Record link in TinybirdrecordLink({...response,diff --git a/apps/web/lib/api/links/delete-link.ts b/apps/web/lib/api/links/delete-link.tsindex 7976a0737c..59194eb331 100644--- a/apps/web/lib/api/links/delete-link.ts+++ b/apps/web/lib/api/links/delete-link.ts@@ -1,3 +1,7 @@+import {+ PARTNER_SEARCH_LINK_SYNC_DELAY_SECONDS,+ queuePartnerSearchSync,+} from "@/lib/api/partners/queue-partner-search-sync";import { enqueueDeleteDiscountCode } from "@/lib/discounts/delete-discount-code";import { prisma } from "@/lib/prisma";import { storage } from "@/lib/storage";@@ -74,6 +78,18 @@ export async function deleteLink(linkId: string) {}),link.discountCode && enqueueDeleteDiscountCode([link.discountCode]),++ // The enrollment outlives the link, so this re-serializes the document+ // without it rather than removing the document. Takes the link delay:+ // a deleted short link matching a search is a stale hit, not a wrong one,+ // since the database still filters the candidates the index returns.+ link.programId &&+ link.partnerId &&+ queuePartnerSearchSync({+ partnerIds: [link.partnerId],+ programId: link.programId,+ delay: PARTNER_SEARCH_LINK_SYNC_DELAY_SECONDS,+ }),]),);diff --git a/apps/web/lib/api/links/update-link.ts b/apps/web/lib/api/links/update-link.tsindex 6f19f679f8..1158eabd65 100644--- a/apps/web/lib/api/links/update-link.ts+++ b/apps/web/lib/api/links/update-link.ts@@ -1,3 +1,7 @@+import {+ PARTNER_SEARCH_LINK_SYNC_DELAY_SECONDS,+ queuePartnerSearchSync,+} from "@/lib/api/partners/queue-partner-search-sync";import { getPartnerEnrollmentInfo } from "@/lib/planetscale/get-partner-enrollment-info";import { prisma } from "@/lib/prisma";import { isNotHostedImage, storage } from "@/lib/storage";@@ -202,6 +206,17 @@ export async function updateLink({// If key is changed: delete the old key in Redis(changedDomain || changedKey) && linkCache.delete(oldLink),+ // Only the short link and destination URL reach the document, and link+ // edits are frequent, so this takes the longer link delay. The job+ // re-reads the whole document, so nothing else goes stale by waiting.+ response.programId &&+ response.partnerId &&+ queuePartnerSearchSync({+ partnerIds: [response.partnerId],+ programId: response.programId,+ delay: PARTNER_SEARCH_LINK_SYNC_DELAY_SECONDS,+ }),+// If proxy is true and image is not stored in R2, upload image to R2proxy &&image &&