@xuda.io/notification_module 1.1.112 → 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 -150
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -64,181 +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
|
-
|
|
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
|
-
|
|
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;
|
|
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`);
|
|
146
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
|
+
}
|
|
147
147
|
|
|
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
|
-
},
|
|
148
|
+
email_msa.send_email({
|
|
149
|
+
email: obj.email,
|
|
150
|
+
subject,
|
|
151
|
+
body,
|
|
152
|
+
ref: obj._id,
|
|
153
|
+
app_id: obj.app_id,
|
|
175
154
|
});
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
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
|
+
}
|
|
181
182
|
}
|
|
182
183
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
const message = {
|
|
187
|
-
notification: {
|
|
188
|
-
title: subject,
|
|
189
|
-
body,
|
|
190
|
-
image: icon,
|
|
191
|
-
},
|
|
192
|
-
token: fcm_token,
|
|
193
|
-
android: {
|
|
184
|
+
for await (let fcm_token of fcm_token_arr) {
|
|
185
|
+
if (fcm_token) {
|
|
186
|
+
const message = {
|
|
194
187
|
notification: {
|
|
195
|
-
|
|
188
|
+
title: subject,
|
|
189
|
+
body,
|
|
190
|
+
image: icon,
|
|
196
191
|
},
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
'mutable-content': 1,
|
|
192
|
+
token: fcm_token,
|
|
193
|
+
android: {
|
|
194
|
+
notification: {
|
|
195
|
+
imageUrl: icon,
|
|
202
196
|
},
|
|
203
197
|
},
|
|
204
|
-
|
|
205
|
-
|
|
198
|
+
apns: {
|
|
199
|
+
payload: {
|
|
200
|
+
aps: {
|
|
201
|
+
'mutable-content': 1,
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
fcm_options: {
|
|
205
|
+
image: icon,
|
|
206
|
+
},
|
|
206
207
|
},
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
208
|
+
webpush: {
|
|
209
|
+
headers: {
|
|
210
|
+
image: icon,
|
|
211
|
+
},
|
|
211
212
|
},
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
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
|
+
}
|
|
225
226
|
}
|
|
226
227
|
}
|
|
228
|
+
} catch (err) {
|
|
229
|
+
console.error(err);
|
|
227
230
|
}
|
|
228
|
-
|
|
229
|
-
console.error(err);
|
|
230
|
-
}
|
|
231
|
-
break;
|
|
231
|
+
break;
|
|
232
232
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
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);
|
|
236
239
|
}
|
|
237
|
-
obj.sent = send_result[uid];
|
|
238
|
-
const ret = await db_module.save_couch_doc('xuda_notification', obj);
|
|
239
240
|
}
|
|
241
|
+
return { code: 111, data: obj };
|
|
242
|
+
} catch (err) {
|
|
243
|
+
console.error(err);
|
|
244
|
+
return { code: -111, data: err.message };
|
|
240
245
|
}
|
|
241
|
-
return { code: 1, data: obj };
|
|
242
246
|
};
|
|
243
247
|
|
|
244
248
|
export const get_notifications = async (req) => {
|