Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
team scoped api/domain/index.ts
Aashish-Upadhyay-101e47c7133427d
diff --git a/pages/api/domains/[domain]/index.ts b/pages/api/teams/[teamId]/domains/[domain]/index.tssimilarity index 100%rename from pages/api/domains/[domain]/index.tsrename to pages/api/teams/[teamId]/domains/[domain]/index.tsdiff --git a/pages/api/domains/[domain]/verify.ts b/pages/api/teams/[teamId]/domains/[domain]/verify.tssimilarity index 100%rename from pages/api/domains/[domain]/verify.tsrename to pages/api/teams/[teamId]/domains/[domain]/verify.tsdiff --git a/pages/api/domains/index.ts b/pages/api/teams/[teamId]/domains/index.tssimilarity index 58%rename from pages/api/domains/index.tsrename to pages/api/teams/[teamId]/domains/index.tsindex f8ae7614..911e1754 100644--- a/pages/api/domains/index.ts+++ b/pages/api/teams/[teamId]/domains/index.ts@@ -1,53 +1,72 @@import { NextApiRequest, NextApiResponse } from "next";import { getServerSession } from "next-auth/next";import prisma from "@/lib/prisma";-import { authOptions } from "../auth/[...nextauth]";+import { authOptions } from "../../../auth/[...nextauth]";import { CustomUser } from "@/lib/types";import { log } from "@/lib/utils";import { addDomainToVercel, validDomainRegex } from "@/lib/domains";import { identifyUser, trackAnalytics } from "@/lib/analytics";+import { errorHanlder } from "@/lib/errorHandler";+import { getTeamWithDomain } from "@/lib/team/helper";export default async function handle(req: NextApiRequest,res: NextApiResponse) {if (req.method === "GET") {- // GET /api/domains+ // GET /api/teams/:teamId/domainsconst session = await getServerSession(req, res, authOptions);if (!session) {return res.status(401).end("Unauthorized");}- const domains = await prisma.domain.findMany({- where: {- userId: (session.user as CustomUser).id,- },- select: {- slug: true,- verified: true,- },- orderBy: {- createdAt: "asc",- },- });+ const { teamId } = req.query as { teamId: string };- // console.log("Domains from GET", domains)- return res.status(200).json(domains);+ const userId = (session.user as CustomUser).id;+ try {+ const { team } = await getTeamWithDomain({+ teamId,+ userId,+ options: {+ select: {+ slug: true,+ verified: true,+ },+ orderBy: {+ createdAt: "asc",+ },+ },+ });++ const domains = team.domains;+ return res.status(200).json(domains);+ } catch (error) {+ errorHanlder(error, res);+ }} else if (req.method === "POST") {- // POST /api/domains+ // POST /api/teams/:teamId/domainsconst session = await getServerSession(req, res, authOptions);if (!session) {res.status(401).end("Unauthorized");return;}- // Assuming data is an object with `domain` properties- const { domain } = req.body;+ const { teamId } = req.query as { teamId: string };++ const userId = (session.user as CustomUser).id;// You could perform some validation heretry {+ await getTeamWithDomain({+ teamId,+ userId,+ });++ // Assuming data is an object with `domain` properties+ const { domain } = req.body;+// TODO: Add check for if the domain already exists on another userconst validDomain = validDomainRegex.test(domain);if (validDomain !== true) {@@ -59,25 +78,22 @@ export default async function handle(const response = await prisma.domain.create({data: {slug: domain,- userId: (session.user as CustomUser).id,+ userId,},});await addDomainToVercel(domain);- await identifyUser((session.user as CustomUser).id);+ await identifyUser(userId);await trackAnalytics({event: "Domain Added",slug: domain,});- res.status(201).json(response);+ return res.status(201).json(response);} catch (error) {log(`Failed to add domain. Error: \n\n ${error}`);- res.status(500).json({- message: "Internal Server Error",- error: (error as Error).message,- });- }+ errorHanlder(error, res);+ }} else {// We only allow GET and POST requestsres.setHeader("Allow", ["GET", "POST"]);