@modernlock/common 1.0.53 → 1.0.54

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.
@@ -0,0 +1,14 @@
1
+ import { Channel } from 'amqplib';
2
+ import { INotification } from '../@types/notification';
3
+ export declare class NotificationPublisher {
4
+ private channel;
5
+ private readonly exchange;
6
+ private readonly exchangeType;
7
+ constructor(channel: Channel);
8
+ /**
9
+ * Publishes a notification event to the topic exchange.
10
+ * @param routingKey The descriptive routing key (e.g., 'alert.critical.water_leak').
11
+ * @param notification The fully-formed notification object.
12
+ */
13
+ publish(routingKey: string, notification: Omit<INotification, '_id' | 'createdAt'>): Promise<void>;
14
+ }
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.NotificationPublisher = void 0;
13
+ class NotificationPublisher {
14
+ constructor(channel) {
15
+ this.exchange = 'notification_exchange'; // The single topic exchange for all notifications
16
+ this.exchangeType = 'topic';
17
+ this.channel = channel;
18
+ }
19
+ /**
20
+ * Publishes a notification event to the topic exchange.
21
+ * @param routingKey The descriptive routing key (e.g., 'alert.critical.water_leak').
22
+ * @param notification The fully-formed notification object.
23
+ */
24
+ publish(routingKey, notification) {
25
+ return __awaiter(this, void 0, void 0, function* () {
26
+ try {
27
+ yield this.channel.assertExchange(this.exchange, this.exchangeType, { durable: true });
28
+ const message = JSON.stringify(notification);
29
+ this.channel.publish(this.exchange, routingKey, Buffer.from(message));
30
+ console.log(`[NotificationPublisher] Published event with key '${routingKey}'`);
31
+ }
32
+ catch (error) {
33
+ console.error('[NotificationPublisher] Failed to publish notification:', error);
34
+ throw error;
35
+ }
36
+ });
37
+ }
38
+ }
39
+ exports.NotificationPublisher = NotificationPublisher;
package/build/index.d.ts CHANGED
@@ -11,6 +11,7 @@ export * from "./events/chat-notification-publisher";
11
11
  export * from "./events/events";
12
12
  export * from "./events/mappings";
13
13
  export * from "./events/priority-consumer";
14
+ export * from "./events/notification-publisher";
14
15
  export * from "./errors/custom-error";
15
16
  export * from "./errors/internal-server-error";
16
17
  export * from "./errors/not-found-error";
package/build/index.js CHANGED
@@ -27,6 +27,7 @@ __exportStar(require("./events/chat-notification-publisher"), exports);
27
27
  __exportStar(require("./events/events"), exports);
28
28
  __exportStar(require("./events/mappings"), exports);
29
29
  __exportStar(require("./events/priority-consumer"), exports);
30
+ __exportStar(require("./events/notification-publisher"), exports);
30
31
  __exportStar(require("./errors/custom-error"), exports);
31
32
  __exportStar(require("./errors/internal-server-error"), exports);
32
33
  __exportStar(require("./errors/not-found-error"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modernlock/common",
3
- "version": "1.0.53",
3
+ "version": "1.0.54",
4
4
  "main": "./build/index.js",
5
5
  "types": "./build/index.d.ts",
6
6
  "files": [