Skip to content

Commit

Permalink
Merge pull request #1852 from dubinc/tenantId
Browse files Browse the repository at this point in the history
Add support for `tenantId`
  • Loading branch information
steven-tey authored Jan 7, 2025
2 parents 24c8f5e + 703f85a commit 32a6a7d
Show file tree
Hide file tree
Showing 29 changed files with 223 additions and 264 deletions.
10 changes: 6 additions & 4 deletions apps/web/app/api/cron/cleanup/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ export async function GET(req: Request) {
},
},
include: {
tags: true,
tags: {
select: {
tag: true,
},
},
},
take: 100,
}),
Expand Down Expand Up @@ -63,9 +67,7 @@ export async function GET(req: Request) {
});

// Post delete cleanup
await bulkDeleteLinks({
links,
});
await bulkDeleteLinks(links);
}

// Delete the domains
Expand Down
20 changes: 6 additions & 14 deletions apps/web/app/api/cron/domains/delete/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ export async function POST(req: Request) {
domain,
},
include: {
tags: true,
tags: {
select: {
tag: true,
},
},
},
take: 100, // TODO: We can adjust this number based on the performance
});
Expand All @@ -53,19 +57,7 @@ export async function POST(req: Request) {
linkCache.deleteMany(links),

// Record link in the Tinybird
recordLink(
links.map((link) => ({
link_id: link.id,
domain: link.domain,
key: link.key,
url: link.url,
tag_ids: link.tags.map((tag) => tag.id),
workspace_id: workspaceId,
program_id: link.programId ?? "",
created_at: link.createdAt,
deleted: true,
})),
),
recordLink(links),

// Remove image from R2 storage if it exists
links
Expand Down
11 changes: 1 addition & 10 deletions apps/web/app/api/cron/domains/transfer/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,7 @@ export async function POST(req: Request) {
}),

recordLink(
links.map((link) => ({
link_id: link.id,
domain: link.domain,
key: link.key,
url: link.url,
tag_ids: [],
program_id: link.programId ?? "",
workspace_id: newWorkspaceId,
created_at: link.createdAt,
})),
links.map((link) => ({ ...link, projectId: newWorkspaceId })),
),
]);

Expand Down
19 changes: 6 additions & 13 deletions apps/web/app/api/cron/domains/update/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ export async function POST(req: Request) {
domain: newDomain,
},
include: {
tags: true,
tags: {
select: {
tag: true,
},
},
},
skip: (page - 1) * pageSize,
take: pageSize,
Expand All @@ -59,18 +63,7 @@ export async function POST(req: Request) {
}),

// update links in Tinybird
recordLink(
links.map((link) => ({
link_id: link.id,
domain: link.domain,
key: link.key,
url: link.url,
tag_ids: link.tags.map((tag) => tag.tagId),
program_id: link.programId ?? "",
workspace_id: link.projectId,
created_at: link.createdAt,
})),
),
recordLink(links),
]);

await queueDomainUpdate({
Expand Down
10 changes: 6 additions & 4 deletions apps/web/app/api/cron/workspaces/delete/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ export async function POST(req: Request) {
projectId: workspace.id,
},
include: {
tags: true,
tags: {
select: {
tag: true,
},
},
},
take: 100, // TODO: We can adjust this number based on the performance
});
Expand All @@ -52,9 +56,7 @@ export async function POST(req: Request) {
},
}),

bulkDeleteLinks({
links,
}),
bulkDeleteLinks(links),
]);

