@xuda.io/notification_module 1.1.112 → 1.1.114

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 (3) hide show
  1. package/index.mjs +184 -149
  2. package/index_msa.mjs +4 -0
  3. package/package.json +1 -1
package/index.mjs CHANGED
@@ -64,181 +64,196 @@ export const update_notification_status = async (req) => {
64
64
  };
65
65
  //, _ch= global[`_notification_module_ch`]
66
66
  export const submit_notification = async (req) => {
67
- debugger;
68
- let { type, app_id, to_app_id, uid_arr, subject = '', body = '', delivery_method = [], display_type = 'info', ref, email, sender_uid, system, topic, params } = req;
69
- let obj = {};
70
- const { notification_categories } = await import(path.join(process.env.XUDA_HOME, 'notification_templates.mjs'));
71
-
72
- let icon = 'https://dev.xuda.ai/dist/images/xuda_ico.png';
73
- if (app_id) {
74
- const app_id_reference = await _common.get_project_app_id(app_id, true);
75
- const app_ret = await db_module.get_couch_doc('xuda_master', app_id_reference);
76
- if (app_ret.code > -1) {
77
- icon = app_ret.data.app_pic;
67
+ try {
68
+ let { type, app_id, to_app_id, uid_arr, subject = '', body = '', delivery_method = [], display_type = 'info', ref, email, sender_uid, system, topic, params } = req;
69
+ let obj = {};
70
+ const { notification_categories } = await import(path.join(process.env.XUDA_HOME, 'notification_templates.mjs'));
71
+
72
+ let icon = 'https://dev.xuda.ai/dist/images/xuda_ico.png';
73
+ if (app_id) {
74
+ const app_id_reference = await _common.get_project_app_id(app_id, true);
75
+ const app_ret = await db_module.get_couch_doc('xuda_master', app_id_reference);
76
+ if (app_ret.code > -1) {
77
+ icon = app_ret.data.app_pic;
78
+ }
78
79
  }
79
- }
80
80
 
81
- if (!type) {
82
- return { code: -1, data: 'error: type is mandatory field' };
83
- }
81
+ if (!type) {
82
+ return { code: -1, data: 'error: type is mandatory field' };
83
+ }
84
84
 
85
- if (topic) {
86
- const _topic = notification_categories?.[type]?.topics?.[topic];
87
- if (_topic) {
88
- if (!subject) {
89
- subject = _.template(_topic.subject)(params);
90
- }
91
- delivery_method = _topic.delivery_method;
92
- display_type = _topic.display_type;
93
- if (!body) {
94
- body = _.template(_topic.body)(params);
85
+ if (topic) {
86
+ const _topic = notification_categories?.[type]?.topics?.[topic];
87
+ if (_topic) {
88
+ if (!subject) {
89
+ subject = _.template(_topic.subject)(params);
90
+ }
91
+ delivery_method = _topic.delivery_method;
92
+ display_type = _topic.display_type;
93
+ if (!body) {
94
+ body = _.template(_topic.body)(params);
95
+ }
96
+ } else {
97
+ return {
98
+ code: -1,
99
+ data: `${topic} topic for ${type} not exist in notification_categories`,
100
+ };
95
101
  }
96
- } else {
97
- return {
98
- code: -1,
99
- data: `${topic} topic for ${type} not exist in notification_categories`,
100
- };
101
102
  }
102
- }
103
103
 
104
- let send_result = {};
104
+ let send_result = {};
105
105
 
106
- for await (let uid of uid_arr) {
107
- obj = {};
108
- for await (let delivery_method_item of delivery_method) {
109
- let account_ret;
110
- if (!system) {
111
- account_ret = await db_module.get_couch_doc('xuda_accounts', uid);
112
- if (account_ret.code < 0) {
113
- throw new Error(`user ${uid} not found`);
106
+ for await (let uid of uid_arr) {
107
+ obj = {};
108
+ for await (let delivery_method_item of delivery_method) {
109
+ let account_ret;
110
+ if (!system) {
111
+ account_ret = await db_module.get_couch_doc('xuda_accounts', uid);
112
+ if (account_ret.code < 0) {
113
+ throw new Error(`user ${uid} not found`);
114
+ }
114
115
  }
115
- }
116
- send_result[uid] = true;
117
- const uuid = await _common.xuda_get_uuid('notification');
118
- obj = {
119
- _id: uuid,
120
- docType: 'notification',
121
- type,
122
- app_id: to_app_id,
123
- uid,
124
- subject,
125
- body,
126
- delivery_method: delivery_method_item,
127
- display_type,
128
- read: false,
129
- ref: ref || uuid,
130
- date_created_ts: Date.now(),
131
- date_created: Date.now(),
132
- sender_uid,
133
- stat: 3,
134
- stat_ts: Date.now(),
135
- system,
136
- topic,
137
- params,
138
- };
139
-
140
- switch (delivery_method_item) {
141
- case 'email':
142
- obj.email = email || account_ret.data.account_info ? account_ret.data.account_info.email : '';
143
- if (!obj.email) {
144
- send_result[uid] = false;
145
- return;
116
+ send_result[uid] = true;
117
+ const uuid = await _common.xuda_get_uuid('notification');
118
+ obj = {
119
+ _id: uuid,
120
+ docType: 'notification',
121
+ type,
122
+ app_id: to_app_id,
123
+ uid,
124
+ subject,
125
+ body,
126
+ delivery_method: delivery_method_item,
127
+ display_type,
128
+ read: false,
129
+ ref: ref || uuid,
130
+ date_created_ts: Date.now(),
131
+ date_created: Date.now(),
132
+ sender_uid,
133
+ stat: 3,
134
+ stat_ts: Date.now(),
135
+ system,
136
+ topic,
137
+ params,
138
+ };
139
+
140
+ switch (delivery_method_item) {
141
+ case 'email': {
142
+ // Honor an explicit email, else fall back to the recipient's account.
143
+ // System notifications skip the lookup above, so resolve it here
144
+ // (superuser alert recipients have real accounts with emails).
145
+ let resolved_email = email;
146
+ if (!resolved_email) {
147
+ if (!account_ret) {
148
+ account_ret = await db_module.get_couch_doc('xuda_accounts', uid).catch(() => null);
149
+ }
150
+ resolved_email = account_ret?.data?.account_info?.email || '';
151
+ }
152
+ obj.email = resolved_email;
153
+ if (!obj.email) {
154
+ send_result[uid] = false;
155
+ return;
156
+ }
157
+
158
+ email_msa.send_email({
159
+ email: obj.email,
160
+ subject,
161
+ body,
162
+ ref: obj._id,
163
+ app_id: obj.app_id,
164
+ });
165
+ break;
146
166
  }
147
167
 
148
- email_msa.send_email({
149
- email: obj.email,
150
- subject,
151
- body,
152
- ref: obj._id,
153
- app_id: obj.app_id,
154
- });
155
- break;
156
-
157
- case 'banner':
158
- case 'consent':
159
- case 'alert':
160
- ws_dashboard_msa.notification({
161
- to: uid,
162
- data: obj,
163
- });
164
-
165
- break;
166
- case 'push':
167
- try {
168
- const ret = await db_module.find_couch_query('xuda_sessions', {
169
- selector: {
170
- docType: 'gtp_session',
171
- stat: { $lt: 3 },
172
- uid,
173
- fcm_token: { $gt: '' },
174
- },
168
+ case 'banner':
169
+ case 'consent':
170
+ case 'alert':
171
+ ws_dashboard_msa.notification({
172
+ to: uid,
173
+ data: obj,
175
174
  });
176
- let fcm_token_arr = [];
177
- for await (let session of ret.docs) {
178
- if (session.fcm_token) {
179
- if (!fcm_token_arr.includes(session.fcm_token)) {
180
- fcm_token_arr.push(session.fcm_token);
175
+
176
+ break;
177
+ case 'push':
178
+ try {
179
+ const ret = await db_module.find_couch_query('xuda_sessions', {
180
+ selector: {
181
+ docType: 'gtp_session',
182
+ stat: { $lt: 3 },
183
+ uid,
184
+ fcm_token: { $gt: '' },
185
+ },
186
+ });
187
+ let fcm_token_arr = [];
188
+ for await (let session of ret.docs) {
189
+ if (session.fcm_token) {
190
+ if (!fcm_token_arr.includes(session.fcm_token)) {
191
+ fcm_token_arr.push(session.fcm_token);
192
+ }
181
193
  }
182
194
  }
183
- }
184
- for await (let fcm_token of fcm_token_arr) {
185
- if (fcm_token) {
186
- const message = {
187
- notification: {
188
- title: subject,
189
- body,
190
- image: icon,
191
- },
192
- token: fcm_token,
193
- android: {
195
+ for await (let fcm_token of fcm_token_arr) {
196
+ if (fcm_token) {
197
+ const message = {
194
198
  notification: {
195
- imageUrl: icon,
199
+ title: subject,
200
+ body,
201
+ image: icon,
196
202
  },
197
- },
198
- apns: {
199
- payload: {
200
- aps: {
201
- 'mutable-content': 1,
203
+ token: fcm_token,
204
+ android: {
205
+ notification: {
206
+ imageUrl: icon,
202
207
  },
203
208
  },
204
- fcm_options: {
205
- image: icon,
209
+ apns: {
210
+ payload: {
211
+ aps: {
212
+ 'mutable-content': 1,
213
+ },
214
+ },
215
+ fcm_options: {
216
+ image: icon,
217
+ },
206
218
  },
207
- },
208
- webpush: {
209
- headers: {
210
- image: icon,
219
+ webpush: {
220
+ headers: {
221
+ image: icon,
222
+ },
211
223
  },
212
- },
213
- };
214
- try {
215
- messaging
216
- .send(message)
217
- .then((response) => {
218
- // console.log("Successfully sent message:", response);
219
- })
220
- .catch((error) => {
221
- console.error('Error sending message:', error);
222
- });
223
- } catch (err) {
224
- console.error(err);
224
+ };
225
+ try {
226
+ messaging
227
+ .send(message)
228
+ .then((response) => {
229
+ // console.log("Successfully sent message:", response);
230
+ })
231
+ .catch((error) => {
232
+ console.error('Error sending message:', error);
233
+ });
234
+ } catch (err) {
235
+ console.error(err);
236
+ }
225
237
  }
226
238
  }
239
+ } catch (err) {
240
+ console.error(err);
227
241
  }
228
- } catch (err) {
229
- console.error(err);
230
- }
231
- break;
242
+ break;
232
243
 
233
- default:
234
- send_result[uid] = false;
235
- break;
244
+ default:
245
+ send_result[uid] = false;
246
+ break;
247
+ }
248
+ obj.sent = send_result[uid];
249
+ const ret = await db_module.save_couch_doc('xuda_notification', obj);
236
250
  }
237
- obj.sent = send_result[uid];
238
- const ret = await db_module.save_couch_doc('xuda_notification', obj);
239
251
  }
252
+ return { code: 111, data: obj };
253
+ } catch (err) {
254
+ console.error(err);
255
+ return { code: -111, data: err.message };
240
256
  }
241
- return { code: 1, data: obj };
242
257
  };
243
258
 
244
259
  export const get_notifications = async (req) => {
@@ -427,6 +442,26 @@ export const submit_superuser_notification = async (req) => {
427
442
  }
428
443
  };
429
444
 
445
+ // Ops/admin alert for the AI error resolver. Renders the `error_resolver`
446
+ // template (notification_templates.mjs -> system category) and delivers to the
447
+ // superuser accounts (which have real emails) via the standard notification path.
448
+ export const submit_error_resolver_alert = async (req) => {
449
+ const { params = {} } = req || {};
450
+ try {
451
+ const recipients = _conf.superuser_account_ids || [];
452
+ if (!recipients.length) return { code: -1, data: 'no superuser recipients configured' };
453
+ return await submit_notification({
454
+ type: 'system',
455
+ topic: 'error_resolver',
456
+ uid_arr: recipients,
457
+ system: true,
458
+ params,
459
+ });
460
+ } catch (err) {
461
+ return { code: -1, data: err.message };
462
+ }
463
+ };
464
+
430
465
  export const submit_app_notification = async (req) => {
431
466
  const { uid, subject, delivery_method, body, display_type, to_uid = [], to_app_id, app_id, system, type } = req;
432
467
  try {
package/index_msa.mjs CHANGED
@@ -41,6 +41,10 @@ export const submit_superuser_notification = function (...args) {
41
41
  broker.send_to_queue_async("submit_superuser_notification", ...args);
42
42
  };
43
43
 
44
+ export const submit_error_resolver_alert = function (...args) {
45
+ broker.send_to_queue_async("submit_error_resolver_alert", ...args);
46
+ };
47
+
44
48
  export const submit_app_notification = function (...args) {
45
49
  broker.send_to_queue_async("submit_app_notification", ...args);
46
50
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/notification_module",
3
- "version": "1.1.112",
3
+ "version": "1.1.114",
4
4
  "description": "Xuda Notification Server Module",
5
5
  "main": "index.mjs",
6
6
  "dependencies": {