dt-common-device
Version:
A secure and robust device management library for IoT applications
259 lines (258 loc) • 7.95 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");
const issues_1 = require("../issues");
// Main Alert schema
const AlertSchema = new mongoose_1.Schema({
category: {
type: String,
enum: Object.values(alert_types_1.AlertCategory),
required: true,
},
propertyId: {
type: String,
required: true,
index: true,
},
zoneId: {
type: String,
required: false,
index: true,
},
title: {
type: String,
required: true,
trim: true,
},
description: {
type: String,
required: true,
trim: true,
},
entityId: {
type: String,
index: true,
required: true,
},
entityType: {
type: String,
enum: Object.values(alert_types_1.EntityType),
required: true,
index: true,
},
entitySubType: {
type: String,
enum: Object.values(issues_1.EntitySubType),
required: true,
index: true,
},
severity: {
type: String,
enum: Object.values(alert_types_1.AlertSeverity),
default: alert_types_1.AlertSeverity.LOW,
},
type: {
type: String,
enum: Object.values(alert_types_1.AlertType),
required: true,
},
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,
index: true,
},
createdAt: {
type: Date,
default: Date.now,
},
updatedAt: {
type: Date,
default: Date.now,
},
}, {
collection: "dt_alerts",
toJSON: { virtuals: true },
toObject: { virtuals: true },
id: false, // Disable the virtual id field since we're handling it manually
});
exports.AlertSchema = AlertSchema;
// Compound indexes to match Prisma schema
AlertSchema.index({ propertyId: 1, isActive: 1, isRead: 1 });
AlertSchema.index({ zoneId: 1, category: 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.findByCategory = function (category, includeDeleted = false) {
const query = { category: category };
if (!includeDeleted) {
query.isDeleted = false;
}
return this.find(query).sort({ 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 && new Date(this.snoozeUntil) > new Date();
});
AlertSchema.virtual("isSnoozeExpired").get(function () {
return this.snoozeUntil && new Date(this.snoozeUntil) <= new Date();
});
// Virtual for soft delete status (different name to avoid conflict)
AlertSchema.virtual("isNotDeleted").get(function () {
return !this.isDeleted;
});
// Post middleware to transform all find results to plain objects
AlertSchema.post(/^find/, function (result) {
if (!result)
return;
// Handle array results (find)
if (Array.isArray(result)) {
result.forEach((doc) => {
if (doc && typeof doc.toObject === "function") {
const plainDoc = doc.toObject();
// Transform _id to id and remove __v
plainDoc.id = plainDoc._id ? plainDoc._id.toString() : plainDoc._id;
delete plainDoc._id;
delete plainDoc.__v;
// Replace the document with plain object
Object.assign(doc, plainDoc);
}
});
}
// Handle single document results (findOne, findById, etc.)
else if (result && typeof result.toObject === "function") {
const plainDoc = result.toObject();
// Transform _id to id and remove __v
plainDoc.id = plainDoc._id ? plainDoc._id.toString() : plainDoc._id;
delete plainDoc._id;
delete plainDoc.__v;
// Replace the document with plain object
Object.assign(result, plainDoc);
}
});
// Ensure virtuals are serialized and transform to plain objects
AlertSchema.set("toJSON", {
virtuals: true,
transform: function (doc, ret) {
ret.id = ret._id ? ret._id.toString() : ret._id;
delete ret._id;
delete ret.__v;
return ret;
},
});
AlertSchema.set("toObject", {
virtuals: true,
transform: function (doc, ret) {
ret.id = ret._id ? ret._id.toString() : ret._id;
delete ret._id;
delete ret.__v;
return ret;
},
});
// Create and export the model
exports.AlertModel = mongoose_1.default.model("Alert", AlertSchema);