"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); } }; var AuthorizationService_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.AuthorizationService = exports.PERMISSION_CACHE = void 0; const common_1 = require("@nestjs/common"); const prisma_service_1 = require("../../shared/prisma.service"); exports.PERMISSION_CACHE = 'PERMISSION_CACHE'; let AuthorizationService = AuthorizationService_1 = class AuthorizationService { prisma; cache; logger = new common_1.Logger(AuthorizationService_1.name); constructor(prisma, cache) { this.prisma = prisma; this.cache = cache; } async getUserPermissions(userId) { try { const cached = await this.cache.get(userId); if (cached) return cached; } catch (e) { this.logger.debug('PermissionCache get failed', e); } const rows = await this.prisma.$queryRaw ` SELECT p.code as code FROM "UserRole" ur JOIN "RolePermission" rp ON rp.role_id = ur.role_id JOIN "Permission" p ON p.id = rp.permission_id WHERE ur.user_id = ${userId}`; const perms = Array.isArray(rows) ? rows.map((r) => r.code) : []; try { await this.cache.set(userId, perms); } catch (e) { this.logger.debug('PermissionCache set failed', e); } return perms; } async hasPermission(userId, permissionCode) { const perms = await this.getUserPermissions(userId); return perms.includes(permissionCode); } async invalidateUserPermissions(userId) { try { await this.cache.invalidate(userId); } catch (e) { this.logger.debug('PermissionCache invalidate failed', e); } } }; exports.AuthorizationService = AuthorizationService; exports.AuthorizationService = AuthorizationService = AuthorizationService_1 = __decorate([ (0, common_1.Injectable)(), __param(1, (0, common_1.Inject)(exports.PERMISSION_CACHE)), __metadata("design:paramtypes", [prisma_service_1.PrismaService, Object]) ], AuthorizationService); //# sourceMappingURL=authorization.service.js.map