The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
add webhookIds to link redis cache
Kiran K945189680bf1
diff --git a/apps/web/app/api/cron/domains/transfer/route.ts b/apps/web/app/api/cron/domains/transfer/route.tsindex bc74d77ab5..695a6d9059 100644--- a/apps/web/app/api/cron/domains/transfer/route.ts+++ b/apps/web/app/api/cron/domains/transfer/route.ts@@ -27,6 +27,13 @@ export async function POST(req: Request) {const links = await prisma.link.findMany({where: { domain, projectId: currentWorkspaceId },take: 100,+ include: {+ webhooks: {+ select: {+ webhookId: true,+ },+ },+ },});// No remaining links to transfer@@ -54,7 +61,16 @@ export async function POST(req: Request) {prisma.linkTag.deleteMany({where: { linkId: { in: linkIds } },}),- updateLinksInRedis({ links, newWorkspaceId, domain }),++ updateLinksInRedis({+ links: links.map((link) => ({+ ...link,+ webhookIds: link.webhooks.map(({ webhookId }) => webhookId),+ })),+ newWorkspaceId,+ domain,+ }),+recordLink(links.map((link) => ({link_id: link.id,diff --git a/apps/web/app/api/cron/domains/transfer/utils.ts b/apps/web/app/api/cron/domains/transfer/utils.tsindex 2aa8f3710c..80c73703af 100644--- a/apps/web/app/api/cron/domains/transfer/utils.ts+++ b/apps/web/app/api/cron/domains/transfer/utils.ts@@ -12,7 +12,7 @@ export const updateLinksInRedis = async ({}: {newWorkspaceId: string;domain: string;- links: Link[];+ links: (Link & { webhookIds: string[] })[];}) => {const pipeline = redis.pipeline();diff --git a/apps/web/app/api/webhooks/[webhookId]/route.ts b/apps/web/app/api/webhooks/[webhookId]/route.tsindex 988b237f1d..ee1f59a4a1 100644--- a/apps/web/app/api/webhooks/[webhookId]/route.ts+++ b/apps/web/app/api/webhooks/[webhookId]/route.ts@@ -1,4 +1,5 @@import { DubApiError } from "@/lib/api/errors";+import { linkCache } from "@/lib/api/links/cache";import { parseRequestBody } from "@/lib/api/utils";import { withWorkspace } from "@/lib/auth";import { prisma } from "@/lib/prisma";@@ -160,6 +161,15 @@ export const DELETE = withWorkspace(async ({ workspace, params }) => {const { webhookId } = params;+ const linkWebhooks = await prisma.linkWebhook.findMany({+ where: {+ webhookId,+ },+ select: {+ linkId: true,+ },+ });+await prisma.webhook.delete({where: {id: webhookId,@@ -185,7 +195,37 @@ export const DELETE = withWorkspace(});}- waitUntil(webhookCache.delete(webhookId));+ waitUntil(+ (async () => {+ const linkIds = linkWebhooks.map((linkWebhook) => linkWebhook.linkId);++ const links = await prisma.link.findMany({+ where: {+ id: { in: linkIds },+ projectId: workspace.id,+ },+ include: {+ webhooks: {+ select: {+ webhookId: true,+ },+ },+ },+ });++ const formatedLinks = links.map((link) => {+ return {+ ...link,+ webhookIds: link.webhooks.map((webhook) => webhook.webhookId),+ };+ });++ await Promise.all([+ linkCache.mset(formatedLinks),+ webhookCache.delete(webhookId),+ ]);+ })(),+ );return NextResponse.json({id: webhookId,diff --git a/apps/web/app/api/webhooks/route.ts b/apps/web/app/api/webhooks/route.tsindex 2c681fbcf1..cdd299ee35 100644--- a/apps/web/app/api/webhooks/route.ts+++ b/apps/web/app/api/webhooks/route.ts@@ -1,4 +1,5 @@import { DubApiError } from "@/lib/api/errors";+import { linkCache } from "@/lib/api/links/cache";import { parseRequestBody } from "@/lib/api/utils";import { withWorkspace } from "@/lib/auth";import { prisma } from "@/lib/prisma";@@ -129,24 +130,51 @@ export const POST = withWorkspace(}waitUntil(- Promise.allSettled([- ...(isLinkLevelWebhook(webhook) ? [webhookCache.set(webhook)] : []),+ (async () => {+ const links = await prisma.link.findMany({+ where: {+ id: { in: linkIds },+ projectId: workspace.id,+ },+ include: {+ webhooks: {+ select: {+ webhookId: true,+ },+ },+ },+ });++ const formatedLinks = links.map((link) => {+ return {+ ...link,+ webhookIds: link.webhooks.map((webhook) => webhook.webhookId),+ };+ });++ Promise.all([+ ...(links && links.length > 0+ ? [linkCache.mset(formatedLinks), []]+ : []),- sendEmail({- email: session.user.email,- subject: "New webhook added",- react: WebhookAdded({+ ...(isLinkLevelWebhook(webhook) ? [webhookCache.set(webhook)] : []),++ sendEmail({email: session.user.email,- workspace: {- name: workspace.name,- slug: workspace.slug,- },- webhook: {- name,- },+ subject: "New webhook added",+ react: WebhookAdded({+ email: session.user.email,+ workspace: {+ name: workspace.name,+ slug: workspace.slug,+ },+ webhook: {+ name,+ },+ }),}),- }),- ]),+ ]);+ })(),);return NextResponse.json(transformWebhook(webhook), { status: 201 });diff --git a/apps/web/lib/api/links/cache.ts b/apps/web/lib/api/links/cache.tsnew file mode 100644index 0000000000..3f1e59839e--- /dev/null+++ b/apps/web/lib/api/links/cache.ts@@ -0,0 +1,26 @@+import { LinkProps } from "@/lib/types";+import { formatRedisLink, redis } from "@/lib/upstash";++class LinkCache {+ async mset(links: (LinkProps & { webhookIds?: string[] })[]) {+ const pipeline = redis.pipeline();++ const redisLinks = await Promise.all(+ links.map(async (link) => ({+ ...(await formatRedisLink(link)),+ key: link.key.toLowerCase(),+ domain: link.domain.toLowerCase(),+ })),+ );++ redisLinks.map(({ key, domain, ...redisLink }) => {+ pipeline.hset(domain, {+ [key]: redisLink,+ });+ });++ await pipeline.exec();+ }+}++export const linkCache = new LinkCache();diff --git a/apps/web/lib/api/links/update-link.ts b/apps/web/lib/api/links/update-link.tsindex a02aaa6e6e..bb54523aa0 100644--- a/apps/web/lib/api/links/update-link.ts+++ b/apps/web/lib/api/links/update-link.ts@@ -111,6 +111,11 @@ export async function updateLink({},},},+ webhooks: {+ select: {+ webhookId: true,+ },+ },},});@@ -118,7 +123,10 @@ export async function updateLink({Promise.all([// record link in Redisredis.hset(updatedLink.domain.toLowerCase(), {- [updatedLink.key.toLowerCase()]: await formatRedisLink(response),+ [updatedLink.key.toLowerCase()]: await formatRedisLink({+ ...response,+ webhookIds: response.webhooks.map(({ webhookId }) => webhookId),+ }),}),// record link in TinybirdrecordLink({diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.tsindex f45ad66e44..6a3f85e717 100644--- a/apps/web/lib/types.ts+++ b/apps/web/lib/types.ts@@ -46,6 +46,7 @@ export interface RedisLinkProps {geo?: object;doIndex?: boolean;projectId?: string;+ webhookIds?: string[];}export interface EdgeLinkProps {diff --git a/apps/web/lib/upstash/format-redis-link.ts b/apps/web/lib/upstash/format-redis-link.tsindex f2406e3849..1d4a8d8076 100644--- a/apps/web/lib/upstash/format-redis-link.ts+++ b/apps/web/lib/upstash/format-redis-link.ts@@ -2,7 +2,7 @@ import { isIframeable } from "@dub/utils";import { LinkProps, RedisLinkProps } from "../types";export async function formatRedisLink(- link: LinkProps,+ link: LinkProps & { webhookIds?: string[] },): Promise<RedisLinkProps> {const {id,@@ -19,6 +19,7 @@ export async function formatRedisLink(geo,doIndex,projectId,+ webhookIds,} = link;const hasPassword = password && password.length > 0 ? true : false;@@ -40,5 +41,6 @@ export async function formatRedisLink(...(geo && { geo: geo as object }),...(projectId && { projectId }), // projectId can be undefined for anonymous links...(doIndex && { doIndex: true }),+ ...(webhookIds && { webhookIds }),};}