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 { Types } from "mongoose";
|
||||
import crypto from "crypto";
|
||||
|
||||
export const createProject = async (name: string, userId?: string) => {
|
||||
const count = await Project.countDocuments({ creatorId: encryptClearField(userId || "") });
|
||||
export const checkProjectAccess = async (userId: string, projectId: string) => {
|
||||
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" };
|
||||
|
||||
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" };
|
||||
export const deleteProject = async (id: string, userId: string) => {
|
||||
const project = await checkProjectAccess(userId, id);
|
||||
if ("code" in project) return project;
|
||||
|
||||
// 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 }
|
||||
}) => {
|
||||
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" };
|
||||
const project = await checkProjectAccess(userId, id);
|
||||
if ("code" in project) return project;
|
||||
|
||||
// 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" };
|
||||
export const regenerateKey = async (id: string, userId: string) => {
|
||||
const project = await checkProjectAccess(userId, id);
|
||||
if ("code" in project) return project;
|
||||
|
||||
await project.updateOne({ validationKey: crypto.randomBytes(24).toString("hex") });
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user