Started implementing the roles permissions
This commit is contained in:
@@ -11,4 +11,24 @@ export const getRolesByEntities = async (entityIDs: string[]) =>
|
||||
|
||||
export const getRolesByEntity = async (entityID: string) => await db.collection("roles").find<Role>({entityID}).toArray();
|
||||
|
||||
export const getRoles = async (ids?: string[]) => await db.collection("roles").find<Role>(!ids ? {} : {id: {$in: ids}}).toArray();
|
||||
export const getRole = async (id: string) => (await db.collection("roles").findOne<Role>({id})) ?? undefined;
|
||||
|
||||
export const createRole = async (role: Role) => await db.collection("roles").insertOne(role)
|
||||
export const deleteRole = async (id: string) => await db.collection("roles").deleteOne({id})
|
||||
|
||||
export const transferRole = async (previousRole: string, newRole: string) =>
|
||||
await db.collection("users")
|
||||
.updateMany(
|
||||
{ "entities.role": previousRole },
|
||||
{ $set: { 'entities.$[elem].role': newRole } },
|
||||
{ arrayFilters: [{ 'elem.role': previousRole }] }
|
||||
);
|
||||
|
||||
export const assignRoleToUsers = async (users: string[], entity: string, newRole: string) =>
|
||||
await db.collection("users")
|
||||
.updateMany(
|
||||
{ id: { $in: users } },
|
||||
{ $set: { 'entities.$[elem].role': newRole } },
|
||||
{ arrayFilters: [{ 'elem.id': entity }] }
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user