Created the projects.ts controller

This commit is contained in:
Mathias Wagner 2023-01-22 01:06:07 +01:00
parent 72cd42dc09
commit 5d8237419f
Signed by: Mathias
GPG Key ID: B8DC354B0A1F5B44

View File

@ -0,0 +1,22 @@
import { Project } from "@models/Project";
import { encryptClearField } from "@utils/decryption";
import { Types } from "mongoose";
export const createProject = async (name: string, userId?: string) => {
const count = await Project.countDocuments({ creatorId: encryptClearField(userId || "") });
if (count > 5) return { code: 95, message: "You have exceeded the project limit" };
await Project.create({ name, creatorId: userId });
};
export const deleteProject = async (id: string) => {
if (!Types.ObjectId.isValid(id))
return { code: 3, message: "Invalid object id provided" };
const project = await Project.findById(id);
if (project === null) return { code: 5009, message: "The provided project id does not exist" };
// TODO: Delete all licenses, groups, permissions, ..
await project.delete();
};