File size: 6,364 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
"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 AdminCommsService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdminCommsService = 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 AdminCommsService = AdminCommsService_1 = class AdminCommsService {
    prisma;
    audit;
    logger = new common_1.Logger(AdminCommsService_1.name);
    constructor(prisma, audit) {
        this.prisma = prisma;
        this.audit = audit;
    }
    async listAnnouncements() {
        const items = await this.prisma.announcement.findMany({
            orderBy: { createdAt: "desc" },
        });
        return items.map((a) => ({
            id: a.id,
            title: a.title,
            body: a.body,
            audience: a.audience,
            severity: a.severity,
            status: a.status,
            publishedAt: a.publishedAt,
            expiresAt: a.expiresAt,
            createdById: a.createdById,
            createdAt: a.createdAt,
        }));
    }
    async createAnnouncement(title, body, audience, severity, status, publishedAt, expiresAt, createdById) {
        const announcement = await this.prisma.announcement.create({
            data: {
                title,
                body,
                audience: audience,
                severity,
                status,
                publishedAt,
                expiresAt,
                createdById,
            },
        });
        await this.audit.log({
            userId: createdById,
            action: "admin.announcements.write",
            entityType: "Announcement",
            entityId: announcement.id,
            metadata: { title, severity, status },
        });
        return announcement;
    }
    async updateAnnouncement(id, patch, actorUserId) {
        const existing = await this.prisma.announcement.findUnique({
            where: { id },
            select: { id: true, title: true, status: true },
        });
        if (!existing)
            throw new common_1.NotFoundException(`Announcement ${id} not found`);
        const data = {};
        if (patch.title !== undefined)
            data.title = patch.title;
        if (patch.body !== undefined)
            data.body = patch.body;
        if (patch.audience !== undefined)
            data.audience = patch.audience;
        if (patch.severity !== undefined)
            data.severity = patch.severity;
        if (patch.status !== undefined)
            data.status = patch.status;
        if (patch.publishedAt !== undefined)
            data.publishedAt = patch.publishedAt;
        if (patch.expiresAt !== undefined)
            data.expiresAt = patch.expiresAt;
        const updated = await this.prisma.announcement.update({ where: { id }, data });
        await this.audit.log({
            userId: actorUserId,
            action: "admin.announcements.write",
            entityType: "Announcement",
            entityId: id,
            metadata: { before: { title: existing.title, status: existing.status }, after: patch },
        });
        return updated;
    }
    async archiveAnnouncement(id, actorUserId) {
        const existing = await this.prisma.announcement.findUnique({
            where: { id },
            select: { id: true, status: true },
        });
        if (!existing)
            throw new common_1.NotFoundException(`Announcement ${id} not found`);
        if (existing.status === "archived")
            throw new common_1.BadRequestException("Announcement is already archived");
        const updated = await this.prisma.announcement.update({
            where: { id },
            data: { status: "archived" },
        });
        await this.audit.log({
            userId: actorUserId,
            action: "admin.announcements.write",
            entityType: "Announcement",
            entityId: id,
            metadata: { before: { status: existing.status }, after: { status: "archived" } },
        });
        return updated;
    }
};
exports.AdminCommsService = AdminCommsService;
__decorate([
    (0, admin_permission_decorator_1.requireAdminPermission)(shared_types_1.AdminPermission.ANNOUNCEMENTS_WRITE),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String, Object, String, String, Object, Object, String]),
    __metadata("design:returntype", Promise)
], AdminCommsService.prototype, "createAnnouncement", null);
__decorate([
    (0, admin_permission_decorator_1.requireAdminPermission)(shared_types_1.AdminPermission.ANNOUNCEMENTS_WRITE),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object, String]),
    __metadata("design:returntype", Promise)
], AdminCommsService.prototype, "updateAnnouncement", null);
__decorate([
    (0, admin_permission_decorator_1.requireAdminPermission)(shared_types_1.AdminPermission.ANNOUNCEMENTS_WRITE),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", Promise)
], AdminCommsService.prototype, "archiveAnnouncement", null);
exports.AdminCommsService = AdminCommsService = AdminCommsService_1 = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [prisma_service_1.PrismaService,
        audit_service_1.AuditService])
], AdminCommsService);
//# sourceMappingURL=admin-comms.service.js.map