Files
Checkmate/server/src/db/models/Incident.ts
T

86 lines
1.9 KiB
TypeScript
Raw Normal View History

2026-01-19 23:41:24 +00:00
import { Schema, model, type Types } from "mongoose";
import { IncidentResolutionTypes, type Incident } from "@/types/incident.js";
2026-01-19 23:41:24 +00:00
type IncidentDocumentBase = Omit<Incident, "id" | "monitorId" | "teamId" | "resolvedBy" | "startTime" | "endTime" | "createdAt" | "updatedAt"> & {
monitorId: Types.ObjectId;
teamId: Types.ObjectId;
resolvedBy?: Types.ObjectId | null;
startTime: Date;
endTime: Date | null;
createdAt: Date;
updatedAt: Date;
};
export interface IncidentDocument extends IncidentDocumentBase {
_id: Types.ObjectId;
}
const IncidentSchema = new Schema<IncidentDocument>(
2025-11-18 16:01:47 -05:00
{
monitorId: {
2026-01-19 23:41:24 +00:00
type: Schema.Types.ObjectId,
2025-11-18 16:01:47 -05:00
ref: "Monitor",
required: true,
immutable: true,
index: true,
},
teamId: {
2026-01-19 23:41:24 +00:00
type: Schema.Types.ObjectId,
2025-11-18 16:01:47 -05:00
ref: "Team",
required: true,
immutable: true,
index: true,
},
startTime: {
type: Date,
immutable: true,
2026-01-19 23:41:24 +00:00
required: true,
2025-11-18 16:01:47 -05:00
},
endTime: {
type: Date,
default: null,
},
status: {
type: Boolean,
default: true,
index: true,
},
message: {
type: String,
default: null,
},
statusCode: {
type: Number,
default: null,
2026-01-19 23:41:24 +00:00
index: true,
2025-11-18 16:01:47 -05:00
},
resolutionType: {
type: String,
2026-01-19 23:41:24 +00:00
enum: IncidentResolutionTypes,
2025-11-18 16:01:47 -05:00
default: null,
},
resolvedBy: {
2026-01-19 23:41:24 +00:00
type: Schema.Types.ObjectId,
2025-11-18 16:01:47 -05:00
ref: "User",
default: null,
},
comment: {
type: String,
default: null,
},
},
{ timestamps: true }
);
IncidentSchema.index({ monitorId: 1, status: 1 });
IncidentSchema.index({ teamId: 1, status: 1 });
IncidentSchema.index({ teamId: 1, startTime: -1 });
IncidentSchema.index({ status: 1, startTime: -1 });
IncidentSchema.index({ resolutionType: 1, status: 1 });
IncidentSchema.index({ resolvedBy: 1, status: 1 });
IncidentSchema.index({ createdAt: -1 });
2026-01-19 23:41:24 +00:00
const IncidentModel = model<IncidentDocument>("Incident", IncidentSchema);
2026-01-19 23:41:24 +00:00
export { IncidentModel };
export default IncidentModel;