src/alerts/alerts.service.ts
Methods |
constructor(prisma: PrismaService)
|
||||||
|
Defined in src/alerts/alerts.service.ts:12
|
||||||
|
Parameters :
|
| Async acknowledge | ||||||
acknowledge(id: string)
|
||||||
|
Defined in src/alerts/alerts.service.ts:69
|
||||||
|
Parameters :
Returns :
unknown
|
| Async create | |||||||||
create(organizationId: string, dto: CreateAlertDto)
|
|||||||||
|
Defined in src/alerts/alerts.service.ts:15
|
|||||||||
|
Parameters :
Returns :
unknown
|
| Async findAll | |||||||||
findAll(organizationId: string, params: PaginationParams)
|
|||||||||
|
Defined in src/alerts/alerts.service.ts:30
|
|||||||||
|
Parameters :
Returns :
unknown
|
| Async findOne | ||||||
findOne(id: string)
|
||||||
|
Defined in src/alerts/alerts.service.ts:51
|
||||||
|
Parameters :
Returns :
unknown
|
| Async getStats | ||||||
getStats(organizationId: string)
|
||||||
|
Defined in src/alerts/alerts.service.ts:91
|
||||||
|
Parameters :
Returns :
unknown
|
| Async remove | ||||||
remove(id: string)
|
||||||
|
Defined in src/alerts/alerts.service.ts:85
|
||||||
|
Parameters :
Returns :
unknown
|
| Async resolve | ||||||
resolve(id: string)
|
||||||
|
Defined in src/alerts/alerts.service.ts:77
|
||||||
|
Parameters :
Returns :
unknown
|
| Async update | |||||||||
update(id: string, dto: UpdateAlertDto)
|
|||||||||
|
Defined in src/alerts/alerts.service.ts:64
|
|||||||||
|
Parameters :
Returns :
unknown
|
import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { CreateAlertDto } from './dto/create-alert.dto';
import { UpdateAlertDto } from './dto/update-alert.dto';
import {
buildPaginationQuery,
buildPaginationMeta,
PaginationParams,
} from '../common/utils/pagination.util';
@Injectable()
export class AlertsService {
constructor(private prisma: PrismaService) {}
async create(organizationId: string, dto: CreateAlertDto) {
return this.prisma.alert.create({
data: {
organizationId,
severity: dto.severity,
title: dto.title,
message: dto.message,
entityType: dto.entityType,
entityId: dto.entityId,
assignedToId: dto.assignedToId,
status: 'ACTIVE',
},
});
}
async findAll(organizationId: string, params: PaginationParams) {
const { skip, take, orderBy, page, limit } = buildPaginationQuery(params);
const [items, total] = await Promise.all([
this.prisma.alert.findMany({
where: { organizationId },
skip,
take,
orderBy,
include: {
assignedTo: {
select: { id: true, firstName: true, lastName: true },
},
},
}),
this.prisma.alert.count({ where: { organizationId } }),
]);
return { data: items, meta: buildPaginationMeta(total, page, limit) };
}
async findOne(id: string) {
const alert = await this.prisma.alert.findUnique({
where: { id },
include: {
assignedTo: {
select: { id: true, firstName: true, lastName: true, email: true },
},
},
});
if (!alert) throw new NotFoundException('Alert not found');
return alert;
}
async update(id: string, dto: UpdateAlertDto) {
await this.findOne(id);
return this.prisma.alert.update({ where: { id }, data: dto });
}
async acknowledge(id: string) {
await this.findOne(id);
return this.prisma.alert.update({
where: { id },
data: { status: 'ACKNOWLEDGED', acknowledgedAt: new Date() },
});
}
async resolve(id: string) {
await this.findOne(id);
return this.prisma.alert.update({
where: { id },
data: { status: 'RESOLVED', resolvedAt: new Date() },
});
}
async remove(id: string) {
await this.findOne(id);
await this.prisma.alert.delete({ where: { id } });
return { deleted: true };
}
async getStats(organizationId: string) {
const statusResults = await this.prisma.alert.groupBy({
by: ['status'],
where: { organizationId },
_count: { status: true },
});
const counts: Record<string, number> = {};
for (const r of statusResults) {
counts[r.status] = r._count.status;
}
const bySeverity = {
INFO: await this.prisma.alert.count({
where: { organizationId, severity: 'INFO', status: 'ACTIVE' },
}),
WARNING: await this.prisma.alert.count({
where: { organizationId, severity: 'WARNING', status: 'ACTIVE' },
}),
CRITICAL: await this.prisma.alert.count({
where: { organizationId, severity: 'CRITICAL', status: 'ACTIVE' },
}),
};
const total = await this.prisma.alert.count({ where: { organizationId } });
return { total, byStatus: counts, bySeverity };
}
}