@rentbase/common 1.0.22 → 1.0.25

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.
@@ -1,2 +1,2 @@
1
1
  import { EventMap } from "./types";
2
- export declare const connectConsumer: <T extends keyof EventMap>(queue: T, rabbitUrl: string, onMessage: (data: EventMap[T]) => void) => Promise<void>;
2
+ export declare const connectConsumer: <T extends keyof EventMap>(queue: T, rabbitUrl: string, onMessage: (data: EventMap[T]) => void | Promise<void>) => Promise<void>;
@@ -14,17 +14,38 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.connectConsumer = void 0;
16
16
  const amqplib_1 = __importDefault(require("amqplib"));
17
+ const DEFAULT_EXCHANGE = process.env.RABBITMQ_EXCHANGE || "rentbase.events";
17
18
  const connectConsumer = (queue, rabbitUrl, onMessage) => __awaiter(void 0, void 0, void 0, function* () {
19
+ if (!rabbitUrl) {
20
+ throw new Error("RabbitMQ URL is required for connectConsumer()");
21
+ }
22
+ const routingKey = String(queue);
23
+ const serviceName = process.env.SERVICE_NAME;
24
+ const queueName = serviceName ? `${serviceName}.${routingKey}` : routingKey;
18
25
  const connection = yield amqplib_1.default.connect(rabbitUrl);
19
26
  const channel = yield connection.createChannel();
20
- yield channel.assertQueue(queue, { durable: true });
21
- channel.consume(queue, (msg) => {
22
- if (msg) {
27
+ // Topic exchange enables pub/sub (each service has its own queue).
28
+ yield channel.assertExchange(DEFAULT_EXCHANGE, "topic", { durable: true });
29
+ // Durable queue allows the service to catch up after being down.
30
+ yield channel.assertQueue(queueName, { durable: true });
31
+ yield channel.bindQueue(queueName, DEFAULT_EXCHANGE, routingKey);
32
+ // Process messages one at a time per consumer to avoid overwhelming downstream DBs.
33
+ yield channel.prefetch(1);
34
+ channel.consume(queueName, (msg) => __awaiter(void 0, void 0, void 0, function* () {
35
+ if (!msg)
36
+ return;
37
+ try {
23
38
  const data = JSON.parse(msg.content.toString());
24
- onMessage(data);
39
+ yield onMessage(data);
25
40
  channel.ack(msg);
26
41
  }
27
- });
28
- console.log(`[Consumer] Listening on ${queue}`);
42
+ catch (error) {
43
+ console.error(`[Consumer] Error processing ${routingKey}:`, error);
44
+ // Requeue so transient failures don't drop events.
45
+ // If you see a poison-message loop, switch this to `false` or add a DLQ.
46
+ channel.nack(msg, false, true);
47
+ }
48
+ }));
49
+ console.log(`[Consumer] Listening on ${queueName} (exchange=${DEFAULT_EXCHANGE}, key=${routingKey})`);
29
50
  });
30
51
  exports.connectConsumer = connectConsumer;
@@ -15,10 +15,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.publishEvent = exports.connectProducer = void 0;
16
16
  const amqplib_1 = __importDefault(require("amqplib"));
17
17
  let channel;
18
+ const DEFAULT_EXCHANGE = process.env.RABBITMQ_EXCHANGE || "rentbase.events";
18
19
  const connectProducer = (rabbitUrl, retries = 5, delay = 3000) => __awaiter(void 0, void 0, void 0, function* () {
19
20
  try {
20
21
  const connection = yield amqplib_1.default.connect(rabbitUrl);
21
22
  channel = yield connection.createChannel();
23
+ yield channel.assertExchange(DEFAULT_EXCHANGE, "topic", { durable: true });
22
24
  console.log("[RabbitMQ Producer ✅] Connected successfully");
23
25
  }
24
26
  catch (err) {
@@ -35,8 +37,22 @@ exports.connectProducer = connectProducer;
35
37
  const publishEvent = (queue, message) => __awaiter(void 0, void 0, void 0, function* () {
36
38
  if (!channel)
37
39
  throw new Error("RabbitMQ channel not initialized. Call connectProducer() first.");
38
- yield channel.assertQueue(queue, { durable: true });
39
- channel.sendToQueue(queue, Buffer.from(JSON.stringify(message)));
40
- console.log(`[Producer ➡️] Sent to ${queue}:`, message);
40
+ // Topic exchange enables pub/sub (each service can bind its own durable queue).
41
+ yield channel.assertExchange(DEFAULT_EXCHANGE, "topic", { durable: true });
42
+ const payload = Buffer.from(JSON.stringify(message));
43
+ const routingKey = String(queue);
44
+ channel.publish(DEFAULT_EXCHANGE, routingKey, payload, {
45
+ persistent: true,
46
+ contentType: "application/json",
47
+ });
48
+ // Optional legacy mode for old consumers that still read directly from a shared queue.
49
+ if (process.env.RABBITMQ_LEGACY_QUEUE === "true") {
50
+ yield channel.assertQueue(routingKey, { durable: true });
51
+ channel.sendToQueue(routingKey, payload, {
52
+ persistent: true,
53
+ contentType: "application/json",
54
+ });
55
+ }
56
+ console.log(`[Producer ➡️] Published ${routingKey} on ${DEFAULT_EXCHANGE}:`, message);
41
57
  });
42
58
  exports.publishEvent = publishEvent;
@@ -78,6 +78,7 @@ export interface LandlordApplicationCreatedEvent {
78
78
  landlordDetails: LandlordDetails;
79
79
  kycDocuments: {
80
80
  idDocument?: string;
81
+ idDocumentBack?: string;
81
82
  selfie?: string;
82
83
  proofOfAddress?: string;
83
84
  };
@@ -154,6 +155,7 @@ export interface LandlordApplicationUpdatedEvent {
154
155
  landlordDetails: LandlordDetails;
155
156
  kycDocuments: {
156
157
  idDocument?: string;
158
+ idDocumentBack?: string;
157
159
  selfie?: string;
158
160
  proofOfAddress?: string;
159
161
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rentbase/common",
3
- "version": "1.0.22",
3
+ "version": "1.0.25",
4
4
  "description": "common library for rentbase",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",