dt-common-device
Version:
A secure and robust device management library for IoT applications
285 lines (284 loc) • 8.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IssueBuilder = void 0;
const issue_types_1 = require("./issue.types");
/**
* IssueBuilder - A builder pattern implementation for constructing CreateIssueData objects
*
* This builder provides a fluent interface for creating issue 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 issueData = new IssueBuilder()
* .setCategory(IssuesCategory.OPERATIONS)
* .setPropertyId("prop123")
* .setTitle("Device Maintenance Required")
* .setDescription("Device requires scheduled maintenance")
* .setEntityId("device456")
* .setEntityType(EntityType.DEVICE)
* .setPriority(IssuePriority.HIGH)
* .setAssignedTo("tech789")
* .setCreatedBy("user123")
* .setDueDate(new Date("2024-01-15"))
* .build();
*/
class IssueBuilder {
constructor() {
this.data = {};
}
/**
* Sets the issue category
*/
setCategory(category) {
this.data.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 zone ID (optional)
*/
setZoneId(zoneId) {
if (zoneId !== undefined) {
if (zoneId.trim() === "") {
throw new Error("Zone ID cannot be empty if provided");
}
this.data.zoneId = zoneId.trim();
}
return this;
}
/**
* Sets the issue type
*/
setType(type) {
this.data.type = type;
return this;
}
/**
* Sets the issue 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 issue 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 entity sub type
*/
setEntitySubType(entitySubType) {
this.data.entitySubType = entitySubType;
return this;
}
/**
* Sets the issue priority (optional, defaults to LOW)
*/
setPriority(priority) {
if (priority !== undefined) {
this.data.priority = priority;
}
return this;
}
/**
* Sets the user assigned to the issue (optional)
*/
setAssignedTo(assignedTo) {
if (assignedTo !== undefined) {
this.data.assignedTo = assignedTo.trim() || undefined;
}
return this;
}
/**
* Sets the user who created the issue (required)
*/
setCreatedBy(createdBy) {
if (!createdBy || createdBy.trim() === "") {
throw new Error("Created by user is required and cannot be empty");
}
this.data.createdBy = createdBy.trim();
return this;
}
/**
* Sets the due date (optional)
*/
setDueDate(dueDate) {
if (dueDate !== undefined) {
this.data.dueDate = dueDate;
}
return this;
}
/**
* Validates that all required fields are present
*/
validate() {
const requiredFields = [
"category",
"propertyId",
"title",
"type",
"description",
"entityId",
"entityType",
"entitySubType",
"createdBy",
];
const missingFields = requiredFields.filter((field) => !this.data[field]);
if (missingFields.length > 0) {
throw new Error(`Missing required fields: ${missingFields.join(", ")}`);
}
}
/**
* Builds and returns the CreateIssueData object
* @throws Error if required fields are missing
*/
build() {
this.validate();
// Set default priority if not provided
if (!this.data.priority) {
this.data.priority = issue_types_1.IssuePriority.LOW;
}
return this.data;
}
/**
* Resets the builder to its initial state
*/
reset() {
this.data = {};
return this;
}
static createOperationsIssue() {
return new IssueBuilder()
.setCategory(issue_types_1.IssuesCategory.OPERATIONS)
.setPriority(issue_types_1.IssuePriority.HIGH);
}
static createSecurityIssue() {
return new IssueBuilder()
.setCategory(issue_types_1.IssuesCategory.SECURITY)
.setPriority(issue_types_1.IssuePriority.CRITICAL);
}
static createEnergyIssue() {
return new IssueBuilder()
.setCategory(issue_types_1.IssuesCategory.ENERGY)
.setPriority(issue_types_1.IssuePriority.LOW);
}
/**
* Creates a device-specific issue builder
*/
static createDeviceIssue(deviceId, propertyId, type, entitySubType) {
const builder = new IssueBuilder()
.setEntityType(issue_types_1.EntityType.DEVICE)
.setEntityId(deviceId)
.setPropertyId(propertyId)
.setType(type)
.setEntitySubType(entitySubType);
return builder;
}
/**
* Creates a hub-specific issue builder
*/
static createHubIssue(hubId, propertyId, type) {
const builder = new IssueBuilder()
.setEntityType(issue_types_1.EntityType.HUB)
.setEntityId(hubId)
.setPropertyId(propertyId)
.setType(type);
return builder;
}
/**
* Creates a user-specific issue builder
*/
static createUserIssue(userId, propertyId, type) {
const builder = new IssueBuilder()
.setEntityType(issue_types_1.EntityType.USER)
.setEntityId(userId)
.setPropertyId(propertyId)
.setType(type);
return builder;
}
/**
* Creates a property-specific issue builder
*/
static createPropertyIssue(propertyId, type) {
const builder = new IssueBuilder()
.setEntityType(issue_types_1.EntityType.PROPERTY)
.setEntityId(propertyId)
.setPropertyId(propertyId)
.setType(type);
return builder;
}
/**
* Creates a maintenance issue builder
*/
static createMaintenanceIssue(propertyId, type, entityId, entityType) {
const builder = new IssueBuilder()
.setCategory(issue_types_1.IssuesCategory.OPERATIONS)
.setPropertyId(propertyId)
.setType(type)
.setPriority(issue_types_1.IssuePriority.LOW);
if (entityId)
builder.setEntityId(entityId);
if (entityType)
builder.setEntityType(entityType);
return builder;
}
/**
* Creates a high priority issue builder
*/
static createHighPriorityIssue(propertyId, type, entityId, entityType) {
const builder = new IssueBuilder()
.setCategory(issue_types_1.IssuesCategory.OPERATIONS)
.setPropertyId(propertyId)
.setType(type)
.setPriority(issue_types_1.IssuePriority.HIGH);
if (entityId)
builder.setEntityId(entityId);
if (entityType)
builder.setEntityType(entityType);
return builder;
}
static createCloudAccountAuthorizationIssue(propertyId, entityId, entityType, type) {
const builder = new IssueBuilder()
.setEntityType(entityType)
.setEntityId(entityId)
.setPropertyId(propertyId)
.setType(type);
return builder;
}
}
exports.IssueBuilder = IssueBuilder;