dt-common-device
Version:
A secure and robust device management library for IoT applications
238 lines (237 loc) • 7.35 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 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 issue priority (optional, defaults to MEDIUM)
*/
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", "description", "entityType", "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.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 issue types
*/
static createReadinessIssue() {
return new IssueBuilder()
.setCategory(issue_types_1.IssuesCategory.READINESS)
.setPriority(issue_types_1.IssuePriority.MEDIUM);
}
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) {
return new IssueBuilder()
.setEntityType(issue_types_1.EntityType.DEVICE)
.setEntityId(deviceId)
.setPropertyId(propertyId);
}
/**
* Creates a hub-specific issue builder
*/
static createHubIssue(hubId, propertyId) {
return new IssueBuilder()
.setEntityType(issue_types_1.EntityType.HUB)
.setEntityId(hubId)
.setPropertyId(propertyId);
}
/**
* Creates a user-specific issue builder
*/
static createUserIssue(userId, propertyId) {
return new IssueBuilder()
.setEntityType(issue_types_1.EntityType.USER)
.setEntityId(userId)
.setPropertyId(propertyId);
}
/**
* Creates a property-specific issue builder
*/
static createPropertyIssue(propertyId) {
return new IssueBuilder()
.setEntityType(issue_types_1.EntityType.PROPERTY)
.setEntityId(propertyId)
.setPropertyId(propertyId);
}
/**
* Creates a maintenance issue builder
*/
static createMaintenanceIssue(propertyId, entityId, entityType) {
const builder = new IssueBuilder()
.setCategory(issue_types_1.IssuesCategory.READINESS)
.setPropertyId(propertyId)
.setPriority(issue_types_1.IssuePriority.MEDIUM);
if (entityId)
builder.setEntityId(entityId);
if (entityType)
builder.setEntityType(entityType);
return builder;
}
/**
* Creates an urgent issue builder
*/
static createUrgentIssue(propertyId, entityId, entityType) {
const builder = new IssueBuilder()
.setCategory(issue_types_1.IssuesCategory.OPERATIONS)
.setPropertyId(propertyId)
.setPriority(issue_types_1.IssuePriority.URGENT);
if (entityId)
builder.setEntityId(entityId);
if (entityType)
builder.setEntityType(entityType);
return builder;
}
}
exports.IssueBuilder = IssueBuilder;