UNPKG

@thealiqaf/nestjs-permission-management

Version:
86 lines 4.35 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PermissionService = void 0; const common_1 = require("@nestjs/common"); const permission_schema_1 = require("../schemas/permission.schema"); const mongoose_1 = require("@nestjs/mongoose"); const mongoose_2 = require("mongoose"); const logger_service_1 = require("../common/services/logger.service"); let PermissionService = class PermissionService { permissionModel; logger; constructor(permissionModel, logger) { this.permissionModel = permissionModel; this.logger = logger; } async createPermission(createPermissionDto) { const existingPermission = await this.permissionModel.findOne({ name: createPermissionDto.name }); if (existingPermission) { throw new common_1.ConflictException('Permission already exists'); } const permission = new this.permissionModel(createPermissionDto); this.logger.log(`Creating permission, with data: ${JSON.stringify(createPermissionDto)}`); return await permission.save(); } async findAllPermission() { const permission = await this.permissionModel.find().lean(); return permission; } async findPermissionById(id) { if (!(0, mongoose_2.isValidObjectId)(id)) { throw new common_1.BadRequestException('Invalid permission ID'); } const permission = await this.permissionModel.findById(id); if (!permission) { throw new common_1.NotFoundException('Permission not found'); } return permission; } async updatePermission(id, updatePermissionDto) { const updates = Object.fromEntries(Object.entries(updatePermissionDto).filter(([_, v]) => v !== undefined)); const existingPermission = await this.permissionModel.findById(id); if (!existingPermission) { throw new common_1.NotFoundException('Permission not found'); } if (updates.name && updates.name !== existingPermission.name) { const permissionWithSameName = await this.permissionModel.findOne({ name: updates.name }); if (permissionWithSameName) { throw new common_1.ConflictException('Another permission with this name already exists'); } } const updatedPermission = await this.permissionModel.findByIdAndUpdate(id, { $set: updates }, { new: true, runValidators: true }); if (!updatedPermission) { throw new common_1.NotFoundException('Permission not found'); } this.logger.log(`Updating permission with ID ${id}: ${JSON.stringify(updatePermissionDto)}`); return updatedPermission; } async deletePermission(id) { const deletedPermission = await this.permissionModel.findByIdAndDelete(id); if (!deletedPermission) { throw new common_1.NotFoundException('Permission not found'); } this.logger.log(`Deleting permission with ID ${id}`); return deletedPermission; } }; exports.PermissionService = PermissionService; exports.PermissionService = PermissionService = __decorate([ (0, common_1.Injectable)(), __param(0, (0, mongoose_1.InjectModel)(permission_schema_1.Permission.name)), __metadata("design:paramtypes", [mongoose_2.Model, logger_service_1.LoggerService]) ], PermissionService); //# sourceMappingURL=permission.service.js.map