Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

refactor: remove outdated getTeamWithDomain helper

Marc Seitz2241ed4fe166
diff --git a/lib/team/helper.ts b/lib/team/helper.ts
index 69fbec22..7282f8d5 100644
--- a/lib/team/helper.ts
+++ b/lib/team/helper.ts
@@ -1,4 +1,4 @@
-import { Document, DocumentVersion, Domain, Link, View } from "@prisma/client";
+import { Document, DocumentVersion, Link, View } from "@prisma/client";
import prisma from "@/lib/prisma";
import { decryptEncrpytedPassword } from "@/lib/utils";
@@ -13,14 +13,6 @@ interface ITeamUserAndDocument {
options?: {};
}
-interface ITeamWithDomain {
- teamId: string;
- userId: string;
- domain?: string;
- teamOptions?: {};
- options?: {};
-}
-
interface IDocumentWithLink {
docId: string;
userId: string;
@@ -94,58 +86,6 @@ export async function getTeamWithUsersAndDocument({
return { team, document };
}
-export async function getTeamWithDomain({
- teamId,
- userId,
- domain: domainSlug,
- teamOptions,
- options,
-}: ITeamWithDomain) {
- const team = await prisma.team.findUnique({
- where: {
- id: teamId,
- },
- include: {
- users: {
- select: {
- userId: true,
- },
- },
- domains: {
- ...options,
- },
- },
- });
-
- // check if the team exists
- if (!team) {
- throw new TeamError("Team doesn't exists");
- }
-
- // check if the user is part the team
- const teamHasUser = team?.users.some((user) => user.userId === userId);
- if (!teamHasUser) {
- throw new TeamError("You are not a member of the team");
- }
-
- // check if the team has a paid plan
- const teamHasPaidPlan = team?.plan !== "free";
- if (!teamHasPaidPlan) {
- throw new TeamError("Team doesn't have a paid plan");
- }
-
- // check if the domain exists in the team
- let domain: Domain | undefined;
- if (domainSlug) {
- domain = team.domains.find((_domain) => _domain.slug === domainSlug);
- if (!domain) {
- throw new TeamError("Domain doesn't exists in the team");
- }
- }
-
- return { team, domain };
-}
-
export async function getDocumentWithTeamAndUser({
docId,
userId,
diff --git a/pages/api/teams/[teamId]/domains/[domain]/index.ts b/pages/api/teams/[teamId]/domains/[domain]/index.ts
index cf3eef51..375631a3 100644
--- a/pages/api/teams/[teamId]/domains/[domain]/index.ts
+++ b/pages/api/teams/[teamId]/domains/[domain]/index.ts
@@ -6,14 +6,12 @@ import { z } from "zod";
import {
deleteDomainRedirectUrl,
- planSupportsRedirects,
setDomainRedirectUrl,
} from "@/lib/api/domains/redis";
import { validateRedirectUrl } from "@/lib/api/domains/validate-redirect-url";
import { getApexDomain, removeDomainFromVercel } from "@/lib/domains";
import { errorhandler } from "@/lib/errorHandler";
import prisma from "@/lib/prisma";
-import { getTeamWithDomain } from "@/lib/team/helper";
import { CustomUser } from "@/lib/types";
import { log } from "@/lib/utils";
@@ -42,11 +40,30 @@ export default async function handle(
}
try {
- const { domain: domainToBeDeleted } = await getTeamWithDomain({
- teamId,
- userId,
- domain,
+ const hasTeamAccess = await prisma.userTeam.findUnique({
+ where: {
+ userId_teamId: {
+ userId,
+ teamId,
+ },
+ },
+ });
+ if (!hasTeamAccess) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ const domainToBeDeleted = await prisma.domain.findUnique({
+ where: {
+ slug: domain,
+ teamId,
+ },
+ select: {
+ id: true,
+ },
});
+ if (!domainToBeDeleted) {
+ return res.status(404).json("Domain not found");
+ }
// calculate the domainCount
const apexDomain = getApexDomain(`https://${domain}`);
@@ -69,7 +86,7 @@ export default async function handle(
removeDomainFromVercel(domain, domainCount),
prisma.domain.delete({
where: {
- id: domainToBeDeleted?.id,
+ id: domainToBeDeleted.id,
},
}),
deleteDomainRedirectUrl(domain),
@@ -101,12 +118,27 @@ export default async function handle(
}
try {
- const { domain: domainToBeUpdated } = await getTeamWithDomain({
- teamId,
- userId,
- domain,
+ const hasTeamAccess = await prisma.userTeam.findUnique({
+ where: {
+ userId_teamId: {
+ userId,
+ teamId,
+ },
+ },
});
+ if (!hasTeamAccess) {
+ return res.status(401).end("Unauthorized");
+ }
+ const domainToBeUpdated = await prisma.domain.findUnique({
+ where: {
+ slug: domain,
+ teamId,
+ },
+ select: {
+ id: true,
+ },
+ });
if (!domainToBeUpdated) {
return res.status(404).json("Domain not found");
}
@@ -159,12 +191,27 @@ export default async function handle(
}
try {
- const { team, domain: domainToBeUpdated } = await getTeamWithDomain({
- teamId,
- userId,
- domain,
+ const hasTeamAccess = await prisma.userTeam.findUnique({
+ where: {
+ userId_teamId: {
+ userId,
+ teamId,
+ },
+ },
});
+ if (!hasTeamAccess) {
+ return res.status(401).end("Unauthorized");
+ }
+ const domainToBeUpdated = await prisma.domain.findUnique({
+ where: {
+ slug: domain,
+ teamId,
+ },
+ select: {
+ id: true,
+ },
+ });
if (!domainToBeUpdated) {
return res.status(404).json("Domain not found");
}
@@ -179,13 +226,6 @@ export default async function handle(
const hasRedirectUrl = "redirectUrl" in parsed.data;
const redirectUrl = parsed.data.redirectUrl;
- if (hasRedirectUrl && redirectUrl && !planSupportsRedirects(team.plan)) {
- return res.status(403).json({
- message:
- "Root domain redirects require a Business plan or higher",
- });
- }
-
let normalizedUrl: string | null | undefined;
if (hasRedirectUrl) {
if (redirectUrl) {
diff --git a/pages/api/teams/[teamId]/domains/[domain]/validate.ts b/pages/api/teams/[teamId]/domains/[domain]/validate.ts
index f837ae91..ee8b0d84 100644
--- a/pages/api/teams/[teamId]/domains/[domain]/validate.ts
+++ b/pages/api/teams/[teamId]/domains/[domain]/validate.ts
@@ -6,15 +6,11 @@ import { getServerSession } from "next-auth/next";
import { validDomainRegex } from "@/lib/domains";
import { errorhandler } from "@/lib/errorHandler";
import prisma from "@/lib/prisma";
-import { getTeamWithDomain } from "@/lib/team/helper";
import { CustomUser } from "@/lib/types";
import { authOptions } from "../../../../auth/[...nextauth]";
-type DomainValidationStatus =
- | "invalid"
- | "has site"
- | "available";
+type DomainValidationStatus = "invalid" | "has site" | "available";
const sanitizeDomain = (value: string) =>
value
@@ -49,7 +45,17 @@ export default async function handle(
const userId = (session.user as CustomUser).id;
try {
- await getTeamWithDomain({ teamId, userId });
+ const hasTeamAccess = await prisma.userTeam.findUnique({
+ where: {
+ userId_teamId: {
+ userId,
+ teamId,
+ },
+ },
+ });
+ if (!hasTeamAccess) {
+ return res.status(401).end("Unauthorized");
+ }
const sanitizedDomain = sanitizeDomain(rawDomain);
diff --git a/pages/api/teams/[teamId]/domains/index.ts b/pages/api/teams/[teamId]/domains/index.ts
index d3a8c2fb..5f2a910d 100644
--- a/pages/api/teams/[teamId]/domains/index.ts
+++ b/pages/api/teams/[teamId]/domains/index.ts
@@ -2,15 +2,11 @@ import { NextApiRequest, NextApiResponse } from "next";
import { getServerSession } from "next-auth/next";
-import {
- planSupportsRedirects,
- setDomainRedirectUrl,
-} from "@/lib/api/domains/redis";
+import { setDomainRedirectUrl } from "@/lib/api/domains/redis";
import { validateRedirectUrl } from "@/lib/api/domains/validate-redirect-url";
import { addDomainToVercel, validDomainRegex } from "@/lib/domains";
import { errorhandler } from "@/lib/errorHandler";
import prisma from "@/lib/prisma";
-import { getTeamWithDomain } from "@/lib/team/helper";
import { CustomUser } from "@/lib/types";
import { log } from "@/lib/utils";
@@ -32,23 +28,33 @@ export default async function handle(
const userId = (session.user as CustomUser).id;
try {
- const { team } = await getTeamWithDomain({
- teamId,
- userId,
- options: {
- select: {
- slug: true,
- verified: true,
- isDefault: true,
- redirectUrl: true,
- },
- orderBy: {
- createdAt: "asc",
+ const hasTeamAccess = await prisma.userTeam.findUnique({
+ where: {
+ userId_teamId: {
+ userId,
+ teamId,
},
},
});
+ if (!hasTeamAccess) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ const domains = await prisma.domain.findMany({
+ where: {
+ teamId,
+ },
+ select: {
+ slug: true,
+ verified: true,
+ isDefault: true,
+ redirectUrl: true,
+ },
+ orderBy: {
+ createdAt: "asc",
+ },
+ });
- const domains = team.domains;
return res.status(200).json(domains);
} catch (error) {
errorhandler(error, res);
@@ -69,10 +75,17 @@ export default async function handle(
}
try {
- const { team } = await getTeamWithDomain({
- teamId,
- userId,
+ const hasTeamAccess = await prisma.userTeam.findUnique({
+ where: {
+ userId_teamId: {
+ userId,
+ teamId,
+ },
+ },
});
+ if (!hasTeamAccess) {
+ return res.status(401).end("Unauthorized");
+ }
const { domain, redirectUrl } = req.body;
@@ -104,19 +117,13 @@ export default async function handle(
});
if (existingDomain) {
- return res
- .status(400)
- .json({ message: "Unable to add this domain. Please try a different one." });
+ return res.status(400).json({
+ message: "Unable to add this domain. Please try a different one.",
+ });
}
let validatedRedirectUrl: string | undefined;
if (redirectUrl) {
- if (!planSupportsRedirects(team.plan)) {
- return res.status(403).json({
- message:
- "Root domain redirects require a Business plan or higher",
- });
- }
const result = await validateRedirectUrl(redirectUrl, teamId);
if (!result.valid) {
return res.status(422).json({ message: result.message });