refactor
This commit is contained in:
1 parent
e91a54b75c
commit
adb2a383cf
3 files changed
+65
-14
No files matched your search
@@ -7,7 +7,8 @@ export interface IIncidentsRepository {
|
|||||||
findActiveByMonitorId(monitorId: string, teamId: string): Promise<Incident | null>;
|
findActiveByMonitorId(monitorId: string, teamId: string): Promise<Incident | null>;
|
||||||
findByTeamId(
|
findByTeamId(
|
||||||
teamId: string,
|
teamId: string,
|
||||||
dateRange: string,
|
startDate: Date,
|
||||||
|
endDate: Date,
|
||||||
page: number,
|
page: number,
|
||||||
rowsPerPage: number,
|
rowsPerPage: number,
|
||||||
sortOrder?: string,
|
sortOrder?: string,
|
||||||
|
|||||||
@@ -77,8 +77,29 @@ class MongoIncidentRepository implements IIncidentsRepository {
|
|||||||
return this.toEntity(incident);
|
return this.toEntity(incident);
|
||||||
};
|
};
|
||||||
|
|
||||||
findByTeamId = async (teamId: string): Promise<Incident[]> => {
|
findByTeamId = async (
|
||||||
throw new Error("Method not implemented.");
|
teamId: string,
|
||||||
|
startDate: Date,
|
||||||
|
endDate: Date,
|
||||||
|
page: number,
|
||||||
|
rowsPerPage: number,
|
||||||
|
sortOrder?: string,
|
||||||
|
status?: string,
|
||||||
|
monitorId?: string,
|
||||||
|
resolutionType?: string
|
||||||
|
): Promise<Incident[]> => {
|
||||||
|
const matchStage: Record<string, any> = {
|
||||||
|
teamId: new mongoose.Types.ObjectId(teamId),
|
||||||
|
...(status !== undefined && { status: status }),
|
||||||
|
...(monitorId && { monitorId: new mongoose.Types.ObjectId(monitorId) }),
|
||||||
|
...(resolutionType && { resolutionType }),
|
||||||
|
createdAt: { $gte: startDate, $lte: endDate },
|
||||||
|
};
|
||||||
|
const incidents = await IncidentModel.find(matchStage)
|
||||||
|
.sort({ createdAt: sortOrder === "asc" ? 1 : -1 })
|
||||||
|
.skip(page * rowsPerPage)
|
||||||
|
.limit(rowsPerPage);
|
||||||
|
return this.mapDocuments(incidents);
|
||||||
};
|
};
|
||||||
|
|
||||||
updateById = async (incidentId: string, teamId: string, patch: Partial<Incident>) => {
|
updateById = async (incidentId: string, teamId: string, patch: Partial<Incident>) => {
|
||||||
|
|||||||
@@ -4,6 +4,15 @@ import { AppError } from "@/utils/AppError.js";
|
|||||||
import type { IIncidentsRepository } from "@/repositories/index.js";
|
import type { IIncidentsRepository } from "@/repositories/index.js";
|
||||||
import type { Incident } from "@/types/index.js";
|
import type { Incident } from "@/types/index.js";
|
||||||
|
|
||||||
|
const dateRangeLookup: Record<string, Date | undefined> = {
|
||||||
|
recent: new Date(new Date().setDate(new Date().getDate() - 2)),
|
||||||
|
hour: new Date(new Date().setHours(new Date().getHours() - 1)),
|
||||||
|
day: new Date(new Date().setDate(new Date().getDate() - 1)),
|
||||||
|
week: new Date(new Date().setDate(new Date().getDate() - 7)),
|
||||||
|
month: new Date(new Date().setMonth(new Date().getMonth() - 1)),
|
||||||
|
all: undefined,
|
||||||
|
};
|
||||||
|
|
||||||
class IncidentService {
|
class IncidentService {
|
||||||
static SERVICE_NAME = SERVICE_NAME;
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
|
|
||||||
@@ -125,18 +134,38 @@ class IncidentService {
|
|||||||
|
|
||||||
const { sortOrder, dateRange, page, rowsPerPage, status, monitorId, resolutionType } = query || {};
|
const { sortOrder, dateRange, page, rowsPerPage, status, monitorId, resolutionType } = query || {};
|
||||||
|
|
||||||
const result = await this.db.incidentModule.getIncidentsByTeam({
|
const startDate = dateRangeLookup[dateRange];
|
||||||
teamId,
|
|
||||||
sortOrder,
|
|
||||||
dateRange,
|
|
||||||
page,
|
|
||||||
rowsPerPage,
|
|
||||||
status,
|
|
||||||
monitorId,
|
|
||||||
resolutionType,
|
|
||||||
});
|
|
||||||
|
|
||||||
return result;
|
const endDate = new Date();
|
||||||
|
|
||||||
|
const parsedPage = Number.isFinite(parseInt(page)) ? parseInt(page) : 0;
|
||||||
|
const parsedRowsPerPage = Number.isFinite(parseInt(rowsPerPage)) ? parseInt(rowsPerPage) : 20;
|
||||||
|
const parsedStatus = status === "true" ? "true" : status === "false" ? "false" : undefined;
|
||||||
|
|
||||||
|
const res = await this.incidentsRepository.findByTeamId(
|
||||||
|
teamId,
|
||||||
|
startDate,
|
||||||
|
endDate,
|
||||||
|
parsedPage,
|
||||||
|
parsedRowsPerPage,
|
||||||
|
sortOrder,
|
||||||
|
parsedStatus,
|
||||||
|
monitorId,
|
||||||
|
resolutionType
|
||||||
|
);
|
||||||
|
|
||||||
|
// const result = await this.db.incidentModule.getIncidentsByTeam({
|
||||||
|
// teamId,
|
||||||
|
// sortOrder,
|
||||||
|
// dateRange,
|
||||||
|
// page,
|
||||||
|
// rowsPerPage,
|
||||||
|
// status,
|
||||||
|
// monitorId,
|
||||||
|
// resolutionType,
|
||||||
|
// });
|
||||||
|
|
||||||
|
return res;
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
this.logger.error({
|
this.logger.error({
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
|
|||||||
Reference in new issue
Block a user