@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.
- package/index.mjs +184 -149
- package/index_msa.mjs +4 -0
- 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
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
-
|
|
82
|
-
|
|
83
|
-
|
|
81
|
+
if (!type) {
|
|
82
|
+
return { code: -1, data: 'error: type is mandatory field' };
|
|
83
|
+
}
|
|
84
84
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
|
-
|
|
104
|
+
let send_result = {};
|
|
105
105
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
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
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
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
|
-
|
|
185
|
-
|
|
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
|
-
|
|
199
|
+
title: subject,
|
|
200
|
+
body,
|
|
201
|
+
image: icon,
|
|
196
202
|
},
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
'mutable-content': 1,
|
|
203
|
+
token: fcm_token,
|
|
204
|
+
android: {
|
|
205
|
+
notification: {
|
|
206
|
+
imageUrl: icon,
|
|
202
207
|
},
|
|
203
208
|
},
|
|
204
|
-
|
|
205
|
-
|
|
209
|
+
apns: {
|
|
210
|
+
payload: {
|
|
211
|
+
aps: {
|
|
212
|
+
'mutable-content': 1,
|
|
213
|
+
},
|
|
214
|
+
},
|
|
215
|
+
fcm_options: {
|
|
216
|
+
image: icon,
|
|
217
|
+
},
|
|
206
218
|
},
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
219
|
+
webpush: {
|
|
220
|
+
headers: {
|
|
221
|
+
image: icon,
|
|
222
|
+
},
|
|
211
223
|
},
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
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
|
-
|
|
229
|
-
console.error(err);
|
|
230
|
-
}
|
|
231
|
-
break;
|
|
242
|
+
break;
|
|
232
243
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
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
|
};
|