Updated the group.ts controller
This commit is contained in:
parent
497ebdba84
commit
654d8c65ee
@ -1,8 +1,13 @@
|
||||
import { checkProjectAccess } from "@controller/projects";
|
||||
import { encryptClearField } from "@utils/decryption";
|
||||
import { IKeyRole } from "@models/AccessKey";
|
||||
import { Group } from "@models/Group";
|
||||
import { Group, IGroup } from "@models/Group";
|
||||
import { planLimits } from "../limits/plans";
|
||||
import { Permission } from "@models/Permission";
|
||||
import { convertIdsToPermissions } from "@controller/permission";
|
||||
|
||||
export const mapGroup = async (projectId: string, group: IGroup) => (
|
||||
{ name: group.name, description: group.description, permissions: await convertIdsToPermissions(projectId, group.permissions) });
|
||||
|
||||
export const listGroups = async (userId: string, projectId: string) => {
|
||||
const access = await checkProjectAccess(IKeyRole.VIEW)(userId, projectId);
|
||||
@ -10,8 +15,7 @@ export const listGroups = async (userId: string, projectId: string) => {
|
||||
|
||||
const groups = await Group.find({ projectId: encryptClearField(String(access._id)) });
|
||||
|
||||
return groups.map(group => ({name: group.name, description: group.description,
|
||||
permissions: group.permissions}));
|
||||
return Promise.all(groups.map(group => mapGroup(projectId, group)));
|
||||
};
|
||||
|
||||
export const getGroup = async (userId: string, projectId: string, groupName: string) => {
|
||||
@ -21,10 +25,10 @@ export const getGroup = async (userId: string, projectId: string, groupName: str
|
||||
const group = await Group.findOne({ projectId: encryptClearField(String(project._id)), name: encryptClearField(groupName) });
|
||||
if (group === null) return { code: 4009, message: "The provided group does not exist" };
|
||||
|
||||
return { name: group.name, description: group.description, permissions: group.permissions };
|
||||
return mapGroup(projectId, group);
|
||||
}
|
||||
|
||||
export const createGroup = async (userId: string, projectId: string, configuration: { name: string, description: string, permissions?: string[] }) => {
|
||||
export const createGroup = async (userId: string, projectId: string, configuration: IGroup) => {
|
||||
const access = await checkProjectAccess(IKeyRole.MANAGE)(userId, projectId);
|
||||
if ("code" in access) return access;
|
||||
|
||||
@ -34,7 +38,11 @@ export const createGroup = async (userId: string, projectId: string, configurati
|
||||
const group = await Group.findOne({ projectId: encryptClearField(String(access._id)), name: encryptClearField(configuration.name) });
|
||||
if (group !== null) return { code: 4008, message: "The provided group name is already in use" };
|
||||
|
||||
// TODO: Check if permissions exist
|
||||
if (configuration.permissions) {
|
||||
let permissionsEncrypted = (configuration.permissions || []).map(permission => encryptClearField(permission));
|
||||
configuration.permissions = (await Permission.find({ projectId: encryptClearField(String(access._id)),
|
||||
permission: { $in: permissionsEncrypted } })).map(permission => permission.id);
|
||||
}
|
||||
|
||||
await Group.create({ ...configuration, projectId });
|
||||
|
||||
@ -51,7 +59,7 @@ export const deleteGroup = async (userId: string, projectId: string, groupName:
|
||||
await group.deleteOne();
|
||||
}
|
||||
|
||||
export const updateGroup = async (userId: string, projectId: string, groupName: string, config: { name?: string, description?: string, permissions?: string[] }) => {
|
||||
export const updateGroup = async (userId: string, projectId: string, groupName: string, config: IGroup) => {
|
||||
const access = await checkProjectAccess(IKeyRole.MANAGE)(userId, projectId);
|
||||
if ("code" in access) return access;
|
||||
|
||||
@ -63,7 +71,11 @@ export const updateGroup = async (userId: string, projectId: string, groupName:
|
||||
if (newGroup !== null) return { code: 4008, message: "The provided group name is already in use" };
|
||||
}
|
||||
|
||||
// TODO: Check if permissions exist
|
||||
if (config.permissions) {
|
||||
let permissionsEncrypted = (config.permissions || []).map(permission => encryptClearField(permission));
|
||||
config.permissions = (await Permission.find({ projectId: encryptClearField(String(access._id)),
|
||||
permission: { $in: permissionsEncrypted } })).map(permission => permission.id);
|
||||
}
|
||||
|
||||
await group.updateOne(config);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user