UNPKG

authenzify

Version:

server to manage authentication authorization of users and more

76 lines (63 loc) 1.9 kB
import { verifyExistence } from '../../../util/util.js' import { generatePermissionId } from '../../../util/record-id-prefixes.js' const fnList = ['find', 'findOne', 'insertOne', 'updateOne'] export class MongoPermissionsService { #modelsCollections constructor(modelsCollections) { verifyExistence(modelsCollections.Permissions, fnList) this.#modelsCollections = modelsCollections } async findPermission({ id }) { const permission = await this.#modelsCollections.Permissions.findOne({ id, }) return permission } async findPermissionByName({ name }) { const permission = await this.#modelsCollections.Permissions.findOne({ name, }) return permission } async findAllPermissions() { const permissions = await this.#modelsCollections.Permissions.find( {}, ).toArray() return permissions } async createPermission(permissionDetails) { const id = generatePermissionId() const now = new Date() const created = await this.#modelsCollections.Permissions.insertOne({ id, _id: id, createdAt: now, updatedAt: now, ...permissionDetails, }) const permission = await this.findPermission({ id: created?.insertedId, }) return permission } async findPermissions(filter) { const permissions = await this.#modelsCollections.Permissions.find(filter).toArray() return permissions } async deletePermission({ id }) { const permissions = await this.#modelsCollections.Permissions.updateOne( { id, }, { $set: { isDeleted: true, updatedAt: new Date() } }, ) return permissions } async findPermissionsByNames({ permissionNames }) { const existingPermissions = await this.#modelsCollections.Permissions.find({ name: { $in: permissionNames }, }).toArray() return existingPermissions } }