@xuda.io/account_module 1.2.100 → 1.2.102
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.js +225 -417
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,19 +1,10 @@
|
|
|
1
|
-
const path = require(
|
|
2
|
-
global._conf = require(path.join(
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
process.env.XUDA_HOME,
|
|
9
|
-
"common",
|
|
10
|
-
"xuda_node_common.js"
|
|
11
|
-
));
|
|
12
|
-
|
|
13
|
-
const _ = require("lodash");
|
|
14
|
-
const module_path =
|
|
15
|
-
path.join(process.env.XUDA_HOME, "cpi") +
|
|
16
|
-
(!_conf.is_debug ? "/node_modules/@xuda.io" : "");
|
|
1
|
+
const path = require('path');
|
|
2
|
+
global._conf = require(path.join(process.env.XUDA_HOME, process.env.XUDA_CONFIG));
|
|
3
|
+
|
|
4
|
+
const _common = require(path.join(process.env.XUDA_HOME, 'common', 'xuda_node_common.js'));
|
|
5
|
+
|
|
6
|
+
const _ = require('lodash');
|
|
7
|
+
const module_path = path.join(process.env.XUDA_HOME, 'cpi') + (!_conf.is_debug ? '/node_modules/@xuda.io' : '');
|
|
17
8
|
const db_module = require(`${module_path}/db_module`);
|
|
18
9
|
|
|
19
10
|
exports.update_account_info = async function (req) {
|
|
@@ -36,64 +27,55 @@ exports.update_account_info = async function (req) {
|
|
|
36
27
|
}
|
|
37
28
|
}
|
|
38
29
|
|
|
39
|
-
const ret = await db_module.get_couch_doc(
|
|
30
|
+
const ret = await db_module.get_couch_doc('xuda_accounts', uid);
|
|
40
31
|
var account_obj = ret.data;
|
|
41
32
|
account_obj.ts = Date.now();
|
|
42
|
-
var change =
|
|
33
|
+
var change = '';
|
|
43
34
|
var error = {};
|
|
44
35
|
|
|
45
36
|
await marketplace_module.marketplace_save_user({ uid });
|
|
46
37
|
|
|
47
38
|
_.forEach(data, function (val, key) {
|
|
48
39
|
if (account_obj.account_info[key] !== val) {
|
|
49
|
-
change +=
|
|
40
|
+
change += ' from ' + account_obj.account_info[key] + ' to ' + val;
|
|
50
41
|
|
|
51
42
|
account_obj.account_info[key] = val;
|
|
52
43
|
|
|
53
|
-
if (account_obj.account_info.account_type ===
|
|
54
|
-
if (
|
|
55
|
-
key
|
|
56
|
-
validate_input(val, 150, true, false) < 0
|
|
57
|
-
) {
|
|
58
|
-
error[key] = "Invalid company";
|
|
44
|
+
if (account_obj.account_info.account_type === 'business') {
|
|
45
|
+
if (key === 'company_name' && validate_input(val, 150, true, false) < 0) {
|
|
46
|
+
error[key] = 'Invalid company';
|
|
59
47
|
}
|
|
60
48
|
}
|
|
61
|
-
if (key ===
|
|
49
|
+
if (key === 'first_name' && !validate_input(val, 35, true, false)) {
|
|
62
50
|
// print_r(validate_input($val, 35, true, false)<0);
|
|
63
51
|
// exit;
|
|
64
|
-
error[key] =
|
|
52
|
+
error[key] = 'Invalid first name';
|
|
65
53
|
}
|
|
66
|
-
if (key ===
|
|
67
|
-
error[key] =
|
|
54
|
+
if (key === 'last_name' && !validate_input(val, 35, true, false)) {
|
|
55
|
+
error[key] = 'Invalid last name';
|
|
68
56
|
}
|
|
69
|
-
if (
|
|
70
|
-
key
|
|
71
|
-
(!validator.isEmail(val) || !val || val.length < 2)
|
|
72
|
-
) {
|
|
73
|
-
error[key] = "Invalid email";
|
|
57
|
+
if (key === 'email' && (!validator.isEmail(val) || !val || val.length < 2)) {
|
|
58
|
+
error[key] = 'Invalid email';
|
|
74
59
|
}
|
|
75
|
-
if (
|
|
76
|
-
key === "tel" &&
|
|
77
|
-
(!/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/.test(val) || !val || val.length < 2)
|
|
78
|
-
) {
|
|
60
|
+
if (key === 'tel' && (!/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/.test(val) || !val || val.length < 2)) {
|
|
79
61
|
// if (key === "tel" && (!preg_match("/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/", $val) || !$val || count($val) < 2)) {
|
|
80
|
-
error[key] =
|
|
62
|
+
error[key] = 'Invalid phone number';
|
|
81
63
|
}
|
|
82
|
-
if (key ===
|
|
64
|
+
if (key === 'address' && !/^[a-z0-9- ]+$/i.test(val)) {
|
|
83
65
|
// if (key === "address" && !preg_match('/^[a-z0-9- ]+$/i', $val)) {
|
|
84
|
-
error[key] =
|
|
66
|
+
error[key] = 'Invalid address';
|
|
85
67
|
}
|
|
86
|
-
if (key ===
|
|
87
|
-
error[key] =
|
|
68
|
+
if (key === 'city' && !validate_input(val, 255, false, false)) {
|
|
69
|
+
error[key] = 'Invalid city';
|
|
88
70
|
}
|
|
89
|
-
if (key ===
|
|
90
|
-
error[key] =
|
|
71
|
+
if (key === 'state' && !validate_input(val, 50, false, false)) {
|
|
72
|
+
error[key] = 'Invalid state';
|
|
91
73
|
}
|
|
92
|
-
if (key ===
|
|
93
|
-
error[key] =
|
|
74
|
+
if (key === 'zip' && validate_input(val, 9, false, false) < 0) {
|
|
75
|
+
error[key] = 'Invalid zip';
|
|
94
76
|
}
|
|
95
|
-
if (key ===
|
|
96
|
-
error[key] =
|
|
77
|
+
if (key === 'country' && !validate_input(val, 55, false, false)) {
|
|
78
|
+
error[key] = 'Invalid country';
|
|
97
79
|
}
|
|
98
80
|
}
|
|
99
81
|
});
|
|
@@ -102,14 +84,14 @@ exports.update_account_info = async function (req) {
|
|
|
102
84
|
return { code: -1310, data: error };
|
|
103
85
|
}
|
|
104
86
|
if (!change) {
|
|
105
|
-
return { code: 1300, data:
|
|
87
|
+
return { code: 1300, data: 'no change' };
|
|
106
88
|
}
|
|
107
89
|
delete account_obj.account_info.gtp_token;
|
|
108
90
|
delete account_obj.account_info.client_id;
|
|
109
91
|
delete account_obj.account_info.uid;
|
|
110
|
-
const ret2 = await db_module.save_couch_doc(
|
|
92
|
+
const ret2 = await db_module.save_couch_doc('xuda_accounts', account_obj);
|
|
111
93
|
if (ret2.code > 0) {
|
|
112
|
-
return { code: 1300, data:
|
|
94
|
+
return { code: 1300, data: 'ok' };
|
|
113
95
|
}
|
|
114
96
|
return ret2;
|
|
115
97
|
};
|
|
@@ -117,13 +99,13 @@ exports.update_account_preferences = async function (req) {
|
|
|
117
99
|
const account_id = req.account_id;
|
|
118
100
|
const data = req;
|
|
119
101
|
|
|
120
|
-
const ret = await db_module.get_couch_doc(
|
|
102
|
+
const ret = await db_module.get_couch_doc('xuda_accounts', account_id);
|
|
121
103
|
var account_obj = ret.data;
|
|
122
104
|
account_obj.ts = Date.now();
|
|
123
105
|
|
|
124
106
|
account_obj.preferences = data;
|
|
125
107
|
|
|
126
|
-
const ret1 = await db_module.save_couch_doc(
|
|
108
|
+
const ret1 = await db_module.save_couch_doc('xuda_accounts', account_obj);
|
|
127
109
|
if (ret1.code < 0) {
|
|
128
110
|
return ret1;
|
|
129
111
|
}
|
|
@@ -131,7 +113,7 @@ exports.update_account_preferences = async function (req) {
|
|
|
131
113
|
};
|
|
132
114
|
|
|
133
115
|
exports.save_admin_presets = async function (req) {
|
|
134
|
-
const ret = await db_module.get_couch_doc(
|
|
116
|
+
const ret = await db_module.get_couch_doc('xuda_master', req.app_id);
|
|
135
117
|
if (ret.code < 0) {
|
|
136
118
|
return ret;
|
|
137
119
|
}
|
|
@@ -144,7 +126,7 @@ exports.save_admin_presets = async function (req) {
|
|
|
144
126
|
if (ret3.code > -1) {
|
|
145
127
|
obj = {
|
|
146
128
|
code: 1,
|
|
147
|
-
data:
|
|
129
|
+
data: 'ok',
|
|
148
130
|
app_obj: _common.get_clean_app_obj(app_obj),
|
|
149
131
|
};
|
|
150
132
|
} else {
|
|
@@ -161,11 +143,7 @@ exports.increment_account_usage = async function (req) {
|
|
|
161
143
|
if (uid) {
|
|
162
144
|
opt.key = uid;
|
|
163
145
|
}
|
|
164
|
-
const accounts_ret = await db_module.get_couch_view_raw(
|
|
165
|
-
"xuda_accounts",
|
|
166
|
-
"all_accounts",
|
|
167
|
-
opt
|
|
168
|
-
);
|
|
146
|
+
const accounts_ret = await db_module.get_couch_view_raw('xuda_accounts', 'all_accounts', opt);
|
|
169
147
|
|
|
170
148
|
const drive_module = require(`${module_path}/drive_module`);
|
|
171
149
|
|
|
@@ -178,27 +156,20 @@ exports.increment_account_usage = async function (req) {
|
|
|
178
156
|
|
|
179
157
|
const run_account_drive = async function (account_data) {
|
|
180
158
|
try {
|
|
181
|
-
const usage_id = await _common.xuda_get_uuid(
|
|
159
|
+
const usage_id = await _common.xuda_get_uuid('usage');
|
|
182
160
|
|
|
183
|
-
const usage_ret = await db_module.get_couch_view_raw(
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
{
|
|
187
|
-
key: account_data._id,
|
|
188
|
-
}
|
|
189
|
-
);
|
|
161
|
+
const usage_ret = await db_module.get_couch_view_raw('xuda_usage', 'open_drive_usage', {
|
|
162
|
+
key: account_data._id,
|
|
163
|
+
});
|
|
190
164
|
|
|
191
165
|
var doc = {
|
|
192
166
|
_id: usage_id,
|
|
193
|
-
docType:
|
|
167
|
+
docType: 'user_drive_usage',
|
|
194
168
|
date_created: new Date(),
|
|
195
169
|
stat: 1,
|
|
196
170
|
uid: account_data._id,
|
|
197
171
|
stripe_customer_id: account_data.stripe_customer_id,
|
|
198
|
-
account_name:
|
|
199
|
-
account_data.account_info.first_name +
|
|
200
|
-
" " +
|
|
201
|
-
account_data.account_info.last_name,
|
|
172
|
+
account_name: account_data.account_info.first_name + ' ' + account_data.account_info.last_name,
|
|
202
173
|
size: 0,
|
|
203
174
|
price: 0,
|
|
204
175
|
hours: 0,
|
|
@@ -211,24 +182,17 @@ exports.increment_account_usage = async function (req) {
|
|
|
211
182
|
}
|
|
212
183
|
|
|
213
184
|
if (Date.now() - doc.ts >= interval) {
|
|
214
|
-
const plan = _conf.PLAN_OBJ[account_data.
|
|
215
|
-
const drive_size_total =
|
|
216
|
-
account_data.user_drive_size +
|
|
217
|
-
account_data.studio_drive_size +
|
|
218
|
-
account_data.workspace_drive_size +
|
|
219
|
-
account_data.builds_drive_size +
|
|
220
|
-
account_data.plugins_drive_size;
|
|
185
|
+
const plan = _conf.PLAN_OBJ[account_data.membership_plan];
|
|
186
|
+
const drive_size_total = account_data.user_drive_size + account_data.studio_drive_size + account_data.workspace_drive_size + account_data.builds_drive_size + account_data.plugins_drive_size;
|
|
221
187
|
if (bytesToGB(drive_size_total) > plan.features.drive) {
|
|
222
|
-
var price_per_hour =
|
|
223
|
-
(bytesToGB(drive_size_total) * _conf.PRICE_OBJ.price_per_gb_drive) /
|
|
224
|
-
_conf.PRICE_OBJ.avg_hours_in_month;
|
|
188
|
+
var price_per_hour = (bytesToGB(drive_size_total) * _conf.PRICE_OBJ.price_per_gb_drive) / _conf.PRICE_OBJ.avg_hours_in_month;
|
|
225
189
|
|
|
226
190
|
doc.hours++;
|
|
227
191
|
doc.size = drive_size_total;
|
|
228
192
|
doc.price += price_per_hour;
|
|
229
193
|
|
|
230
194
|
doc.ts = Date.now();
|
|
231
|
-
await db_module.save_couch_doc(
|
|
195
|
+
await db_module.save_couch_doc('xuda_usage', doc);
|
|
232
196
|
}
|
|
233
197
|
}
|
|
234
198
|
} catch (err) {
|
|
@@ -237,77 +201,58 @@ exports.increment_account_usage = async function (req) {
|
|
|
237
201
|
};
|
|
238
202
|
|
|
239
203
|
const run_account_apps = async function (account_data) {
|
|
240
|
-
const apps_ret = await db_module.get_couch_view(
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
endkey: [account_data._id, "ZZZZZ"],
|
|
246
|
-
include_docs: true,
|
|
247
|
-
}
|
|
248
|
-
);
|
|
204
|
+
const apps_ret = await db_module.get_couch_view('xuda_master', 'user_apps', {
|
|
205
|
+
startkey: [account_data._id, ''],
|
|
206
|
+
endkey: [account_data._id, 'ZZZZZ'],
|
|
207
|
+
include_docs: true,
|
|
208
|
+
});
|
|
249
209
|
|
|
250
210
|
try {
|
|
251
211
|
for await (let app of apps_ret.data.rows) {
|
|
252
|
-
if (app.value.app_type ===
|
|
212
|
+
if (app.value.app_type === 'master') {
|
|
253
213
|
let size_ret = await drive_module.get_drive_size({
|
|
254
|
-
drive_type:
|
|
214
|
+
drive_type: 'studio',
|
|
255
215
|
app_id: app.id,
|
|
256
216
|
});
|
|
257
|
-
if (size_ret.code > -1)
|
|
258
|
-
account_data.studio_drive_size += size_ret.data;
|
|
217
|
+
if (size_ret.code > -1) account_data.studio_drive_size += size_ret.data;
|
|
259
218
|
|
|
260
219
|
size_ret = await drive_module.get_drive_size({
|
|
261
|
-
drive_type:
|
|
220
|
+
drive_type: 'workspace',
|
|
262
221
|
app_id: app.id,
|
|
263
222
|
});
|
|
264
|
-
if (size_ret.code > -1)
|
|
265
|
-
account_data.workspace_drive_size += size_ret.data;
|
|
223
|
+
if (size_ret.code > -1) account_data.workspace_drive_size += size_ret.data;
|
|
266
224
|
|
|
267
225
|
size_ret = await drive_module.get_drive_size({
|
|
268
|
-
drive_type:
|
|
226
|
+
drive_type: 'builds',
|
|
269
227
|
app_id: app.id,
|
|
270
228
|
});
|
|
271
|
-
if (size_ret.code > -1)
|
|
272
|
-
account_data.builds_drive_size += size_ret.data;
|
|
229
|
+
if (size_ret.code > -1) account_data.builds_drive_size += size_ret.data;
|
|
273
230
|
|
|
274
231
|
size_ret = await drive_module.get_drive_size({
|
|
275
|
-
drive_type:
|
|
232
|
+
drive_type: 'plugins',
|
|
276
233
|
app_id: app.id,
|
|
277
234
|
});
|
|
278
|
-
if (size_ret.code > -1)
|
|
279
|
-
account_data.plugins_drive_size += size_ret.data;
|
|
235
|
+
if (size_ret.code > -1) account_data.plugins_drive_size += size_ret.data;
|
|
280
236
|
|
|
281
237
|
// db data
|
|
282
238
|
const couch_info_ret = await db_module.get_couch_app_info(app.id);
|
|
283
|
-
account_data.project_data_size +=
|
|
284
|
-
couch_info_ret?.data?.sizes?.active || 0;
|
|
239
|
+
account_data.project_data_size += couch_info_ret?.data?.sizes?.active || 0;
|
|
285
240
|
}
|
|
286
241
|
|
|
287
|
-
const usage_id = await _common.xuda_get_uuid(
|
|
242
|
+
const usage_id = await _common.xuda_get_uuid('usage');
|
|
288
243
|
|
|
289
|
-
const usage_ret = await db_module.get_couch_view_raw(
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
{
|
|
293
|
-
key: [
|
|
294
|
-
app.id,
|
|
295
|
-
app.doc?.app_hosting?.app_server_type || app.doc.app_type,
|
|
296
|
-
],
|
|
297
|
-
}
|
|
298
|
-
);
|
|
244
|
+
const usage_ret = await db_module.get_couch_view_raw('xuda_usage', 'open_app_usage', {
|
|
245
|
+
key: [app.id, app.doc?.app_hosting?.app_server_type || app.doc.app_type],
|
|
246
|
+
});
|
|
299
247
|
|
|
300
248
|
var doc = {
|
|
301
249
|
_id: usage_id,
|
|
302
|
-
docType:
|
|
250
|
+
docType: 'app_usage',
|
|
303
251
|
date_created: new Date(),
|
|
304
252
|
stat: 1,
|
|
305
253
|
uid: account_data._id,
|
|
306
254
|
stripe_customer_id: account_data.stripe_customer_id,
|
|
307
|
-
account_name:
|
|
308
|
-
account_data.account_info.first_name +
|
|
309
|
-
" " +
|
|
310
|
-
account_data.account_info.last_name,
|
|
255
|
+
account_name: account_data.account_info.first_name + ' ' + account_data.account_info.last_name,
|
|
311
256
|
app_id: app.id,
|
|
312
257
|
price: 0,
|
|
313
258
|
hours: 0,
|
|
@@ -318,7 +263,7 @@ exports.increment_account_usage = async function (req) {
|
|
|
318
263
|
subscription_id: account_data.stripe_membership_subscription_id,
|
|
319
264
|
};
|
|
320
265
|
|
|
321
|
-
if (doc.app_type ===
|
|
266
|
+
if (doc.app_type === 'user_group') {
|
|
322
267
|
doc = {
|
|
323
268
|
...doc,
|
|
324
269
|
...{
|
|
@@ -342,46 +287,34 @@ exports.increment_account_usage = async function (req) {
|
|
|
342
287
|
doc.hours++;
|
|
343
288
|
|
|
344
289
|
switch (doc.app_type) {
|
|
345
|
-
case
|
|
290
|
+
case 'backup':
|
|
346
291
|
if (app.value.app_hosting) {
|
|
347
292
|
// backup of deployment
|
|
348
|
-
price_per_hour =
|
|
349
|
-
(app.value.app_hosting.disk *
|
|
350
|
-
_conf.PRICE_OBJ.price_per_gb_backup) /
|
|
351
|
-
_conf.PRICE_OBJ.avg_hours_in_month;
|
|
293
|
+
price_per_hour = (app.value.app_hosting.disk * _conf.PRICE_OBJ.price_per_gb_backup) / _conf.PRICE_OBJ.avg_hours_in_month;
|
|
352
294
|
} else {
|
|
353
295
|
// backup of project
|
|
354
296
|
|
|
355
297
|
const info_ret = await db_module.get_couch_app_info(doc.app_id);
|
|
356
298
|
if (info_ret.code > 0) {
|
|
357
299
|
const size = info_ret.data.sizes.active / 1000 / 1000 / 1000; //gb
|
|
358
|
-
price_per_hour =
|
|
359
|
-
(size * _conf.PRICE_OBJ.price_per_gb_backup) /
|
|
360
|
-
_conf.PRICE_OBJ.avg_hours_in_month;
|
|
300
|
+
price_per_hour = (size * _conf.PRICE_OBJ.price_per_gb_backup) / _conf.PRICE_OBJ.avg_hours_in_month;
|
|
361
301
|
}
|
|
362
302
|
}
|
|
363
303
|
break;
|
|
364
304
|
|
|
365
|
-
case
|
|
366
|
-
const team_ret = await db_module.get_couch_view(
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
reduce: true,
|
|
372
|
-
group_level: 1,
|
|
373
|
-
}
|
|
374
|
-
);
|
|
305
|
+
case 'user_group': {
|
|
306
|
+
const team_ret = await db_module.get_couch_view('xuda_team', 'user_group_shares_count', {
|
|
307
|
+
key: app.id,
|
|
308
|
+
reduce: true,
|
|
309
|
+
group_level: 1,
|
|
310
|
+
});
|
|
375
311
|
|
|
376
312
|
const team_members = team_ret.data.rows?.[0]?.value || 0;
|
|
377
|
-
const price_per_hour_team =
|
|
378
|
-
(team_members * _conf.PRICE_OBJ.price_per_user_group_member) /
|
|
379
|
-
_conf.PRICE_OBJ.avg_hours_in_month;
|
|
313
|
+
const price_per_hour_team = (team_members * _conf.PRICE_OBJ.price_per_user_group_member) / _conf.PRICE_OBJ.avg_hours_in_month;
|
|
380
314
|
doc.price_user_group += price_per_hour_team;
|
|
381
315
|
doc.user_group_members = team_members;
|
|
382
316
|
doc.user_group_members_accumulated += team_members;
|
|
383
|
-
doc.user_group_members_avg =
|
|
384
|
-
doc.user_group_members_accumulated / doc.hours;
|
|
317
|
+
doc.user_group_members_avg = doc.user_group_members_accumulated / doc.hours;
|
|
385
318
|
}
|
|
386
319
|
|
|
387
320
|
default:
|
|
@@ -390,8 +323,7 @@ exports.increment_account_usage = async function (req) {
|
|
|
390
323
|
|
|
391
324
|
doc.app_hosting = app.doc.app_hosting;
|
|
392
325
|
doc.server_type = app.doc.app_hosting.app_server_type;
|
|
393
|
-
price_per_hour =
|
|
394
|
-
app.doc.app_hosting.price / _conf.PRICE_OBJ.avg_hours_in_month;
|
|
326
|
+
price_per_hour = app.doc.app_hosting.price / _conf.PRICE_OBJ.avg_hours_in_month;
|
|
395
327
|
|
|
396
328
|
break;
|
|
397
329
|
}
|
|
@@ -401,7 +333,7 @@ exports.increment_account_usage = async function (req) {
|
|
|
401
333
|
}
|
|
402
334
|
|
|
403
335
|
doc.ts = Date.now();
|
|
404
|
-
await db_module.save_couch_doc(
|
|
336
|
+
await db_module.save_couch_doc('xuda_usage', doc);
|
|
405
337
|
}
|
|
406
338
|
}
|
|
407
339
|
} catch (err) {
|
|
@@ -415,7 +347,7 @@ exports.increment_account_usage = async function (req) {
|
|
|
415
347
|
|
|
416
348
|
const size_ret = await drive_module.get_drive_size({
|
|
417
349
|
uid: account_data._id,
|
|
418
|
-
drive_type:
|
|
350
|
+
drive_type: 'user',
|
|
419
351
|
});
|
|
420
352
|
|
|
421
353
|
account_data.user_drive_size = size_ret.data;
|
|
@@ -428,14 +360,8 @@ exports.increment_account_usage = async function (req) {
|
|
|
428
360
|
await run_account_apps(account_data);
|
|
429
361
|
await run_account_drive(account_data);
|
|
430
362
|
|
|
431
|
-
account_data.total_drive_size =
|
|
432
|
-
|
|
433
|
-
account_data.studio_drive_size +
|
|
434
|
-
account_data.workspace_drive_size +
|
|
435
|
-
account_data.plugins_drive_size +
|
|
436
|
-
account_data.builds_drive_size +
|
|
437
|
-
account_data.project_data_size;
|
|
438
|
-
await db_module.save_couch_doc("xuda_accounts", account_data);
|
|
363
|
+
account_data.total_drive_size = account_data.user_drive_size + account_data.studio_drive_size + account_data.workspace_drive_size + account_data.plugins_drive_size + account_data.builds_drive_size + account_data.project_data_size;
|
|
364
|
+
await db_module.save_couch_doc('xuda_accounts', account_data);
|
|
439
365
|
}
|
|
440
366
|
};
|
|
441
367
|
|
|
@@ -456,17 +382,14 @@ exports.get_account_data = async function (req) {
|
|
|
456
382
|
// };
|
|
457
383
|
|
|
458
384
|
try {
|
|
459
|
-
const { data: acc_obj } = await db_module.get_couch_doc(
|
|
460
|
-
"xuda_accounts",
|
|
461
|
-
uid
|
|
462
|
-
);
|
|
385
|
+
const { data: acc_obj } = await db_module.get_couch_doc('xuda_accounts', uid);
|
|
463
386
|
// const account_obj= account_ret.data
|
|
464
387
|
var ret = { code: 1 };
|
|
465
388
|
ret.data = {
|
|
466
389
|
_id: acc_obj._id,
|
|
467
390
|
account_info: acc_obj.account_info,
|
|
468
391
|
usage: acc_obj.activity_usage,
|
|
469
|
-
plan: acc_obj.
|
|
392
|
+
plan: acc_obj.membership_plan,
|
|
470
393
|
plan_changed: acc_obj.plan_changed,
|
|
471
394
|
plan_info: acc_obj.plan_info,
|
|
472
395
|
support_plan: acc_obj.support_plan,
|
|
@@ -522,30 +445,27 @@ exports.get_account_projects = async function (req) {
|
|
|
522
445
|
key: uid,
|
|
523
446
|
};
|
|
524
447
|
|
|
525
|
-
if (
|
|
526
|
-
_conf.superuser_account_ids.includes(uid) ||
|
|
527
|
-
_conf.support.support_team_account_ids.includes(uid)
|
|
528
|
-
) {
|
|
448
|
+
if (_conf.superuser_account_ids.includes(uid) || _conf.support.support_team_account_ids.includes(uid)) {
|
|
529
449
|
opt = {};
|
|
530
450
|
}
|
|
531
451
|
|
|
532
|
-
var ret = await db_module.get_couch_view(
|
|
452
|
+
var ret = await db_module.get_couch_view('xuda_master', 'user_projects', opt);
|
|
533
453
|
|
|
534
454
|
return ret;
|
|
535
455
|
};
|
|
536
456
|
|
|
537
457
|
exports.get_account_datacenters = async function (req) {
|
|
538
|
-
return await db_module.get_couch_view(
|
|
458
|
+
return await db_module.get_couch_view('xuda_master', 'user_datacenters', {
|
|
539
459
|
key: req.uid,
|
|
540
460
|
});
|
|
541
461
|
};
|
|
542
462
|
exports.get_account_deployments = async function (req) {
|
|
543
|
-
return await db_module.get_couch_view(
|
|
463
|
+
return await db_module.get_couch_view('xuda_master', 'user_deployments', {
|
|
544
464
|
key: req.uid,
|
|
545
465
|
});
|
|
546
466
|
};
|
|
547
467
|
exports.get_account_instances = async function (req) {
|
|
548
|
-
return await db_module.get_couch_view(
|
|
468
|
+
return await db_module.get_couch_view('xuda_master', 'user_all_instances', {
|
|
549
469
|
key: req.uid,
|
|
550
470
|
});
|
|
551
471
|
};
|
|
@@ -558,17 +478,17 @@ exports.get_account_info = async function (req) {
|
|
|
558
478
|
};
|
|
559
479
|
|
|
560
480
|
exports.get_account_name = async function (req) {
|
|
561
|
-
const data = await db_module.get_couch_doc(
|
|
481
|
+
const data = await db_module.get_couch_doc('xuda_accounts', req.uid_query);
|
|
562
482
|
if (data.code < 0) {
|
|
563
483
|
return data;
|
|
564
484
|
}
|
|
565
485
|
var obj = {
|
|
566
|
-
first_name:
|
|
567
|
-
last_name:
|
|
568
|
-
email:
|
|
569
|
-
phone:
|
|
570
|
-
profile_picture:
|
|
571
|
-
username:
|
|
486
|
+
first_name: '',
|
|
487
|
+
last_name: '',
|
|
488
|
+
email: '',
|
|
489
|
+
phone: '',
|
|
490
|
+
profile_picture: '',
|
|
491
|
+
username: '',
|
|
572
492
|
};
|
|
573
493
|
if (data.data.account_info) {
|
|
574
494
|
obj = {
|
|
@@ -585,17 +505,17 @@ exports.get_account_name = async function (req) {
|
|
|
585
505
|
|
|
586
506
|
exports.account_validate_username = async function (req) {
|
|
587
507
|
const opt = {
|
|
588
|
-
selector: {
|
|
589
|
-
fields: [
|
|
508
|
+
selector: { 'account_info.username': req.username },
|
|
509
|
+
fields: ['_id'],
|
|
590
510
|
limit: 1,
|
|
591
511
|
};
|
|
592
512
|
|
|
593
|
-
var ret = await db_module.find_couch_query(
|
|
513
|
+
var ret = await db_module.find_couch_query('xuda_accounts', opt);
|
|
594
514
|
|
|
595
515
|
if (ret.docs.length) {
|
|
596
|
-
return { code: -400, data:
|
|
516
|
+
return { code: -400, data: 'User exist' };
|
|
597
517
|
} else {
|
|
598
|
-
return { code: 1, data:
|
|
518
|
+
return { code: 1, data: 'ok' };
|
|
599
519
|
}
|
|
600
520
|
};
|
|
601
521
|
|
|
@@ -612,17 +532,17 @@ exports.account_validate_username = async function (req) {
|
|
|
612
532
|
exports.verify_account = async function (req) {
|
|
613
533
|
const { account_id } = req;
|
|
614
534
|
if (!account_id) {
|
|
615
|
-
return { code: -10, data:
|
|
535
|
+
return { code: -10, data: 'Missing id' };
|
|
616
536
|
}
|
|
617
537
|
|
|
618
|
-
const ret = await db_module.get_couch_doc(
|
|
538
|
+
const ret = await db_module.get_couch_doc('xuda_accounts', account_id);
|
|
619
539
|
|
|
620
540
|
if (ret.code < 0) {
|
|
621
|
-
return { code: -1, data:
|
|
541
|
+
return { code: -1, data: 'account not found' };
|
|
622
542
|
}
|
|
623
543
|
var obj = ret.data;
|
|
624
544
|
obj.stat = 3;
|
|
625
|
-
const ret_acc = await db_module.save_couch_doc(
|
|
545
|
+
const ret_acc = await db_module.save_couch_doc('xuda_accounts', obj);
|
|
626
546
|
if (ret.code < 0) {
|
|
627
547
|
return ret_acc;
|
|
628
548
|
}
|
|
@@ -632,16 +552,12 @@ exports.verify_account = async function (req) {
|
|
|
632
552
|
exports.validate_user_plan = async function (req) {
|
|
633
553
|
const { account_id, app_obj } = req;
|
|
634
554
|
|
|
635
|
-
const apps_ret = await db_module.get_couch_view(
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
reduce: true,
|
|
642
|
-
group_level: 2,
|
|
643
|
-
}
|
|
644
|
-
);
|
|
555
|
+
const apps_ret = await db_module.get_couch_view('xuda_master', 'user_apps_count', {
|
|
556
|
+
startkey: [account_id, ''],
|
|
557
|
+
endkey: [account_id, 'ZZZZZ'],
|
|
558
|
+
reduce: true,
|
|
559
|
+
group_level: 2,
|
|
560
|
+
});
|
|
645
561
|
|
|
646
562
|
var ret_obj = _.reduce(
|
|
647
563
|
apps_ret.data.rows,
|
|
@@ -649,69 +565,43 @@ exports.validate_user_plan = async function (req) {
|
|
|
649
565
|
ret[value.key[1]] = value.value;
|
|
650
566
|
return ret;
|
|
651
567
|
},
|
|
652
|
-
{}
|
|
568
|
+
{},
|
|
653
569
|
);
|
|
654
570
|
|
|
655
|
-
const ret = await db_module.get_couch_doc(
|
|
571
|
+
const ret = await db_module.get_couch_doc('xuda_accounts', account_id);
|
|
656
572
|
if (ret.code < 0) {
|
|
657
573
|
return ret;
|
|
658
574
|
}
|
|
659
|
-
var plan = _conf.PLAN_OBJ[ret.data.
|
|
575
|
+
var plan = _conf.PLAN_OBJ[ret.data.membership_plan];
|
|
660
576
|
// get plan from user
|
|
661
577
|
|
|
662
578
|
if (!plan) {
|
|
663
|
-
return { code: -7, data:
|
|
579
|
+
return { code: -7, data: 'error - no plan defined' };
|
|
664
580
|
}
|
|
665
581
|
|
|
666
582
|
// validate number of projects
|
|
667
|
-
if (
|
|
668
|
-
app_obj.app_type === "master" &&
|
|
669
|
-
ret_obj.master >= plan.features.projects
|
|
670
|
-
) {
|
|
583
|
+
if (app_obj.app_type === 'master' && ret_obj.master >= plan.features.projects) {
|
|
671
584
|
return {
|
|
672
585
|
code: -8,
|
|
673
|
-
data: `Number of projects (${ret.data.
|
|
586
|
+
data: `Number of projects (${ret.data.membership_plan}) exceeds to the ${plan.features.projects} plan limits`,
|
|
674
587
|
};
|
|
675
588
|
}
|
|
676
589
|
|
|
677
590
|
// validate number of team members
|
|
678
591
|
|
|
679
|
-
const user_active_app_requests_count_ret = await db_module.get_couch_view(
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
reduce: true,
|
|
685
|
-
// group_level: 2,
|
|
686
|
-
}
|
|
687
|
-
);
|
|
592
|
+
const user_active_app_requests_count_ret = await db_module.get_couch_view('xuda_team', 'user_active_app_requests_count', {
|
|
593
|
+
key: account_id,
|
|
594
|
+
reduce: true,
|
|
595
|
+
// group_level: 2,
|
|
596
|
+
});
|
|
688
597
|
|
|
689
|
-
if (
|
|
690
|
-
app_obj.app_type === "master" &&
|
|
691
|
-
user_active_app_requests_count_ret.data?.rows?.[0]?.value >=
|
|
692
|
-
plan.features.team
|
|
693
|
-
) {
|
|
598
|
+
if (app_obj.app_type === 'master' && user_active_app_requests_count_ret.data?.rows?.[0]?.value >= plan.features.team) {
|
|
694
599
|
return {
|
|
695
600
|
code: -9,
|
|
696
601
|
data: `Number of team shares (${user_active_app_requests_count_ret.data.rows[0].value}) exceeds to the ${plan.features.team} plan limits`,
|
|
697
602
|
};
|
|
698
603
|
}
|
|
699
604
|
|
|
700
|
-
// if (!app_obj.access_type) {
|
|
701
|
-
// return {
|
|
702
|
-
// code: -10,
|
|
703
|
-
// data: `error - access_type missing`,
|
|
704
|
-
// };
|
|
705
|
-
// }
|
|
706
|
-
// }
|
|
707
|
-
|
|
708
|
-
// if (app_obj.app_type === "backup" && ret.data.plan === "free") {
|
|
709
|
-
// return {
|
|
710
|
-
// code: -10,
|
|
711
|
-
// data: `The free plan does not include the ability to perform backups. To access backup capabilities, please consider upgrading your plan. You can do so by visiting https://xuda.io/dashboard/settings/billing.`,
|
|
712
|
-
// };
|
|
713
|
-
// }
|
|
714
|
-
|
|
715
605
|
return { code: 1, data: ret.data };
|
|
716
606
|
};
|
|
717
607
|
|
|
@@ -722,11 +612,7 @@ exports.get_hosting_plan = function (req) {
|
|
|
722
612
|
return _conf.PRICE_OBJ.server_slugs[app_obj.app_hosting.app_server_type];
|
|
723
613
|
}
|
|
724
614
|
debugger;
|
|
725
|
-
console.error(
|
|
726
|
-
"error: " +
|
|
727
|
-
app_obj.app_hosting.app_server_type +
|
|
728
|
-
" not found in PRICE_OBJ.server_slugs"
|
|
729
|
-
);
|
|
615
|
+
console.error('error: ' + app_obj.app_hosting.app_server_type + ' not found in PRICE_OBJ.server_slugs');
|
|
730
616
|
return { cpu: 0, price: 0 };
|
|
731
617
|
}
|
|
732
618
|
};
|
|
@@ -746,16 +632,7 @@ exports.get_cpu = async function (req) {
|
|
|
746
632
|
return cpu;
|
|
747
633
|
};
|
|
748
634
|
|
|
749
|
-
const get_account_log_object = function (
|
|
750
|
-
body,
|
|
751
|
-
status,
|
|
752
|
-
service,
|
|
753
|
-
source,
|
|
754
|
-
response_data,
|
|
755
|
-
ip,
|
|
756
|
-
headers,
|
|
757
|
-
security
|
|
758
|
-
) {
|
|
635
|
+
const get_account_log_object = function (body, status, service, source, response_data, ip, headers, security) {
|
|
759
636
|
return {
|
|
760
637
|
uid: body.uid,
|
|
761
638
|
ip,
|
|
@@ -773,15 +650,7 @@ const get_account_log_object = function (
|
|
|
773
650
|
};
|
|
774
651
|
};
|
|
775
652
|
|
|
776
|
-
exports.add_account_log_util = function (
|
|
777
|
-
body,
|
|
778
|
-
status,
|
|
779
|
-
service,
|
|
780
|
-
source,
|
|
781
|
-
response_data,
|
|
782
|
-
ip,
|
|
783
|
-
headers
|
|
784
|
-
) {
|
|
653
|
+
exports.add_account_log_util = function (body, status, service, source, response_data, ip, headers) {
|
|
785
654
|
// non error log
|
|
786
655
|
// if (status < 400) {
|
|
787
656
|
// console.log("error", "add_account_log_util", service);
|
|
@@ -790,22 +659,11 @@ exports.add_account_log_util = function (
|
|
|
790
659
|
if (_conf.cpi_methods?.[service]?.private) return;
|
|
791
660
|
const security = _conf.cpi_methods?.[service].security;
|
|
792
661
|
if (!security) {
|
|
793
|
-
if (service.substr(0, 4) ===
|
|
662
|
+
if (service.substr(0, 4) === 'get_' && !body.api_pk && !body.api_sk) return;
|
|
794
663
|
}
|
|
795
664
|
// }
|
|
796
665
|
const logs_module = require(`${module_path}/logs_module`);
|
|
797
|
-
logs_module.add_account_log(
|
|
798
|
-
get_account_log_object(
|
|
799
|
-
body,
|
|
800
|
-
status,
|
|
801
|
-
service,
|
|
802
|
-
source,
|
|
803
|
-
response_data,
|
|
804
|
-
ip,
|
|
805
|
-
headers,
|
|
806
|
-
security
|
|
807
|
-
)
|
|
808
|
-
);
|
|
666
|
+
logs_module.add_account_log(get_account_log_object(body, status, service, source, response_data, ip, headers, security));
|
|
809
667
|
};
|
|
810
668
|
|
|
811
669
|
exports.save_ssh_key = async function (req) {
|
|
@@ -813,46 +671,45 @@ exports.save_ssh_key = async function (req) {
|
|
|
813
671
|
|
|
814
672
|
function isValidSSHPublicKey(sshPublicKey) {
|
|
815
673
|
// Regular expression to match SSH public keys
|
|
816
|
-
const sshKeyRegex =
|
|
817
|
-
/^ssh-(rsa|ed25519|ecdsa-sha2-nistp[0-9]+) [A-Za-z0-9+/]+[=]{0,3}( [^@]+@[^@]+)?$/;
|
|
674
|
+
const sshKeyRegex = /^ssh-(rsa|ed25519|ecdsa-sha2-nistp[0-9]+) [A-Za-z0-9+/]+[=]{0,3}( [^@]+@[^@]+)?$/;
|
|
818
675
|
|
|
819
676
|
return sshKeyRegex.test(sshPublicKey);
|
|
820
677
|
}
|
|
821
678
|
|
|
822
679
|
if (!isValidSSHPublicKey(ssh_key_content)) {
|
|
823
|
-
return { code: -1, data:
|
|
680
|
+
return { code: -1, data: 'invalid ssh key' };
|
|
824
681
|
}
|
|
825
682
|
|
|
826
683
|
var doc = {
|
|
827
684
|
uid,
|
|
828
|
-
docType:
|
|
685
|
+
docType: 'ssh_key',
|
|
829
686
|
date_created_ts: Date.now(),
|
|
830
687
|
date_created: Date.now(),
|
|
831
688
|
stat: 3,
|
|
832
689
|
};
|
|
833
690
|
|
|
834
691
|
if (_id) {
|
|
835
|
-
const ret = await db_module.get_couch_doc(
|
|
692
|
+
const ret = await db_module.get_couch_doc('xuda_ssh_keys', _id);
|
|
836
693
|
if (ret.code < 0) {
|
|
837
694
|
return ret;
|
|
838
695
|
}
|
|
839
696
|
doc = ret.data;
|
|
840
697
|
doc.date_updated_ts = Date.now();
|
|
841
698
|
} else {
|
|
842
|
-
doc._id = await _common.xuda_get_uuid(
|
|
699
|
+
doc._id = await _common.xuda_get_uuid('ssh_key');
|
|
843
700
|
}
|
|
844
701
|
|
|
845
702
|
doc.ssh_key_name = ssh_key_name;
|
|
846
703
|
doc.ssh_key_content = ssh_key_content;
|
|
847
704
|
|
|
848
|
-
const save_ret = await db_module.save_couch_doc(
|
|
705
|
+
const save_ret = await db_module.save_couch_doc('xuda_ssh_keys', doc);
|
|
849
706
|
|
|
850
707
|
return save_ret;
|
|
851
708
|
};
|
|
852
709
|
exports.delete_ssh_key = async function (req) {
|
|
853
710
|
const { ssh_key_id } = req;
|
|
854
711
|
|
|
855
|
-
const ret = await db_module.get_couch_doc(
|
|
712
|
+
const ret = await db_module.get_couch_doc('xuda_ssh_keys', ssh_key_id);
|
|
856
713
|
if (ret.code < 0) {
|
|
857
714
|
return ret;
|
|
858
715
|
}
|
|
@@ -860,7 +717,7 @@ exports.delete_ssh_key = async function (req) {
|
|
|
860
717
|
doc.date_updated_ts = Date.now();
|
|
861
718
|
doc.stat = 4;
|
|
862
719
|
|
|
863
|
-
const save_ret = await db_module.save_couch_doc(
|
|
720
|
+
const save_ret = await db_module.save_couch_doc('xuda_ssh_keys', doc);
|
|
864
721
|
|
|
865
722
|
return save_ret;
|
|
866
723
|
};
|
|
@@ -869,14 +726,14 @@ exports.get_ssh_keys = async function (req) {
|
|
|
869
726
|
const { uid, _id } = req;
|
|
870
727
|
|
|
871
728
|
let opt = {
|
|
872
|
-
selector: { docType:
|
|
729
|
+
selector: { docType: 'ssh_key', uid, stat: { $lt: 4 } },
|
|
873
730
|
};
|
|
874
731
|
|
|
875
732
|
if (_id) {
|
|
876
733
|
opt._id = _id;
|
|
877
734
|
}
|
|
878
735
|
|
|
879
|
-
var ret = await db_module.find_couch_query(
|
|
736
|
+
var ret = await db_module.find_couch_query('xuda_ssh_keys', opt);
|
|
880
737
|
return ret;
|
|
881
738
|
};
|
|
882
739
|
|
|
@@ -887,11 +744,11 @@ exports.search_users = async function (req) {
|
|
|
887
744
|
// bookmark
|
|
888
745
|
try {
|
|
889
746
|
if (!_conf.superuser_account_ids.includes(req.uid)) {
|
|
890
|
-
throw new Error(
|
|
747
|
+
throw new Error('user is not authorized for this method');
|
|
891
748
|
}
|
|
892
749
|
|
|
893
750
|
let selector = {
|
|
894
|
-
docType:
|
|
751
|
+
docType: 'account',
|
|
895
752
|
stat: 3,
|
|
896
753
|
};
|
|
897
754
|
|
|
@@ -900,32 +757,32 @@ exports.search_users = async function (req) {
|
|
|
900
757
|
...selector,
|
|
901
758
|
$or: [
|
|
902
759
|
{
|
|
903
|
-
|
|
760
|
+
'account_info.first_name': {
|
|
904
761
|
$regex: `(?i)${req.search}`,
|
|
905
762
|
},
|
|
906
763
|
},
|
|
907
764
|
{
|
|
908
|
-
|
|
765
|
+
'account_info.last_name': {
|
|
909
766
|
$regex: `(?i)${req.search}`,
|
|
910
767
|
},
|
|
911
768
|
},
|
|
912
769
|
{
|
|
913
|
-
|
|
770
|
+
'account_info.company_name': {
|
|
914
771
|
$regex: `(?i)${req.search}`,
|
|
915
772
|
},
|
|
916
773
|
},
|
|
917
774
|
{
|
|
918
|
-
|
|
775
|
+
'account_info.tel': {
|
|
919
776
|
$regex: `(?i)${req.search}`,
|
|
920
777
|
},
|
|
921
778
|
},
|
|
922
779
|
{
|
|
923
|
-
|
|
780
|
+
'account_info.email': {
|
|
924
781
|
$regex: `(?i)${req.search}`,
|
|
925
782
|
},
|
|
926
783
|
},
|
|
927
784
|
{
|
|
928
|
-
|
|
785
|
+
'account_info.address': {
|
|
929
786
|
$regex: `(?i)${req.search}`,
|
|
930
787
|
},
|
|
931
788
|
},
|
|
@@ -948,12 +805,12 @@ exports.search_users = async function (req) {
|
|
|
948
805
|
opt.skip = req.skip;
|
|
949
806
|
}
|
|
950
807
|
|
|
951
|
-
if (req?.bookmark !==
|
|
808
|
+
if (req?.bookmark !== 'nil') {
|
|
952
809
|
opt.bookmark = req.bookmark;
|
|
953
810
|
}
|
|
954
811
|
|
|
955
812
|
try {
|
|
956
|
-
var ret = await db_module.find_couch_query(
|
|
813
|
+
var ret = await db_module.find_couch_query('xuda_accounts', opt);
|
|
957
814
|
|
|
958
815
|
return {
|
|
959
816
|
code: 1,
|
|
@@ -971,23 +828,22 @@ exports.search_users = async function (req) {
|
|
|
971
828
|
};
|
|
972
829
|
|
|
973
830
|
exports.read_emails = async function (req) {
|
|
974
|
-
const { uid, email_account_address, email_account_id, accessToken, vendor } =
|
|
975
|
-
req;
|
|
831
|
+
const { uid, email_account_address, email_account_id, accessToken, vendor } = req;
|
|
976
832
|
|
|
977
833
|
const email_module = require(`${module_path}/email_module`);
|
|
978
834
|
|
|
979
835
|
const read_mailbox = async function (mailbox_path, mailbox_name, is_sent) {
|
|
980
|
-
var emails_ret = await db_module.find_couch_query(
|
|
836
|
+
var emails_ret = await db_module.find_couch_query('xuda_emails', {
|
|
981
837
|
selector: {
|
|
982
|
-
docType:
|
|
838
|
+
docType: 'email',
|
|
983
839
|
uid,
|
|
984
840
|
email_account_address,
|
|
985
841
|
email_account_id,
|
|
986
842
|
mailbox_path,
|
|
987
843
|
},
|
|
988
|
-
fields: [
|
|
844
|
+
fields: ['message.seq'],
|
|
989
845
|
limit: 1,
|
|
990
|
-
sort: [{
|
|
846
|
+
sort: [{ 'message.seq': 'desc' }],
|
|
991
847
|
});
|
|
992
848
|
|
|
993
849
|
var imap_ret;
|
|
@@ -995,7 +851,7 @@ exports.read_emails = async function (req) {
|
|
|
995
851
|
const seq = emails_ret?.docs?.[0]?.message?.seq || 1;
|
|
996
852
|
imap_ret = await email_module.query_imap({
|
|
997
853
|
email_account_address,
|
|
998
|
-
type:
|
|
854
|
+
type: 'all',
|
|
999
855
|
boxLock: mailbox_path,
|
|
1000
856
|
seq,
|
|
1001
857
|
accessToken,
|
|
@@ -1009,8 +865,8 @@ exports.read_emails = async function (req) {
|
|
|
1009
865
|
delete message.modseq;
|
|
1010
866
|
if (message.seq > seq) {
|
|
1011
867
|
docs.push({
|
|
1012
|
-
_id: await _common.xuda_get_uuid(
|
|
1013
|
-
docType:
|
|
868
|
+
_id: await _common.xuda_get_uuid('email'),
|
|
869
|
+
docType: 'email',
|
|
1014
870
|
uid,
|
|
1015
871
|
email_account_address,
|
|
1016
872
|
email_account_id,
|
|
@@ -1019,21 +875,17 @@ exports.read_emails = async function (req) {
|
|
|
1019
875
|
mailbox_name,
|
|
1020
876
|
});
|
|
1021
877
|
if (is_sent) {
|
|
1022
|
-
recipient[message.envelope.to[0].address] =
|
|
1023
|
-
message.envelope.to[0].name;
|
|
878
|
+
recipient[message.envelope.to[0].address] = message.envelope.to[0].name;
|
|
1024
879
|
} else {
|
|
1025
|
-
senders[message.envelope.sender[0].address] =
|
|
1026
|
-
message.envelope.sender[0].name;
|
|
880
|
+
senders[message.envelope.sender[0].address] = message.envelope.sender[0].name;
|
|
1027
881
|
}
|
|
1028
882
|
}
|
|
1029
883
|
}
|
|
1030
884
|
|
|
1031
885
|
if (docs.length) {
|
|
1032
|
-
await db_module.
|
|
886
|
+
await db_module.save_couch_bulk_docs('xuda_emails', docs);
|
|
1033
887
|
|
|
1034
|
-
for await (let [email, name] of Object.entries(
|
|
1035
|
-
is_sent ? recipient : senders
|
|
1036
|
-
)) {
|
|
888
|
+
for await (let [email, name] of Object.entries(is_sent ? recipient : senders)) {
|
|
1037
889
|
// const contact_ret = await db_module.get_couch_view_raw(
|
|
1038
890
|
// "xuda_contacts",
|
|
1039
891
|
// "contacts_by_email_address",
|
|
@@ -1064,29 +916,15 @@ exports.read_emails = async function (req) {
|
|
|
1064
916
|
|
|
1065
917
|
for await (let mailbox_obj of mailboxes_ret) {
|
|
1066
918
|
switch (vendor) {
|
|
1067
|
-
case
|
|
1068
|
-
if (
|
|
1069
|
-
|
|
1070
|
-
"[Gmail]/Starred",
|
|
1071
|
-
"[Gmail]/Drafts",
|
|
1072
|
-
"[Gmail]/All Mail",
|
|
1073
|
-
"[Gmail]/Spam",
|
|
1074
|
-
"[Gmail]/Trash",
|
|
1075
|
-
"[Gmail]",
|
|
1076
|
-
"[Gmail]/Important",
|
|
1077
|
-
].includes(mailbox_obj.path)
|
|
1078
|
-
) {
|
|
1079
|
-
await read_mailbox(
|
|
1080
|
-
mailbox_obj.path,
|
|
1081
|
-
mailbox_obj.name,
|
|
1082
|
-
mailbox_obj.path === "[Gmail]/Sent Mail"
|
|
1083
|
-
);
|
|
919
|
+
case 'gmail':
|
|
920
|
+
if (!['[Gmail]/Starred', '[Gmail]/Drafts', '[Gmail]/All Mail', '[Gmail]/Spam', '[Gmail]/Trash', '[Gmail]', '[Gmail]/Important'].includes(mailbox_obj.path)) {
|
|
921
|
+
await read_mailbox(mailbox_obj.path, mailbox_obj.name, mailbox_obj.path === '[Gmail]/Sent Mail');
|
|
1084
922
|
}
|
|
1085
923
|
|
|
1086
924
|
break;
|
|
1087
925
|
|
|
1088
926
|
default:
|
|
1089
|
-
if ([
|
|
927
|
+
if (['INBOX', 'Sent Mail'].includes(mailbox_obj.path)) {
|
|
1090
928
|
await read_mailbox(mailbox_obj.path, mailbox_obj.name);
|
|
1091
929
|
}
|
|
1092
930
|
break;
|
|
@@ -1098,9 +936,9 @@ exports.get_email_accounts = async function (req) {
|
|
|
1098
936
|
const { uid, stat } = req;
|
|
1099
937
|
|
|
1100
938
|
const db_module = require(`${module_path}/db_module`);
|
|
1101
|
-
var email_account_ret = await db_module.find_couch_query(
|
|
939
|
+
var email_account_ret = await db_module.find_couch_query('xuda_emails', {
|
|
1102
940
|
selector: {
|
|
1103
|
-
docType:
|
|
941
|
+
docType: 'email_account',
|
|
1104
942
|
uid,
|
|
1105
943
|
stat: stat || 3,
|
|
1106
944
|
},
|
|
@@ -1110,14 +948,14 @@ exports.get_email_accounts = async function (req) {
|
|
|
1110
948
|
|
|
1111
949
|
const generate_gmail_access_token = async function (email_account) {
|
|
1112
950
|
return new Promise((resolve, reject) => {
|
|
1113
|
-
fetch(
|
|
1114
|
-
method:
|
|
1115
|
-
headers: {
|
|
951
|
+
fetch('https://oauth2.googleapis.com/token', {
|
|
952
|
+
method: 'POST',
|
|
953
|
+
headers: { 'Content-Type': 'application/json' },
|
|
1116
954
|
body: JSON.stringify({
|
|
1117
955
|
refresh_token: email_account.authorization_data.refresh_token,
|
|
1118
956
|
client_id: _conf.gmail.clientId,
|
|
1119
957
|
client_secret: _conf.gmail.clientSecret,
|
|
1120
|
-
grant_type:
|
|
958
|
+
grant_type: 'refresh_token',
|
|
1121
959
|
}),
|
|
1122
960
|
})
|
|
1123
961
|
.then((response) => response.json())
|
|
@@ -1131,17 +969,17 @@ exports.find_contact_duplicates = async function (req) {
|
|
|
1131
969
|
const { uid, name } = req;
|
|
1132
970
|
var opt = {
|
|
1133
971
|
selector: {
|
|
1134
|
-
docType:
|
|
972
|
+
docType: 'contact',
|
|
1135
973
|
stat: { $lt: 4 },
|
|
1136
974
|
uid,
|
|
1137
975
|
},
|
|
1138
976
|
limit: 999999,
|
|
1139
977
|
};
|
|
1140
978
|
|
|
1141
|
-
if (typeof name !==
|
|
979
|
+
if (typeof name !== 'undefined') {
|
|
1142
980
|
opt.selector.name = name;
|
|
1143
981
|
}
|
|
1144
|
-
const contact_ret = await db_module.find_couch_query(
|
|
982
|
+
const contact_ret = await db_module.find_couch_query('xuda_contacts', opt);
|
|
1145
983
|
var duplicates = {};
|
|
1146
984
|
for (contact of contact_ret.docs) {
|
|
1147
985
|
if (!duplicates[contact.name]) {
|
|
@@ -1175,7 +1013,7 @@ exports.merge_contact = async function (req) {
|
|
|
1175
1013
|
|
|
1176
1014
|
var duplicate_arr = duplicate_ret?.data?.[name] || [];
|
|
1177
1015
|
if (!duplicate_arr.length) {
|
|
1178
|
-
return { code: 1, data:
|
|
1016
|
+
return { code: 1, data: 'no merge performed' };
|
|
1179
1017
|
}
|
|
1180
1018
|
var contact_to_merge;
|
|
1181
1019
|
var contact_to_delete = [];
|
|
@@ -1192,12 +1030,7 @@ exports.merge_contact = async function (req) {
|
|
|
1192
1030
|
for (let [i, contact_id] of Object.entries(contact_to_delete)) {
|
|
1193
1031
|
var contact_obj = duplicate_arr.find((e) => e._id === contact_id);
|
|
1194
1032
|
|
|
1195
|
-
if (
|
|
1196
|
-
contact_obj.name &&
|
|
1197
|
-
!["spam", "newsletter", "noreply", "no-reply"].includes(
|
|
1198
|
-
contact_obj.email?.[0]?.toLowerCase()
|
|
1199
|
-
)
|
|
1200
|
-
) {
|
|
1033
|
+
if (contact_obj.name && !['spam', 'newsletter', 'noreply', 'no-reply'].includes(contact_obj.email?.[0]?.toLowerCase())) {
|
|
1201
1034
|
contact_to_merge = contact_id;
|
|
1202
1035
|
contact_to_delete = contact_to_delete.splice(i, 1);
|
|
1203
1036
|
break;
|
|
@@ -1211,35 +1044,25 @@ exports.merge_contact = async function (req) {
|
|
|
1211
1044
|
}
|
|
1212
1045
|
|
|
1213
1046
|
// get the primary account
|
|
1214
|
-
const primary_contact_ret = await db_module.get_couch_doc(
|
|
1215
|
-
"xuda_contacts",
|
|
1216
|
-
contact_to_merge
|
|
1217
|
-
);
|
|
1047
|
+
const primary_contact_ret = await db_module.get_couch_doc('xuda_contacts', contact_to_merge);
|
|
1218
1048
|
if (primary_contact_ret.code < 0) {
|
|
1219
1049
|
return primary_contact_ret;
|
|
1220
1050
|
}
|
|
1221
1051
|
var primary_contact_doc = primary_contact_ret.data;
|
|
1222
1052
|
|
|
1223
1053
|
primary_contact_doc.stat_ts = Date.now();
|
|
1224
|
-
primary_contact_doc.stat_reason =
|
|
1054
|
+
primary_contact_doc.stat_reason = 'merge';
|
|
1225
1055
|
|
|
1226
1056
|
for (let contact_id of contact_to_delete) {
|
|
1227
|
-
var email_address = duplicate_arr.find((e) => e._id === contact_id)
|
|
1228
|
-
.email[0];
|
|
1057
|
+
var email_address = duplicate_arr.find((e) => e._id === contact_id).email[0];
|
|
1229
1058
|
|
|
1230
1059
|
primary_contact_doc.email.push(email_address);
|
|
1231
1060
|
}
|
|
1232
1061
|
|
|
1233
|
-
const primary_contact_save_ret = await db_module.save_couch_doc(
|
|
1234
|
-
"xuda_contacts",
|
|
1235
|
-
primary_contact_doc
|
|
1236
|
-
);
|
|
1062
|
+
const primary_contact_save_ret = await db_module.save_couch_doc('xuda_contacts', primary_contact_doc);
|
|
1237
1063
|
|
|
1238
1064
|
for (let contact_id of contact_to_delete) {
|
|
1239
|
-
const contact_ret = await db_module.get_couch_doc(
|
|
1240
|
-
"xuda_contacts",
|
|
1241
|
-
contact_id
|
|
1242
|
-
);
|
|
1065
|
+
const contact_ret = await db_module.get_couch_doc('xuda_contacts', contact_id);
|
|
1243
1066
|
if (contact_ret.code < 0) {
|
|
1244
1067
|
continue;
|
|
1245
1068
|
}
|
|
@@ -1247,8 +1070,8 @@ exports.merge_contact = async function (req) {
|
|
|
1247
1070
|
var contact_doc = contact_ret.data;
|
|
1248
1071
|
contact_doc.stat = 4;
|
|
1249
1072
|
contact_doc.stat_ts = Date.now();
|
|
1250
|
-
contact_doc.stat_reason =
|
|
1251
|
-
await db_module.save_couch_doc(
|
|
1073
|
+
contact_doc.stat_reason = 'merge';
|
|
1074
|
+
await db_module.save_couch_doc('xuda_contacts', contact_doc);
|
|
1252
1075
|
}
|
|
1253
1076
|
|
|
1254
1077
|
return primary_contact_save_ret; //{ code: 1, data: duplicate_arr };
|
|
@@ -1258,14 +1081,14 @@ exports.get_contacts = async function (req) {
|
|
|
1258
1081
|
const { uid, name, limit, skip, bookmark } = req;
|
|
1259
1082
|
var opt = {
|
|
1260
1083
|
selector: {
|
|
1261
|
-
docType:
|
|
1084
|
+
docType: 'contact',
|
|
1262
1085
|
uid,
|
|
1263
1086
|
stat: { $lt: 4 },
|
|
1264
1087
|
},
|
|
1265
1088
|
limit: limit ? limit : 99999,
|
|
1266
1089
|
};
|
|
1267
1090
|
|
|
1268
|
-
if (typeof name !==
|
|
1091
|
+
if (typeof name !== 'undefined') {
|
|
1269
1092
|
opt.selector.name = { $regex: `(?i)${name}` };
|
|
1270
1093
|
}
|
|
1271
1094
|
|
|
@@ -1273,10 +1096,10 @@ exports.get_contacts = async function (req) {
|
|
|
1273
1096
|
opt.skip = skip;
|
|
1274
1097
|
}
|
|
1275
1098
|
|
|
1276
|
-
if (bookmark !==
|
|
1099
|
+
if (bookmark !== 'nil') {
|
|
1277
1100
|
opt.bookmark = bookmark;
|
|
1278
1101
|
}
|
|
1279
|
-
const contacts_ret = await db_module.find_couch_query(
|
|
1102
|
+
const contacts_ret = await db_module.find_couch_query('xuda_contacts', opt);
|
|
1280
1103
|
|
|
1281
1104
|
return contacts_ret;
|
|
1282
1105
|
};
|
|
@@ -1284,19 +1107,16 @@ exports.get_contacts = async function (req) {
|
|
|
1284
1107
|
exports.delete_contact = async function (req) {
|
|
1285
1108
|
const { _id } = req;
|
|
1286
1109
|
|
|
1287
|
-
const contact_ret = await db_module.get_couch_doc(
|
|
1110
|
+
const contact_ret = await db_module.get_couch_doc('xuda_contacts', _id);
|
|
1288
1111
|
if (contact_ret.code < 0) {
|
|
1289
1112
|
return contact_ret;
|
|
1290
1113
|
}
|
|
1291
1114
|
var contact_doc = contact_ret.data;
|
|
1292
1115
|
|
|
1293
1116
|
contact_doc.stat_ts = Date.now();
|
|
1294
|
-
contact_doc.stat_reason =
|
|
1117
|
+
contact_doc.stat_reason = 'deleted';
|
|
1295
1118
|
|
|
1296
|
-
const contact_save_ret = await db_module.save_couch_doc(
|
|
1297
|
-
"xuda_contacts",
|
|
1298
|
-
contact_doc
|
|
1299
|
-
);
|
|
1119
|
+
const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
|
|
1300
1120
|
|
|
1301
1121
|
return contact_save_ret;
|
|
1302
1122
|
};
|
|
@@ -1315,43 +1135,31 @@ exports.delete_contact = async function (req) {
|
|
|
1315
1135
|
|
|
1316
1136
|
exports.add_contact = async function (req) {
|
|
1317
1137
|
const { uid, email, name, stat, contact_uid, req_id } = req;
|
|
1318
|
-
const contact_ret = await db_module.get_couch_view_raw(
|
|
1319
|
-
"xuda_contacts",
|
|
1320
|
-
"contacts_by_email_address",
|
|
1321
|
-
{ key: [uid, email] }
|
|
1322
|
-
);
|
|
1138
|
+
const contact_ret = await db_module.get_couch_view_raw('xuda_contacts', 'contacts_by_email_address', { key: [uid, email] });
|
|
1323
1139
|
|
|
1324
1140
|
if (!contact_ret.rows.length) {
|
|
1325
|
-
const ret = await db_module.save_couch_doc(
|
|
1326
|
-
_id: await _common.xuda_get_uuid(
|
|
1141
|
+
const ret = await db_module.save_couch_doc('xuda_contacts', {
|
|
1142
|
+
_id: await _common.xuda_get_uuid('contact'),
|
|
1327
1143
|
email: [email],
|
|
1328
1144
|
name,
|
|
1329
1145
|
stat: stat || 1,
|
|
1330
1146
|
date_created: Date.now(),
|
|
1331
|
-
docType:
|
|
1147
|
+
docType: 'contact',
|
|
1332
1148
|
contact_uid,
|
|
1333
1149
|
uid,
|
|
1334
1150
|
req_id,
|
|
1335
1151
|
});
|
|
1336
1152
|
return ret;
|
|
1337
1153
|
}
|
|
1338
|
-
return { code: -1, data:
|
|
1154
|
+
return { code: -1, data: 'contact already exist active or deleted' };
|
|
1339
1155
|
};
|
|
1340
1156
|
exports.update_contact = async function (req) {
|
|
1341
1157
|
const { uid, email, contact_uid, stat, req_id } = req;
|
|
1342
1158
|
var contact_ret;
|
|
1343
1159
|
if (email) {
|
|
1344
|
-
contact_ret = await db_module.get_couch_view_raw(
|
|
1345
|
-
"xuda_contacts",
|
|
1346
|
-
"contacts_by_email_address",
|
|
1347
|
-
{ key: [uid, email] }
|
|
1348
|
-
);
|
|
1160
|
+
contact_ret = await db_module.get_couch_view_raw('xuda_contacts', 'contacts_by_email_address', { key: [uid, email] });
|
|
1349
1161
|
} else if (contact_uid) {
|
|
1350
|
-
contact_ret = await db_module.get_couch_view_raw(
|
|
1351
|
-
"xuda_contacts",
|
|
1352
|
-
"contacts_by_contact_uid",
|
|
1353
|
-
{ key: [uid, contact_uid] }
|
|
1354
|
-
);
|
|
1162
|
+
contact_ret = await db_module.get_couch_view_raw('xuda_contacts', 'contacts_by_contact_uid', { key: [uid, contact_uid] });
|
|
1355
1163
|
}
|
|
1356
1164
|
if (contact_ret.rows.length) {
|
|
1357
1165
|
var doc = contact_ret.rows[0].value;
|
|
@@ -1362,9 +1170,9 @@ exports.update_contact = async function (req) {
|
|
|
1362
1170
|
if (req_id) {
|
|
1363
1171
|
doc.req_id = req_id;
|
|
1364
1172
|
}
|
|
1365
|
-
return await db_module.save_couch_doc(
|
|
1173
|
+
return await db_module.save_couch_doc('xuda_contacts', doc);
|
|
1366
1174
|
}
|
|
1367
|
-
return { code: -1, data:
|
|
1175
|
+
return { code: -1, data: 'contact not found' };
|
|
1368
1176
|
};
|
|
1369
1177
|
exports.invite_user = async function (req) {
|
|
1370
1178
|
const { email, name, uid } = req;
|
|
@@ -1372,7 +1180,7 @@ exports.invite_user = async function (req) {
|
|
|
1372
1180
|
const team_module = require(`${module_path}/team_module`);
|
|
1373
1181
|
const ret = await team_module.create_team_request({
|
|
1374
1182
|
email,
|
|
1375
|
-
access_type:
|
|
1183
|
+
access_type: 'contact',
|
|
1376
1184
|
uid,
|
|
1377
1185
|
});
|
|
1378
1186
|
|
|
@@ -1380,9 +1188,9 @@ exports.invite_user = async function (req) {
|
|
|
1380
1188
|
};
|
|
1381
1189
|
|
|
1382
1190
|
exports.get_account_rt_info = async function (uid) {
|
|
1383
|
-
const doc_ret = await db_module.get_couch_doc(
|
|
1191
|
+
const doc_ret = await db_module.get_couch_doc('xuda_accounts', uid);
|
|
1384
1192
|
if (doc_ret.code < 0) {
|
|
1385
|
-
console.error(
|
|
1193
|
+
console.error('get_account_rt_info', doc_ret);
|
|
1386
1194
|
return {};
|
|
1387
1195
|
}
|
|
1388
1196
|
var account_info = doc_ret.data.account_info;
|
|
@@ -1451,7 +1259,7 @@ exports.validate_account_topup = async function (uid, items = []) {
|
|
|
1451
1259
|
err.code = -90;
|
|
1452
1260
|
err.billing_problem = true;
|
|
1453
1261
|
|
|
1454
|
-
throw new Error(
|
|
1262
|
+
throw new Error('no card on file');
|
|
1455
1263
|
}
|
|
1456
1264
|
|
|
1457
1265
|
if (data?.balance?.past_due) {
|
|
@@ -1495,49 +1303,49 @@ const get_prices = function (uid, item) {
|
|
|
1495
1303
|
let price;
|
|
1496
1304
|
|
|
1497
1305
|
switch (item) {
|
|
1498
|
-
case
|
|
1499
|
-
case
|
|
1500
|
-
case
|
|
1501
|
-
case
|
|
1502
|
-
case
|
|
1503
|
-
case
|
|
1504
|
-
case
|
|
1306
|
+
case 'user_group':
|
|
1307
|
+
case 'pwa':
|
|
1308
|
+
case 'preview':
|
|
1309
|
+
case 'android':
|
|
1310
|
+
case 'ios':
|
|
1311
|
+
case 'macos':
|
|
1312
|
+
case 'emitter':
|
|
1505
1313
|
price = _conf.PRICE_OBJ.deployments[item];
|
|
1506
1314
|
break;
|
|
1507
1315
|
|
|
1508
|
-
case
|
|
1316
|
+
case 'drive_ocr':
|
|
1509
1317
|
price = _conf.PRICE_OBJ.drive_addons.ocr.price;
|
|
1510
1318
|
break;
|
|
1511
1319
|
|
|
1512
|
-
case
|
|
1320
|
+
case 'drive_edit_image':
|
|
1513
1321
|
price = _conf.PRICE_OBJ.drive_addons.edit_image.price;
|
|
1514
1322
|
break;
|
|
1515
1323
|
|
|
1516
|
-
case
|
|
1324
|
+
case 'auto_backup':
|
|
1517
1325
|
price = _conf.PRICE_OBJ.auto_backup_per_month;
|
|
1518
1326
|
break;
|
|
1519
1327
|
|
|
1520
|
-
case
|
|
1328
|
+
case 'deployment_offline':
|
|
1521
1329
|
price = _conf.PRICE_OBJ.deployment_offline_per_month;
|
|
1522
1330
|
break;
|
|
1523
1331
|
|
|
1524
|
-
case
|
|
1332
|
+
case 'xuda_subdomain':
|
|
1525
1333
|
price = _conf.PRICE_OBJ.xuda_subdomain_per_month;
|
|
1526
1334
|
break;
|
|
1527
1335
|
|
|
1528
|
-
case
|
|
1336
|
+
case 'custom_domain':
|
|
1529
1337
|
price = _conf.PRICE_OBJ.custom_domain_per_month;
|
|
1530
1338
|
break;
|
|
1531
1339
|
|
|
1532
|
-
case
|
|
1340
|
+
case 'user_monitor':
|
|
1533
1341
|
price = _conf.PRICE_OBJ.user_monitor_per_month;
|
|
1534
1342
|
break;
|
|
1535
1343
|
|
|
1536
|
-
case
|
|
1344
|
+
case 'branding':
|
|
1537
1345
|
price = _conf.PRICE_OBJ.branding_per_month;
|
|
1538
1346
|
break;
|
|
1539
1347
|
|
|
1540
|
-
case
|
|
1348
|
+
case 'utility_screen':
|
|
1541
1349
|
price = _conf.PRICE_OBJ.utility_screen_per_month;
|
|
1542
1350
|
break;
|
|
1543
1351
|
|
|
@@ -1567,7 +1375,7 @@ const get_prices = function (uid, item) {
|
|
|
1567
1375
|
// // val._deleted = true;
|
|
1568
1376
|
// // contact_docs.push(val);
|
|
1569
1377
|
// // }
|
|
1570
|
-
// // await db_module.
|
|
1378
|
+
// // await db_module.save_couch_bulk_docs("xuda_contacts", contact_docs);
|
|
1571
1379
|
|
|
1572
1380
|
// // const emails_ret = await db_module.find_couch_query("xuda_emails", {
|
|
1573
1381
|
// // selector: {
|
|
@@ -1581,7 +1389,7 @@ const get_prices = function (uid, item) {
|
|
|
1581
1389
|
// // email_docs.push(val);
|
|
1582
1390
|
// // }
|
|
1583
1391
|
|
|
1584
|
-
// // await db_module.
|
|
1392
|
+
// // await db_module.save_couch_bulk_docs("xuda_emails", email_docs);
|
|
1585
1393
|
|
|
1586
1394
|
// ///////////////////////////////////
|
|
1587
1395
|
// return;
|