initial commit
This commit is contained in:
1 parent
7cb985116e
commit
75acf7b38f
955 files changed
+37333
-87054
No files matched your search
@@ -0,0 +1,44 @@
|
||||
import mongoose, { Schema, Document, Types } from "mongoose";
|
||||
|
||||
export interface IInvite extends Document {
|
||||
_id: Types.ObjectId;
|
||||
orgId: Types.ObjectId;
|
||||
orgRoleId?: Types.ObjectId;
|
||||
teamId: Types.ObjectId;
|
||||
teamRoleId: Types.ObjectId;
|
||||
email: string;
|
||||
tokenHash: string;
|
||||
createdBy: Types.ObjectId;
|
||||
updatedBy: Types.ObjectId;
|
||||
expiry: Date;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
const InviteSchema = new Schema<IInvite>(
|
||||
{
|
||||
orgId: { type: Schema.Types.ObjectId, ref: "Org", required: true },
|
||||
teamId: { type: Schema.Types.ObjectId, ref: "Team", required: true },
|
||||
orgRoleId: { type: Schema.Types.ObjectId, ref: "Role" },
|
||||
teamRoleId: { type: Schema.Types.ObjectId, ref: "Role", required: true },
|
||||
email: {
|
||||
type: String,
|
||||
required: true,
|
||||
unique: true,
|
||||
trim: true,
|
||||
lowercase: true,
|
||||
},
|
||||
|
||||
tokenHash: { type: String, required: true, unique: true },
|
||||
expiry: {
|
||||
type: Date,
|
||||
default: () => new Date(Date.now() + 24 * 60 * 60 * 1000),
|
||||
expires: 0,
|
||||
},
|
||||
createdBy: { type: Schema.Types.ObjectId, ref: "User", required: true },
|
||||
updatedBy: { type: Schema.Types.ObjectId, ref: "User", required: true },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
export const Invite = mongoose.model<IInvite>("Invite", InviteSchema);
|
||||
@@ -0,0 +1,18 @@
|
||||
import mongoose, { Schema, Document, Types } from "mongoose";
|
||||
export interface IOrg extends Document {
|
||||
_id: Types.ObjectId;
|
||||
name: string;
|
||||
ownerId: Types.ObjectId;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
const orgSchema = new Schema<IOrg>(
|
||||
{
|
||||
name: { type: String, required: true },
|
||||
ownerId: { type: Schema.Types.ObjectId, ref: "User", required: true },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
export const Org = mongoose.model<IOrg>("Org", orgSchema);
|
||||
@@ -0,0 +1,51 @@
|
||||
import mongoose, { Schema, Document, Types } from "mongoose";
|
||||
import { invalidateCachesForUser } from "@/middleware/AddUserContext.js";
|
||||
|
||||
export interface IOrgMembership extends Document {
|
||||
_id: Types.ObjectId;
|
||||
orgId: Types.ObjectId;
|
||||
userId: Types.ObjectId;
|
||||
roleId?: Types.ObjectId;
|
||||
role: string;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
const orgMembershipSchema = new Schema<IOrgMembership>(
|
||||
{
|
||||
orgId: { type: Schema.Types.ObjectId, ref: "Org", required: true },
|
||||
userId: { type: Schema.Types.ObjectId, ref: "User", required: true },
|
||||
roleId: { type: Schema.Types.ObjectId, ref: "Role" },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
orgMembershipSchema.post<IOrgMembership>("save", async function (doc) {
|
||||
invalidateCachesForUser(doc.userId.toString());
|
||||
});
|
||||
|
||||
orgMembershipSchema.post<IOrgMembership>(
|
||||
"deleteOne",
|
||||
{ document: true, query: false },
|
||||
async function (doc) {
|
||||
invalidateCachesForUser(doc.userId.toString());
|
||||
}
|
||||
);
|
||||
|
||||
orgMembershipSchema.post("findOneAndDelete", function (doc: IOrgMembership) {
|
||||
if (!doc) return;
|
||||
invalidateCachesForUser(doc.userId.toString());
|
||||
});
|
||||
|
||||
orgMembershipSchema.post(
|
||||
"findOneAndUpdate",
|
||||
async function (doc: IOrgMembership) {
|
||||
if (!doc) return;
|
||||
invalidateCachesForUser(doc.userId.toString());
|
||||
}
|
||||
);
|
||||
|
||||
export const OrgMembership = mongoose.model<IOrgMembership>(
|
||||
"OrgMembership",
|
||||
orgMembershipSchema
|
||||
);
|
||||
@@ -0,0 +1,26 @@
|
||||
import mongoose, { Schema, Document, Types } from "mongoose";
|
||||
|
||||
export const ScopeTypes = ["organization", "team"] as const;
|
||||
export type ScopeType = (typeof ScopeTypes)[number];
|
||||
|
||||
export interface IRole extends Document {
|
||||
_id: Types.ObjectId;
|
||||
organizationId: Types.ObjectId;
|
||||
name: string;
|
||||
scope: ScopeType;
|
||||
permissions: string[];
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
const roleSchema = new Schema<IRole>(
|
||||
{
|
||||
organizationId: { type: Schema.Types.ObjectId, ref: "Org" },
|
||||
name: { type: String, required: true },
|
||||
scope: { type: String, enum: ScopeTypes, required: true },
|
||||
permissions: { type: [String], default: [] },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
export const Role = mongoose.model<IRole>("Role", roleSchema);
|
||||
@@ -0,0 +1,22 @@
|
||||
import mongoose, { Schema, Document, Types } from "mongoose";
|
||||
export interface ITeam extends Document {
|
||||
_id: Types.ObjectId;
|
||||
orgId: Types.ObjectId;
|
||||
name: string;
|
||||
description?: string;
|
||||
isSystem?: boolean;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
const teamSchema = new Schema<ITeam>(
|
||||
{
|
||||
orgId: { type: Schema.Types.ObjectId, ref: "Org", required: true },
|
||||
name: { type: String, required: true },
|
||||
description: { type: String, required: false },
|
||||
isSystem: { type: Boolean, default: false },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
export const Team = mongoose.model<ITeam>("Team", teamSchema);
|
||||
@@ -0,0 +1,51 @@
|
||||
import mongoose, { Schema, Document, Types } from "mongoose";
|
||||
import { invalidateCachesForUser } from "@/middleware/AddUserContext.js";
|
||||
export interface ITeamMembership extends Document {
|
||||
_id: Types.ObjectId;
|
||||
orgId: Types.ObjectId;
|
||||
teamId: Types.ObjectId;
|
||||
userId: Types.ObjectId;
|
||||
roleId: Types.ObjectId;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
const teamMembershipSchema = new Schema<ITeamMembership>(
|
||||
{
|
||||
orgId: { type: Schema.Types.ObjectId, ref: "Org", required: true },
|
||||
teamId: { type: Schema.Types.ObjectId, ref: "Team", required: true },
|
||||
userId: { type: Schema.Types.ObjectId, ref: "User", required: true },
|
||||
roleId: { type: Schema.Types.ObjectId, ref: "Role" },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
teamMembershipSchema.post<ITeamMembership>("save", async function (doc) {
|
||||
invalidateCachesForUser(doc.userId.toString());
|
||||
});
|
||||
|
||||
teamMembershipSchema.post<ITeamMembership>(
|
||||
"deleteOne",
|
||||
{ document: true, query: false },
|
||||
async function (doc) {
|
||||
invalidateCachesForUser(doc.userId.toString());
|
||||
}
|
||||
);
|
||||
|
||||
teamMembershipSchema.post("findOneAndDelete", function (doc: ITeamMembership) {
|
||||
if (!doc) return;
|
||||
invalidateCachesForUser(doc.userId.toString());
|
||||
});
|
||||
|
||||
teamMembershipSchema.post(
|
||||
"findOneAndUpdate",
|
||||
async function (doc: ITeamMembership) {
|
||||
if (!doc) return;
|
||||
invalidateCachesForUser(doc.userId.toString());
|
||||
}
|
||||
);
|
||||
|
||||
export const TeamMembership = mongoose.model<ITeamMembership>(
|
||||
"TeamMembership",
|
||||
teamMembershipSchema
|
||||
);
|
||||
@@ -0,0 +1,67 @@
|
||||
import mongoose, { Schema, Document, Types } from "mongoose";
|
||||
import { IRole, ITeam } from "@/db/models/index.js";
|
||||
|
||||
export interface ITokenizedUser {
|
||||
sub: string;
|
||||
email: string;
|
||||
orgId: string;
|
||||
}
|
||||
export interface IUserContext {
|
||||
sub: string;
|
||||
email: string;
|
||||
orgId: string;
|
||||
teamIds?: string[];
|
||||
teams?: ITeam[];
|
||||
currentTeamId?: string;
|
||||
roles?: {
|
||||
orgRole?: IRole;
|
||||
teamRole: IRole;
|
||||
};
|
||||
}
|
||||
|
||||
export interface IUserReturnable {
|
||||
id: string;
|
||||
email: string;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
org: {
|
||||
name: string;
|
||||
permissions: string[];
|
||||
};
|
||||
teams: {
|
||||
id: string;
|
||||
name: string;
|
||||
permissions: string[];
|
||||
}[];
|
||||
}
|
||||
|
||||
export interface IUser extends Document {
|
||||
_id: Types.ObjectId;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
email: string;
|
||||
passwordHash: string;
|
||||
lastLoginAt?: Date;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
export interface IUserProfile {
|
||||
firstName?: string;
|
||||
lastName?: string;
|
||||
password?: string;
|
||||
confirmPassword?: string;
|
||||
}
|
||||
|
||||
const userSchema = new Schema<IUser>(
|
||||
{
|
||||
firstName: { type: String, required: true },
|
||||
lastName: { type: String, required: true },
|
||||
email: { type: String, required: true, unique: true },
|
||||
passwordHash: { type: String, required: true },
|
||||
lastLoginAt: { type: Date },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
export const User = mongoose.model<IUser>("User", userSchema);
|
||||
@@ -0,0 +1,315 @@
|
||||
import mongoose, { Schema, Document, Types } from "mongoose";
|
||||
import {
|
||||
MonitorType,
|
||||
MonitorTypes,
|
||||
MonitorStatus,
|
||||
MonitorStatuses,
|
||||
} from "../monitors/Monitor.js";
|
||||
import type { Response } from "got";
|
||||
export type GotTimings = Response["timings"];
|
||||
export interface ITimingPhases {
|
||||
wait: number;
|
||||
dns: number;
|
||||
tcp: number;
|
||||
tls: number;
|
||||
request: number;
|
||||
firstByte: number;
|
||||
download: number;
|
||||
total: number;
|
||||
}
|
||||
|
||||
export interface ICpuInfo {
|
||||
physical_core: number;
|
||||
logical_core: number;
|
||||
frequency: number;
|
||||
current_frequency: number;
|
||||
temperature: number[]; // per-core temps
|
||||
free_percent: number;
|
||||
usage_percent: number;
|
||||
}
|
||||
|
||||
export interface IMemoryInfo {
|
||||
total_bytes: number;
|
||||
available_bytes: number;
|
||||
used_bytes: number;
|
||||
usage_percent: number;
|
||||
}
|
||||
|
||||
export interface IHostInfo {
|
||||
os?: string;
|
||||
platform?: string;
|
||||
kernel_version?: string;
|
||||
pretty_name?: string;
|
||||
}
|
||||
|
||||
export interface IDiskInfo {
|
||||
device: string;
|
||||
total_bytes: number;
|
||||
free_bytes: number;
|
||||
used_bytes: number;
|
||||
usage_percent: number;
|
||||
total_inodes?: number;
|
||||
free_inodes?: number;
|
||||
used_inodes?: number;
|
||||
inodes_usage_percent?: number;
|
||||
read_bytes?: number;
|
||||
write_bytes?: number;
|
||||
read_time?: number;
|
||||
write_time?: number;
|
||||
}
|
||||
|
||||
export interface INetInfo {
|
||||
name: string;
|
||||
bytes_sent: number;
|
||||
bytes_recv: number;
|
||||
packets_sent: number;
|
||||
packets_recv: number;
|
||||
err_in: number;
|
||||
err_out: number;
|
||||
drop_in: number;
|
||||
drop_out: number;
|
||||
fifo_in: number;
|
||||
fifo_out: number;
|
||||
}
|
||||
|
||||
export interface ICaptureInfo {
|
||||
version?: string;
|
||||
mode?: string;
|
||||
}
|
||||
|
||||
export interface ISystemInfo {
|
||||
cpu: ICpuInfo;
|
||||
memory: IMemoryInfo;
|
||||
disk: IDiskInfo[];
|
||||
host: IHostInfo;
|
||||
net: INetInfo[];
|
||||
}
|
||||
|
||||
export interface ILighthouseAudit {
|
||||
id?: string;
|
||||
title?: string;
|
||||
score?: number | null;
|
||||
displayValue?: string;
|
||||
numericValue?: number;
|
||||
numericUnit?: string;
|
||||
}
|
||||
export interface ILighthouseCategories {
|
||||
accessibility?: { score?: number | null };
|
||||
"best-practices"?: { score?: number | null };
|
||||
seo?: { score?: number | null };
|
||||
performance?: { score?: number | null };
|
||||
}
|
||||
|
||||
export interface ILighthouseResult {
|
||||
categories?: ILighthouseCategories;
|
||||
audits?: Record<string, ILighthouseAudit>;
|
||||
}
|
||||
|
||||
export interface ICheckLighthouseFields {
|
||||
accessibility: number;
|
||||
bestPractices: number;
|
||||
seo: number;
|
||||
performance: number;
|
||||
audits: {
|
||||
cls: ILighthouseAudit;
|
||||
si: ILighthouseAudit;
|
||||
fcp: ILighthouseAudit;
|
||||
lcp: ILighthouseAudit;
|
||||
tbt: ILighthouseAudit;
|
||||
};
|
||||
}
|
||||
|
||||
export interface ICheck extends Document {
|
||||
_id: Types.ObjectId;
|
||||
metadata: {
|
||||
monitorId: Types.ObjectId;
|
||||
teamId: Types.ObjectId;
|
||||
type: MonitorType;
|
||||
};
|
||||
status: MonitorStatus;
|
||||
httpStatusCode?: number;
|
||||
message: string;
|
||||
responseTime?: number;
|
||||
timings?: GotTimings;
|
||||
errorMessage?: string;
|
||||
ackAt?: Date;
|
||||
ackBy?: Types.ObjectId;
|
||||
system?: ISystemInfo;
|
||||
capture?: ICaptureInfo;
|
||||
lighthouse?: ICheckLighthouseFields;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
expiry: Date;
|
||||
}
|
||||
|
||||
const CheckSchema = new Schema<ICheck>(
|
||||
{
|
||||
metadata: {
|
||||
monitorId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: "Monitor",
|
||||
required: true,
|
||||
},
|
||||
teamId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: "Team",
|
||||
required: true,
|
||||
},
|
||||
type: {
|
||||
type: String,
|
||||
required: true,
|
||||
enum: MonitorTypes,
|
||||
},
|
||||
},
|
||||
status: {
|
||||
type: String,
|
||||
required: true,
|
||||
enum: MonitorStatuses,
|
||||
},
|
||||
httpStatusCode: { type: Number },
|
||||
|
||||
message: { type: String, trim: true },
|
||||
responseTime: { type: Number },
|
||||
timings: {
|
||||
start: { type: Date },
|
||||
socket: { type: Date },
|
||||
lookup: { type: Date },
|
||||
connect: { type: Date },
|
||||
secureConnect: { type: Date },
|
||||
response: { type: Date },
|
||||
end: { type: Date },
|
||||
phases: {
|
||||
wait: { type: Number },
|
||||
dns: { type: Number },
|
||||
tcp: { type: Number },
|
||||
tls: { type: Number },
|
||||
request: { type: Number },
|
||||
firstByte: { type: Number },
|
||||
download: { type: Number },
|
||||
total: { type: Number },
|
||||
},
|
||||
},
|
||||
system: {
|
||||
type: {
|
||||
cpu: {
|
||||
physical_core: { type: Number },
|
||||
logical_core: { type: Number },
|
||||
frequency: { type: Number },
|
||||
current_frequency: { type: Number },
|
||||
temperature: [{ type: Number }],
|
||||
free_percent: { type: Number },
|
||||
usage_percent: { type: Number },
|
||||
},
|
||||
memory: {
|
||||
total_bytes: { type: Number },
|
||||
available_bytes: { type: Number },
|
||||
used_bytes: { type: Number },
|
||||
usage_percent: { type: Number },
|
||||
},
|
||||
disk: [
|
||||
{
|
||||
device: { type: String },
|
||||
total_bytes: { type: Number },
|
||||
free_bytes: { type: Number },
|
||||
used_bytes: { type: Number },
|
||||
usage_percent: { type: Number },
|
||||
total_inodes: { type: Number },
|
||||
free_inodes: { type: Number },
|
||||
used_inodes: { type: Number },
|
||||
inodes_usage_percent: { type: Number },
|
||||
read_bytes: { type: Number },
|
||||
write_bytes: { type: Number },
|
||||
read_time: { type: Number },
|
||||
write_time: { type: Number },
|
||||
},
|
||||
],
|
||||
host: {
|
||||
os: { type: String },
|
||||
platform: { type: String },
|
||||
kernel_version: { type: String },
|
||||
pretty_name: { type: String },
|
||||
},
|
||||
net: [
|
||||
{
|
||||
name: { type: String },
|
||||
bytes_sent: { type: Number },
|
||||
bytes_recv: { type: Number },
|
||||
packets_sent: { type: Number },
|
||||
packets_recv: { type: Number },
|
||||
err_in: { type: Number },
|
||||
err_out: { type: Number },
|
||||
drop_in: { type: Number },
|
||||
drop_out: { type: Number },
|
||||
fifo_in: { type: Number },
|
||||
fifo_out: { type: Number },
|
||||
},
|
||||
],
|
||||
},
|
||||
required: false,
|
||||
},
|
||||
|
||||
capture: {
|
||||
type: {
|
||||
version: { type: String },
|
||||
mode: { type: String },
|
||||
},
|
||||
required: false,
|
||||
},
|
||||
lighthouse: {
|
||||
accessibility: { type: Number, required: false },
|
||||
bestPractices: { type: Number, required: false },
|
||||
seo: { type: Number, required: false },
|
||||
performance: { type: Number, required: false },
|
||||
audits: {
|
||||
cls: {
|
||||
type: Object,
|
||||
},
|
||||
si: {
|
||||
type: Object,
|
||||
},
|
||||
fcp: {
|
||||
type: Object,
|
||||
},
|
||||
lcp: {
|
||||
type: Object,
|
||||
},
|
||||
tbt: {
|
||||
type: Object,
|
||||
},
|
||||
},
|
||||
type: {
|
||||
accessibility: { type: Number },
|
||||
bestPractices: { type: Number },
|
||||
seo: { type: Number },
|
||||
performance: { type: Number },
|
||||
audits: {
|
||||
cls: { type: Object },
|
||||
si: { type: Object },
|
||||
fcp: { type: Object },
|
||||
lcp: { type: Object },
|
||||
tbt: { type: Object },
|
||||
},
|
||||
},
|
||||
required: false,
|
||||
},
|
||||
|
||||
errorMessage: { type: String, trim: true },
|
||||
ackAt: { type: Date },
|
||||
ackBy: { type: Schema.Types.ObjectId, ref: "User" },
|
||||
expiry: {
|
||||
type: Date,
|
||||
default: Date.now,
|
||||
expires: 60 * 60 * 24 * 30,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
timeseries: {
|
||||
timeField: "createdAt",
|
||||
metaField: "metadata",
|
||||
granularity: "seconds",
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
export const Check = mongoose.model<ICheck>("Check", CheckSchema);
|
||||
@@ -0,0 +1,39 @@
|
||||
import mongoose, { Schema, Document, Types } from "mongoose";
|
||||
|
||||
export const ResolutionTypes = ["auto", "manual"] as const;
|
||||
export type ResolutionType = (typeof ResolutionTypes)[number];
|
||||
|
||||
export interface IIncident {
|
||||
_id: Types.ObjectId;
|
||||
monitorId: Types.ObjectId;
|
||||
teamId: Types.ObjectId;
|
||||
startedAt: Date;
|
||||
startCheck: Types.ObjectId;
|
||||
endedAt?: Date;
|
||||
endCheck?: Types.ObjectId;
|
||||
resolved: boolean;
|
||||
resolvedBy?: Types.ObjectId;
|
||||
resolutionType?: ResolutionType;
|
||||
resolutionNote?: string;
|
||||
}
|
||||
|
||||
const IncidentSchema = new Schema<IIncident>(
|
||||
{
|
||||
monitorId: { type: Schema.Types.ObjectId, ref: "Monitor", required: true },
|
||||
teamId: { type: Schema.Types.ObjectId, ref: "Team", required: true },
|
||||
startedAt: { type: Date, required: true },
|
||||
startCheck: { type: Schema.Types.ObjectId, ref: "Check", required: true },
|
||||
endedAt: { type: Date },
|
||||
endCheck: { type: Schema.Types.ObjectId, ref: "Check" },
|
||||
resolved: { type: Boolean, default: false },
|
||||
resolvedBy: { type: Schema.Types.ObjectId, ref: "User" },
|
||||
resolutionType: {
|
||||
type: String,
|
||||
enum: ResolutionTypes,
|
||||
},
|
||||
resolutionNote: { type: String },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
export const Incident = mongoose.model<IIncident>("Incident", IncidentSchema);
|
||||
@@ -0,0 +1,58 @@
|
||||
export { Check } from "./checks/Check.js";
|
||||
export type {
|
||||
ICheck,
|
||||
ISystemInfo,
|
||||
ICaptureInfo,
|
||||
ILighthouseResult,
|
||||
} from "./checks/Check.js";
|
||||
|
||||
export { Monitor, MonitorStatuses } from "./monitors/Monitor.js";
|
||||
export type { IMonitor } from "./monitors/Monitor.js";
|
||||
|
||||
export { MonitorStats } from "./monitors/MonitorStats.js";
|
||||
export type { IMonitorStats } from "./monitors/MonitorStats.js";
|
||||
|
||||
export { User } from "./auth/User.js";
|
||||
export type {
|
||||
IUser,
|
||||
ITokenizedUser,
|
||||
IUserReturnable,
|
||||
IUserContext,
|
||||
IUserProfile,
|
||||
} from "./auth/User.js";
|
||||
|
||||
export {
|
||||
NotificationChannel,
|
||||
ChannelTypes,
|
||||
} from "./notification-channel/NotificationChannel.js";
|
||||
export type { INotificationChannel } from "./notification-channel/NotificationChannel.js";
|
||||
|
||||
export { Invite } from "./auth/Invite.js";
|
||||
export type { IInvite } from "./auth/Invite.js";
|
||||
|
||||
export { Role } from "./auth/Role.js";
|
||||
export type { IRole } from "./auth/Role.js";
|
||||
|
||||
export { Maintenance, MaintenanceRepeats } from "./maintenance/Maintenance.js";
|
||||
export type { IMaintenance } from "./maintenance/Maintenance.js";
|
||||
|
||||
export { Org } from "./auth/Org.js";
|
||||
export type { IOrg } from "./auth/Org.js";
|
||||
|
||||
export { OrgMembership } from "./auth/OrgMembership.js";
|
||||
export type { IOrgMembership } from "./auth/OrgMembership.js";
|
||||
|
||||
export { Team } from "./auth/Team.js";
|
||||
export type { ITeam } from "./auth/Team.js";
|
||||
|
||||
export { TeamMembership } from "./auth/TeamMembership.js";
|
||||
export type { ITeamMembership } from "./auth/TeamMembership.js";
|
||||
|
||||
export { StatusPage } from "./status-page/StatusPage.js";
|
||||
export type { IStatusPage } from "./status-page/StatusPage.js";
|
||||
|
||||
export { RecoveryToken } from "./recovery/RecoveryToken.js";
|
||||
export type { IRecoveryToken } from "./recovery/RecoveryToken.js";
|
||||
|
||||
export { Incident, ResolutionTypes } from "./incidents/Incident.js";
|
||||
export type { IIncident, ResolutionType } from "./incidents/Incident.js";
|
||||
@@ -0,0 +1,54 @@
|
||||
import mongoose, { Schema, Document, Types } from "mongoose";
|
||||
export const MaintenanceRepeats = ["no repeat", "daily", "weekly"] as const;
|
||||
export type MaintenanceRepeat = (typeof MaintenanceRepeats)[number];
|
||||
export interface IMaintenance extends Document {
|
||||
_id: Types.ObjectId;
|
||||
orgId: Types.ObjectId;
|
||||
teamId: Types.ObjectId;
|
||||
name: string;
|
||||
isActive: boolean;
|
||||
repeat: MaintenanceRepeat;
|
||||
monitors: Types.ObjectId[];
|
||||
startTime: Date;
|
||||
endTime: Date;
|
||||
createdBy: Types.ObjectId;
|
||||
updatedBy: Types.ObjectId;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
const MaintenanceSchema = new Schema<IMaintenance>(
|
||||
{
|
||||
orgId: { type: Schema.Types.ObjectId, ref: "Org", required: true },
|
||||
teamId: { type: Schema.Types.ObjectId, ref: "Team", required: true },
|
||||
name: { type: String, required: true, trim: true },
|
||||
isActive: { type: Boolean, required: true, default: true },
|
||||
repeat: {
|
||||
type: String,
|
||||
enum: MaintenanceRepeats,
|
||||
required: false,
|
||||
default: null,
|
||||
},
|
||||
monitors: [
|
||||
{
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: "Monitor",
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
startTime: { type: Date, required: true },
|
||||
endTime: { type: Date, required: true },
|
||||
createdBy: { type: Schema.Types.ObjectId, ref: "User", required: true },
|
||||
updatedBy: { type: Schema.Types.ObjectId, ref: "User", required: true },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
MaintenanceSchema.index({ isActive: 1 });
|
||||
MaintenanceSchema.index({ startTime: 1 });
|
||||
MaintenanceSchema.index({ endTime: 1 });
|
||||
|
||||
export const Maintenance = mongoose.model<IMaintenance>(
|
||||
"Maintenance",
|
||||
MaintenanceSchema
|
||||
);
|
||||
@@ -0,0 +1,134 @@
|
||||
import mongoose, { Schema, Document, Types } from "mongoose";
|
||||
import { Check, MonitorStats } from "@/db/models/index.js";
|
||||
|
||||
export const MonitorTypes = [
|
||||
"http",
|
||||
"https",
|
||||
"ping",
|
||||
"infrastructure",
|
||||
"pagespeed",
|
||||
] as const;
|
||||
export type MonitorType = (typeof MonitorTypes)[number];
|
||||
|
||||
export const MonitorStatuses = [
|
||||
"up",
|
||||
"down",
|
||||
"paused",
|
||||
"initializing",
|
||||
] as const;
|
||||
export type MonitorStatus = (typeof MonitorStatuses)[number];
|
||||
export interface IMonitor extends Document {
|
||||
_id: Types.ObjectId;
|
||||
orgId: Types.ObjectId;
|
||||
teamId: Types.ObjectId;
|
||||
name: string;
|
||||
url: string;
|
||||
secret?: string;
|
||||
type: MonitorType;
|
||||
interval: number; // in ms
|
||||
isActive: boolean;
|
||||
status: MonitorStatus;
|
||||
n: number; // Number of consecutive successes required to change status
|
||||
lastCheckedAt?: Date;
|
||||
latestChecks: {
|
||||
status: MonitorStatus;
|
||||
responseTime: number;
|
||||
checkedAt: Date;
|
||||
}[];
|
||||
notificationChannels?: Types.ObjectId[];
|
||||
createdBy: Types.ObjectId;
|
||||
updatedBy: Types.ObjectId;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
const MonitorSchema = new Schema<IMonitor>(
|
||||
{
|
||||
orgId: { type: Schema.Types.ObjectId, ref: "Org", required: true },
|
||||
teamId: { type: Schema.Types.ObjectId, ref: "Team", required: true },
|
||||
name: { type: String, required: true, trim: true, maxlength: 100 },
|
||||
url: { type: String, required: true, trim: true },
|
||||
secret: { type: String, required: false },
|
||||
type: {
|
||||
type: String,
|
||||
required: true,
|
||||
enum: MonitorTypes,
|
||||
},
|
||||
interval: { type: Number, required: true, default: 60000 },
|
||||
isActive: { type: Boolean, required: true, default: true },
|
||||
status: {
|
||||
type: String,
|
||||
required: true,
|
||||
default: "initializing",
|
||||
enum: MonitorStatuses,
|
||||
},
|
||||
n: { type: Number, required: true, default: 1 },
|
||||
lastCheckedAt: { type: Date },
|
||||
latestChecks: {
|
||||
type: [
|
||||
{
|
||||
status: {
|
||||
type: String,
|
||||
required: true,
|
||||
enum: MonitorStatuses,
|
||||
},
|
||||
responseTime: { type: Number, required: true },
|
||||
checkedAt: { type: Date, required: true },
|
||||
},
|
||||
],
|
||||
default: [],
|
||||
},
|
||||
notificationChannels: {
|
||||
type: [{ type: Schema.Types.ObjectId, ref: "NotificationChannel" }],
|
||||
default: [],
|
||||
},
|
||||
|
||||
createdBy: { type: Schema.Types.ObjectId, ref: "User", required: true },
|
||||
updatedBy: { type: Schema.Types.ObjectId, ref: "User", required: true },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
MonitorSchema.pre(
|
||||
"deleteOne",
|
||||
{ document: true, query: false },
|
||||
async function (next) {
|
||||
try {
|
||||
const monitorId = this._id;
|
||||
await Check.deleteMany({ "metadata.monitorId": monitorId });
|
||||
await MonitorStats.deleteMany({ monitorId });
|
||||
next();
|
||||
} catch (error: any) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
MonitorSchema.pre(
|
||||
"deleteMany",
|
||||
{ document: false, query: true },
|
||||
async function (next) {
|
||||
try {
|
||||
const filter = this.getFilter();
|
||||
const monitors = await this.model.find(filter).select("_id");
|
||||
const monitorIds = monitors.map((m) => m._id);
|
||||
if (monitorIds.length > 0) {
|
||||
await Check.deleteMany({ "metadata.monitorId": { $in: monitorIds } });
|
||||
await MonitorStats.deleteMany({ monitorId: { $in: monitorIds } });
|
||||
}
|
||||
next();
|
||||
} catch (error: any) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
MonitorSchema.index({ isActive: 1 });
|
||||
MonitorSchema.index({ status: 1 });
|
||||
MonitorSchema.index({ type: 1 });
|
||||
MonitorSchema.index({ lastCheckedAt: 1 });
|
||||
MonitorSchema.index({ isActive: 1, status: 1 });
|
||||
MonitorSchema.index({ createdBy: 1 });
|
||||
MonitorSchema.index({ updatedBy: 1 });
|
||||
|
||||
export const Monitor = mongoose.model<IMonitor>("Monitor", MonitorSchema);
|
||||
@@ -0,0 +1,80 @@
|
||||
import mongoose, { Schema, Document, Types } from "mongoose";
|
||||
import { MonitorStatus, MonitorStatuses } from "./Monitor.js";
|
||||
|
||||
export interface IMonitorStats extends mongoose.Document {
|
||||
monitorId: mongoose.Types.ObjectId;
|
||||
avgResponseTime: number;
|
||||
maxResponseTime: number;
|
||||
totalChecks: number;
|
||||
totalUpChecks: number;
|
||||
totalDownChecks: number;
|
||||
uptimePercentage: number;
|
||||
lastCheckTimestamp: number;
|
||||
lastResponseTime: number;
|
||||
timeOfLastFailure: number;
|
||||
currentStreak: number;
|
||||
currentStreakStatus: MonitorStatus;
|
||||
currentStreakStartedAt: number;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
const MonitorStatsSchema = new Schema<IMonitorStats>(
|
||||
{
|
||||
monitorId: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: "Monitor",
|
||||
immutable: true,
|
||||
index: true,
|
||||
},
|
||||
avgResponseTime: {
|
||||
type: Number,
|
||||
default: 0,
|
||||
},
|
||||
maxResponseTime: {
|
||||
type: Number,
|
||||
default: 0,
|
||||
},
|
||||
lastResponseTime: {
|
||||
type: Number,
|
||||
default: 0,
|
||||
},
|
||||
totalChecks: {
|
||||
type: Number,
|
||||
default: 0,
|
||||
},
|
||||
totalUpChecks: {
|
||||
type: Number,
|
||||
default: 0,
|
||||
},
|
||||
totalDownChecks: {
|
||||
type: Number,
|
||||
default: 0,
|
||||
},
|
||||
uptimePercentage: {
|
||||
type: Number,
|
||||
default: 0,
|
||||
},
|
||||
lastCheckTimestamp: {
|
||||
type: Number,
|
||||
default: 0,
|
||||
},
|
||||
timeOfLastFailure: {
|
||||
type: Number,
|
||||
default: 0,
|
||||
},
|
||||
currentStreak: { type: Number, required: false, default: 0 },
|
||||
currentStreakStatus: {
|
||||
type: String,
|
||||
required: false,
|
||||
enum: MonitorStatuses,
|
||||
},
|
||||
currentStreakStartedAt: { type: Number, required: false },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
export const MonitorStats = mongoose.model<IMonitorStats>(
|
||||
"MonitorStats",
|
||||
MonitorStatsSchema
|
||||
);
|
||||
@@ -0,0 +1,57 @@
|
||||
import mongoose, { Schema, Types } from "mongoose";
|
||||
export const ChannelTypes = ["email", "slack", "discord", "webhook"] as const;
|
||||
export type ChannelType = (typeof ChannelTypes)[number];
|
||||
|
||||
export interface INotificationChannelConfig {
|
||||
url?: string; // For webhook, slack, discord
|
||||
emailAddress?: string; // For email
|
||||
}
|
||||
|
||||
export interface INotificationChannel {
|
||||
_id: Types.ObjectId;
|
||||
orgId: Types.ObjectId;
|
||||
teamId: Types.ObjectId;
|
||||
name: string;
|
||||
type: ChannelType;
|
||||
config: INotificationChannelConfig;
|
||||
isActive: boolean;
|
||||
createdBy: Types.ObjectId;
|
||||
updatedBy: Types.ObjectId;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
const NotificationChannelConfigSchema = new Schema<INotificationChannelConfig>(
|
||||
{
|
||||
url: { type: String, required: false },
|
||||
emailAddress: { type: String, required: false },
|
||||
},
|
||||
{ _id: false, strict: "throw" }
|
||||
);
|
||||
|
||||
const NotificationChannelSchema = new Schema<INotificationChannel>(
|
||||
{
|
||||
orgId: { type: Schema.Types.ObjectId, ref: "Org", required: true },
|
||||
teamId: { type: Schema.Types.ObjectId, ref: "Team", required: true },
|
||||
name: { type: String, required: true, trim: true },
|
||||
type: {
|
||||
type: String,
|
||||
required: true,
|
||||
enum: ChannelTypes,
|
||||
},
|
||||
config: { type: NotificationChannelConfigSchema, required: true },
|
||||
isActive: { type: Boolean, required: true, default: true },
|
||||
createdBy: { type: Schema.Types.ObjectId, ref: "User", required: true },
|
||||
updatedBy: { type: Schema.Types.ObjectId, ref: "User", required: true },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
NotificationChannelSchema.index({ isActive: 1 });
|
||||
NotificationChannelSchema.index({ type: 1 });
|
||||
NotificationChannelSchema.index({ type: 1, isActive: 1 });
|
||||
|
||||
export const NotificationChannel = mongoose.model<INotificationChannel>(
|
||||
"NotificationChannel",
|
||||
NotificationChannelSchema
|
||||
);
|
||||
@@ -0,0 +1,35 @@
|
||||
import mongoose, { Schema, Document, Types } from "mongoose";
|
||||
|
||||
export interface IRecoveryToken {
|
||||
_id: string;
|
||||
userId: Types.ObjectId;
|
||||
tokenHash: string;
|
||||
expiry: Date;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
const RecoveryTokenSchema = new Schema<IRecoveryToken>(
|
||||
{
|
||||
userId: {
|
||||
type: Schema.Types.ObjectId,
|
||||
ref: "User",
|
||||
required: true,
|
||||
unique: true,
|
||||
},
|
||||
tokenHash: { type: String, required: true },
|
||||
expiry: {
|
||||
type: Date,
|
||||
default: () => new Date(Date.now() + 24 * 60 * 60 * 1000),
|
||||
expires: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
}
|
||||
);
|
||||
|
||||
export const RecoveryToken = mongoose.model<IRecoveryToken>(
|
||||
"RecoveryToken",
|
||||
RecoveryTokenSchema
|
||||
);
|
||||
@@ -0,0 +1,38 @@
|
||||
import mongoose, { Schema, Document, Types } from "mongoose";
|
||||
|
||||
export interface IStatusPage {
|
||||
_id: Types.ObjectId;
|
||||
orgId: Types.ObjectId;
|
||||
teamId: Types.ObjectId;
|
||||
name: string;
|
||||
description?: string;
|
||||
url: string;
|
||||
isPublished: boolean;
|
||||
monitors: Types.ObjectId[];
|
||||
createdBy: Types.ObjectId;
|
||||
updatedBy: Types.ObjectId;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
const StatusPageSchema = new Schema<IStatusPage>(
|
||||
{
|
||||
orgId: { type: Schema.Types.ObjectId, ref: "Org", required: true },
|
||||
teamId: { type: Schema.Types.ObjectId, ref: "Team", required: true },
|
||||
name: { type: String, required: true, trim: true },
|
||||
description: { type: String, required: false, trim: true },
|
||||
url: { type: String, required: true, trim: true },
|
||||
isPublished: { type: Boolean, required: true, default: false },
|
||||
monitors: [{ type: Schema.Types.ObjectId, ref: "Monitor", required: true }],
|
||||
createdBy: { type: Schema.Types.ObjectId, ref: "User", required: true },
|
||||
updatedBy: { type: Schema.Types.ObjectId, ref: "User", required: true },
|
||||
},
|
||||
{ timestamps: true }
|
||||
);
|
||||
|
||||
StatusPageSchema.index({ isPublished: 1 });
|
||||
|
||||
export const StatusPage = mongoose.model<IStatusPage>(
|
||||
"StatusPage",
|
||||
StatusPageSchema
|
||||
);
|
||||
Reference in new issue
Block a user