@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.
Files changed (2) hide show
  1. package/index.mjs +154 -149
  2. 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
- let { type, app_id, to_app_id, uid_arr, subject = '', body = '', delivery_method = [], display_type = 'info', ref, email, sender_uid, system, topic, params } = req;
68
- let obj = {};
69
- const { notification_categories } = await import(path.join(process.env.XUDA_HOME, 'notification_templates.mjs'));
70
-
71
- let icon = 'https://dev.xuda.ai/dist/images/xuda_ico.png';
72
- if (app_id) {
73
- const app_id_reference = await _common.get_project_app_id(app_id, true);
74
- const app_ret = await db_module.get_couch_doc('xuda_master', app_id_reference);
75
- if (app_ret.code > -1) {
76
- 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
+ }
77
79
  }
78
- }
79
80
 
80
- if (!type) {
81
- return { code: -1, data: 'error: type is mandatory field' };
82
- }
81
+ if (!type) {
82
+ return { code: -1, data: 'error: type is mandatory field' };
83
+ }
83
84
 
84
- if (topic) {
85
- const _topic = notification_categories?.[type]?.topics?.[topic];
86
- if (_topic) {
87
- if (!subject) {
88
- subject = _.template(_topic.subject)(params);
89
- }
90
- delivery_method = _topic.delivery_method;
91
- display_type = _topic.display_type;
92
- if (!body) {
93
- 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
+ };
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
- let send_result = {};
104
+ let send_result = {};
104
105
 
105
- for await (let uid of uid_arr) {
106
- obj = {};
107
- for await (let delivery_method_item of delivery_method) {
108
- let account_ret;
109
- if (!system) {
110
- account_ret = await db_module.get_couch_doc('xuda_accounts', uid);
111
- if (account_ret.code < 0) {
112
- throw new Error(`user ${uid} not found`);
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
- email_msa.send_email({
148
- email: obj.email,
149
- subject,
150
- body,
151
- ref: obj._id,
152
- app_id: obj.app_id,
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
- let fcm_token_arr = [];
176
- for await (let session of ret.docs) {
177
- if (session.fcm_token) {
178
- if (!fcm_token_arr.includes(session.fcm_token)) {
179
- fcm_token_arr.push(session.fcm_token);
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
- for await (let fcm_token of fcm_token_arr) {
184
- if (fcm_token) {
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
- imageUrl: icon,
188
+ title: subject,
189
+ body,
190
+ image: icon,
195
191
  },
196
- },
197
- apns: {
198
- payload: {
199
- aps: {
200
- 'mutable-content': 1,
192
+ token: fcm_token,
193
+ android: {
194
+ notification: {
195
+ imageUrl: icon,
201
196
  },
202
197
  },
203
- fcm_options: {
204
- image: icon,
198
+ apns: {
199
+ payload: {
200
+ aps: {
201
+ 'mutable-content': 1,
202
+ },
203
+ },
204
+ fcm_options: {
205
+ image: icon,
206
+ },
205
207
  },
206
- },
207
- webpush: {
208
- headers: {
209
- image: icon,
208
+ webpush: {
209
+ headers: {
210
+ image: icon,
211
+ },
210
212
  },
211
- },
212
- };
213
- try {
214
- messaging
215
- .send(message)
216
- .then((response) => {
217
- // console.log("Successfully sent message:", response);
218
- })
219
- .catch((error) => {
220
- console.error('Error sending message:', error);
221
- });
222
- } catch (err) {
223
- console.error(err);
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
- } catch (err) {
228
- console.error(err);
229
- }
230
- break;
231
+ break;
231
232
 
232
- default:
233
- send_result[uid] = false;
234
- break;
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) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/notification_module",
3
- "version": "1.1.111",
3
+ "version": "1.1.113",
4
4
  "description": "Xuda Notification Server Module",
5
5
  "main": "index.mjs",
6
6
  "dependencies": {