@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 +1 -1
- package/build/app.js +2 -2
- package/build/index.d.ts +5 -1
- package/build/index.js +94 -9
- package/build/kafkaServer.js +4 -4
- package/build/store/interfaces/notification.interface.d.ts +23 -23
- package/build/utils/kafka/publisher.kafka.js +2 -0
- package/build/utils/schemaRegistry/notificationSchema.registry.d.ts +2 -1
- package/build/utils/schemaRegistry/notificationSchema.registry.js +5 -1
- package/package.json +1 -1
package/build/app.d.ts
CHANGED
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,
|
|
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
|
|
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
|
|
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.
|
|
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
|
-
|
|
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:
|
|
66
|
+
yield publishToKafka(topic, schemaId, { key: userId, value: payload });
|
|
67
|
+
console.log(`✅ Notification sent to user: ${userId}`);
|
|
53
68
|
}
|
|
54
|
-
catch (
|
|
55
|
-
console.log("🔴 kafka-utils:
|
|
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
|
}
|
package/build/kafkaServer.js
CHANGED
|
@@ -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
|
-
|
|
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(
|
|
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
|
|
3
|
-
pushNotificationUserId
|
|
4
|
-
emailIds
|
|
5
|
-
phoneNumbers
|
|
2
|
+
inAppNotificationUserId?: string;
|
|
3
|
+
pushNotificationUserId?: string;
|
|
4
|
+
emailIds?: string[];
|
|
5
|
+
phoneNumbers?: string[];
|
|
6
6
|
}
|
|
7
7
|
export interface IMessage {
|
|
8
|
-
body
|
|
9
|
-
subject
|
|
10
|
-
priority
|
|
11
|
-
attachments
|
|
12
|
-
html
|
|
8
|
+
body?: string;
|
|
9
|
+
subject?: string;
|
|
10
|
+
priority?: number;
|
|
11
|
+
attachments?: Uint8Array[];
|
|
12
|
+
html?: string;
|
|
13
13
|
}
|
|
14
14
|
export interface ISmsObj {
|
|
15
|
-
smsType
|
|
16
|
-
templateId
|
|
15
|
+
smsType?: string;
|
|
16
|
+
templateId?: string;
|
|
17
17
|
}
|
|
18
18
|
export interface IOneSignalPayload {
|
|
19
|
-
alert
|
|
20
|
-
sos
|
|
21
|
-
vin
|
|
19
|
+
alert?: string;
|
|
20
|
+
sos?: boolean;
|
|
21
|
+
vin?: string;
|
|
22
22
|
}
|
|
23
23
|
export interface IAttachment {
|
|
24
|
-
filename
|
|
25
|
-
filetype
|
|
26
|
-
content
|
|
24
|
+
filename?: string;
|
|
25
|
+
filetype?: string;
|
|
26
|
+
content?: Uint8Array;
|
|
27
27
|
}
|
|
28
28
|
export interface INotification {
|
|
29
|
-
user
|
|
30
|
-
message
|
|
31
|
-
appId
|
|
32
|
-
smsObj
|
|
33
|
-
oneSignalPayload
|
|
34
|
-
Attachment
|
|
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
|
|
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
|