dt-common-device 13.0.23 → 13.0.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -20,6 +20,7 @@ export declare function getLogger(): ILogger;
20
20
  export declare function getAdminPostgresDbUri(): string;
21
21
  export declare function getAccessPostgresDbUri(): string;
22
22
  export declare function getPmsPostgresDbUri(): string;
23
+ export declare function getWebhookPostgresDbUri(): string;
23
24
  export declare function getMongoUri(): string;
24
25
  export declare function getDTApiKey(): string;
25
26
  /**
@@ -52,6 +52,7 @@ exports.getLogger = getLogger;
52
52
  exports.getAdminPostgresDbUri = getAdminPostgresDbUri;
53
53
  exports.getAccessPostgresDbUri = getAccessPostgresDbUri;
54
54
  exports.getPmsPostgresDbUri = getPmsPostgresDbUri;
55
+ exports.getWebhookPostgresDbUri = getWebhookPostgresDbUri;
55
56
  exports.getMongoUri = getMongoUri;
56
57
  exports.getDTApiKey = getDTApiKey;
57
58
  exports.getRedisDbHostAndPort = getRedisDbHostAndPort;
@@ -299,6 +300,14 @@ function getPmsPostgresDbUri() {
299
300
  }
300
301
  return fullUri;
301
302
  }
303
+ function getWebhookPostgresDbUri() {
304
+ const fullUri = process.env[db_keys?.webhook];
305
+ if (!fullUri) {
306
+ getConfig().LOGGER.error("WEBHOOK_DB_URI must be set in environment variables or .env file");
307
+ throw new Error("dt-common-device: WEBHOOK_DB_URI must be set in environment variables or .env file");
308
+ }
309
+ return fullUri;
310
+ }
302
311
  function getMongoUri() {
303
312
  const fullUri = process.env.MONGODB_URI;
304
313
  if (!fullUri) {
@@ -45,6 +45,7 @@ export declare const CONFIG_KEYS: {
45
45
  pms: string;
46
46
  admin: string;
47
47
  access: string;
48
+ webhook: string;
48
49
  };
49
50
  };
50
51
  MIGRATION: {
@@ -88,12 +88,14 @@ exports.CONFIG_KEYS = {
88
88
  "HEARTBEAT_SQS_URL",
89
89
  "CRONICLE_ENDPOINT",
90
90
  "CRONICLE_API_KEY",
91
+ "WEBHOOK_DB_URI",
91
92
  ],
92
93
  INTERNAL_EVENT_HANDLER: true,
93
94
  db_keys: {
94
95
  pms: "DATABASE_URL",
95
96
  admin: "ADMIN_DB_URI",
96
97
  access: "ACCESS_DB_URI",
98
+ webhook: "WEBHOOK_DB_URI",
97
99
  },
98
100
  },
99
101
  MIGRATION: {
package/dist/db/db.d.ts CHANGED
@@ -2,4 +2,5 @@ import { Pool } from "pg";
2
2
  export declare function getPostgresClient(): Pool;
3
3
  export declare function getAccessPostgresClient(): Pool;
4
4
  export declare function getPmsPostgresClient(): Pool;
5
+ export declare function getWebhookPostgresClient(): Pool;
5
6
  export declare const connectDatabase: () => Promise<void>;
package/dist/db/db.js CHANGED
@@ -7,6 +7,7 @@ exports.connectDatabase = void 0;
7
7
  exports.getPostgresClient = getPostgresClient;
8
8
  exports.getAccessPostgresClient = getAccessPostgresClient;
9
9
  exports.getPmsPostgresClient = getPmsPostgresClient;
10
+ exports.getWebhookPostgresClient = getWebhookPostgresClient;
10
11
  const pg_1 = require("pg");
11
12
  const config_1 = require("../config/config");
12
13
  const mongoose_1 = __importDefault(require("mongoose"));
@@ -40,6 +41,16 @@ function getPmsPostgresClient() {
40
41
  }
41
42
  return pool;
42
43
  }
44
+ function getWebhookPostgresClient() {
45
+ const URI = (0, config_1.getWebhookPostgresDbUri)();
46
+ let pool = null;
47
+ if (!pool) {
48
+ pool = new pg_1.Pool({
49
+ connectionString: URI,
50
+ });
51
+ }
52
+ return pool;
53
+ }
43
54
  const connectDatabase = async () => {
44
55
  try {
45
56
  const URI = (0, config_1.getMongoUri)();
@@ -27,5 +27,6 @@ export declare enum ConnectionProvider {
27
27
  Devicethread = "Devicethread",
28
28
  Dormakaba = "Dormakaba",
29
29
  Dusaw = "Dusaw",
30
- Lockly = "Lockly"
30
+ Lockly = "Lockly",
31
+ Sifely = "Sifely"
31
32
  }
@@ -18,4 +18,5 @@ var ConnectionProvider;
18
18
  ConnectionProvider["Dormakaba"] = "Dormakaba";
19
19
  ConnectionProvider["Dusaw"] = "Dusaw";
20
20
  ConnectionProvider["Lockly"] = "Lockly";
21
+ ConnectionProvider["Sifely"] = "Sifely";
21
22
  })(ConnectionProvider || (exports.ConnectionProvider = ConnectionProvider = {}));
package/dist/index.d.ts CHANGED
@@ -21,3 +21,4 @@ export * from "./webhookQueue";
21
21
  export * from "./copilotQueue";
22
22
  export * from "./serviceQueue";
23
23
  export * from "./notificationQueue";
24
+ export * from "./webhooks";
package/dist/index.js CHANGED
@@ -67,3 +67,5 @@ __exportStar(require("./copilotQueue"), exports);
67
67
  __exportStar(require("./serviceQueue"), exports);
68
68
  // Export Notification Queue (BullMQ for guaranteed delivery)
69
69
  __exportStar(require("./notificationQueue"), exports);
70
+ // Export Webhooks
71
+ __exportStar(require("./webhooks"), exports);
@@ -0,0 +1 @@
1
+ export * from "./webhook.service";
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./webhook.service"), exports);
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Payload structure for webhook messages
3
+ * Can be extended with specific payload types for different event types
4
+ */
5
+ export interface IWebhookMessagePayload {
6
+ [key: string]: any;
7
+ }
8
+ /**
9
+ * Request DTO for creating a webhook message
10
+ */
11
+ export interface ICreateWebhookMessageRequest {
12
+ appId: string;
13
+ eventType: string;
14
+ payload: IWebhookMessagePayload;
15
+ }
16
+ export interface ICreateWebhookAppRequest {
17
+ name: string;
18
+ uid: string;
19
+ metadata?: Record<string, any>;
20
+ }
21
+ export interface IRegisterWebhookUrlRequest {
22
+ appId: string;
23
+ url: string;
24
+ }
25
+ export interface IWebhookAppResponse {
26
+ id: string;
27
+ uid: string;
28
+ name: string;
29
+ metadata: Record<string, any>;
30
+ createdAt: string;
31
+ updatedAt: string;
32
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,6 @@
1
+ import { IWebhookAppResponse } from "./interface/webhook.interface";
2
+ export declare class WebHookRepository {
3
+ private readonly postgres;
4
+ constructor();
5
+ getWebhookAppByUid(uid: string): Promise<IWebhookAppResponse | null>;
6
+ }
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
3
+ function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
4
+ var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
5
+ var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
6
+ var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
7
+ var _, done = false;
8
+ for (var i = decorators.length - 1; i >= 0; i--) {
9
+ var context = {};
10
+ for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
11
+ for (var p in contextIn.access) context.access[p] = contextIn.access[p];
12
+ context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
13
+ var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
14
+ if (kind === "accessor") {
15
+ if (result === void 0) continue;
16
+ if (result === null || typeof result !== "object") throw new TypeError("Object expected");
17
+ if (_ = accept(result.get)) descriptor.get = _;
18
+ if (_ = accept(result.set)) descriptor.set = _;
19
+ if (_ = accept(result.init)) initializers.unshift(_);
20
+ }
21
+ else if (_ = accept(result)) {
22
+ if (kind === "field") initializers.unshift(_);
23
+ else descriptor[key] = _;
24
+ }
25
+ }
26
+ if (target) Object.defineProperty(target, contextIn.name, descriptor);
27
+ done = true;
28
+ };
29
+ var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
30
+ var useValue = arguments.length > 2;
31
+ for (var i = 0; i < initializers.length; i++) {
32
+ value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
33
+ }
34
+ return useValue ? value : void 0;
35
+ };
36
+ var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
37
+ if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
38
+ return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
39
+ };
40
+ Object.defineProperty(exports, "__esModule", { value: true });
41
+ exports.WebHookRepository = void 0;
42
+ const typedi_1 = require("typedi");
43
+ const db_1 = require("../db");
44
+ let WebHookRepository = (() => {
45
+ let _classDecorators = [(0, typedi_1.Service)()];
46
+ let _classDescriptor;
47
+ let _classExtraInitializers = [];
48
+ let _classThis;
49
+ var WebHookRepository = _classThis = class {
50
+ constructor() {
51
+ this.postgres = (0, db_1.getWebhookPostgresClient)();
52
+ }
53
+ async getWebhookAppByUid(uid) {
54
+ const query = `
55
+ SELECT * FROM application WHERE uid = $1
56
+ `;
57
+ const result = await this.postgres.query(query, [uid]);
58
+ if (result.rows.length > 0) {
59
+ return result.rows[0];
60
+ }
61
+ return null;
62
+ }
63
+ };
64
+ __setFunctionName(_classThis, "WebHookRepository");
65
+ (() => {
66
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
67
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
68
+ WebHookRepository = _classThis = _classDescriptor.value;
69
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
70
+ __runInitializers(_classThis, _classExtraInitializers);
71
+ })();
72
+ return WebHookRepository = _classThis;
73
+ })();
74
+ exports.WebHookRepository = WebHookRepository;
@@ -0,0 +1,11 @@
1
+ import { ApplicationOut, EndpointOut, MessageOut } from "svix";
2
+ import { ICreateWebhookMessageRequest, ICreateWebhookAppRequest, IRegisterWebhookUrlRequest, IWebhookAppResponse } from "./interface/webhook.interface";
3
+ export declare class WebhookService {
4
+ private svix;
5
+ private readonly webhookRepository;
6
+ constructor();
7
+ createMessage(request: ICreateWebhookMessageRequest): Promise<MessageOut>;
8
+ createWebhookApp(request: ICreateWebhookAppRequest): Promise<ApplicationOut>;
9
+ registerUrlForWebhook(request: IRegisterWebhookUrlRequest): Promise<EndpointOut>;
10
+ getWebhookAppByUid(uid: string): Promise<IWebhookAppResponse | null>;
11
+ }
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
3
+ function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
4
+ var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
5
+ var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
6
+ var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
7
+ var _, done = false;
8
+ for (var i = decorators.length - 1; i >= 0; i--) {
9
+ var context = {};
10
+ for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
11
+ for (var p in contextIn.access) context.access[p] = contextIn.access[p];
12
+ context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
13
+ var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
14
+ if (kind === "accessor") {
15
+ if (result === void 0) continue;
16
+ if (result === null || typeof result !== "object") throw new TypeError("Object expected");
17
+ if (_ = accept(result.get)) descriptor.get = _;
18
+ if (_ = accept(result.set)) descriptor.set = _;
19
+ if (_ = accept(result.init)) initializers.unshift(_);
20
+ }
21
+ else if (_ = accept(result)) {
22
+ if (kind === "field") initializers.unshift(_);
23
+ else descriptor[key] = _;
24
+ }
25
+ }
26
+ if (target) Object.defineProperty(target, contextIn.name, descriptor);
27
+ done = true;
28
+ };
29
+ var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
30
+ var useValue = arguments.length > 2;
31
+ for (var i = 0; i < initializers.length; i++) {
32
+ value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
33
+ }
34
+ return useValue ? value : void 0;
35
+ };
36
+ var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
37
+ if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
38
+ return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
39
+ };
40
+ var __importDefault = (this && this.__importDefault) || function (mod) {
41
+ return (mod && mod.__esModule) ? mod : { "default": mod };
42
+ };
43
+ Object.defineProperty(exports, "__esModule", { value: true });
44
+ exports.WebhookService = void 0;
45
+ const svix_1 = require("svix");
46
+ const node_crypto_1 = __importDefault(require("node:crypto"));
47
+ const typedi_1 = require("typedi");
48
+ const webhook_repository_1 = require("./webhook.repository");
49
+ let WebhookService = (() => {
50
+ let _classDecorators = [(0, typedi_1.Service)()];
51
+ let _classDescriptor;
52
+ let _classExtraInitializers = [];
53
+ let _classThis;
54
+ var WebhookService = _classThis = class {
55
+ constructor() {
56
+ const token = process.env.SVIX_TOKEN || "";
57
+ const serverUrl = process.env.SVIX_SERVER_URL || "";
58
+ this.svix = new svix_1.Svix(token, {
59
+ serverUrl,
60
+ });
61
+ this.webhookRepository = typedi_1.Container.get(webhook_repository_1.WebHookRepository);
62
+ }
63
+ // -----------------------------
64
+ // Send Webhook Message
65
+ // -----------------------------
66
+ async createMessage(request) {
67
+ try {
68
+ const { appId, eventType, payload } = request;
69
+ // Generate a unique eventId using timestamp and random string if not provided
70
+ const uniqueId = `evt_${node_crypto_1.default.randomUUID().substring(0, 12)}`;
71
+ const response = await this.svix.message.create(appId, {
72
+ eventType,
73
+ eventId: uniqueId,
74
+ payload,
75
+ });
76
+ return response;
77
+ }
78
+ catch (error) {
79
+ console.error("Error creating message:", error);
80
+ throw error;
81
+ }
82
+ }
83
+ // -----------------------------
84
+ // Create Webhook App
85
+ // -----------------------------
86
+ async createWebhookApp(request) {
87
+ try {
88
+ const { name, uid, metadata } = request;
89
+ const response = await this.svix.application.create({
90
+ name,
91
+ uid,
92
+ metadata,
93
+ });
94
+ return response;
95
+ }
96
+ catch (error) {
97
+ console.error("Error registering url for webhook:", error);
98
+ throw error;
99
+ }
100
+ }
101
+ // -----------------------------
102
+ // Register Webhook URL (Endpoint)
103
+ // -----------------------------
104
+ async registerUrlForWebhook(request) {
105
+ try {
106
+ const { appId, url } = request;
107
+ const response = await this.svix.endpoint.create(appId, {
108
+ url,
109
+ });
110
+ return response;
111
+ }
112
+ catch (error) {
113
+ console.error("Error registering url for webhook:", error);
114
+ throw error;
115
+ }
116
+ }
117
+ // -------------------------------------
118
+ // Get Webhook App by UID from the table
119
+ // -------------------------------------
120
+ async getWebhookAppByUid(uid) {
121
+ try {
122
+ const application = await this.webhookRepository.getWebhookAppByUid(uid);
123
+ return application;
124
+ }
125
+ catch (error) {
126
+ console.error("Error getting webhook app by uid:", error.message);
127
+ throw error;
128
+ }
129
+ }
130
+ };
131
+ __setFunctionName(_classThis, "WebhookService");
132
+ (() => {
133
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
134
+ __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
135
+ WebhookService = _classThis = _classDescriptor.value;
136
+ if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
137
+ __runInitializers(_classThis, _classExtraInitializers);
138
+ })();
139
+ return WebhookService = _classThis;
140
+ })();
141
+ exports.WebhookService = WebhookService;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dt-common-device",
3
- "version": "13.0.23",
3
+ "version": "13.0.26",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [
@@ -10,9 +10,9 @@
10
10
  ],
11
11
  "scripts": {
12
12
  "build": "tsc",
13
- "patch": "npm version patch && npm run build && npm publish && git push",
14
- "minor": "npm version minor && npm run build && npm publish && git push",
15
- "major": "npm version major && npm run build && npm publish && git push",
13
+ "patch": "npm login && npm version patch && npm run build && npm publish && git push",
14
+ "minor": "npm login && npm version minor && npm run build && npm publish && git push",
15
+ "major": "npm login && npm version major && npm run build && npm publish && git push",
16
16
  "security:audit": "npm audit --audit-level=moderate",
17
17
  "security:fix": "npm audit fix",
18
18
  "security:check": "npm audit && npm outdated",
@@ -51,6 +51,7 @@
51
51
  "lodash": "^4.17.21",
52
52
  "mongoose": "8.17.1",
53
53
  "pg": "8.16.3",
54
+ "svix": "1.84.1",
54
55
  "typedi": "0.10.0"
55
56
  }
56
57
  }