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.

chore: Refactor domain checks in import routes

Kiran K565b27e8eb0d
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/import/bitly/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/import/bitly/route.ts
index 1d0fbe7128..cef88dafee 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/import/bitly/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/import/bitly/route.ts
@@ -52,10 +52,14 @@ export const GET = withWorkspace(async ({ workspace }) => {
export const POST = withWorkspace(async ({ req, workspace, session }) => {
const { selectedDomains, selectedGroupTags } = await req.json();
+ const domains = await prisma.domain.findMany({
+ where: { projectId: workspace.id },
+ });
+
// check if there are domains that are not in the workspace
// if yes, add them to the workspace
const doaminsNotInWorkspace = selectedDomains.filter(
- ({ domain }) => !workspace.domains?.find((d) => d.slug === domain),
+ ({ domain }) => !domains?.find((d) => d.slug === domain),
);
if (doaminsNotInWorkspace.length > 0) {
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/import/rebrandly/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/import/rebrandly/route.ts
index e2df5fbaf6..7d98ddccfc 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/import/rebrandly/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/import/rebrandly/route.ts
@@ -68,10 +68,14 @@ export const PUT = withWorkspace(async ({ req, workspace }) => {
export const POST = withWorkspace(async ({ req, workspace, session }) => {
const { selectedDomains, importTags } = await req.json();
+ const domains = await prisma.domain.findMany({
+ where: { projectId: workspace.id },
+ });
+
// check if there are domains that are not in the workspace
// if yes, add them to the workspace
const domainsNotInWorkspace = selectedDomains.filter(
- ({ domain }) => !workspace.domains?.find((d) => d.slug === domain),
+ ({ domain }) => !domains?.find((d) => d.slug === domain),
);
if (domainsNotInWorkspace.length > 0) {
await Promise.allSettled([
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/import/short/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/import/short/route.ts
index efe75592a3..14f5b1af71 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/import/short/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/import/short/route.ts
@@ -62,10 +62,14 @@ export const PUT = withWorkspace(async ({ req, workspace }) => {
export const POST = withWorkspace(async ({ req, workspace, session }) => {
const { selectedDomains, importTags } = await req.json();
+ const domains = await prisma.domain.findMany({
+ where: { projectId: workspace.id },
+ });
+
// check if there are domains that are not in the workspace
// if yes, add them to the workspace
const domainsNotInWorkspace = selectedDomains.filter(
- ({ domain }) => !workspace.domains?.find((d) => d.slug === domain),
+ ({ domain }) => !domains?.find((d) => d.slug === domain),
);
if (domainsNotInWorkspace.length > 0) {
await Promise.allSettled([
diff --git a/apps/web/lib/api/domains/create-domain.ts b/apps/web/lib/api/domains/create-domain.ts
deleted file mode 100644
index d0fc29ffc5..0000000000
--- a/apps/web/lib/api/domains/create-domain.ts
+++ /dev/null
@@ -1,27 +0,0 @@
-import { prisma } from "@/lib/prisma";
-import { WorkspaceProps } from "@/lib/types";
-import z from "@/lib/zod";
-import { createDomainBodySchema } from "@/lib/zod/schemas/domains";
-
-type CreateDomainInput = z.infer<typeof createDomainBodySchema> & {
- workspace: WorkspaceProps;
- userId: string;
- archived?: boolean;
-};
-
-export const createDomain = async (input: CreateDomainInput) => {
- const { slug, workspace, placeholder, expiredUrl } = input;
-
- // Add domain to the workspace
- return await prisma.domain.create({
- data: {
- slug: slug,
- projectId: workspace.id,
- primary: workspace.domains.length === 0,
- ...(placeholder && { placeholder }),
- ...(workspace.plan !== "free" && {
- expiredUrl,
- }),
- },
- });
-};
diff --git a/apps/web/lib/api/links/process-link.ts b/apps/web/lib/api/links/process-link.ts
index 5588b7e5cc..d101883dfb 100644
--- a/apps/web/lib/api/links/process-link.ts
+++ b/apps/web/lib/api/links/process-link.ts
@@ -28,7 +28,7 @@ export async function processLink<T extends Record<string, any>>({
skipKeyChecks = false, // only skip when key doesn't change (e.g. when editing a link)
}: {
payload: NewLinkProps & T;
- workspace?: Pick<WorkspaceProps, "id" | "plan" | "domains">;
+ workspace?: Pick<WorkspaceProps, "id" | "plan">;
userId?: string;
bulk?: boolean;
skipKeyChecks?: boolean;
@@ -133,9 +133,15 @@ export async function processLink<T extends Record<string, any>>({
}
}
+ const domains = workspace
+ ? await prisma.domain.findMany({
+ where: { projectId: workspace.id },
+ })
+ : [];
+
// if domain is not defined, set it to the workspace's primary domain
if (!domain) {
- domain = workspace?.domains?.find((d) => d.primary)?.slug || SHORT_DOMAIN;
+ domain = domains?.find((d) => d.primary)?.slug || SHORT_DOMAIN;
}
// checks for dub.sh links
@@ -177,7 +183,7 @@ export async function processLink<T extends Record<string, any>>({
}
// else, check if the domain belongs to the workspace
- } else if (!workspace?.domains?.find((d) => d.slug === domain)) {
+ } else if (!domains?.find((d) => d.slug === domain)) {
return {
link: payload,
error: "Domain does not belong to workspace.",
diff --git a/apps/web/lib/api/users.ts b/apps/web/lib/api/users.ts
index 74a431e237..963e04392b 100644
--- a/apps/web/lib/api/users.ts
+++ b/apps/web/lib/api/users.ts
@@ -1,6 +1,6 @@
import { Session, hashToken } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
-import { WorkspaceProps } from "@/lib/types";
+import { WorkspaceWithUsers } from "@/lib/types";
import { TWO_WEEKS_IN_SECONDS } from "@dub/utils";
import { randomBytes } from "crypto";
import { sendEmail } from "emails";
@@ -13,7 +13,7 @@ export async function inviteUser({
session,
}: {
email: string;
- workspace: WorkspaceProps;
+ workspace: WorkspaceWithUsers;
session?: Session;
}) {
// same method of generating a token as next-auth