@bolt.tech/kafka-utils 1.0.8 → 1.0.10

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.
package/build/app.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- declare const initialiseNotification: any, sendNotification: any;
1
+ declare const initialiseNotification: any, sendSmsNotifications: any;
2
2
  declare const KafkaServer: any;
3
3
  declare const notificationMessage: {
4
4
  user: {
package/build/app.js CHANGED
@@ -7,7 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- const { initialiseNotification, sendNotification } = require('./index');
10
+ const { initialiseNotification, sendSmsNotifications } = require('./index');
11
11
  const KafkaServer = require('./kafkaServer');
12
12
  // Example notification message
13
13
  const notificationMessage = {
@@ -49,7 +49,7 @@ const notificationMessage = {
49
49
  console.log('🟢 Kafka Notification Utility Initialised.');
50
50
  // Step 2: Send a notification
51
51
  console.log('🟡 Sending Notification...');
52
- yield sendNotification(notificationMessage.user.phoneNumbers[0], notificationMessage);
52
+ yield sendSmsNotifications(notificationMessage.user, notificationMessage.message, notificationMessage.smsObj);
53
53
  console.log('🟢 Notification Sent Successfully.');
54
54
  // Clean up (if any shutdown handling or disconnect required)
55
55
  const kafkaServerInstance = new KafkaServer();
package/build/index.d.ts CHANGED
@@ -1,3 +1,7 @@
1
+ import { IAttachment, IMessage, IOneSignalPayload, ISmsObj, IUser } from './store/interfaces/notification.interface';
1
2
  import { notificationType } from './store/types/notification.type';
2
3
  export declare function initialiseNotification(paramStorePath: "dev" | "prod", nodeEnvLocal: "TRUE" | "FALSE", serviceName: string, notificationType: notificationType): Promise<void>;
3
- export declare function sendNotification(userId: string, message: any): Promise<void>;
4
+ export declare function sendEmailNotifications(user: IUser, message: IMessage, attachment?: IAttachment): Promise<void>;
5
+ export declare function sendSmsNotifications(user: IUser, message: IMessage, smsObj: ISmsObj): Promise<void>;
6
+ export declare function sendPushNotification(user: IUser, message: IMessage, appId: string, oneSignalPayload: IOneSignalPayload): Promise<void>;
7
+ export declare function sendInAppNotification(user: IUser, message: IMessage, appId: string): Promise<void>;
package/build/index.js CHANGED
@@ -10,12 +10,19 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.initialiseNotification = initialiseNotification;
13
- exports.sendNotification = sendNotification;
13
+ exports.sendEmailNotifications = sendEmailNotifications;
14
+ exports.sendSmsNotifications = sendSmsNotifications;
15
+ exports.sendPushNotification = sendPushNotification;
16
+ exports.sendInAppNotification = sendInAppNotification;
14
17
  const getTopic = require('./utils/kafka/topicMap.kafka');
15
18
  let currentNotificationType;
16
19
  function initialiseNotification(paramStorePath, nodeEnvLocal, serviceName, notificationType) {
17
20
  return __awaiter(this, void 0, void 0, function* () {
18
21
  try {
22
+ registry_util_1.SchemaRegistryS.connect(process.env.SCHEMA_REGISTRY_URL);
23
+ console.log('🔵 Connecting to Schema Registry...');
24
+ registry_util_1.SchemaRegistryS.getInstance();
25
+ console.log('✅ Schema Registry connected.');
19
26
  currentNotificationType = notificationType;
20
27
  const KafkaServer = require('./kafkaServer');
21
28
  const kafkaInstance = new KafkaServer();
@@ -33,26 +40,104 @@ function initialiseNotification(paramStorePath, nodeEnvLocal, serviceName, notif
33
40
  const notificationSchema_registry_1 = require("./utils/schemaRegistry/notificationSchema.registry");
34
41
  const registry_util_1 = require("./utils/schemaRegistry/registry.util");
35
42
  const publishToKafka = require('./utils/kafka/publisher.kafka');
36
- function sendNotification(userId, message) {
43
+ /* initialize schema registry if not initialized */
44
+ function initSchemaRegistry() {
45
+ return __awaiter(this, void 0, void 0, function* () {
46
+ console.log('🔵 Connecting to Schema Registry...');
47
+ registry_util_1.SchemaRegistryS.connect(process.env.SCHEMA_REGISTRY_URL);
48
+ registry_util_1.SchemaRegistryS.getInstance();
49
+ console.log('✅ Schema Registry connected successfully.');
50
+ });
51
+ }
52
+ /* publish notifications to kafka */
53
+ function sendNotificationToKafka(userId, payload) {
37
54
  return __awaiter(this, void 0, void 0, function* () {
38
55
  try {
56
+ yield initSchemaRegistry();
39
57
  const instance = notificationSchema_registry_1.NotificationSchema.getInstance();
40
58
  if (!instance) {
41
59
  throw new Error("Notification schema is not initialised or Schema Registry is not connected.");
42
60
  }
43
- if (!registry_util_1.SchemaRegistryS.isConnected()) {
44
- yield registry_util_1.SchemaRegistryS.connect(process.env.SCHEMA_REGISTRY_URL);
45
- }
46
61
  if (!notificationSchema_registry_1.NotificationSchema) {
47
62
  throw new Error("Notification schema is not initialised");
48
63
  }
49
64
  const topic = getTopic(currentNotificationType);
50
- let key = userId;
51
65
  const schemaId = yield instance.getSchemaId();
52
- yield publishToKafka(topic, schemaId, { key, value: message });
66
+ yield publishToKafka(topic, schemaId, { key: userId, value: payload });
67
+ console.log(`✅ Notification sent to user: ${userId}`);
53
68
  }
54
- catch (e) {
55
- console.log("🔴 kafka-utils: sendNotification", e);
69
+ catch (error) {
70
+ console.log("🔴 kafka-utils: sendNotificationToKafka", error);
71
+ }
72
+ });
73
+ }
74
+ /* send emaail notifications to users */
75
+ function sendEmailNotifications(user, message, attachment) {
76
+ return __awaiter(this, void 0, void 0, function* () {
77
+ if (user.emailIds.length === 0) {
78
+ console.log("No email addresses provided");
79
+ return;
80
+ }
81
+ const basePayload = {
82
+ user: Object.assign(Object.assign({}, user), { emailIds: user.emailIds }),
83
+ message,
84
+ Attachment: attachment || {}
85
+ };
86
+ for (const emailId of user.emailIds) {
87
+ const payload = Object.assign(Object.assign({}, basePayload), { user: Object.assign(Object.assign({}, basePayload.user), { emailIds: [emailId] }) });
88
+ yield sendNotificationToKafka(emailId, payload);
89
+ }
90
+ });
91
+ }
92
+ /* send sms notifications to users */
93
+ function sendSmsNotifications(user, message, smsObj) {
94
+ return __awaiter(this, void 0, void 0, function* () {
95
+ if (user.phoneNumbers.length === 0) {
96
+ console.log("No phone numbers provided");
97
+ return;
98
+ }
99
+ const basePayload = {
100
+ user: Object.assign(Object.assign({}, user), { phoneNumbers: user.phoneNumbers }),
101
+ message,
102
+ smsObj,
103
+ };
104
+ for (const phoneNumber of user.phoneNumbers) {
105
+ const payload = Object.assign(Object.assign({}, basePayload), { user: Object.assign(Object.assign({}, basePayload.user), { phoneNumbers: [phoneNumber] }) });
106
+ yield sendNotificationToKafka(phoneNumber, payload);
107
+ }
108
+ });
109
+ }
110
+ /* send push notifications to users */
111
+ function sendPushNotification(user, message, appId, oneSignalPayload) {
112
+ return __awaiter(this, void 0, void 0, function* () {
113
+ if (!user.pushNotificationUserId) {
114
+ console.log("No push notification user ID provided");
115
+ return;
116
+ }
117
+ const payload = {
118
+ user: Object.assign(Object.assign({}, user), { pushNotificationUserId: user.pushNotificationUserId }),
119
+ message,
120
+ appId,
121
+ smsObj: {},
122
+ oneSignalPayload,
123
+ };
124
+ yield sendNotificationToKafka(user.pushNotificationUserId, payload);
125
+ });
126
+ }
127
+ /* send in-app notifications to users */
128
+ function sendInAppNotification(user, message, appId) {
129
+ return __awaiter(this, void 0, void 0, function* () {
130
+ if (!user.inAppNotificationUserId) {
131
+ console.log("No in-app notification user ID provided");
132
+ return;
56
133
  }
134
+ const payload = {
135
+ user: Object.assign(Object.assign({}, user), { inAppNotificationUserId: user.inAppNotificationUserId }),
136
+ message,
137
+ appId,
138
+ smsObj: {},
139
+ oneSignalPayload: {},
140
+ };
141
+ yield sendNotificationToKafka(user.inAppNotificationUserId, payload);
57
142
  });
58
143
  }
@@ -64,11 +64,11 @@ class KafkaServer {
64
64
  const kafka = config_kafka_1.KafkaConfig.getConfig(kafkaConnectionDetails, serviceName);
65
65
  // Connect to Kafka
66
66
  yield producer_kafka_1.KafkaProducer.connect(kafka);
67
- // Connect to Schema Registry
68
- registry_util_1.SchemaRegistryS.connect(process.env.SCHEMA_REGISTRY_URL);
67
+ // Connect to Schema Registry and wait for completion
68
+ console.log('🔵 Connecting to Schema Registry...');
69
+ yield registry_util_1.SchemaRegistryS.connect(process.env.SCHEMA_REGISTRY_URL);
69
70
  this.schemaRegistry = registry_util_1.SchemaRegistryS.getInstance();
70
- console.log(`Schema url:- ${process.env.SCHEMA_REGISTRY_URL}`);
71
- console.log(this.schemaRegistry = registry_util_1.SchemaRegistryS.getInstance());
71
+ console.log('✅ Schema Registry connected.');
72
72
  // Register Notification schema
73
73
  this.notificationSchema = notificationSchema_registry_1.NotificationSchema.getInstance();
74
74
  yield this.notificationSchema.register(notificationType);
@@ -1,35 +1,35 @@
1
1
  export interface IUser {
2
- inAppNotificationUserId: string;
3
- pushNotificationUserId: string;
4
- emailIds: string[];
5
- phoneNumbers: string[];
2
+ inAppNotificationUserId?: string;
3
+ pushNotificationUserId?: string;
4
+ emailIds?: string[];
5
+ phoneNumbers?: string[];
6
6
  }
7
7
  export interface IMessage {
8
- body: string;
9
- subject: string;
10
- priority: number;
11
- attachments: Uint8Array[];
12
- html: string;
8
+ body?: string;
9
+ subject?: string;
10
+ priority?: number;
11
+ attachments?: Uint8Array[];
12
+ html?: string;
13
13
  }
14
14
  export interface ISmsObj {
15
- smsType: string;
16
- templateId: string;
15
+ smsType?: string;
16
+ templateId?: string;
17
17
  }
18
18
  export interface IOneSignalPayload {
19
- alert: string;
20
- sos: boolean;
21
- vin: string;
19
+ alert?: string;
20
+ sos?: boolean;
21
+ vin?: string;
22
22
  }
23
23
  export interface IAttachment {
24
- filename: string;
25
- filetype: string;
26
- content: Uint8Array;
24
+ filename?: string;
25
+ filetype?: string;
26
+ content?: Uint8Array;
27
27
  }
28
28
  export interface INotification {
29
- user: IUser;
30
- message: IMessage;
31
- appId: string;
32
- smsObj: ISmsObj;
33
- oneSignalPayload: IOneSignalPayload;
34
- Attachment: IAttachment;
29
+ user?: IUser;
30
+ message?: IMessage;
31
+ appId?: string;
32
+ smsObj?: ISmsObj;
33
+ oneSignalPayload?: IOneSignalPayload;
34
+ Attachment?: IAttachment;
35
35
  }
@@ -14,6 +14,8 @@ const producer_kafka_1 = require("./producer.kafka");
14
14
  function publishToKafka(topic, schemaId, message) {
15
15
  return __awaiter(this, void 0, void 0, function* () {
16
16
  try {
17
+ registry_util_1.SchemaRegistryS.connect(process.env.SCHEMA_REGISTRY_URL);
18
+ registry_util_1.SchemaRegistryS.getInstance();
17
19
  const encodedMessage = yield registry_util_1.SchemaRegistryS.getInstance().encode(schemaId, message.value);
18
20
  const payload = { topic, messages: [{ key: message.key, value: encodedMessage }] };
19
21
  const producer = yield producer_kafka_1.KafkaProducer.getProducer();
@@ -3,10 +3,11 @@ export declare class NotificationSchema {
3
3
  private static instance;
4
4
  private registry;
5
5
  private registeredSchemaId;
6
+ private notificationType;
6
7
  private constructor();
7
8
  static getInstance(): NotificationSchema;
8
9
  register(notificationType: notificationType): Promise<void>;
9
- getSchemaId(): number;
10
+ getSchemaId(): Promise<number>;
10
11
  /**
11
12
  * Reads schema from protobuf file and stringifies it. Will throw an error in case proto file has multiple message
12
13
  * @returns Stringified schema
@@ -46,7 +46,11 @@ class NotificationSchema {
46
46
  });
47
47
  }
48
48
  getSchemaId() {
49
- return this.registeredSchemaId;
49
+ return __awaiter(this, void 0, void 0, function* () {
50
+ if (!this.registeredSchemaId)
51
+ yield this.register(this.notificationType);
52
+ return this.registeredSchemaId;
53
+ });
50
54
  }
51
55
  /**
52
56
  * Reads schema from protobuf file and stringifies it. Will throw an error in case proto file has multiple message
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bolt.tech/kafka-utils",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",