@xuda.io/notification_module 1.1.111 → 1.1.113
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 +154 -149
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -64,180 +64,185 @@ 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
|
-
|
|
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
|
+
}
|
|
77
79
|
}
|
|
78
|
-
}
|
|
79
80
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
81
|
+
if (!type) {
|
|
82
|
+
return { code: -1, data: 'error: type is mandatory field' };
|
|
83
|
+
}
|
|
83
84
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
+
};
|
|
94
101
|
}
|
|
95
|
-
} else {
|
|
96
|
-
return {
|
|
97
|
-
code: -1,
|
|
98
|
-
data: `${topic} topic for ${type} not exist in notification_categories`,
|
|
99
|
-
};
|
|
100
102
|
}
|
|
101
|
-
}
|
|
102
103
|
|
|
103
|
-
|
|
104
|
+
let send_result = {};
|
|
104
105
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
send_result[uid] = true;
|
|
116
|
-
const uuid = await _common.xuda_get_uuid('notification');
|
|
117
|
-
obj = {
|
|
118
|
-
_id: uuid,
|
|
119
|
-
docType: 'notification',
|
|
120
|
-
type,
|
|
121
|
-
app_id: to_app_id,
|
|
122
|
-
uid,
|
|
123
|
-
subject,
|
|
124
|
-
body,
|
|
125
|
-
delivery_method: delivery_method_item,
|
|
126
|
-
display_type,
|
|
127
|
-
read: false,
|
|
128
|
-
ref: ref || uuid,
|
|
129
|
-
date_created_ts: Date.now(),
|
|
130
|
-
date_created: Date.now(),
|
|
131
|
-
sender_uid,
|
|
132
|
-
stat: 3,
|
|
133
|
-
stat_ts: Date.now(),
|
|
134
|
-
system,
|
|
135
|
-
topic,
|
|
136
|
-
params,
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
switch (delivery_method_item) {
|
|
140
|
-
case 'email':
|
|
141
|
-
obj.email = email || account_ret.data.account_info ? account_ret.data.account_info.email : '';
|
|
142
|
-
if (!obj.email) {
|
|
143
|
-
send_result[uid] = false;
|
|
144
|
-
return;
|
|
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`);
|
|
145
114
|
}
|
|
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;
|
|
146
|
+
}
|
|
146
147
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
});
|
|
154
|
-
break;
|
|
155
|
-
|
|
156
|
-
case 'banner':
|
|
157
|
-
case 'consent':
|
|
158
|
-
case 'alert':
|
|
159
|
-
ws_dashboard_msa.notification({
|
|
160
|
-
to: uid,
|
|
161
|
-
data: obj,
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
break;
|
|
165
|
-
case 'push':
|
|
166
|
-
try {
|
|
167
|
-
const ret = await db_module.find_couch_query('xuda_sessions', {
|
|
168
|
-
selector: {
|
|
169
|
-
docType: 'gtp_session',
|
|
170
|
-
stat: { $lt: 3 },
|
|
171
|
-
uid,
|
|
172
|
-
fcm_token: { $gt: '' },
|
|
173
|
-
},
|
|
148
|
+
email_msa.send_email({
|
|
149
|
+
email: obj.email,
|
|
150
|
+
subject,
|
|
151
|
+
body,
|
|
152
|
+
ref: obj._id,
|
|
153
|
+
app_id: obj.app_id,
|
|
174
154
|
});
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
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
|
+
},
|
|
175
|
+
});
|
|
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);
|
|
181
|
+
}
|
|
180
182
|
}
|
|
181
183
|
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
const message = {
|
|
186
|
-
notification: {
|
|
187
|
-
title: subject,
|
|
188
|
-
body,
|
|
189
|
-
image: icon,
|
|
190
|
-
},
|
|
191
|
-
token: fcm_token,
|
|
192
|
-
android: {
|
|
184
|
+
for await (let fcm_token of fcm_token_arr) {
|
|
185
|
+
if (fcm_token) {
|
|
186
|
+
const message = {
|
|
193
187
|
notification: {
|
|
194
|
-
|
|
188
|
+
title: subject,
|
|
189
|
+
body,
|
|
190
|
+
image: icon,
|
|
195
191
|
},
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
'mutable-content': 1,
|
|
192
|
+
token: fcm_token,
|
|
193
|
+
android: {
|
|
194
|
+
notification: {
|
|
195
|
+
imageUrl: icon,
|
|
201
196
|
},
|
|
202
197
|
},
|
|
203
|
-
|
|
204
|
-
|
|
198
|
+
apns: {
|
|
199
|
+
payload: {
|
|
200
|
+
aps: {
|
|
201
|
+
'mutable-content': 1,
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
fcm_options: {
|
|
205
|
+
image: icon,
|
|
206
|
+
},
|
|
205
207
|
},
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
208
|
+
webpush: {
|
|
209
|
+
headers: {
|
|
210
|
+
image: icon,
|
|
211
|
+
},
|
|
210
212
|
},
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
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);
|
|
225
|
+
}
|
|
224
226
|
}
|
|
225
227
|
}
|
|
228
|
+
} catch (err) {
|
|
229
|
+
console.error(err);
|
|
226
230
|
}
|
|
227
|
-
|
|
228
|
-
console.error(err);
|
|
229
|
-
}
|
|
230
|
-
break;
|
|
231
|
+
break;
|
|
231
232
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
233
|
+
default:
|
|
234
|
+
send_result[uid] = false;
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
obj.sent = send_result[uid];
|
|
238
|
+
const ret = await db_module.save_couch_doc('xuda_notification', obj);
|
|
235
239
|
}
|
|
236
|
-
obj.sent = send_result[uid];
|
|
237
|
-
const ret = await db_module.save_couch_doc('xuda_notification', obj);
|
|
238
240
|
}
|
|
241
|
+
return { code: 111, data: obj };
|
|
242
|
+
} catch (err) {
|
|
243
|
+
console.error(err);
|
|
244
|
+
return { code: -111, data: err.message };
|
|
239
245
|
}
|
|
240
|
-
return { code: 1, data: obj };
|
|
241
246
|
};
|
|
242
247
|
|
|
243
248
|
export const get_notifications = async (req) => {
|