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.

update link webhookIds on webhook update

Kiran K88179ffa903d
diff --git a/apps/web/app/api/cron/domains/transfer/route.ts b/apps/web/app/api/cron/domains/transfer/route.ts
index 695a6d9059..ca9809fd06 100644
--- a/apps/web/app/api/cron/domains/transfer/route.ts
+++ b/apps/web/app/api/cron/domains/transfer/route.ts
@@ -58,10 +58,16 @@ export async function POST(req: Request) {
where: { domain, projectId: currentWorkspaceId, id: { in: linkIds } },
data: { projectId: newWorkspaceId },
}),
+
prisma.linkTag.deleteMany({
where: { linkId: { in: linkIds } },
}),
+ // Remove the webhooks associated with the links
+ prisma.linkWebhook.deleteMany({
+ where: { linkId: { in: linkIds } },
+ }),
+
updateLinksInRedis({
links: links.map((link) => ({
...link,
diff --git a/apps/web/app/api/links/[linkId]/transfer/route.ts b/apps/web/app/api/links/[linkId]/transfer/route.ts
index 6108e00a50..cf32e55624 100644
--- a/apps/web/app/api/links/[linkId]/transfer/route.ts
+++ b/apps/web/app/api/links/[linkId]/transfer/route.ts
@@ -122,6 +122,13 @@ export const POST = withWorkspace(
},
},
}),
+
+ // Remove the webhooks associated with the link
+ prisma.linkWebhook.deleteMany({
+ where: {
+ linkId: link.id,
+ },
+ }),
]),
);
diff --git a/apps/web/app/api/webhooks/[webhookId]/route.ts b/apps/web/app/api/webhooks/[webhookId]/route.ts
index d8a0cbbe00..b949546ec2 100644
--- a/apps/web/app/api/webhooks/[webhookId]/route.ts
+++ b/apps/web/app/api/webhooks/[webhookId]/route.ts
@@ -77,6 +77,9 @@ export const PATCH = withWorkspace(
id: { in: linkIds },
projectId: workspace.id,
},
+ select: {
+ id: true,
+ },
});
if (links.length !== linkIds.length) {
@@ -88,6 +91,15 @@ export const PATCH = withWorkspace(
}
}
+ const oldLinks = await prisma.linkWebhook.findMany({
+ where: {
+ webhookId,
+ },
+ select: {
+ linkId: true,
+ },
+ });
+
const webhook = await prisma.webhook.update({
where: {
id: webhookId,
@@ -112,7 +124,11 @@ export const PATCH = withWorkspace(
url: true,
secret: true,
triggers: true,
- links: true,
+ links: {
+ select: {
+ linkId: true,
+ },
+ },
},
});
@@ -131,13 +147,76 @@ export const PATCH = withWorkspace(
!isLinkLevelWebhook(webhook)
) {
await webhookCache.delete(webhookId);
- return;
+
+ const links = await prisma.link.findMany({
+ where: {
+ id: { in: oldLinks.map(({ linkId }) => linkId) },
+ },
+ include: {
+ webhooks: {
+ select: {
+ webhookId: true,
+ },
+ },
+ },
+ });
+
+ const formatedLinks = links.map((link) => {
+ return {
+ ...link,
+ webhookIds: link.webhooks.map(({ webhookId }) => webhookId),
+ };
+ });
+
+ await linkCache.mset(formatedLinks);
}
// If the webhook is being changed from workspace level to link level, set the cache
- if (isLinkLevelWebhook(webhook)) {
+ else if (isLinkLevelWebhook(webhook)) {
await webhookCache.set(webhook);
}
+
+ const newLinkIds = webhook.links.map(({ linkId }) => linkId);
+ const oldLinkIds = oldLinks.map(({ linkId }) => linkId);
+
+ // No changes in the links
+ if (!newLinkIds.length && !oldLinkIds.length) {
+ return;
+ }
+
+ const linksAdded = newLinkIds.filter(
+ (linkId) => !oldLinkIds.includes(linkId),
+ );
+
+ const linksRemoved = oldLinkIds.filter(
+ (linkId) => !newLinkIds.includes(linkId),
+ );
+
+ const linksToBeUpdated = [...linksAdded, ...linksRemoved];
+
+ console.log("linksToBeUpdated", linksToBeUpdated);
+
+ const links = await prisma.link.findMany({
+ where: {
+ id: { in: linksToBeUpdated },
+ },
+ include: {
+ webhooks: {
+ select: {
+ webhookId: true,
+ },
+ },
+ },
+ });
+
+ const formatedLinks = links.map((link) => {
+ return {
+ ...link,
+ webhookIds: link.webhooks.map(({ webhookId }) => webhookId),
+ };
+ });
+
+ await linkCache.mset(formatedLinks);
})(),
);
diff --git a/apps/web/lib/webhook/transform.ts b/apps/web/lib/webhook/transform.ts
index f9ff06fa8d..b2ed0d6094 100644
--- a/apps/web/lib/webhook/transform.ts
+++ b/apps/web/lib/webhook/transform.ts
@@ -14,7 +14,7 @@ export const transformWebhook = (webhook: TransformWebhookProps) => {
url: webhook.url,
secret: webhook.secret,
triggers: webhook.triggers,
- linkIds: webhook.links.map((link) => link.linkId),
+ linkIds: webhook.links.map(({ linkId }) => linkId),
};
};