dt-common-device
Version:
A secure and robust device management library for IoT applications
208 lines (207 loc) • 9.7 kB
JavaScript
;
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
var _, done = false;
for (var i = decorators.length - 1; i >= 0; i--) {
var context = {};
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
if (kind === "accessor") {
if (result === void 0) continue;
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
if (_ = accept(result.get)) descriptor.get = _;
if (_ = accept(result.set)) descriptor.set = _;
if (_ = accept(result.init)) initializers.unshift(_);
}
else if (_ = accept(result)) {
if (kind === "field") initializers.unshift(_);
else descriptor[key] = _;
}
}
if (target) Object.defineProperty(target, contextIn.name, descriptor);
done = true;
};
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
var useValue = arguments.length > 2;
for (var i = 0; i < initializers.length; i++) {
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
}
return useValue ? value : void 0;
};
var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebhookQueueIntegration = void 0;
const typedi_1 = require("typedi");
const WebhookQueueFactory_1 = require("../services/WebhookQueueFactory");
/**
* Integration service that provides a simple interface for using webhook queues
* in existing PMS services without major refactoring
*/
let WebhookQueueIntegration = (() => {
let _classDecorators = [(0, typedi_1.Service)()];
let _classDescriptor;
let _classExtraInitializers = [];
let _classThis;
var WebhookQueueIntegration = _classThis = class {
constructor() {
this.webhookQueue = null;
this.isInitialized = false;
}
/**
* Initialize the webhook queue system
* @param config Configuration for the webhook queue
* @param webhookProcessor Function to process webhooks
*/
async initialize(config, webhookProcessor) {
if (this.isInitialized) {
return;
}
try {
// Create webhook queue service
this.webhookQueue = WebhookQueueFactory_1.WebhookQueueFactory.createWebhookQueue(config);
// Set webhook processor for all PMS types
const pmsTypes = ["CLOUDBEDS", "HOTELKEY", "STAYNTOUCH"];
// Note: We'll set processors when specific queues are created
// This is a placeholder for now
this.isInitialized = true;
console.log("Webhook queue integration initialized successfully");
}
catch (error) {
console.error("Failed to initialize webhook queue integration:", error);
throw error;
}
}
/**
* Queue a webhook for processing
* @param propertyId Property ID
* @param pmsType PMS type
* @param webhookData Webhook payload
* @returns Job ID
*/
async queueWebhook(propertyId, pmsType, webhookData) {
if (!this.isInitialized || !this.webhookQueue) {
throw new Error("Webhook queue integration not initialized");
}
try {
// Set webhook processor for this specific queue if not already set
await this.webhookQueue.setWebhookProcessor(propertyId, pmsType, this.getDefaultWebhookProcessor());
// Add webhook to queue
const jobId = await this.webhookQueue.addWebhookToQueue(propertyId, pmsType, webhookData);
console.log(`Webhook queued successfully for property: ${propertyId}, PMS: ${pmsType}, Job ID: ${jobId}`);
return jobId;
}
catch (error) {
console.error(`Failed to queue webhook for property: ${propertyId}, PMS: ${pmsType}:`, error);
throw error;
}
}
/**
* Get queue status for monitoring
* @param propertyId Property ID
* @param pmsType PMS type
*/
async getQueueStatus(propertyId, pmsType) {
if (!this.isInitialized || !this.webhookQueue) {
throw new Error("Webhook queue integration not initialized");
}
return await this.webhookQueue.getQueueStatus(propertyId, pmsType);
}
/**
* Get all queue statuses for a property
* @param propertyId Property ID
*/
async getAllQueueStatuses(propertyId) {
if (!this.isInitialized || !this.webhookQueue) {
throw new Error("Webhook queue integration not initialized");
}
return await this.webhookQueue.getAllPropertyQueueStatuses(propertyId);
}
/**
* Check if webhook queue integration is initialized
*/
isReady() {
return this.isInitialized && this.webhookQueue !== null;
}
/**
* Shutdown the webhook queue system
*/
async shutdown() {
if (this.isInitialized) {
await WebhookQueueFactory_1.WebhookQueueFactory.shutdownAll();
this.isInitialized = false;
this.webhookQueue = null;
console.log("Webhook queue integration shutdown successfully");
}
}
/**
* Get the underlying webhook queue service
*/
getWebhookQueue() {
return this.webhookQueue;
}
/**
* Default webhook processor that can be overridden
*/
getDefaultWebhookProcessor() {
return async (webhookData, pmsType) => {
console.log(`[Default Processor] Processing webhook for PMS: ${pmsType}`);
console.log("Webhook data:", webhookData);
// This should be overridden with actual processing logic
// For now, just return success
return { success: true, processedAt: new Date().toISOString() };
};
}
/**
* Set a custom webhook processor for a specific queue
* @param propertyId Property ID
* @param pmsType PMS type
* @param processor Custom processor function
*/
async setCustomProcessor(propertyId, pmsType, processor) {
if (!this.isInitialized || !this.webhookQueue) {
throw new Error("Webhook queue integration not initialized");
}
await this.webhookQueue.setWebhookProcessor(propertyId, pmsType, processor);
}
/**
* Clean up old jobs from a queue
* @param propertyId Property ID
* @param pmsType PMS type
*/
async cleanupQueue(propertyId, pmsType) {
if (!this.isInitialized || !this.webhookQueue) {
throw new Error("Webhook queue integration not initialized");
}
await this.webhookQueue.cleanupQueue(propertyId, pmsType);
}
/**
* Get queue metrics for monitoring
* @param propertyId Property ID
* @param pmsType PMS type
*/
async getQueueMetrics(propertyId, pmsType) {
if (!this.isInitialized || !this.webhookQueue) {
throw new Error("Webhook queue integration not initialized");
}
return await this.webhookQueue.getQueueMetrics(propertyId, pmsType);
}
};
__setFunctionName(_classThis, "WebhookQueueIntegration");
(() => {
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
WebhookQueueIntegration = _classThis = _classDescriptor.value;
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
__runInitializers(_classThis, _classExtraInitializers);
})();
return WebhookQueueIntegration = _classThis;
})();
exports.WebhookQueueIntegration = WebhookQueueIntegration;