@xuda.io/notification_module 1.1.78 → 1.1.80

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.
Files changed (2) hide show
  1. package/index.js +85 -143
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,130 +1,88 @@
1
- const path = require("path");
2
- global._conf = require(path.join(
3
- process.env.XUDA_HOME,
4
- process.env.XUDA_CONFIG
5
- ));
6
-
7
- const _ = require("lodash");
8
-
9
- const _common = require(path.join(
10
- process.env.XUDA_HOME,
11
- "common",
12
- "xuda_node_common.js"
13
- ));
14
- const module_path =
15
- path.join(process.env.XUDA_HOME, "cpi") +
16
- (!_conf.is_debug ? "/node_modules/@xuda.io" : "");
1
+ const path = require('path');
2
+ global._conf = require(path.join(process.env.XUDA_HOME, process.env.XUDA_CONFIG));
3
+
4
+ const _ = require('lodash');
5
+
6
+ const _common = require(path.join(process.env.XUDA_HOME, 'common', 'xuda_node_common.js'));
7
+ const module_path = path.join(process.env.XUDA_HOME, 'cpi') + (!_conf.is_debug ? '/node_modules/@xuda.io' : '');
17
8
  const db_module = require(`${module_path}/db_module`);
18
9
 
19
10
  //////////////////// FCM /////////////////////
20
- const admin = require("firebase-admin");
11
+ const admin = require('firebase-admin');
21
12
  const serviceAccount = _conf.firebase_serviceAccount;
22
13
  admin.initializeApp({
23
14
  credential: admin.credential.cert(serviceAccount),
24
- databaseURL: "https://your-project-id.firebaseio.com", // Replace with your Firebase project's database URL
15
+ databaseURL: 'https://your-project-id.firebaseio.com', // Replace with your Firebase project's database URL
25
16
  });
26
17
  const messaging = admin.messaging();
27
18
  //////////////////////////////////////////////
28
19
 
29
20
  exports.get_new_notification_count = async function (req) {
30
- return await db_module.get_couch_view(
31
- "xuda_notification",
32
- "new_notifications_count",
33
- {
34
- key: [req.uid],
35
- reduce: true,
36
- group_level: 1,
37
- }
38
- );
21
+ return await db_module.get_couch_view('xuda_notification', 'new_notifications_count', {
22
+ key: [req.uid],
23
+ reduce: true,
24
+ group_level: 1,
25
+ });
39
26
  };
40
27
 
41
28
  exports.set_notification_read = async function (req, job_id, header) {
42
- const ret = await db_module.get_couch_doc(
43
- "xuda_notification",
44
- req.notification_id
45
- );
29
+ const ret = await db_module.get_couch_doc('xuda_notification', req.notification_id);
46
30
  var notification_doc = ret.data;
47
31
 
48
32
  if (notification_doc.system) {
49
- return { code: -1, data: "system notification not allow read" };
33
+ return { code: -1, data: 'system notification not allow read' };
50
34
  }
51
35
 
52
36
  notification_doc.read_date = Date.now();
53
37
  notification_doc.read = true;
54
38
  notification_doc.read_header = header;
55
- return await db_module.save_couch_doc("xuda_notification", notification_doc);
39
+ return await db_module.save_couch_doc('xuda_notification', notification_doc);
56
40
  };
57
41
 
58
42
  exports.update_notification_status = async function (req) {
59
43
  const { notification_id, stat, uid } = req;
60
- const ret = await db_module.get_couch_doc(
61
- "xuda_notification",
62
- notification_id
63
- );
44
+ const ret = await db_module.get_couch_doc('xuda_notification', notification_id);
64
45
  var notification_doc = ret.data;
65
46
  notification_doc.stat_ts = Date.now();
66
47
  notification_doc.stat = stat;
67
48
 
68
49
  global[`_notification_module_ch`].sendToQueue(
69
- "ws_dashboard_module",
50
+ 'ws_dashboard_module',
70
51
  Buffer.from(
71
52
  JSON.stringify({
72
- method: "notification",
53
+ method: 'notification',
73
54
 
74
55
  data: {
75
56
  to: notification_doc.system ? uid : notification_doc.uid,
76
57
  data: notification_doc,
77
58
  },
78
- })
79
- )
59
+ }),
60
+ ),
80
61
  );
