@bolt.tech/kafka-utils 1.0.9 → 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,7 +10,10 @@ 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) {
@@ -37,15 +40,20 @@ function initialiseNotification(paramStorePath, nodeEnvLocal, serviceName, notif
37
40
  const notificationSchema_registry_1 = require("./utils/schemaRegistry/notificationSchema.registry");
38
41
  const registry_util_1 = require("./utils/schemaRegistry/registry.util");
39
42
  const publishToKafka = require('./utils/kafka/publisher.kafka');
40
- 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) {
41
54
  return __awaiter(this, void 0, void 0, function* () {
42
55
  try {
43
- if (!registry_util_1.SchemaRegistryS.isConnected()) {
44
- console.log('🔵 Connecting to Schema Registry...');
45
- registry_util_1.SchemaRegistryS.connect(process.env.SCHEMA_REGISTRY_URL);
46
- registry_util_1.SchemaRegistryS.getInstance();
47
- console.log('✅ Schema Registry connected successfully.');
48
- }
56
+ yield initSchemaRegistry();
49
57
  const instance = notificationSchema_registry_1.NotificationSchema.getInstance();
50
58
  if (!instance) {
51
59
  throw new Error("Notification schema is not initialised or Schema Registry is not connected.");
@@ -54,13 +62,82 @@ function sendNotification(userId, message) {
54
62
  throw new Error("Notification schema is not initialised");
55
63
  }
56
64
  const topic = getTopic(currentNotificationType);
57
- let key = userId;
58
65
  const schemaId = yield instance.getSchemaId();
59
- yield publishToKafka(topic, schemaId, { key, value: message });
66
+ yield publishToKafka(topic, schemaId, { key: userId, value: payload });
60
67
  console.log(`✅ Notification sent to user: ${userId}`);
61
68
  }
62
- catch (e) {
63
- 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;
64
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);
65
142
  });
66
143
  }
@@ -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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bolt.tech/kafka-utils",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",