dt-common-device
Version:
A secure and robust device management library for IoT applications
229 lines (228 loc) • 7.08 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.AlertSchema = exports.AlertModel = void 0;
const mongoose_1 = __importStar(require("mongoose"));
const alert_types_1 = require("./alert.types");
// Main Alert schema
const AlertSchema = new mongoose_1.Schema({
category: {
type: [String],
enum: Object.values(alert_types_1.AlertCategory),
required: true,
validate: {
validator: function (categories) {
return categories && categories.length > 0;
},
message: "At least one category is required",
},
},
propertyId: {
type: String,
required: true,
index: true,
},
title: {
type: String,
required: true,
trim: true,
},
description: {
type: String,
required: true,
trim: true,
},
entityId: {
type: String,
index: true,
},
entityType: {
type: String,
enum: Object.values(alert_types_1.EntityType),
required: true,
index: true,
},
severity: {
type: String,
enum: Object.values(alert_types_1.AlertSeverity),
default: alert_types_1.AlertSeverity.MEDIUM,
},
isRead: {
type: Boolean,
default: false,
index: true,
},
isActive: {
type: Boolean,
default: true,
index: true,
},
isDeleted: {
type: Boolean,
default: false,
},
snoozeUntil: {
type: Date,
},
createdBy: {
type: String,
},
updatedBy: {
type: String,
},
createdAt: {
type: Date,
default: Date.now,
},
updatedAt: {
type: Date,
default: Date.now,
},
}, {
timestamps: true,
collection: "dt_alerts",
});
exports.AlertSchema = AlertSchema;
// Compound indexes to match Prisma schema
AlertSchema.index({ propertyId: 1, isActive: 1, isRead: 1 });
AlertSchema.index({ entityId: 1, entityType: 1 });
AlertSchema.index({ createdAt: 1 });
// Pre-save middleware to update the updatedAt field
AlertSchema.pre("save", function (next) {
this.updatedAt = new Date();
next();
});
// Pre-update middleware to update the updatedAt field
AlertSchema.pre(["updateOne", "findOneAndUpdate", "updateMany"], function (next) {
this.set({ updatedAt: new Date() });
next();
});
// Instance methods
AlertSchema.methods.markAsRead = function (updatedBy) {
this.isRead = true;
this.updatedBy = updatedBy;
};
AlertSchema.methods.markAsUnread = function (updatedBy) {
this.isRead = false;
this.updatedBy = updatedBy;
};
AlertSchema.methods.activate = function (updatedBy) {
this.isActive = true;
this.updatedBy = updatedBy;
};
AlertSchema.methods.deactivate = function (updatedBy) {
this.isActive = false;
this.updatedBy = updatedBy;
};
AlertSchema.methods.snooze = function (until, updatedBy) {
this.snoozeUntil = until;
this.updatedBy = updatedBy;
};
AlertSchema.methods.unsnooze = function (updatedBy) {
this.snoozeUntil = undefined;
this.updatedBy = updatedBy;
};
// Static methods
AlertSchema.statics.findByProperty = function (propertyId, includeDeleted = false) {
const query = { propertyId };
if (!includeDeleted) {
query.isDeleted = false;
}
return this.find(query).sort({ createdAt: -1 });
};
AlertSchema.statics.findByEntity = function (entityId, entityType, includeDeleted = false) {
const query = { entityId, entityType };
if (!includeDeleted) {
query.isDeleted = false;
}
return this.find(query).sort({ createdAt: -1 });
};
AlertSchema.statics.findByCategory = function (category, includeDeleted = false) {
const query = { category: { $in: [category] } };
if (!includeDeleted) {
query.isDeleted = false;
}
return this.find(query).sort({ createdAt: -1 });
};
AlertSchema.statics.findBySeverity = function (severity, includeDeleted = false) {
const query = { severity };
if (!includeDeleted) {
query.isDeleted = false;
}
return this.find(query).sort({ severity: -1, createdAt: -1 });
};
AlertSchema.statics.findActive = function (includeDeleted = false) {
const query = { isActive: true };
if (!includeDeleted) {
query.isDeleted = false;
}
return this.find(query).sort({ severity: -1, createdAt: -1 });
};
AlertSchema.statics.findUnread = function (includeDeleted = false) {
const query = { isRead: false };
if (!includeDeleted) {
query.isDeleted = false;
}
return this.find(query).sort({ severity: -1, createdAt: -1 });
};
AlertSchema.statics.findSnoozed = function (includeDeleted = false) {
const query = { snoozeUntil: { $exists: true, $ne: null } };
if (!includeDeleted) {
query.isDeleted = false;
}
return this.find(query).sort({ snoozeUntil: 1 });
};
AlertSchema.statics.findExpiredSnooze = function (includeDeleted = false) {
const query = {
snoozeUntil: { $lt: new Date() },
isActive: true,
};
if (!includeDeleted) {
query.isDeleted = false;
}
return this.find(query).sort({ snoozeUntil: 1 });
};
// Virtual for snooze status
AlertSchema.virtual("isSnoozed").get(function () {
return this.snoozeUntil && this.snoozeUntil > new Date();
});
AlertSchema.virtual("isSnoozeExpired").get(function () {
return this.snoozeUntil && this.snoozeUntil <= new Date();
});
// Ensure virtuals are serialized
AlertSchema.set("toJSON", { virtuals: true });
AlertSchema.set("toObject", { virtuals: true });
// Create and export the model
exports.AlertModel = mongoose_1.default.model("Alert", AlertSchema);