@xuda.io/notification_module 1.1.84 → 1.1.86

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.
@@ -1,24 +1,30 @@
1
+ import { createRequire } from 'module';
2
+ import path from 'path';
3
+ import _ from 'lodash';
4
+ import admin from 'firebase-admin';
5
+
1
6
  console.log('Notifications Module loaded...');
2
- const path = require('path');
3
- global._conf = require(path.join(process.env.XUDA_HOME, process.env.XUDA_CONFIG));
4
7
 
5
- const _ = require('lodash');
8
+ // Initialize require for dynamic paths
9
+ const require = createRequire(import.meta.url);
10
+
11
+ // Load Globals
12
+ global._conf = require(path.join(process.env.XUDA_HOME, process.env.XUDA_CONFIG));
6
13
 
7
14
  const _common = require(path.join(process.env.XUDA_HOME, 'common', 'xuda_node_common.mjs'));
8
15
  const module_path = path.join(process.env.XUDA_HOME, 'cpi') + (!_conf.is_debug ? '/node_modules/@xuda.io' : '');
9
16
  const db_module = require(`${module_path}/db_module`);
10
17
 
11
18
  //////////////////// FCM /////////////////////
12
- const admin = require('firebase-admin');
13
19
  const serviceAccount = _conf.firebase_serviceAccount;
14
20
  admin.initializeApp({
15
21
  credential: admin.credential.cert(serviceAccount),
16
- databaseURL: 'https://your-project-id.firebaseio.com', // Replace with your Firebase project's database URL
22
+ databaseURL: 'https://your-project-id.firebaseio.com',
17
23
  });
18
24
  const messaging = admin.messaging();
19
25
  //////////////////////////////////////////////
20
26
 
