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