"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 AdminSettingsService_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.AdminSettingsService = void 0; const common_1 = require("@nestjs/common"); const prisma_service_1 = require("../prisma/prisma.service"); const audit_service_1 = require("../audit/audit.service"); const shared_types_1 = require("@micprotocol/shared-types"); const admin_permission_decorator_1 = require("../common/decorators/admin-permission.decorator"); let AdminSettingsService = AdminSettingsService_1 = class AdminSettingsService { prisma; audit; logger = new common_1.Logger(AdminSettingsService_1.name); constructor(prisma, audit) { this.prisma = prisma; this.audit = audit; } async listSettings() { const items = await this.prisma.platformSetting.findMany({ orderBy: [{ category: "asc" }, { key: "asc" }], }); return items.map((s) => ({ key: s.key, value: s.value, description: s.description, category: s.category, updatedById: s.updatedById, updatedAt: s.updatedAt, })); } async getSetting(key) { const setting = await this.prisma.platformSetting.findUnique({ where: { key } }); if (!setting) throw new common_1.NotFoundException(`Platform setting ${key} not found`); return setting; } async upsertSetting(key, value, description, category, actorUserId) { const existing = await this.prisma.platformSetting.findUnique({ where: { key } }); const before = existing ? existing.value : null; const setting = await this.prisma.platformSetting.upsert({ where: { key }, update: { value: value, ...(description !== undefined ? { description } : {}), ...(category !== undefined ? { category: category } : {}), updatedById: actorUserId, }, create: { key, value: value, description, category: category, updatedById: actorUserId, }, }); await this.audit.log({ userId: actorUserId, action: "admin.settings.write", entityType: "PlatformSetting", entityId: key, metadata: { before, after: value, category }, }); return setting; } async deleteSetting(key, actorUserId) { const existing = await this.prisma.platformSetting.findUnique({ where: { key }, select: { key: true, value: true }, }); if (!existing) throw new common_1.NotFoundException(`Platform setting ${key} not found`); await this.prisma.platformSetting.delete({ where: { key } }); await this.audit.log({ userId: actorUserId, action: "admin.settings.write", entityType: "PlatformSetting", entityId: key, metadata: { before: existing.value, after: null, deleted: true }, }); return { deleted: key }; } async listFlags() { const items = await this.prisma.featureFlag.findMany({ orderBy: { key: "asc" }, }); return items.map((f) => ({ key: f.key, enabled: f.enabled, rolloutPercentage: f.rolloutPercentage, allowlistWorkspaceIds: f.allowlistWorkspaceIds, description: f.description, updatedById: f.updatedById, updatedAt: f.updatedAt, })); } async isFlagEnabled(key, workspaceId) { const flag = await this.prisma.featureFlag.findUnique({ where: { key } }); if (!flag || !flag.enabled) return false; if (workspaceId && flag.allowlistWorkspaceIds.length > 0) { return flag.allowlistWorkspaceIds.includes(workspaceId); } return flag.rolloutPercentage >= 100; } async upsertFlag(key, enabled, rolloutPercentage, allowlistWorkspaceIds, description, actorUserId) { const existing = await this.prisma.featureFlag.findUnique({ where: { key } }); const before = existing ? { enabled: existing.enabled, rolloutPercentage: existing.rolloutPercentage, allowlistWorkspaceIds: existing.allowlistWorkspaceIds } : null; const flag = await this.prisma.featureFlag.upsert({ where: { key }, update: { enabled, rolloutPercentage, allowlistWorkspaceIds, ...(description !== undefined ? { description } : {}), updatedById: actorUserId, }, create: { key, enabled, rolloutPercentage, allowlistWorkspaceIds, description, updatedById: actorUserId, }, }); await this.audit.log({ userId: actorUserId, action: "admin.flags.write", entityType: "FeatureFlag", entityId: key, metadata: { before, after: { enabled, rolloutPercentage, allowlistWorkspaceIds } }, }); return flag; } async deleteFlag(key, actorUserId) { const existing = await this.prisma.featureFlag.findUnique({ where: { key }, select: { key: true, enabled: true }, }); if (!existing) throw new common_1.NotFoundException(`Feature flag ${key} not found`); await this.prisma.featureFlag.delete({ where: { key } }); await this.audit.log({ userId: actorUserId, action: "admin.flags.write", entityType: "FeatureFlag", entityId: key, metadata: { before: existing, after: null, deleted: true }, }); return { deleted: key }; } }; exports.AdminSettingsService = AdminSettingsService; __decorate([ (0, admin_permission_decorator_1.requireAdminPermission)(shared_types_1.AdminPermission.SETTINGS_WRITE), __metadata("design:type", Function), __metadata("design:paramtypes", [String, Object, Object, String, String]), __metadata("design:returntype", Promise) ], AdminSettingsService.prototype, "upsertSetting", null); __decorate([ (0, admin_permission_decorator_1.requireAdminPermission)(shared_types_1.AdminPermission.SETTINGS_WRITE), __metadata("design:type", Function), __metadata("design:paramtypes", [String, String]), __metadata("design:returntype", Promise) ], AdminSettingsService.prototype, "deleteSetting", null); __decorate([ (0, admin_permission_decorator_1.requireAdminPermission)(shared_types_1.AdminPermission.FLAGS_WRITE), __metadata("design:type", Function), __metadata("design:paramtypes", [String, Boolean, Number, Array, Object, String]), __metadata("design:returntype", Promise) ], AdminSettingsService.prototype, "upsertFlag", null); __decorate([ (0, admin_permission_decorator_1.requireAdminPermission)(shared_types_1.AdminPermission.FLAGS_WRITE), __metadata("design:type", Function), __metadata("design:paramtypes", [String, String]), __metadata("design:returntype", Promise) ], AdminSettingsService.prototype, "deleteFlag", null); exports.AdminSettingsService = AdminSettingsService = AdminSettingsService_1 = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [prisma_service_1.PrismaService, audit_service_1.AuditService]) ], AdminSettingsService); //# sourceMappingURL=admin-settings.service.js.map