Added regenerateKey to the projects.ts controller

This commit is contained in:
Mathias Wagner 2023-01-22 02:32:14 +01:00
parent d3b5ed8b3b
commit 265447f068
Signed by: Mathias
GPG Key ID: B8DC354B0A1F5B44

View File

@ -1,6 +1,7 @@
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 || "") });
@ -33,4 +34,14 @@ export const patchProject = async (id: string, config: {
// 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") });
};