@bolt.tech/kafka-utils 1.0.9 → 1.0.11
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/index.d.ts +5 -1
- package/build/index.js +79 -17
- package/build/store/interfaces/notification.interface.d.ts +23 -23
- package/build/utils/kafka/publisher.kafka.js +0 -2
- package/package.json +1 -1
- package/build/app.d.ts +0 -14
- package/build/app.js +0 -63
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,16 +10,15 @@ 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 {
|
|
19
|
-
registry_util_1.SchemaRegistryS.connect(process.env.SCHEMA_REGISTRY_URL);
|
|
20
|
-
console.log('🔵 Connecting to Schema Registry...');
|
|
21
|
-
registry_util_1.SchemaRegistryS.getInstance();
|
|
22
|
-
console.log('✅ Schema Registry connected.');
|
|
23
22
|
currentNotificationType = notificationType;
|
|
24
23
|
const KafkaServer = require('./kafkaServer');
|
|
25
24
|
const kafkaInstance = new KafkaServer();
|
|
@@ -35,17 +34,11 @@ function initialiseNotification(paramStorePath, nodeEnvLocal, serviceName, notif
|
|
|
35
34
|
});
|
|
36
35
|
}
|
|
37
36
|
const notificationSchema_registry_1 = require("./utils/schemaRegistry/notificationSchema.registry");
|
|
38
|
-
const registry_util_1 = require("./utils/schemaRegistry/registry.util");
|
|
39
37
|
const publishToKafka = require('./utils/kafka/publisher.kafka');
|
|
40
|
-
|
|
38
|
+
/* publish notifications to kafka */
|
|
39
|
+
function sendNotificationToKafka(userId, payload) {
|
|
41
40
|
return __awaiter(this, void 0, void 0, function* () {
|
|
42
41
|
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
|
-
}
|
|
49
42
|
const instance = notificationSchema_registry_1.NotificationSchema.getInstance();
|
|
50
43
|
if (!instance) {
|
|
51
44
|
throw new Error("Notification schema is not initialised or Schema Registry is not connected.");
|
|
@@ -54,13 +47,82 @@ function sendNotification(userId, message) {
|
|
|
54
47
|
throw new Error("Notification schema is not initialised");
|
|
55
48
|
}
|
|
56
49
|
const topic = getTopic(currentNotificationType);
|
|
57
|
-
let key = userId;
|
|
58
50
|
const schemaId = yield instance.getSchemaId();
|
|
59
|
-
yield publishToKafka(topic, schemaId, { key, value:
|
|
51
|
+
yield publishToKafka(topic, schemaId, { key: userId, value: payload });
|
|
60
52
|
console.log(`✅ Notification sent to user: ${userId}`);
|
|
61
53
|
}
|
|
62
|
-
catch (
|
|
63
|
-
console.log("🔴 kafka-utils:
|
|
54
|
+
catch (error) {
|
|
55
|
+
console.log("🔴 kafka-utils: sendNotificationToKafka", error);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
/* send emaail notifications to users */
|
|
60
|
+
function sendEmailNotifications(user, message, attachment) {
|
|
61
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
62
|
+
if (user.emailIds.length === 0) {
|
|
63
|
+
console.log("No email addresses provided");
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const basePayload = {
|
|
67
|
+
user: Object.assign(Object.assign({}, user), { emailIds: user.emailIds }),
|
|
68
|
+
message,
|
|
69
|
+
Attachment: attachment || {}
|
|
70
|
+
};
|
|
71
|
+
for (const emailId of user.emailIds) {
|
|
72
|
+
const payload = Object.assign(Object.assign({}, basePayload), { user: Object.assign(Object.assign({}, basePayload.user), { emailIds: [emailId] }) });
|
|
73
|
+
yield sendNotificationToKafka(emailId, payload);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
/* send sms notifications to users */
|
|
78
|
+
function sendSmsNotifications(user, message, smsObj) {
|
|
79
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
80
|
+
if (user.phoneNumbers.length === 0) {
|
|
81
|
+
console.log("No phone numbers provided");
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const basePayload = {
|
|
85
|
+
user: Object.assign(Object.assign({}, user), { phoneNumbers: user.phoneNumbers }),
|
|
86
|
+
message,
|
|
87
|
+
smsObj,
|
|
88
|
+
};
|
|
89
|
+
for (const phoneNumber of user.phoneNumbers) {
|
|
90
|
+
const payload = Object.assign(Object.assign({}, basePayload), { user: Object.assign(Object.assign({}, basePayload.user), { phoneNumbers: [phoneNumber] }) });
|
|
91
|
+
yield sendNotificationToKafka(phoneNumber, payload);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
/* send push notifications to users */
|
|
96
|
+
function sendPushNotification(user, message, appId, oneSignalPayload) {
|
|
97
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
98
|
+
if (!user.pushNotificationUserId) {
|
|
99
|
+
console.log("No push notification user ID provided");
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const payload = {
|
|
103
|
+
user: Object.assign(Object.assign({}, user), { pushNotificationUserId: user.pushNotificationUserId }),
|
|
104
|
+
message,
|
|
105
|
+
appId,
|
|
106
|
+
smsObj: {},
|
|
107
|
+
oneSignalPayload,
|
|
108
|
+
};
|
|
109
|
+
yield sendNotificationToKafka(user.pushNotificationUserId, payload);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
/* send in-app notifications to users */
|
|
113
|
+
function sendInAppNotification(user, message, appId) {
|
|
114
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
115
|
+
if (!user.inAppNotificationUserId) {
|
|
116
|
+
console.log("No in-app notification user ID provided");
|
|
117
|
+
return;
|
|
64
118
|
}
|
|
119
|
+
const payload = {
|
|
120
|
+
user: Object.assign(Object.assign({}, user), { inAppNotificationUserId: user.inAppNotificationUserId }),
|
|
121
|
+
message,
|
|
122
|
+
appId,
|
|
123
|
+
smsObj: {},
|
|
124
|
+
oneSignalPayload: {},
|
|
125
|
+
};
|
|
126
|
+
yield sendNotificationToKafka(user.inAppNotificationUserId, payload);
|
|
65
127
|
});
|
|
66
128
|
}
|
|
@@ -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,8 +14,6 @@ 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();
|
|
19
17
|
const encodedMessage = yield registry_util_1.SchemaRegistryS.getInstance().encode(schemaId, message.value);
|
|
20
18
|
const payload = { topic, messages: [{ key: message.key, value: encodedMessage }] };
|
|
21
19
|
const producer = yield producer_kafka_1.KafkaProducer.getProducer();
|
package/package.json
CHANGED
package/build/app.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
declare const initialiseNotification: any, sendNotification: any;
|
|
2
|
-
declare const KafkaServer: any;
|
|
3
|
-
declare const notificationMessage: {
|
|
4
|
-
user: {
|
|
5
|
-
phoneNumbers: string[];
|
|
6
|
-
};
|
|
7
|
-
message: {
|
|
8
|
-
body: string;
|
|
9
|
-
};
|
|
10
|
-
smsObj: {
|
|
11
|
-
smsType: string;
|
|
12
|
-
templateId: string;
|
|
13
|
-
};
|
|
14
|
-
};
|
package/build/app.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
const { initialiseNotification, sendNotification } = require('./index');
|
|
11
|
-
const KafkaServer = require('./kafkaServer');
|
|
12
|
-
// Example notification message
|
|
13
|
-
const notificationMessage = {
|
|
14
|
-
user: {
|
|
15
|
-
// inAppNotificationUserId: '66222b43527f81ea8e2f0ea3',
|
|
16
|
-
// pushNotificationUserId: '66222b43527f81ea8e2f0ea3',
|
|
17
|
-
//emailIds: ["monakundnani113@gmail.com"],
|
|
18
|
-
phoneNumbers: ["+917850855689"],
|
|
19
|
-
},
|
|
20
|
-
message: {
|
|
21
|
-
body: "Dear Bolt.Earth Host, We have updated our Bank settlement cycle to a Weekly basis. These changes will be effective from September 6th, 2024 and you'll now receive settlements for bookings made on your charger on a weekly basis. Have Questions? Email us at support@bolt.earth"
|
|
22
|
-
//subject: 'Test',
|
|
23
|
-
// priority: 1,
|
|
24
|
-
// attachments: [],
|
|
25
|
-
// html: '<h1>This is a test message</h1>'
|
|
26
|
-
},
|
|
27
|
-
// appId: 'e5feb55e-ac98-4a16-a5d6-3675c3edb591',
|
|
28
|
-
smsObj: {
|
|
29
|
-
smsType: 'MKT',
|
|
30
|
-
templateId: '1307172536061229921'
|
|
31
|
-
}
|
|
32
|
-
// oneSignalPayload: {
|
|
33
|
-
// alert: 'This is a test message',
|
|
34
|
-
// sos: false,
|
|
35
|
-
// vin: '123'
|
|
36
|
-
// },
|
|
37
|
-
// Attachment: {
|
|
38
|
-
// filename: 'test.txt',
|
|
39
|
-
// filetype: 'text/plain',
|
|
40
|
-
// content: new Uint8Array([1, 2, 3, 4])
|
|
41
|
-
// }
|
|
42
|
-
};
|
|
43
|
-
(function testKafkaUtils() {
|
|
44
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
45
|
-
try {
|
|
46
|
-
console.log('🟡 Initialising Kafka Notification Utility...');
|
|
47
|
-
// Step 1: Initialise the notification system
|
|
48
|
-
yield initialiseNotification("dev", "TRUE", "common", "common");
|
|
49
|
-
console.log('🟢 Kafka Notification Utility Initialised.');
|
|
50
|
-
// Step 2: Send a notification
|
|
51
|
-
console.log('🟡 Sending Notification...');
|
|
52
|
-
yield sendNotification(notificationMessage.user.phoneNumbers[0], notificationMessage);
|
|
53
|
-
console.log('🟢 Notification Sent Successfully.');
|
|
54
|
-
// Clean up (if any shutdown handling or disconnect required)
|
|
55
|
-
const kafkaServerInstance = new KafkaServer();
|
|
56
|
-
kafkaServerInstance.handleShutdown();
|
|
57
|
-
console.log('🟢 Test Completed Successfully.');
|
|
58
|
-
}
|
|
59
|
-
catch (error) {
|
|
60
|
-
console.error('🔴 Test Error:', error);
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
})();
|