21
- exports.get_new_notification_count = async function (req) {
27
+ export const get_new_notification_count = async (req) => {
22
28
  return await db_module.get_couch_view('xuda_notification', 'new_notifications_count', {
23
29
  key: [req.uid],
24
30
  reduce: true,
@@ -26,9 +32,9 @@ exports.get_new_notification_count = async function (req) {
26
32
  });
27
33
  };
28
34
 
29
- exports.set_notification_read = async function (req, job_id, header) {
35
+ export const set_notification_read = async (req, job_id, header) => {
30
36
  const ret = await db_module.get_couch_doc('xuda_notification', req.notification_id);
31
- var notification_doc = ret.data;
37
+ let notification_doc = ret.data;
32
38
 
33
39
  if (notification_doc.system) {
34
40
  return { code: -1, data: 'system notification not allow read' };
@@ -40,10 +46,10 @@ exports.set_notification_read = async function (req, job_id, header) {
40
46
  return await db_module.save_couch_doc('xuda_notification', notification_doc);
41
47
  };
42
48
 
43
- exports.update_notification_status = async function (req) {
49
+ export const update_notification_status = async (req) => {
44
50
  const { notification_id, stat, uid } = req;
45
51
  const ret = await db_module.get_couch_doc('xuda_notification', notification_id);
46
- var notification_doc = ret.data;
52
+ let notification_doc = ret.data;
47
53
  notification_doc.stat_ts = Date.now();
48
54
  notification_doc.stat = stat;
49
55
 
@@ -64,16 +70,12 @@ exports.update_notification_status = async function (req) {
64
70
  return await db_module.save_couch_doc('xuda_notification', notification_doc);
65
71
  };
66
72
 
67
- exports.submit_notification = async function (req, _ch = global[`_notification_module_ch`]) {
68
- var { type, app_id, to_app_id, uid_arr, subject = '', body = '', delivery_method = [], display_type = 'info', ref, email, sender_uid, system, topic, params } = req;
69
-
70
- // if (!uid) {
71
- // return { code: -1, data: "error: uid is mandatory field" };
72
- // }
73
+ export const submit_notification = async (req, _ch = global[`_notification_module_ch`]) => {
74
+ let { type, app_id, to_app_id, uid_arr, subject = '', body = '', delivery_method = [], display_type = 'info', ref, email, sender_uid, system, topic, params } = req;
73
75
 
74
76
  const { notification_categories } = await import(path.join(process.env.XUDA_HOME, 'notification_templates.mjs'));
75
77
 
76
- var icon = 'https://dev.xuda.io/dist/images/xuda_ico.png';
78
+ let icon = 'https://dev.xuda.io/dist/images/xuda_ico.png';
77
79
  if (app_id) {
78
80
  const app_id_reference = await _common.get_project_app_id(app_id, true);
79
81
  const app_ret = await db_module.get_couch_doc('xuda_master', app_id_reference);
@@ -105,11 +107,11 @@ exports.submit_notification = async function (req, _ch = global[`_notification_m
105
107
  }
106
108
  }
107
109
 
108
- var send_result = {};
110
+ let send_result = {};
109
111
 
110
112
  for await (let uid of uid_arr) {
111
113
  for await (let delivery_method_item of delivery_method) {
112
- var account_ret;
114
+ let account_ret;
113
115
  if (!system) {
114
116
  account_ret = await db_module.get_couch_doc('xuda_accounts', uid);
115
117
  if (account_ret.code < 0) {
@@ -118,7 +120,7 @@ exports.submit_notification = async function (req, _ch = global[`_notification_m
118
120
  }
119
121
  send_result[uid] = true;
120
122
  const uuid = await _common.xuda_get_uuid('notification');
121
- var obj = {
123
+ let obj = {
122
124
  _id: uuid,
123
125
  docType: 'notification',
124
126
  type,
@@ -145,7 +147,7 @@ exports.submit_notification = async function (req, _ch = global[`_notification_m
145
147
  obj.email = email || account_ret.data.account_info ? account_ret.data.account_info.email : '';
146
148
  if (!obj.email) {
147
149
  send_result[uid] = false;
148
- return; //{ code: -1, data: "error: email is empty" };
150
+ return;
149
151
  }
150
152
  const email_module = require(`${module_path}/email_module`);
151
153
  email_module.send_email({
@@ -184,7 +186,7 @@ exports.submit_notification = async function (req, _ch = global[`_notification_m
184
186
  fcm_token: { $gt: '' },
185
187
  },
186
188
  });
187
- var fcm_token_arr = [];
189
+ let fcm_token_arr = [];
188
190
  for await (let session of ret.docs) {
189
191
  if (session.fcm_token) {
190
192
  if (!fcm_token_arr.includes(session.fcm_token)) {
@@ -252,24 +254,7 @@ exports.submit_notification = async function (req, _ch = global[`_notification_m
252
254
  return { code: 1, data: obj };
253
255
  };
254
256
 
255
- exports.get_notifications = async function (req) {
256
- // uid
257
- // app_id
258
- // search
259
- // date_from
260
- // date_to
261
- // type
262
- // delivery_method
263
- // read
264
- // skip
265
- // limit
266
- // bookmark
267
- // ref
268
- // display_type
269
- // system
270
- // outbox
271
- // to_app_id
272
-
257
+ export const get_notifications = async (req) => {
273
258
  if (req.date_to < req.date_from) {
274
259
  return {
275
260
  code: -2,
@@ -386,11 +371,11 @@ exports.get_notifications = async function (req) {
386
371
  }
387
372
 
388
373
  try {
389
- var ret = await db_module.find_couch_query('xuda_notification', opt);
374
+ let ret = await db_module.find_couch_query('xuda_notification', opt);
390
375
  let data = { docs: [] };
391
376
  if (ret.docs) {
392
377
  for await (let doc of ret.docs) {
393
- if (await module.exports.notifications_validation_fx(doc._id)) {
378
+ if (await notifications_validation_fx(doc._id)) {
394
379
  data.docs.push(doc);
395
380
  }
396
381
  }
@@ -408,19 +393,8 @@ exports.get_notifications = async function (req) {
408
393
  }
409
394
  };
410
395
 
411
- exports.submit_superuser_notification = async function (req) {
412
- const {
413
- uid,
414
- type,
415
- subject,
416
- delivery_method,
417
- body,
418
- display_type, // info,warn/error
419
- to_uid = [],
420
- to_app_id,
421
- app_id,
422
- system,
423
- } = req;
396
+ export const submit_superuser_notification = async (req) => {
397
+ const { uid, type, subject, delivery_method, body, display_type, to_uid = [], to_app_id, app_id, system } = req;
424
398
  try {
425
399
  if (!_conf.superuser_account_ids.includes(uid)) {
426
400
  throw new Error('user is not authorized for this method');
@@ -460,29 +434,15 @@ exports.submit_superuser_notification = async function (req) {
460
434
  system,
461
435
  };
462
436
 
463
- return module.exports.submit_notification(msg_obj);
437
+ return submit_notification(msg_obj);
464
438
  } catch (err) {
465
439
  return { code: -1, data: err.message };
466
440
  }
467
441
  };
468
- exports.submit_app_notification = async function (req) {
469
- const {
470
- uid,
471
- subject,
472
- delivery_method,
473
- body,
474
- display_type, // info,warn/error
475
- to_uid = [],
476
- to_app_id,
477
- app_id,
478
- system,
479
- type,
480
- } = req;
481
- try {
482
- // if (!_conf.superuser_account_ids.includes(uid)) {
483
- // throw new Error("user is not authorized for this method");
484
- // }
485
442
 
443
+ export const submit_app_notification = async (req) => {
444
+ const { uid, subject, delivery_method, body, display_type, to_uid = [], to_app_id, app_id, system, type } = req;
445
+ try {
486
446
  if (!_.isArray(to_uid)) {
487
447
  throw new Error('to_uid must be in the form of an array');
488
448
  }
@@ -506,10 +466,6 @@ exports.submit_app_notification = async function (req) {
506
466
  throw new Error("some of the uid's are not authorized for the selected app");
507
467
  }
508
468
 
509
- // if (!_conf.superuser_account_ids.includes(uid)) {
510
- // throw new Error("user is not authorized for this method");
511
- // }
512
-
513
469
  let msg_obj = {
514
470
  type: _conf.superuser_account_ids.includes(uid) && type ? type : 'user',
515
471
  uid_arr: to_uid,
@@ -524,16 +480,16 @@ exports.submit_app_notification = async function (req) {
524
480
  system,
525
481
  };
526
482
 
527
- return module.exports.submit_notification(msg_obj);
483
+ return submit_notification(msg_obj);
528
484
  } catch (err) {
529
485
  return { code: -1, data: err.message };
530
486
  }
531
487
  };
532
488
 
533
- exports.update_system_notification = async function (req) {
489
+ export const update_system_notification = async (req) => {
534
490
  const { display_type, subject, body, delivery_method, uid, type } = req;
535
491
  const ret = await db_module.get_couch_doc('xuda_notification', req._id);
536
- var notification_doc = ret.data;
492
+ let notification_doc = ret.data;
537
493
 
538
494
  if (!notification_doc.system) {
539
495
  return { code: -1, data: 'only system notification allow update' };
@@ -572,7 +528,7 @@ exports.update_system_notification = async function (req) {
572
528
  return await db_module.save_couch_doc('xuda_notification', notification_doc);
573
529
  };
574
530
 
575
- exports.submit_topic_app_notification = async function (req, _ch) {
531
+ export const submit_topic_app_notification = async (req, _ch) => {
576
532
  const { uid, topic, to_uid = [], to_app_id, app_id, system, type, params } = req;
577
533
  try {
578
534
  if (!_.isArray(to_uid)) {
@@ -609,62 +565,26 @@ exports.submit_topic_app_notification = async function (req, _ch) {
609
565
  params,
610
566
  };
611
567
 
612
- return module.exports.submit_notification(msg_obj, _ch);
568
+ return submit_notification(msg_obj, _ch);
613
569
  } catch (err) {
614
570
  return { code: -1, data: err.message };
615
571
  }
616
572
  };
617
573
 
618
- exports.get_system_notifications = async function (req) {
574
+ export const get_system_notifications = async (req) => {
619
575
  req.system = true;
620
576
  req.to_app_id = '';
621
577
  req.read = false;
622
- return await module.exports.get_notifications(req);
578
+ return await get_notifications(req);
623
579
  };
624
580
 
625
- // setTimeout(async () => {
626
- // // const app_id = "vps4d6ec68e01d97ef65a8bd86088b26ca2",
627
- // // uid = "341cf2d32991a68e4d8aaba6c16e15cc",
628
- // // ip = "192.11.22.333",
629
- // // name = "Boaz Avrahami";
630
- // // const aa = await module.exports.submit_notification({
631
- // // type: "deploy",
632
- // // app_id: app_id,
633
- // // uid_arr: [uid],
634
- // // topic: "welcome_aboard_vps",
635
- // // params: {
636
- // // name: name,
637
- // // ip: ip,
638
- // // dashboard_url: `https://xuda.io/dashboard/project/${app_id}/admin/overview`,
639
- // // dashboard_ssh_url: `https://xuda.io/dashboard/project/${app_id}/admin/overview`,
640
- // // dashboard_ssh_assign_url: `https://xuda.io/dashboard/settings/ssh_keys`,
641
- // // support_url: `https://xuda.io/schedule/sales?topic=Sales&subtopic=General+Inquiries`,
642
- // // },
643
- // // ref: null,
644
- // // email: null,
645
- // // });
646
- // // console.log(aa);
647
- // }, 2000);
648
-
649
- // class notifications_validation_class {
650
- // constructor(name) {
651
- // this.name = name;
652
- // }
653
-
654
- // new_version_available(params) {
655
- // console.log(`Hello, my name is ${this.name}`);
656
- // }
657
- // }
658
-
659
- // module.exports = notifications_validation_class;
660
-
661
- exports.notifications_validation_fx = async function (notification_id) {
581
+ export const notifications_validation_fx = async (notification_id) => {
662
582
  let { code, data: doc } = await db_module.get_couch_doc('xuda_notification', notification_id);
663
- const delete_notification = async function () {
583
+ const delete_notification = async () => {
664
584
  doc.stat = 4;
665
585
  await db_module.save_couch_doc('xuda_notification', doc);
666
586
  };
667
- const read_notification = async function () {
587
+ const read_notification = async () => {
668
588
  doc.read = true;
669
589
  doc.read_ts = Date.now();
670
590
  doc.read_note = 'read by notifications_validation_fx';
@@ -678,7 +598,7 @@ exports.notifications_validation_fx = async function (notification_id) {
678
598
  return false;
679
599
  }
680
600
  const fx = {
681
- new_version_available: async function () {
601
+ new_version_available: async () => {
682
602
  const { code, data: app_doc } = await db_module.get_app_obj(doc.app_id);
683
603
  if (app_doc.app_build_id >= doc.params.build) {
684
604
  await read_notification();
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@xuda.io/notification_module",
3
- "version": "1.1.84",
3
+ "version": "1.1.86",
4
4
  "description": "Xuda Notification Server Module",
5
- "main": "index.js",
5
+ "main": "index.mjs",
6
6
  "dependencies": {
7
7
  "lodash": "^4.17.21",
8
8
  "firebase-admin": "^12.0.0"