Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
refactor: split prisma function to api only helper component
Marc Seitze63febc62c08
diff --git a/lib/api/domains.ts b/lib/api/domains.tsnew file mode 100644index 00000000..e5784691--- /dev/null+++ b/lib/api/domains.ts@@ -0,0 +1,46 @@+import prisma from "@/lib/prisma";+import { getApexDomain, removeDomainFromVercel } from "../domains";++// calculate the domainCount+export async function getDomainCount(domain: string) {+ const apexDomain = getApexDomain(`https://${domain}`);+ const response = await prisma.domain.count({+ where: {+ OR: [+ {+ slug: apexDomain,+ },+ {+ slug: {+ endsWith: `.${apexDomain}`,+ },+ },+ ],+ },+ });++ return response;+}++/* Delete a domain */+export async function deleteDomain(+ domain: string,+ {+ // Note: in certain cases, we don't need to remove the domain from the Prisma+ skipPrismaDelete = false,+ } = {}+) {+ const domainCount = await getDomainCount(domain);++ return await Promise.allSettled([+ // remove the domain from Vercel+ removeDomainFromVercel(domain, domainCount),+ // delete domain+ !skipPrismaDelete &&+ prisma.domain.delete({+ where: {+ slug: domain,+ },+ }),+ ]);+}diff --git a/lib/domains.ts b/lib/domains.tsindex 8bee8c8a..c6de37b0 100644--- a/lib/domains.ts+++ b/lib/domains.ts@@ -4,7 +4,7 @@ import {DomainVerificationResponse,} from "@/lib/types";-import prisma from "@/lib/prisma"+export const addDomainToVercel = async (domain: string) => {return await fetch(@@ -128,49 +128,4 @@ export const getApexDomain = (url: string) => {// courtesy of ChatGPT: https://sharegpt.com/c/pUYXtRsexport const validDomainRegex = new RegExp(/^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/-);--// calculate the domainCount-export async function getDomainCount(domain: string) {- const apexDomain = getApexDomain(`https://${domain}`);- const response = await prisma.domain.count({- where: {- OR: [- {- slug: apexDomain,- },- {- slug: {- endsWith: `.${apexDomain}`,- },- },- ],- },- });-- return response;-}---/* Delete a domain */-export async function deleteDomain(- domain: string,- {- // Note: in certain cases, we don't need to remove the domain from the Prisma- skipPrismaDelete = false,- } = {},-) {- const domainCount = await getDomainCount(domain);-- return await Promise.allSettled([- // remove the domain from Vercel- removeDomainFromVercel(domain, domainCount),- // delete domain- !skipPrismaDelete &&- prisma.domain.delete({- where: {- slug: domain,- },- }),- ]);-}+);\ No newline at end of file