Updated the projects.ts controller & added the listProjects & getProject method
This commit is contained in:
parent
c80e7707ed
commit
332e618d52
@ -1,47 +1,70 @@
|
|||||||
import { Project } from "@models/Project";
|
import { IProject, Project } from "@models/Project";
|
||||||
import { encryptClearField } from "@utils/decryption";
|
import { encryptClearField } from "@utils/decryption";
|
||||||
import { Types } from "mongoose";
|
import { Types } from "mongoose";
|
||||||
import crypto from "crypto";
|
import crypto from "crypto";
|
||||||
|
|
||||||
export const createProject = async (name: string, userId?: string) => {
|
export const checkProjectAccess = async (userId: string, projectId: string) => {
|
||||||
const count = await Project.countDocuments({ creatorId: encryptClearField(userId || "") });
|
if (!Types.ObjectId.isValid(projectId))
|
||||||
|
return { code: 3, message: "Invalid object id provided" };
|
||||||
|
|
||||||
|
const project = await Project.findOne({ _id: projectId, creatorId: encryptClearField(userId) });
|
||||||
|
if (project === null) return { code: 5009, message: "The provided project id does not exist" };
|
||||||
|
|
||||||
|
// TODO: Get project where userId is a member
|
||||||
|
|
||||||
|
return project;
|
||||||
|
};
|
||||||
|
|
||||||
|
const projectMapper = (project: IProject) => ({
|
||||||
|
id: project._id, name: project.name, validationKey: project.validationKey,
|
||||||
|
defaults: project.defaults,
|
||||||
|
});
|
||||||
|
|
||||||
|
export const listProjects = async (userId?: string) => {
|
||||||
|
const projects = await Project.find({ creatorId: encryptClearField(userId || "") });
|
||||||
|
|
||||||
|
// TODO: Find projects where userId is a member
|
||||||
|
|
||||||
|
return projects.map(project => projectMapper(project));
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getProject = async (projectId: string, userId: string) => {
|
||||||
|
const project = await checkProjectAccess(userId, projectId);
|
||||||
|
if ("code" in project) return project;
|
||||||
|
|
||||||
|
return projectMapper(project);
|
||||||
|
};
|
||||||
|
|
||||||
|
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" };
|
if (count > 5) return { code: 95, message: "You have exceeded the project limit" };
|
||||||
|
|
||||||
await Project.create({ name, creatorId: userId });
|
await Project.create({ name, creatorId: userId });
|
||||||
};
|
};
|
||||||
|
|
||||||
export const deleteProject = async (id: string) => {
|
export const deleteProject = async (id: string, userId: string) => {
|
||||||
if (!Types.ObjectId.isValid(id))
|
const project = await checkProjectAccess(userId, id);
|
||||||
return { code: 3, message: "Invalid object id provided" };
|
if ("code" in project) return project;
|
||||||
|
|
||||||
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, ..
|
// TODO: Delete all licenses, groups, permissions, ..
|
||||||
|
|
||||||
await project.delete();
|
await project?.delete();
|
||||||
};
|
};
|
||||||
|
|
||||||
export const patchProject = async (id: string, config: {
|
export const patchProject = async (id: string, userId: string, config: {
|
||||||
name: string, defaults: { licenseKey: string, groups: [], permissions: [], expirationDate: Date }
|
name: string, defaults: { licenseKey: string, groups: [], permissions: [], expirationDate: Date }
|
||||||
}) => {
|
}) => {
|
||||||
if (!Types.ObjectId.isValid(id))
|
const project = await checkProjectAccess(userId, id);
|
||||||
return { code: 3, message: "Invalid object id provided" };
|
if ("code" in project) return project;
|
||||||
|
|
||||||
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
|
// TODO: Check if groups & permissions exist
|
||||||
|
|
||||||
await project.updateOne({ name: config.name, defaults: Object.assign(project.defaults, config.defaults) });
|
await project.updateOne({ name: config.name, defaults: Object.assign(project.defaults, config.defaults) });
|
||||||
};
|
};
|
||||||
|
|
||||||
export const regenerateKey = async (id: string) => {
|
export const regenerateKey = async (id: string, userId: string) => {
|
||||||
if (!Types.ObjectId.isValid(id))
|
const project = await checkProjectAccess(userId, id);
|
||||||
return { code: 3, message: "Invalid object id provided" };
|
if ("code" in project) return project;
|
||||||
|
|
||||||
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") });
|
await project.updateOne({ validationKey: crypto.randomBytes(24).toString("hex") });
|
||||||
};
|
};
|
Loading…
x
Reference in New Issue
Block a user