File size: 8,403 Bytes
303c85f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"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