Skip to content

Commit

Permalink
Merge pull request #26 from piyushgarg-dev/feat/remove-slug-for-project
Browse files Browse the repository at this point in the history
refactor: removed the support for slug from project and added support…
  • Loading branch information
piyushgarg-dev authored Oct 8, 2023
2 parents dd65c48 + 0d88ef3 commit e34f7c5
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 17 deletions.
3 changes: 2 additions & 1 deletion functions/graphql/project/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface CreateProjectData {
name: string
slug: string
subdomain: string
customDomain?: string
}
2 changes: 1 addition & 1 deletion functions/graphql/project/queries.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const queries = `#graphql
getUserProjects: [Project]
getProjectBySlug(slug: String!): Project
getProjectByDomain(domain: String!): Project
`
12 changes: 6 additions & 6 deletions functions/graphql/project/resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import slugify from 'slugify'
import ProjectService from '../../../services/project'
import { ensureAuthenticated } from '../../../utils/auth'
import { ServerContext } from '../interfaces'
Expand All @@ -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
Expand All @@ -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',
Expand Down
6 changes: 4 additions & 2 deletions functions/graphql/project/types.ts
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
3 changes: 2 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down
12 changes: 6 additions & 6 deletions services/project.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import prismaClient from '../db'

class ProjectService {
public static create = prismaClient.project.create

Expand All @@ -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
}
}

Expand Down

0 comments on commit e34f7c5

Please sign in to comment.