@platform-x/hep-push-notification-client 1.0.0 → 1.0.1
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/README.md +39 -5
- package/dist/src/PushNotificationManager.d.ts +9 -9
- package/dist/src/PushNotificationManager.js +55 -55
- package/dist/src/common/util/commonUtil.d.ts +12 -6
- package/dist/src/common/util/commonUtil.js +67 -42
- package/dist/src/common/util/errorHandler.d.ts +41 -41
- package/dist/src/common/util/errorHandler.js +80 -80
- package/dist/src/common/util/logger.d.ts +68 -68
- package/dist/src/common/util/logger.js +192 -192
- package/dist/src/common/util/requestTracer.d.ts +2 -2
- package/dist/src/common/util/requestTracer.js +16 -16
- package/dist/src/config/index.d.ts +22 -22
- package/dist/src/config/index.js +31 -31
- package/dist/src/index.d.ts +4 -4
- package/dist/src/index.js +35 -33
- package/dist/src/platform-x/constants/index.d.ts +41 -41
- package/dist/src/platform-x/constants/index.js +44 -44
- package/dist/src/platform-x/database/connection.d.ts +10 -8
- package/dist/src/platform-x/database/connection.js +72 -53
- package/dist/src/platform-x/database/dao/site_domain.dao.d.ts +10 -10
- package/dist/src/platform-x/database/dao/site_domain.dao.js +44 -44
- package/dist/src/platform-x/database/index.d.ts +16 -14
- package/dist/src/platform-x/database/index.js +9 -9
- package/dist/src/platform-x/database/interfaces/site_domain.interface.d.ts +33 -33
- package/dist/src/platform-x/database/interfaces/site_domain.interface.js +2 -2
- package/dist/src/platform-x/database/models/site_domain.model.d.ts +9 -9
- package/dist/src/platform-x/database/models/site_domain.model.js +47 -47
- package/dist/src/platform-x/services/fcmservices.d.ts +18 -18
- package/dist/src/platform-x/services/fcmservices.js +187 -187
- package/package.json +1 -1
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
//# sourceMappingURL=site_domain.interface.js.map
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { SiteDomain } from '../interfaces/site_domain.interface';
|
|
2
|
-
declare const _default: import("mongoose").Model<SiteDomain, {}, {}, {}, import("mongoose").Document<unknown, {}, SiteDomain> & Omit<SiteDomain & {
|
|
3
|
-
_id: import("mongoose").Types.ObjectId;
|
|
4
|
-
}, never>, import("mongoose").Schema<SiteDomain, import("mongoose").Model<SiteDomain, any, any, any, import("mongoose").Document<unknown, any, SiteDomain> & Omit<SiteDomain & {
|
|
5
|
-
_id: import("mongoose").Types.ObjectId;
|
|
6
|
-
}, never>, any>, {}, {}, {}, {}, import("mongoose").DefaultSchemaOptions, SiteDomain, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<SiteDomain>> & Omit<import("mongoose").FlatRecord<SiteDomain> & {
|
|
7
|
-
_id: import("mongoose").Types.ObjectId;
|
|
8
|
-
}, never>>>;
|
|
9
|
-
export default _default;
|
|
1
|
+
import { SiteDomain } from '../interfaces/site_domain.interface';
|
|
2
|
+
declare const _default: import("mongoose").Model<SiteDomain, {}, {}, {}, import("mongoose").Document<unknown, {}, SiteDomain> & Omit<SiteDomain & {
|
|
3
|
+
_id: import("mongoose").Types.ObjectId;
|
|
4
|
+
}, never>, import("mongoose").Schema<SiteDomain, import("mongoose").Model<SiteDomain, any, any, any, import("mongoose").Document<unknown, any, SiteDomain> & Omit<SiteDomain & {
|
|
5
|
+
_id: import("mongoose").Types.ObjectId;
|
|
6
|
+
}, never>, any>, {}, {}, {}, {}, import("mongoose").DefaultSchemaOptions, SiteDomain, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<SiteDomain>> & Omit<import("mongoose").FlatRecord<SiteDomain> & {
|
|
7
|
+
_id: import("mongoose").Types.ObjectId;
|
|
8
|
+
}, never>>>;
|
|
9
|
+
export default _default;
|
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const connection_1 = __importDefault(require("../connection"));
|
|
7
|
-
const { mongoose } = connection_1.default;
|
|
8
|
-
const { Schema, model } = mongoose;
|
|
9
|
-
const siteDomainSchema = new Schema({
|
|
10
|
-
id: Schema.Types.ObjectId,
|
|
11
|
-
domain: String,
|
|
12
|
-
sitename: String,
|
|
13
|
-
olTenantId: String,
|
|
14
|
-
olTenantName: String,
|
|
15
|
-
userInfo: {
|
|
16
|
-
type: Schema.Types.Mixed,
|
|
17
|
-
default: {},
|
|
18
|
-
},
|
|
19
|
-
staticSiteStatus: {
|
|
20
|
-
type: String,
|
|
21
|
-
enum: ['NOT_CREATED', 'INPROGRESS', 'SUCCESS', 'FAILED'],
|
|
22
|
-
default: 'NOT_CREATED',
|
|
23
|
-
},
|
|
24
|
-
isBackupRestored: {
|
|
25
|
-
type: Boolean,
|
|
26
|
-
default: false,
|
|
27
|
-
},
|
|
28
|
-
isStaticSiteBackup: {
|
|
29
|
-
type: Boolean,
|
|
30
|
-
default: false,
|
|
31
|
-
},
|
|
32
|
-
supportMail: String,
|
|
33
|
-
emailTemplates: [
|
|
34
|
-
{
|
|
35
|
-
default_email_template: String,
|
|
36
|
-
site_email_template: String,
|
|
37
|
-
},
|
|
38
|
-
],
|
|
39
|
-
providers: {
|
|
40
|
-
type: Schema.Types.Mixed,
|
|
41
|
-
default: {}
|
|
42
|
-
}
|
|
43
|
-
}, {
|
|
44
|
-
timestamps: true,
|
|
45
|
-
minimize: false,
|
|
46
|
-
});
|
|
47
|
-
exports.default = model('site_domains', siteDomainSchema);
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const connection_1 = __importDefault(require("../connection"));
|
|
7
|
+
const { mongoose } = connection_1.default;
|
|
8
|
+
const { Schema, model } = mongoose;
|
|
9
|
+
const siteDomainSchema = new Schema({
|
|
10
|
+
id: Schema.Types.ObjectId,
|
|
11
|
+
domain: String,
|
|
12
|
+
sitename: String,
|
|
13
|
+
olTenantId: String,
|
|
14
|
+
olTenantName: String,
|
|
15
|
+
userInfo: {
|
|
16
|
+
type: Schema.Types.Mixed,
|
|
17
|
+
default: {},
|
|
18
|
+
},
|
|
19
|
+
staticSiteStatus: {
|
|
20
|
+
type: String,
|
|
21
|
+
enum: ['NOT_CREATED', 'INPROGRESS', 'SUCCESS', 'FAILED'],
|
|
22
|
+
default: 'NOT_CREATED',
|
|
23
|
+
},
|
|
24
|
+
isBackupRestored: {
|
|
25
|
+
type: Boolean,
|
|
26
|
+
default: false,
|
|
27
|
+
},
|
|
28
|
+
isStaticSiteBackup: {
|
|
29
|
+
type: Boolean,
|
|
30
|
+
default: false,
|
|
31
|
+
},
|
|
32
|
+
supportMail: String,
|
|
33
|
+
emailTemplates: [
|
|
34
|
+
{
|
|
35
|
+
default_email_template: String,
|
|
36
|
+
site_email_template: String,
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
providers: {
|
|
40
|
+
type: Schema.Types.Mixed,
|
|
41
|
+
default: {}
|
|
42
|
+
}
|
|
43
|
+
}, {
|
|
44
|
+
timestamps: true,
|
|
45
|
+
minimize: false,
|
|
46
|
+
});
|
|
47
|
+
exports.default = model('site_domains', siteDomainSchema);
|
|
48
48
|
//# sourceMappingURL=site_domain.model.js.map
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { PushNotifyProvider } from '../interface/interface';
|
|
2
|
-
export declare class FcmClient implements PushNotifyProvider {
|
|
3
|
-
readonly authorizedClient: any;
|
|
4
|
-
private constructor();
|
|
5
|
-
static fcmClientObject(sitename: string): Promise<FcmClient>;
|
|
6
|
-
sendPushNotifications(token: string, notificationMessage: any): Promise<any>;
|
|
7
|
-
sendPushNotification(token: string, notificationMessage: any): Promise<any>;
|
|
8
|
-
sendChunkedPushNotification(tokens: string[], notificationMessage: string): Promise<unknown>;
|
|
9
|
-
chunkTokens(tokens: string[], chunkSize: number): Promise<any>;
|
|
10
|
-
fetchTopics(): Promise<any>;
|
|
11
|
-
/**
|
|
12
|
-
* Method to send notification based on topics
|
|
13
|
-
* @param topics
|
|
14
|
-
* @param notification
|
|
15
|
-
* @returns
|
|
16
|
-
*/
|
|
17
|
-
sendNotificationsToTopics(topics: string[], notification: any): Promise<any>;
|
|
18
|
-
}
|
|
1
|
+
import { PushNotifyProvider } from '../interface/interface';
|
|
2
|
+
export declare class FcmClient implements PushNotifyProvider {
|
|
3
|
+
readonly authorizedClient: any;
|
|
4
|
+
private constructor();
|
|
5
|
+
static fcmClientObject(sitename: string): Promise<FcmClient>;
|
|
6
|
+
sendPushNotifications(token: string, notificationMessage: any): Promise<any>;
|
|
7
|
+
sendPushNotification(token: string, notificationMessage: any): Promise<any>;
|
|
8
|
+
sendChunkedPushNotification(tokens: string[], notificationMessage: string): Promise<unknown>;
|
|
9
|
+
chunkTokens(tokens: string[], chunkSize: number): Promise<any>;
|
|
10
|
+
fetchTopics(): Promise<any>;
|
|
11
|
+
/**
|
|
12
|
+
* Method to send notification based on topics
|
|
13
|
+
* @param topics
|
|
14
|
+
* @param notification
|
|
15
|
+
* @returns
|
|
16
|
+
*/
|
|
17
|
+
sendNotificationsToTopics(topics: string[], notification: any): Promise<any>;
|
|
18
|
+
}
|
|
@@ -1,188 +1,188 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.FcmClient = void 0;
|
|
16
|
-
/* eslint-disable max-len */
|
|
17
|
-
const commonUtil_1 = require("../../common/util/commonUtil");
|
|
18
|
-
const logger_1 = require("../../common/util/logger");
|
|
19
|
-
const config_1 = __importDefault(require("../../config"));
|
|
20
|
-
const constants_1 = require("../constants");
|
|
21
|
-
const admin = require('firebase-admin');
|
|
22
|
-
class FcmClient {
|
|
23
|
-
constructor(credentials) {
|
|
24
|
-
this.authorizedClient = {};
|
|
25
|
-
if (!admin.apps.length) {
|
|
26
|
-
// Initialize default app
|
|
27
|
-
admin.initializeApp({
|
|
28
|
-
credential: admin.credential.cert(credentials),
|
|
29
|
-
databaseURL: `https://${credentials.project_id}.firebaseio.com`, // For Realtime DB, remove if not using
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
// If default app already exists, skip initializing again
|
|
34
|
-
logger_1.Logger.info('FCM Client', 'Firebase app already initialized, skipping initialization');
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
static fcmClientObject(sitename) {
|
|
38
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
39
|
-
var _a, _b, _c, _d, _e;
|
|
40
|
-
logger_1.Logger.info('FCM-Client>FCMClient: Reached fcmClientObject', 'fcmClientObject');
|
|
41
|
-
const credentials = {
|
|
42
|
-
type: config_1.default.FCM.TYPE,
|
|
43
|
-
project_id: ((_a = yield (0, commonUtil_1.getDynamicSecretdata)(sitename, constants_1.PROVIDERS.FCM, constants_1.CONFIG_KEYS === null || constants_1.CONFIG_KEYS === void 0 ? void 0 : constants_1.CONFIG_KEYS.FIREBASEPROJECTID)) !== null && _a !== void 0 ? _a : ''),
|
|
44
|
-
private_key_id: ((_b = yield (0, commonUtil_1.getDynamicSecretdata)(sitename, constants_1.PROVIDERS.FCM, constants_1.CONFIG_KEYS === null || constants_1.CONFIG_KEYS === void 0 ? void 0 : constants_1.CONFIG_KEYS.FIREBASEPRIVATEKEY)) !== null && _b !== void 0 ? _b : ''),
|
|
45
|
-
client_email: ((_c = yield (0, commonUtil_1.getDynamicSecretdata)(sitename, constants_1.PROVIDERS.FCM, constants_1.CONFIG_KEYS === null || constants_1.CONFIG_KEYS === void 0 ? void 0 : constants_1.CONFIG_KEYS.FIREBASECLIENTEMAIL)) !== null && _c !== void 0 ? _c : ''),
|
|
46
|
-
client_id: ((_d = yield (0, commonUtil_1.getDynamicSecretdata)(sitename, constants_1.PROVIDERS.FCM, constants_1.CONFIG_KEYS === null || constants_1.CONFIG_KEYS === void 0 ? void 0 : constants_1.CONFIG_KEYS.FIREBASECLIENTID)) !== null && _d !== void 0 ? _d : ''),
|
|
47
|
-
auth_uri: config_1.default.FCM.AUTH_URI,
|
|
48
|
-
token_uri: config_1.default.FCM.TOKEN_URI,
|
|
49
|
-
auth_provider_x509_cert_url: config_1.default.FCM.AUTH_PROVIDER_X509_CERT_URL,
|
|
50
|
-
client_x509_cert_url: ((_e = yield (0, commonUtil_1.getDynamicSecretdata)(sitename, constants_1.PROVIDERS.FCM, constants_1.CONFIG_KEYS === null || constants_1.CONFIG_KEYS === void 0 ? void 0 : constants_1.CONFIG_KEYS.FIREBASECLIENTCERTURL)) !== null && _e !== void 0 ? _e : ''),
|
|
51
|
-
universe_domain: config_1.default.FCM.UNIVERSE_DOMAIN,
|
|
52
|
-
private_key: `-----BEGIN PRIVATE KEY-----\n${(yield (0, commonUtil_1.getDynamicSecretdata)(sitename, constants_1.PROVIDERS.FCM, constants_1.CONFIG_KEYS === null || constants_1.CONFIG_KEYS === void 0 ? void 0 : constants_1.CONFIG_KEYS.FIREBASEPRIVATEKEY))}\n-----END PRIVATE KEY-----\n`, // pragma: allowlist secret
|
|
53
|
-
};
|
|
54
|
-
logger_1.Logger.info('FCM-Client>FCMClient: FCM Client Credentials fetched', 'fcmClientObject');
|
|
55
|
-
return new FcmClient(credentials);
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
sendPushNotifications(token, notificationMessage) {
|
|
59
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
-
try {
|
|
61
|
-
logger_1.Logger.info('FCM-Client>FCMClient: Reached authorize', 'sendPushNotifications');
|
|
62
|
-
const pushnotificationRes = yield this.sendPushNotification(token, notificationMessage);
|
|
63
|
-
return pushnotificationRes;
|
|
64
|
-
}
|
|
65
|
-
catch (err) {
|
|
66
|
-
logger_1.Logger.error('FCM-Client>FCMClient: Error in sendPushNotifications', 'sendPushNotifications', err);
|
|
67
|
-
return err;
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
sendPushNotification(token, notificationMessage) {
|
|
72
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
73
|
-
try {
|
|
74
|
-
logger_1.Logger.info('FCM-Client>FCMClient: Reached sendPushNotification', 'sendPushNotification');
|
|
75
|
-
const message = {
|
|
76
|
-
notification: {
|
|
77
|
-
title: notificationMessage.title,
|
|
78
|
-
body: notificationMessage.body,
|
|
79
|
-
},
|
|
80
|
-
token,
|
|
81
|
-
};
|
|
82
|
-
logger_1.Logger.info('FCM-Client>FCMClient: Reached sendPushNotification', 'sendPushNotification');
|
|
83
|
-
let notificationRes = yield admin.messaging().send(message);
|
|
84
|
-
logger_1.Logger.info('FCM-Client>FCMClient: Reached sendPushNotification', 'sendPushNotification');
|
|
85
|
-
return notificationRes;
|
|
86
|
-
}
|
|
87
|
-
catch (err) {
|
|
88
|
-
logger_1.Logger.error('FCM-Client>FCMClient: Error in sendPushNotifications', 'sendPushNotifications', err);
|
|
89
|
-
return err;
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
sendChunkedPushNotification(tokens, notificationMessage) {
|
|
94
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
95
|
-
try {
|
|
96
|
-
logger_1.Logger.info('FCM-Client>FCMClient: Reached sendPushNotification', 'sendPushNotification');
|
|
97
|
-
// Get the FCM tokens for this segment and chunk them
|
|
98
|
-
const CHUNK_SIZE = 1000;
|
|
99
|
-
const chunkedTokens = this.chunkTokens(tokens, CHUNK_SIZE);
|
|
100
|
-
// Send the notification to each chunk
|
|
101
|
-
for (const chunk of chunkedTokens) {
|
|
102
|
-
// Send notifications for the current chunk
|
|
103
|
-
for (const token of chunk) {
|
|
104
|
-
yield this.sendPushNotifications(token, notificationMessage);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
return true;
|
|
108
|
-
}
|
|
109
|
-
catch (err) {
|
|
110
|
-
logger_1.Logger.error('FCM-Client>FCMClient: Error in sendPushNotifications', 'sendPushNotifications', err);
|
|
111
|
-
return err;
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
chunkTokens(tokens, chunkSize) {
|
|
116
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
117
|
-
try {
|
|
118
|
-
logger_1.Logger.info('FCM-Client>FCMClient: Reached chunkTokens', 'chunkTokens');
|
|
119
|
-
// Maximum number of tokens to send in one chunk (FCM allows a max of 1000 tokens per request)
|
|
120
|
-
const chunks = [];
|
|
121
|
-
for (let i = 0; i < tokens.length; i += chunkSize) {
|
|
122
|
-
chunks.push(tokens.slice(i, i + chunkSize));
|
|
123
|
-
}
|
|
124
|
-
return chunks;
|
|
125
|
-
}
|
|
126
|
-
catch (err) {
|
|
127
|
-
logger_1.Logger.error('FCM-Client>FCMClient: Error in chunkTokens', 'chunkTokens', err);
|
|
128
|
-
return err;
|
|
129
|
-
}
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
fetchTopics() {
|
|
133
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
134
|
-
try {
|
|
135
|
-
const db = admin.firestore(); // Use Firestore// Firebase Realtime Database reference
|
|
136
|
-
const snapshot = yield db.collection('topics').get(); // Correct method for Firestore
|
|
137
|
-
if (snapshot.empty) {
|
|
138
|
-
logger_1.Logger.debug('FCM client', 'fetchTopics', 'No Topics Found');
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
const topics = [];
|
|
142
|
-
snapshot.forEach((doc) => {
|
|
143
|
-
topics.push({ name: doc.data().name }); // Assuming each topic has a 'name' field
|
|
144
|
-
});
|
|
145
|
-
return topics;
|
|
146
|
-
}
|
|
147
|
-
catch (error) {
|
|
148
|
-
logger_1.Logger.error('FCM-Client>FCMClient: Error in fetchTopics', 'fetchTopics', error);
|
|
149
|
-
return error;
|
|
150
|
-
}
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Method to send notification based on topics
|
|
155
|
-
* @param topics
|
|
156
|
-
* @param notification
|
|
157
|
-
* @returns
|
|
158
|
-
*/
|
|
159
|
-
sendNotificationsToTopics(topics, notification) {
|
|
160
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
161
|
-
try {
|
|
162
|
-
if (Array.isArray(topics) && topics.length > 0) {
|
|
163
|
-
const topicsStr = topics.map(t => `'${t}' in topics`).join(' || ');
|
|
164
|
-
logger_1.Logger.debug('FCM-Client>FCMClient: Reached sendNotificationsToTopics', 'sendNotificationsToTopics', { topicsStr });
|
|
165
|
-
const message = {
|
|
166
|
-
notification: {
|
|
167
|
-
title: notification.title,
|
|
168
|
-
body: notification.body,
|
|
169
|
-
},
|
|
170
|
-
data: notification.data,
|
|
171
|
-
// Using the "condition" field to target multiple topics
|
|
172
|
-
condition: `${topicsStr}`,
|
|
173
|
-
};
|
|
174
|
-
logger_1.Logger.debug('FCM-Client>FCMClient: Reached sendNotificationsToTopics', 'sendNotificationsToTopics', message);
|
|
175
|
-
const response = yield admin.messaging().send(message);
|
|
176
|
-
logger_1.Logger.debug('FCM-Client>FCMClient: Reached after sendNotificationsToTopics', 'sendNotificationsToTopics', { response });
|
|
177
|
-
return response;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
catch (error) {
|
|
181
|
-
logger_1.Logger.error('FCM-Client>FCMClient: Error in sendNotificationsToTopics', 'sendNotificationsToTopics', error);
|
|
182
|
-
return error;
|
|
183
|
-
}
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
exports.FcmClient = FcmClient;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.FcmClient = void 0;
|
|
16
|
+
/* eslint-disable max-len */
|
|
17
|
+
const commonUtil_1 = require("../../common/util/commonUtil");
|
|
18
|
+
const logger_1 = require("../../common/util/logger");
|
|
19
|
+
const config_1 = __importDefault(require("../../config"));
|
|
20
|
+
const constants_1 = require("../constants");
|
|
21
|
+
const admin = require('firebase-admin');
|
|
22
|
+
class FcmClient {
|
|
23
|
+
constructor(credentials) {
|
|
24
|
+
this.authorizedClient = {};
|
|
25
|
+
if (!admin.apps.length) {
|
|
26
|
+
// Initialize default app
|
|
27
|
+
admin.initializeApp({
|
|
28
|
+
credential: admin.credential.cert(credentials),
|
|
29
|
+
databaseURL: `https://${credentials.project_id}.firebaseio.com`, // For Realtime DB, remove if not using
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
// If default app already exists, skip initializing again
|
|
34
|
+
logger_1.Logger.info('FCM Client', 'Firebase app already initialized, skipping initialization');
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
static fcmClientObject(sitename) {
|
|
38
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
39
|
+
var _a, _b, _c, _d, _e;
|
|
40
|
+
logger_1.Logger.info('FCM-Client>FCMClient: Reached fcmClientObject', 'fcmClientObject');
|
|
41
|
+
const credentials = {
|
|
42
|
+
type: config_1.default.FCM.TYPE,
|
|
43
|
+
project_id: ((_a = yield (0, commonUtil_1.getDynamicSecretdata)(sitename, constants_1.PROVIDERS.FCM, constants_1.CONFIG_KEYS === null || constants_1.CONFIG_KEYS === void 0 ? void 0 : constants_1.CONFIG_KEYS.FIREBASEPROJECTID)) !== null && _a !== void 0 ? _a : ''),
|
|
44
|
+
private_key_id: ((_b = yield (0, commonUtil_1.getDynamicSecretdata)(sitename, constants_1.PROVIDERS.FCM, constants_1.CONFIG_KEYS === null || constants_1.CONFIG_KEYS === void 0 ? void 0 : constants_1.CONFIG_KEYS.FIREBASEPRIVATEKEY)) !== null && _b !== void 0 ? _b : ''),
|
|
45
|
+
client_email: ((_c = yield (0, commonUtil_1.getDynamicSecretdata)(sitename, constants_1.PROVIDERS.FCM, constants_1.CONFIG_KEYS === null || constants_1.CONFIG_KEYS === void 0 ? void 0 : constants_1.CONFIG_KEYS.FIREBASECLIENTEMAIL)) !== null && _c !== void 0 ? _c : ''),
|
|
46
|
+
client_id: ((_d = yield (0, commonUtil_1.getDynamicSecretdata)(sitename, constants_1.PROVIDERS.FCM, constants_1.CONFIG_KEYS === null || constants_1.CONFIG_KEYS === void 0 ? void 0 : constants_1.CONFIG_KEYS.FIREBASECLIENTID)) !== null && _d !== void 0 ? _d : ''),
|
|
47
|
+
auth_uri: config_1.default.FCM.AUTH_URI,
|
|
48
|
+
token_uri: config_1.default.FCM.TOKEN_URI,
|
|
49
|
+
auth_provider_x509_cert_url: config_1.default.FCM.AUTH_PROVIDER_X509_CERT_URL,
|
|
50
|
+
client_x509_cert_url: ((_e = yield (0, commonUtil_1.getDynamicSecretdata)(sitename, constants_1.PROVIDERS.FCM, constants_1.CONFIG_KEYS === null || constants_1.CONFIG_KEYS === void 0 ? void 0 : constants_1.CONFIG_KEYS.FIREBASECLIENTCERTURL)) !== null && _e !== void 0 ? _e : ''),
|
|
51
|
+
universe_domain: config_1.default.FCM.UNIVERSE_DOMAIN,
|
|
52
|
+
private_key: `-----BEGIN PRIVATE KEY-----\n${(yield (0, commonUtil_1.getDynamicSecretdata)(sitename, constants_1.PROVIDERS.FCM, constants_1.CONFIG_KEYS === null || constants_1.CONFIG_KEYS === void 0 ? void 0 : constants_1.CONFIG_KEYS.FIREBASEPRIVATEKEY))}\n-----END PRIVATE KEY-----\n`, // pragma: allowlist secret
|
|
53
|
+
};
|
|
54
|
+
logger_1.Logger.info('FCM-Client>FCMClient: FCM Client Credentials fetched', 'fcmClientObject');
|
|
55
|
+
return new FcmClient(credentials);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
sendPushNotifications(token, notificationMessage) {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
try {
|
|
61
|
+
logger_1.Logger.info('FCM-Client>FCMClient: Reached authorize', 'sendPushNotifications');
|
|
62
|
+
const pushnotificationRes = yield this.sendPushNotification(token, notificationMessage);
|
|
63
|
+
return pushnotificationRes;
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
logger_1.Logger.error('FCM-Client>FCMClient: Error in sendPushNotifications', 'sendPushNotifications', err);
|
|
67
|
+
return err;
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
sendPushNotification(token, notificationMessage) {
|
|
72
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
73
|
+
try {
|
|
74
|
+
logger_1.Logger.info('FCM-Client>FCMClient: Reached sendPushNotification', 'sendPushNotification');
|
|
75
|
+
const message = {
|
|
76
|
+
notification: {
|
|
77
|
+
title: notificationMessage.title,
|
|
78
|
+
body: notificationMessage.body,
|
|
79
|
+
},
|
|
80
|
+
token,
|
|
81
|
+
};
|
|
82
|
+
logger_1.Logger.info('FCM-Client>FCMClient: Reached sendPushNotification', 'sendPushNotification');
|
|
83
|
+
let notificationRes = yield admin.messaging().send(message);
|
|
84
|
+
logger_1.Logger.info('FCM-Client>FCMClient: Reached sendPushNotification', 'sendPushNotification');
|
|
85
|
+
return notificationRes;
|
|
86
|
+
}
|
|
87
|
+
catch (err) {
|
|
88
|
+
logger_1.Logger.error('FCM-Client>FCMClient: Error in sendPushNotifications', 'sendPushNotifications', err);
|
|
89
|
+
return err;
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
sendChunkedPushNotification(tokens, notificationMessage) {
|
|
94
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
95
|
+
try {
|
|
96
|
+
logger_1.Logger.info('FCM-Client>FCMClient: Reached sendPushNotification', 'sendPushNotification');
|
|
97
|
+
// Get the FCM tokens for this segment and chunk them
|
|
98
|
+
const CHUNK_SIZE = 1000;
|
|
99
|
+
const chunkedTokens = this.chunkTokens(tokens, CHUNK_SIZE);
|
|
100
|
+
// Send the notification to each chunk
|
|
101
|
+
for (const chunk of chunkedTokens) {
|
|
102
|
+
// Send notifications for the current chunk
|
|
103
|
+
for (const token of chunk) {
|
|
104
|
+
yield this.sendPushNotifications(token, notificationMessage);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
catch (err) {
|
|
110
|
+
logger_1.Logger.error('FCM-Client>FCMClient: Error in sendPushNotifications', 'sendPushNotifications', err);
|
|
111
|
+
return err;
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
chunkTokens(tokens, chunkSize) {
|
|
116
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
117
|
+
try {
|
|
118
|
+
logger_1.Logger.info('FCM-Client>FCMClient: Reached chunkTokens', 'chunkTokens');
|
|
119
|
+
// Maximum number of tokens to send in one chunk (FCM allows a max of 1000 tokens per request)
|
|
120
|
+
const chunks = [];
|
|
121
|
+
for (let i = 0; i < tokens.length; i += chunkSize) {
|
|
122
|
+
chunks.push(tokens.slice(i, i + chunkSize));
|
|
123
|
+
}
|
|
124
|
+
return chunks;
|
|
125
|
+
}
|
|
126
|
+
catch (err) {
|
|
127
|
+
logger_1.Logger.error('FCM-Client>FCMClient: Error in chunkTokens', 'chunkTokens', err);
|
|
128
|
+
return err;
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
fetchTopics() {
|
|
133
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
134
|
+
try {
|
|
135
|
+
const db = admin.firestore(); // Use Firestore// Firebase Realtime Database reference
|
|
136
|
+
const snapshot = yield db.collection('topics').get(); // Correct method for Firestore
|
|
137
|
+
if (snapshot.empty) {
|
|
138
|
+
logger_1.Logger.debug('FCM client', 'fetchTopics', 'No Topics Found');
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
const topics = [];
|
|
142
|
+
snapshot.forEach((doc) => {
|
|
143
|
+
topics.push({ name: doc.data().name }); // Assuming each topic has a 'name' field
|
|
144
|
+
});
|
|
145
|
+
return topics;
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
logger_1.Logger.error('FCM-Client>FCMClient: Error in fetchTopics', 'fetchTopics', error);
|
|
149
|
+
return error;
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Method to send notification based on topics
|
|
155
|
+
* @param topics
|
|
156
|
+
* @param notification
|
|
157
|
+
* @returns
|
|
158
|
+
*/
|
|
159
|
+
sendNotificationsToTopics(topics, notification) {
|
|
160
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
161
|
+
try {
|
|
162
|
+
if (Array.isArray(topics) && topics.length > 0) {
|
|
163
|
+
const topicsStr = topics.map(t => `'${t}' in topics`).join(' || ');
|
|
164
|
+
logger_1.Logger.debug('FCM-Client>FCMClient: Reached sendNotificationsToTopics', 'sendNotificationsToTopics', { topicsStr });
|
|
165
|
+
const message = {
|
|
166
|
+
notification: {
|
|
167
|
+
title: notification.title,
|
|
168
|
+
body: notification.body,
|
|
169
|
+
},
|
|
170
|
+
data: notification.data,
|
|
171
|
+
// Using the "condition" field to target multiple topics
|
|
172
|
+
condition: `${topicsStr}`,
|
|
173
|
+
};
|
|
174
|
+
logger_1.Logger.debug('FCM-Client>FCMClient: Reached sendNotificationsToTopics', 'sendNotificationsToTopics', message);
|
|
175
|
+
const response = yield admin.messaging().send(message);
|
|
176
|
+
logger_1.Logger.debug('FCM-Client>FCMClient: Reached after sendNotificationsToTopics', 'sendNotificationsToTopics', { response });
|
|
177
|
+
return response;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
catch (error) {
|
|
181
|
+
logger_1.Logger.error('FCM-Client>FCMClient: Error in sendNotificationsToTopics', 'sendNotificationsToTopics', error);
|
|
182
|
+
return error;
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
exports.FcmClient = FcmClient;
|
|
188
188
|
//# sourceMappingURL=fcmservices.js.map
|