diff --git a/functions/graphql/project/interfaces.ts b/functions/graphql/project/interfaces.ts index 7693a4e..7337bff 100644 --- a/functions/graphql/project/interfaces.ts +++ b/functions/graphql/project/interfaces.ts @@ -1,4 +1,5 @@ export interface CreateProjectData { name: string - slug: string + subdomain: string + customDomain?: string } diff --git a/functions/graphql/project/queries.ts b/functions/graphql/project/queries.ts index b41c518..cbdf540 100644 --- a/functions/graphql/project/queries.ts +++ b/functions/graphql/project/queries.ts @@ -1,4 +1,4 @@ export const queries = `#graphql getUserProjects: [Project] - getProjectBySlug(slug: String!): Project + getProjectByDomain(domain: String!): Project ` diff --git a/functions/graphql/project/resolvers.ts b/functions/graphql/project/resolvers.ts index 8e9d963..ffd4901 100644 --- a/functions/graphql/project/resolvers.ts +++ b/functions/graphql/project/resolvers.ts @@ -1,4 +1,3 @@ -import slugify from 'slugify' import ProjectService from '../../../services/project' import { ensureAuthenticated } from '../../../utils/auth' import { ServerContext } from '../interfaces' @@ -11,14 +10,14 @@ const queries = { ensureAuthenticated(ctx) return ProjectService.getUserProjects(ctx.user?.id!) }, - getProjectBySlug: async ( + getProjectByDomain: async ( _: any, - { slug }: { slug: string }, + { domain }: { domain: string }, ctx: ServerContext ) => { ensureAuthenticated(ctx) - const project = await ProjectService.getProjectBySlug({ - slug, + const project = await ProjectService.getProjectByDomain({ + domain, userId: ctx.user?.id!, }) return project @@ -34,7 +33,8 @@ const mutations = { const project = await ProjectService.create({ data: { name: data.name, - slug: slugify(data.slug), + customDomain: data.customDomain, + subdomain: data.subdomain, ProjectAccessMapping: { create: { role: 'OWNER', diff --git a/functions/graphql/project/types.ts b/functions/graphql/project/types.ts index e395e68..bf0ad0e 100644 --- a/functions/graphql/project/types.ts +++ b/functions/graphql/project/types.ts @@ -1,13 +1,15 @@ export const types = `#graphql input CreateProjectData { name: String! - slug: String! + subdomain: String! + customDomain: String } type Project { id: ID! name: String! - slug: String! + subdomain: String! + customDomain: String createdAt: Date updatedAt: Date } diff --git a/prisma/migrations/20231008073028_add_custom_subdomain_to_project/migration.sql b/prisma/migrations/20231008073028_add_custom_subdomain_to_project/migration.sql new file mode 100644 index 0000000..140d38c --- /dev/null +++ b/prisma/migrations/20231008073028_add_custom_subdomain_to_project/migration.sql @@ -0,0 +1,22 @@ +/* + Warnings: + + - You are about to drop the column `slug` on the `projects` table. All the data in the column will be lost. + - A unique constraint covering the columns `[subdomain]` on the table `projects` will be added. If there are existing duplicate values, this will fail. + - A unique constraint covering the columns `[custom_domain]` on the table `projects` will be added. If there are existing duplicate values, this will fail. + - Added the required column `subdomain` to the `projects` table without a default value. This is not possible if the table is not empty. + +*/ +-- DropIndex +DROP INDEX "projects_slug_key"; + +-- AlterTable +ALTER TABLE "projects" DROP COLUMN "slug", +ADD COLUMN "custom_domain" TEXT, +ADD COLUMN "subdomain" TEXT NOT NULL; + +-- CreateIndex +CREATE UNIQUE INDEX "projects_subdomain_key" ON "projects"("subdomain"); + +-- CreateIndex +CREATE UNIQUE INDEX "projects_custom_domain_key" ON "projects"("custom_domain"); diff --git a/prisma/schema.prisma b/prisma/schema.prisma index a81a371..2103a96 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -47,7 +47,8 @@ model User { model Project { id String @id @default(uuid()) name String - slug String @unique + subdomain String @unique + customDomain String? @unique @map("custom_domain") createdAt DateTime @default(now()) @map("created_at") updatedAt DateTime @updatedAt @map("updated_at") ProjectAccessMapping ProjectAccessMapping[] diff --git a/services/project.ts b/services/project.ts index b0b560f..8b5d9b7 100644 --- a/services/project.ts +++ b/services/project.ts @@ -1,5 +1,4 @@ import prismaClient from '../db' - class ProjectService { public static create = prismaClient.project.create @@ -9,19 +8,20 @@ class ProjectService { }) } - public static getProjectBySlug({ - slug, + public static async getProjectByDomain({ + domain, userId, }: { - slug: string + domain: string userId: string }) { - return prismaClient.project.findUnique({ + const [project] = await prismaClient.project.findMany({ where: { - slug, + OR: [{ subdomain: domain }, { customDomain: domain }], ProjectAccessMapping: { every: { user: { id: userId } } }, }, }) + return project } }