dt-common-device
Version:
A secure and robust device management library for IoT applications
186 lines (185 loc) • 5.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AlertBuilder = void 0;
const alert_types_1 = require("./alert.types");
/**
* AlertBuilder - A builder pattern implementation for constructing CreateAlertData objects
*
* This builder provides a fluent interface for creating alert data with proper validation
* and default values. It follows the Builder pattern which is a standard design pattern
* in TypeScript for constructing complex objects.
*
* Usage example:
* const alertData = new AlertBuilder()
* .setCategory(AlertCategory.OPERATIONS)
* .setPropertyId("prop123")
* .setTitle("Device Offline")
* .setDescription("Device has been offline for more than 5 minutes")
* .setEntityId("device456")
* .setEntityType(EntityType.DEVICE)
* .setSeverity(AlertSeverity.HIGH)
* .setCreatedBy("user789")
* .build();
*/
class AlertBuilder {
constructor() {
this.data = {};
}
/**
* Sets the alert category
*/
setCategory(category) {
this.data.category = Array.isArray(category) ? category : [category];
return this;
}
/**
* Sets the property ID
*/
setPropertyId(propertyId) {
if (!propertyId || propertyId.trim() === "") {
throw new Error("Property ID is required and cannot be empty");
}
this.data.propertyId = propertyId;
return this;
}
/**
* Sets the alert title
*/
setTitle(title) {
if (!title || title.trim() === "") {
throw new Error("Title is required and cannot be empty");
}
this.data.title = title.trim();
return this;
}
/**
* Sets the alert description
*/
setDescription(description) {
if (!description || description.trim() === "") {
throw new Error("Description is required and cannot be empty");
}
this.data.description = description.trim();
return this;
}
/**
* Sets the entity ID (optional)
*/
setEntityId(entityId) {
if (entityId !== undefined) {
this.data.entityId = entityId.trim() || undefined;
}
return this;
}
/**
* Sets the entity type
*/
setEntityType(entityType) {
this.data.entityType = entityType;
return this;
}
/**
* Sets the alert severity (optional, defaults to MEDIUM)
*/
setSeverity(severity) {
if (severity !== undefined) {
this.data.severity = severity;
}
return this;
}
/**
* Sets the user who created the alert (optional)
*/
setCreatedBy(createdBy) {
if (createdBy !== undefined) {
this.data.createdBy = createdBy.trim() || undefined;
}
return this;
}
/**
* Sets the snooze until date (optional)
*/
setSnoozeUntil(snoozeUntil) {
if (snoozeUntil !== undefined) {
this.data.snoozeUntil = snoozeUntil;
}
return this;
}
/**
* Validates that all required fields are present
*/
validate() {
const requiredFields = [
"category",
"propertyId",
"title",
"description",
"entityType",
];
const missingFields = requiredFields.filter((field) => !this.data[field]);
if (missingFields.length > 0) {
throw new Error(`Missing required fields: ${missingFields.join(", ")}`);
}
}
/**
* Builds and returns the CreateAlertData object
* @throws Error if required fields are missing
*/
build() {
this.validate();
// Set default severity if not provided
if (!this.data.severity) {
this.data.severity = alert_types_1.AlertSeverity.MEDIUM;
}
return this.data;
}
/**
* Resets the builder to its initial state
*/
reset() {
this.data = {};
return this;
}
/**
* Creates a new builder instance with predefined values for common alert types
*/
static createReadinessAlert() {
return new AlertBuilder()
.setCategory(alert_types_1.AlertCategory.READINESS)
.setSeverity(alert_types_1.AlertSeverity.MEDIUM);
}
static createOperationsAlert() {
return new AlertBuilder()
.setCategory(alert_types_1.AlertCategory.OPERATIONS)
.setSeverity(alert_types_1.AlertSeverity.HIGH);
}
static createSecurityAlert() {
return new AlertBuilder()
.setCategory(alert_types_1.AlertCategory.SECURITY)
.setSeverity(alert_types_1.AlertSeverity.CRITICAL);
}
static createEnergyAlert() {
return new AlertBuilder()
.setCategory(alert_types_1.AlertCategory.ENERGY)
.setSeverity(alert_types_1.AlertSeverity.LOW);
}
/**
* Creates a device-specific alert builder
*/
static createDeviceAlert(deviceId, propertyId) {
return new AlertBuilder()
.setEntityType(alert_types_1.EntityType.DEVICE)
.setEntityId(deviceId)
.setPropertyId(propertyId);
}
/**
* Creates a hub-specific alert builder
*/
static createHubAlert(hubId, propertyId) {
return new AlertBuilder()
.setEntityType(alert_types_1.EntityType.HUB)
.setEntityId(hubId)
.setPropertyId(propertyId);
}
}
exports.AlertBuilder = AlertBuilder;