import config from "@lib/config"; import TableService from "../TableService"; import { CProjectContract } from "@lib/types/ContractTypes"; import { ProjectErrors } from "@lib/vix/ClientErrors"; import { VERB } from "@dunemask/vix/logging"; export default class ProjectTableService extends TableService { protected table = "Project"; async byId(projectId: string) { return this.pg.project.findUnique({ where: { id: projectId } }); } async bySlug(slug: string) { return this.pg.project.findUnique({ where: { slug } }); } async $upsertDefaultProject() { const { projectSlug: slug, projectName: name } = config.Server; const createOptions = { slug, name, parentProject: slug }; const existingProject = await this.pg.project.findUnique({ where: { slug } }); if (!!existingProject) return VERB("PROJECT", "Default project already exists!"); VERB("PROJECT", "Default project not found! Creating now!"); const proj = await this.pg.project.upsert({ where: { slug }, create: createOptions, update: createOptions }); await this.pg.project.update({ where: { id: proj.id }, data: { parentProject: proj.id } }); // Use ProjectID instead of slug } async create(project: CProjectContract["Create"] & { parentProject: string }) { const existingProject = await this.pg.project.findMany({ where: { id: project.slug } }); if (existingProject.length > 1) throw ProjectErrors.BadRequestSlugInvalid; return this.pg.project.create({ data: project }).catch(() => { throw ProjectErrors.ConflictNonUnique; }); } }