The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
conditional sync for link operations
Steven Tey6f0117f70753
diff --git a/apps/web/lib/api/links/bulk-create-links.ts b/apps/web/lib/api/links/bulk-create-links.tsindex fffd7f48f8..9a246736b1 100644--- a/apps/web/lib/api/links/bulk-create-links.ts+++ b/apps/web/lib/api/links/bulk-create-links.ts@@ -236,8 +236,11 @@ export async function bulkCreateLinks({linksCount: links.length,timestamp: new Date().toISOString(),}),- // Queue an index update because the new links carry searchable link keys- queuePartnerSearchSyncForLinks(createdLinksData),+ // Queue an index update if any of the new links are associated with a partner+ createdLinksData.some((link) => link.partnerId) &&+ queuePartnerSearchSyncForLinks(+ createdLinksData.filter((link) => link.partnerId),+ ),]),);diff --git a/apps/web/lib/api/links/bulk-delete-links.ts b/apps/web/lib/api/links/bulk-delete-links.tsindex 151b865829..9230d59958 100644--- a/apps/web/lib/api/links/bulk-delete-links.ts+++ b/apps/web/lib/api/links/bulk-delete-links.ts@@ -62,9 +62,12 @@ export async function bulkDeleteLinks(storage.delete({ key: link.image!.replace(`${R2_URL}/`, "") }),),- // Queue an index update because the links were deleted. The enrollment- // outlives them, so the document is re-serialized without them.- queuePartnerSearchSyncForLinks(links),+ // Queue an index update if any of the deleted links are associated with a partner+ // The program enrollments outlives them, so the document is re-serialized without them.+ links.some((link) => link.partnerId) &&+ queuePartnerSearchSyncForLinks(+ links.filter((link) => link.partnerId),+ ),]),);}diff --git a/apps/web/lib/api/links/bulk-update-links.ts b/apps/web/lib/api/links/bulk-update-links.tsindex 5f5f052c60..61fa319058 100644--- a/apps/web/lib/api/links/bulk-update-links.ts+++ b/apps/web/lib/api/links/bulk-update-links.ts@@ -40,10 +40,10 @@ export async function bulkUpdateLinks(const imageUrlNonce = nanoid(7);- // The bulk payload omits domain/key/externalId but not `partnerId`, so an+ // The bulk payload omits link key but not `partnerId`, so an// update can move links between partners. The former owners are unrecoverable// after the write, so read them first, and only when the payload can move one.- const previousOwners =+ const previousPartnersForLinks =rest.partnerId !== undefined || rest.programId !== undefined? await prisma.link.findMany({where: { id: { in: linkIds } },@@ -127,9 +127,12 @@ export async function bulkUpdateLinks(propagateBulkLinkChanges({links: updatedLinks,}),- // Queue an index update because a bulk edit can change the destination- // URL or the link owner.- queuePartnerSearchSyncForLinks([...previousOwners, ...updatedLinks]),+ // Queue an index update if there are previous partners (meaning partnerId was bulk updated)+ previousPartnersForLinks.length > 0 &&+ queuePartnerSearchSyncForLinks([+ ...previousPartnersForLinks,+ ...updatedLinks,+ ]),// if proxy is true and image is not stored in R2, upload image to R2proxy &&image &&diff --git a/apps/web/lib/api/links/update-link.ts b/apps/web/lib/api/links/update-link.tsindex a3f9013d83..d817179e29 100644--- a/apps/web/lib/api/links/update-link.ts+++ b/apps/web/lib/api/links/update-link.ts@@ -51,9 +51,11 @@ export async function updateLink({proxy,geo,publicStats,+ partnerId,} = updatedLink;const changedKey = key.toLowerCase() !== oldLink.key.toLowerCase();const changedDomain = domain !== oldLink.domain;+ const changedPartnerId = partnerId !== oldLink.partnerId;const { utm_source, utm_medium, utm_campaign, utm_term, utm_content } =getParamsFromURL(url);@@ -208,10 +210,9 @@ export async function updateLink({// If key is changed: delete the old key in Redis(changedDomain || changedKey) && linkCache.delete(oldLink),- // Queue an index update because the edit can change the destination URL- // or move the link. Both owners, so a former one is re-serialized- // without it.- queuePartnerSearchSyncForLinks([oldLink, response]),+ // Queue an index update if the link key or partner ID changed (indexed fields)+ (changedKey || changedPartnerId) &&+ queuePartnerSearchSyncForLinks([oldLink, response]),// If proxy is true and image is not stored in R2, upload image to R2proxy &&