81
62
 
82
- return await db_module.save_couch_doc("xuda_notification", notification_doc);
63
+ return await db_module.save_couch_doc('xuda_notification', notification_doc);
83
64
  };
84
65
 
85
- exports.submit_notification = async function (
86
- req,
87
- _ch = global[`_notification_module_ch`]
88
- ) {
89
- var {
90
- type,
91
- app_id,
92
- to_app_id,
93
- uid_arr,
94
- subject = "",
95
- body = "",
96
- delivery_method = [],
97
- display_type = "info",
98
- ref,
99
- email,
100
- sender_uid,
101
- system,
102
- topic,
103
- params,
104
- } = req;
66
+ exports.submit_notification = async function (req, _ch = global[`_notification_module_ch`]) {
67
+ var { type, app_id, to_app_id, uid_arr, subject = '', body = '', delivery_method = [], display_type = 'info', ref, email, sender_uid, system, topic, params } = req;
105
68
 
106
69
  // if (!uid) {
107
70
  // return { code: -1, data: "error: uid is mandatory field" };
108
71
  // }
109
72
 
110
- const { notification_categories } = await import(
111
- path.join(process.env.XUDA_HOME, "notification_templates.mjs")
112
- );
73
+ const { notification_categories } = await import(path.join(process.env.XUDA_HOME, 'notification_templates.mjs'));
113
74
 
114
- var icon = "https://dev.xuda.io/dist/images/xuda_ico.png";
75
+ var icon = 'https://dev.xuda.io/dist/images/xuda_ico.png';
115
76
  if (app_id) {
116
77
  const app_id_reference = await _common.get_project_app_id(app_id, true);
117
- const app_ret = await db_module.get_couch_doc(
118
- "xuda_master",
119
- app_id_reference
120
- );
78
+ const app_ret = await db_module.get_couch_doc('xuda_master', app_id_reference);
121
79
  if (app_ret.code > -1) {
122
80
  icon = app_ret.data.app_pic;
123
81
  }
124
82
  }
125
83
 
126
84
  if (!type) {
127
- return { code: -1, data: "error: type is mandatory field" };
85
+ return { code: -1, data: 'error: type is mandatory field' };
128
86
  }
129
87
 
130
88
  if (topic) {
@@ -148,16 +106,16 @@ exports.submit_notification = async function (
148
106
  for await (let delivery_method_item of delivery_method) {
149
107
  var account_ret;
150
108
  if (!system) {
151
- account_ret = await db_module.get_couch_doc("xuda_accounts", uid);
109
+ account_ret = await db_module.get_couch_doc('xuda_accounts', uid);
152
110
  if (account_ret.code < 0) {
153
111
  throw new Error(`user ${uid} not found`);
154
112
  }
155
113
  }
156
114
  send_result[uid] = true;
157
- const uuid = await _common.xuda_get_uuid("notification");
115
+ const uuid = await _common.xuda_get_uuid('notification');
158
116
  var obj = {
159
117
  _id: uuid,
160
- docType: "notification",
118
+ docType: 'notification',
161
119
  type,
162
120
  app_id: to_app_id,
163
121
  uid,
@@ -178,11 +136,8 @@ exports.submit_notification = async function (
178
136
  };
179
137
 
180
138
  switch (delivery_method_item) {
181
- case "email":
182
- obj.email =
183
- email || account_ret.data.account_info
184
- ? account_ret.data.account_info.email
185
- : "";
139
+ case 'email':
140
+ obj.email = email || account_ret.data.account_info ? account_ret.data.account_info.email : '';
186
141
  if (!obj.email) {
187
142
  send_result[uid] = false;
188
143
  return; //{ code: -1, data: "error: email is empty" };
@@ -193,34 +148,35 @@ exports.submit_notification = async function (
193
148
  subject,
194
149
  body,
195
150
  ref: obj._id,
151
+ app_id: obj.app_id,
196
152
  });
197
153
  break;
198
154
 
199
- case "banner":
200
- case "consent":
201
- case "alert":
155
+ case 'banner':
156
+ case 'consent':
157
+ case 'alert':
202
158
  _ch.sendToQueue(
203
- "ws_dashboard_module",
159
+ 'ws_dashboard_module',
204
160
  Buffer.from(
205
161
  JSON.stringify({
206
- method: "notification",
162
+ method: 'notification',
207
163
  data: {
208
164
  to: uid,
209
165
  data: obj,
210
166
  },
211
- })
212
- )
167
+ }),
168
+ ),
213
169
  );
214
170
 
215
171
  break;
216
- case "push":
172
+ case 'push':
217
173
  try {
218
- const ret = await db_module.find_couch_query("xuda_sessions", {
174
+ const ret = await db_module.find_couch_query('xuda_sessions', {
219
175
  selector: {
220
- docType: "gtp_session",
176
+ docType: 'gtp_session',
221
177
  stat: { $lt: 3 },
222
178
  uid,
223
- fcm_token: { $gt: "" },
179
+ fcm_token: { $gt: '' },
224
180
  },
225
181
  });
226
182
  var fcm_token_arr = [];
@@ -248,7 +204,7 @@ exports.submit_notification = async function (
248
204
  apns: {
249
205
  payload: {
250
206
  aps: {
251
- "mutable-content": 1,
207
+ 'mutable-content': 1,
252
208
  },
253
209
  },
254
210
  fcm_options: {
@@ -268,7 +224,7 @@ exports.submit_notification = async function (
268
224
  // console.log("Successfully sent message:", response);
269
225
  })
270
226
  .catch((error) => {
271
- console.error("Error sending message:", error);
227
+ console.error('Error sending message:', error);
272
228
  });
273
229
  } catch (err) {
274
230
  console.error(err);
@@ -285,7 +241,7 @@ exports.submit_notification = async function (
285
241
  break;
286
242
  }
287
243
  obj.sent = send_result[uid];
288
- const ret = await db_module.save_couch_doc("xuda_notification", obj);
244
+ const ret = await db_module.save_couch_doc('xuda_notification', obj);
289
245
  }
290
246
  }
291
247
  return { code: 1, data: obj };
@@ -312,7 +268,7 @@ exports.get_notifications = async function (req) {
312
268
  if (req.date_to < req.date_from) {
313
269
  return {
314
270
  code: -2,
315
- data: "error - invalid range date_from and date_to",
271
+ data: 'error - invalid range date_from and date_to',
316
272
  };
317
273
  }
318
274
 
@@ -330,7 +286,7 @@ exports.get_notifications = async function (req) {
330
286
  uid: req.uid,
331
287
  },
332
288
  {
333
- uid: "system",
289
+ uid: 'system',
334
290
  },
335
291
  {
336
292
  uid: req.to_app_id,
@@ -384,7 +340,7 @@ exports.get_notifications = async function (req) {
384
340
  if (req.ref) {
385
341
  selector.ref = req.ref;
386
342
  }
387
- if (typeof req.to_app_id !== "undefined") {
343
+ if (typeof req.to_app_id !== 'undefined') {
388
344
  selector.app_id = req.to_app_id;
389
345
  }
390
346
 
@@ -401,7 +357,7 @@ exports.get_notifications = async function (req) {
401
357
  selector.read = false;
402
358
  }
403
359
 
404
- if (typeof req.read === "undefined") {
360
+ if (typeof req.read === 'undefined') {
405
361
  delete selector.read;
406
362
  }
407
363
 
@@ -410,7 +366,7 @@ exports.get_notifications = async function (req) {
410
366
 
411
367
  sort: [
412
368
  {
413
- date_created_ts: "desc",
369
+ date_created_ts: 'desc',
414
370
  },
415
371
  ],
416
372
  limit: req.limit ? req.limit : 99999,
@@ -420,12 +376,12 @@ exports.get_notifications = async function (req) {
420
376
  opt.skip = req.skip;
421
377
  }
422
378
 
423
- if (req?.bookmark !== "nil") {
379
+ if (req?.bookmark !== 'nil') {
424
380
  opt.bookmark = req.bookmark;
425
381
  }
426
382
 
427
383
  try {
428
- var ret = await db_module.find_couch_query("xuda_notification", opt);
384
+ var ret = await db_module.find_couch_query('xuda_notification', opt);
429
385
  let data = { docs: [] };
430
386
  if (ret.docs) {
431
387
  for await (let doc of ret.docs) {
@@ -462,18 +418,18 @@ exports.submit_superuser_notification = async function (req) {
462
418
  } = req;
463
419
  try {
464
420
  if (!_conf.superuser_account_ids.includes(uid)) {
465
- throw new Error("user is not authorized for this method");
421
+ throw new Error('user is not authorized for this method');
466
422
  }
467
423
 
468
424
  if (!_.isArray(to_uid)) {
469
- throw new Error("to_uid must be in the form of an array");
425
+ throw new Error('to_uid must be in the form of an array');
470
426
  }
471
427
 
472
428
  if (_.isEmpty(to_uid)) {
473
429
  if (!system) {
474
- const all_users = await db_module.find_couch_query("xuda_accounts", {
430
+ const all_users = await db_module.find_couch_query('xuda_accounts', {
475
431
  selector: {
476
- docType: "account",
432
+ docType: 'account',
477
433
  stat: { $lt: 4 },
478
434
  },
479
435
  });
@@ -481,7 +437,7 @@ exports.submit_superuser_notification = async function (req) {
481
437
  to_uid.push(user._id);
482
438
  }
483
439
  } else {
484
- to_uid.push("system");
440
+ to_uid.push('system');
485
441
  }
486
442
  }
487
443
 
@@ -489,13 +445,13 @@ exports.submit_superuser_notification = async function (req) {
489
445
  type,
490
446
  uid_arr: to_uid,
491
447
  app_id,
492
- to_app_id: "",
448
+ to_app_id: '',
493
449
  subject,
494
450
  body,
495
451
  delivery_method: [delivery_method],
496
452
  display_type,
497
453
  sender_uid: uid,
498
- ref: "",
454
+ ref: '',
499
455
  system,
500
456
  };
501
457
 
@@ -523,12 +479,12 @@ exports.submit_app_notification = async function (req) {
523
479
  // }
524
480
 
525
481
  if (!_.isArray(to_uid)) {
526
- throw new Error("to_uid must be in the form of an array");
482
+ throw new Error('to_uid must be in the form of an array');
527
483
  }
528
484
 
529
485
  if (_.isEmpty(to_uid)) {
530
486
  if (!system) {
531
- throw new Error("to_uid is empty");
487
+ throw new Error('to_uid is empty');
532
488
  } else {
533
489
  to_uid.push(app_id);
534
490
  }
@@ -542,9 +498,7 @@ exports.submit_app_notification = async function (req) {
542
498
  ).data;
543
499
  const exists = _.some(to_uid, (val) => _.includes(users_arr, val));
544
500
  if (!exists) {
545
- throw new Error(
546
- "some of the uid's are not authorized for the selected app"
547
- );
501
+ throw new Error("some of the uid's are not authorized for the selected app");
548
502
  }
549
503
 
550
504
  // if (!_conf.superuser_account_ids.includes(uid)) {
@@ -552,7 +506,7 @@ exports.submit_app_notification = async function (req) {
552
506
  // }
553
507
 
554
508
  let msg_obj = {
555
- type: _conf.superuser_account_ids.includes(uid) && type ? type : "user",
509
+ type: _conf.superuser_account_ids.includes(uid) && type ? type : 'user',
556
510
  uid_arr: to_uid,
557
511
  app_id,
558
512
  to_app_id: to_app_id,
@@ -561,7 +515,7 @@ exports.submit_app_notification = async function (req) {
561
515
  delivery_method: [delivery_method],
562
516
  display_type,
563
517
  sender_uid: uid,
564
- ref: "",
518
+ ref: '',
565
519
  system,
566
520
  };
567
521
 
@@ -573,13 +527,13 @@ exports.submit_app_notification = async function (req) {
573
527
 
574
528
  exports.update_system_notification = async function (req) {
575
529
  const { display_type, subject, body, delivery_method, uid, type } = req;
576
- const ret = await db_module.get_couch_doc("xuda_notification", req._id);
530
+ const ret = await db_module.get_couch_doc('xuda_notification', req._id);
577
531
  var notification_doc = ret.data;
578
532
 
579
533
  if (!notification_doc.system) {
580
- return { code: -1, data: "only system notification allow update" };
534
+ return { code: -1, data: 'only system notification allow update' };
581
535
  }
582
- const allowed_delivery_methods = ["banner", "alert"];
536
+ const allowed_delivery_methods = ['banner', 'alert'];
583
537
  if (!allowed_delivery_methods.includes(delivery_method)) {
584
538
  return {
585
539
  code: -1,
@@ -597,36 +551,27 @@ exports.update_system_notification = async function (req) {
597
551
  }
598
552
 
599
553
  global[`_notification_module_ch`].sendToQueue(
600
- "ws_dashboard_module",
554
+ 'ws_dashboard_module',
601
555
  Buffer.from(
602
556
  JSON.stringify({
603
- method: "notification",
557
+ method: 'notification',
604
558
 
605
559
  data: {
606
560
  to: notification_doc.system ? uid : notification_doc.uid,
607
561
  data: notification_doc,
608
562
  },
609
- })
610
- )
563
+ }),
564
+ ),
611
565
  );
612
566
 
613
- return await db_module.save_couch_doc("xuda_notification", notification_doc);
567
+ return await db_module.save_couch_doc('xuda_notification', notification_doc);
614
568
  };
615
569
 
616
570
  exports.submit_topic_app_notification = async function (req, _ch) {
617
- const {
618
- uid,
619
- topic,
620
- to_uid = [],
621
- to_app_id,
622
- app_id,
623
- system,
624
- type,
625
- params,
626
- } = req;
571
+ const { uid, topic, to_uid = [], to_app_id, app_id, system, type, params } = req;
627
572
  try {
628
573
  if (!_.isArray(to_uid)) {
629
- throw new Error("to_uid must be in the form of an array");
574
+ throw new Error('to_uid must be in the form of an array');
630
575
  }
631
576
 
632
577
  if (_.isEmpty(to_uid)) {
@@ -648,13 +593,13 @@ exports.submit_topic_app_notification = async function (req, _ch) {
648
593
  }
649
594
 
650
595
  let msg_obj = {
651
- type: type || "user",
596
+ type: type || 'user',
652
597
  uid_arr: to_uid,
653
598
  app_id,
654
599
  to_app_id: to_app_id,
655
600
  topic,
656
601
  sender_uid: uid,
657
- ref: "",
602
+ ref: '',
658
603
  system,
659
604
  params,
660
605
  };
@@ -667,7 +612,7 @@ exports.submit_topic_app_notification = async function (req, _ch) {
667
612
 
668
613
  exports.get_system_notifications = async function (req) {
669
614
  req.system = true;
670
- req.to_app_id = "";
615
+ req.to_app_id = '';
671
616
  req.read = false;
672
617
  return await module.exports.get_notifications(req);
673
618
  };
@@ -709,19 +654,16 @@ exports.get_system_notifications = async function (req) {
709
654
  // module.exports = notifications_validation_class;
710
655
 
711
656
  exports.notifications_validation_fx = async function (notification_id) {
712
- let { code, data: doc } = await db_module.get_couch_doc(
713
- "xuda_notification",
714
- notification_id
715
- );
657
+ let { code, data: doc } = await db_module.get_couch_doc('xuda_notification', notification_id);
716
658
  const delete_notification = async function () {
717
659
  doc.stat = 4;
718
- await db_module.save_couch_doc("xuda_notification", doc);
660
+ await db_module.save_couch_doc('xuda_notification', doc);
719
661
  };
720
662
  const read_notification = async function () {
721
663
  doc.read = true;
722
664
  doc.read_ts = Date.now();
723
- doc.read_note = "read by notifications_validation_fx";
724
- await db_module.save_couch_doc("xuda_notification", doc);
665
+ doc.read_note = 'read by notifications_validation_fx';
666
+ await db_module.save_couch_doc('xuda_notification', doc);
725
667
  };
726
668
 
727
669
  if (code < 0) return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/notification_module",
3
- "version": "1.1.78",
3
+ "version": "1.1.80",
4
4
  "description": "Xuda Notification Server Module",
5
5
  "main": "index.js",
6
6
  "dependencies": {