Backend/src/controller/projects.ts

47 lines
1.8 KiB
TypeScript

import { Project } from "@models/Project";
import { encryptClearField } from "@utils/decryption";
import { Types } from "mongoose";
import crypto from "crypto";
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();
};
export const patchProject = async (id: string, config: {
name: string, defaults: { licenseKey: string, groups: [], permissions: [], expirationDate: Date }
}) => {
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: Check if groups & permissions exist
await project.updateOne({ name: config.name, defaults: Object.assign(project.defaults, config.defaults) });
};
export const regenerateKey = 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" };
await project.updateOne({ validationKey: crypto.randomBytes(24).toString("hex") });
};