console.log(res);
Expand Down
12 changes: 1 addition & 11 deletions apps/web/app/api/links/[linkId]/transfer/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,7 @@ export const POST = withWorkspace(
waitUntil(
Promise.all([
linkCache.set({ ...link, projectId: newWorkspaceId }),

recordLink({
link_id: link.id,
domain: link.domain,
key: link.key,
url: link.url,
tag_ids: [],
program_id: link.programId ?? "",
workspace_id: newWorkspaceId,
created_at: link.createdAt,
}),
recordLink({ ...link, projectId: newWorkspaceId }),
// increment new workspace usage
prisma.project.update({
where: {
Expand Down
12 changes: 6 additions & 6 deletions apps/web/app/api/links/bulk/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,11 @@ export const DELETE = withWorkspace(
],
},
include: {
tags: true,
tags: {
select: {
tag: true,
},
},
},
});

Expand All @@ -406,11 +410,7 @@ export const DELETE = withWorkspace(
},
});

waitUntil(
bulkDeleteLinks({
links,
}),
);
waitUntil(bulkDeleteLinks(links));

return NextResponse.json(
{
Expand Down
2 changes: 2 additions & 0 deletions apps/web/app/api/links/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const GET = withWorkspace(
includeWebhooks,
includeDashboard,
linkIds,
tenantId,
} = getLinksQuerySchemaExtended.parse(searchParams);

if (domain) {
Expand All @@ -57,6 +58,7 @@ export const GET = withWorkspace(
includeWebhooks,
includeDashboard,
linkIds,
tenantId,
});

return NextResponse.json(response, {
Expand Down
16 changes: 7 additions & 9 deletions apps/web/app/api/stripe/webhook/customer-subscription-deleted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ export async function customerSubscriptionDeleted(event: Stripe.Event) {
key: "_root",
},
include: {
tags: true,
tags: {
select: {
tag: true,
},
},
},
},
users: {
Expand Down Expand Up @@ -138,14 +142,8 @@ export async function customerSubscriptionDeleted(event: Stripe.Event) {
// record root domain link for all domains from Tinybird
recordLink(
workspaceLinks.map((link) => ({
link_id: link.id,
domain: link.domain,
key: link.key,
url: link.url,
tag_ids: link.tags.map((tag) => tag.tagId),
program_id: link.programId ?? "",
workspace_id: link.projectId,
created_at: link.createdAt,
...link,
url: "",
})),
),
log({
Expand Down
32 changes: 14 additions & 18 deletions apps/web/app/api/tags/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { withWorkspace } from "@/lib/auth";
import { recordLink } from "@/lib/tinybird";
import { TagSchema, updateTagBodySchema } from "@/lib/zod/schemas/tags";
import { prisma } from "@dub/prisma";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";

// PATCH /api/workspaces/[idOrSlug]/tags/[id] – update a tag for a workspace
Expand Down Expand Up @@ -69,13 +70,12 @@ export const DELETE = withWorkspace(
links: {
select: {
link: {
select: {
id: true,
domain: true,
key: true,
url: true,
programId: true,
createdAt: true,
include: {
tags: {
select: {
tag: true,
},
},
},
},
},
Expand All @@ -91,17 +91,13 @@ export const DELETE = withWorkspace(
}

// update links metadata in tinybird after deleting a tag
await recordLink(
response.links.map(({ link }) => ({
link_id: link.id,
domain: link.domain,
key: link.key,
url: link.url,
tag_ids: [],
program_id: link.programId ?? "",
workspace_id: workspace.id,
created_at: link.createdAt,
})),
waitUntil(
recordLink(
response.links.map(({ link }) => ({
...link,
tags: link.tags.filter(({ tag }) => tag.id !== id),
})),
),
);

return NextResponse.json({ id });
Expand Down
20 changes: 6 additions & 14 deletions apps/web/lib/actions/partners/approve-partner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,16 @@ export const approvePartnerAction = authActionClient
programId,
},
include: {
tags: true,
tags: {
select: {
tag: true,
},
},
},
}),
]);

waitUntil(
recordLink({
domain: updatedLink.domain,
key: updatedLink.key,
link_id: updatedLink.id,
created_at: updatedLink.createdAt,
url: updatedLink.url,
tag_ids: updatedLink.tags.map((t) => t.tagId),
program_id: program.id,
workspace_id: workspace.id,
deleted: false,
}),
);
waitUntil(recordLink(updatedLink));

// TODO: [partners] Notify partner of approval?

Expand Down
1 change: 0 additions & 1 deletion apps/web/lib/actions/partners/invite-partner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,5 @@ export const invitePartnerAction = authActionClient
email,
program,
link,
workspace,
});
});
21 changes: 5 additions & 16 deletions apps/web/lib/api/links/bulk-delete-links.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { storage } from "@/lib/storage";
import { recordLink } from "@/lib/tinybird";
import { recordLinkTB, transformLinkTB } from "@/lib/tinybird";
import { R2_URL } from "@dub/utils";
import { Link } from "@prisma/client";
import { linkCache } from "./cache";
import { ExpandedLink } from "./utils";

export async function bulkDeleteLinks({
links,
}: {
links: (Link & { tags: { tagId: string }[] })[];
}) {
export async function bulkDeleteLinks(links: ExpandedLink[]) {
if (links.length === 0) {
return;
}
Expand All @@ -18,16 +14,9 @@ export async function bulkDeleteLinks({
linkCache.deleteMany(links),

// Record the links deletion in Tinybird
recordLink(
recordLinkTB(
links.map((link) => ({
link_id: link.id,
domain: link.domain,
key: link.key,
url: link.url,
tag_ids: link.tags.map(({ tagId }) => tagId),
program_id: link.programId ?? "",
workspace_id: link.projectId,
created_at: link.createdAt,
...transformLinkTB(link),
deleted: true,
})),
),
Expand Down
13 changes: 2 additions & 11 deletions apps/web/lib/api/links/create-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,11 @@ export async function createLink(link: ProcessedLinkProps) {
const uploadedImageUrl = `${R2_URL}/images/${response.id}`;

waitUntil(
Promise.all([
Promise.allSettled([
// cache link in Redis
linkCache.set(response),
// record link in Tinybird
recordLink({
link_id: response.id,
domain: response.domain,
key: response.key,
url: response.url,
tag_ids: response.tags.map(({ tag }) => tag.id),
program_id: link.programId ?? "",
workspace_id: response.projectId,
created_at: response.createdAt,
}),
recordLink(response),
// Upload image to R2 and update the link with the uploaded image URL when
// proxy is enabled and image is set and not stored in R2
...(proxy && image && !isStored(image)
Expand Down
Loading

0 comments on commit 32a6a7d

Please sign in to comment.