@xuda.io/account_module 1.2.2072 → 1.2.2074

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.mjs +124 -123
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -42,7 +42,8 @@ const _utils = await import(path.join(process.env.XUDA_HOME, 'common', 'xuda-cpi
42
42
  // Module Paths
43
43
  const module_path = path.join(process.env.XUDA_HOME, 'cpi') + (!_conf.is_debug ? '/node_modules/@xuda.io' : '');
44
44
 
45
- const db_ms = await import(`${module_path}/db_module/index.mjs`);
45
+ const db_module = await import(`${module_path}/db_module/index.mjs`);
46
+
46
47
  const ai_ms = await import(`${module_path}/ai_module/index_ms.mjs`);
47
48
  const drive_ms = await import(`${module_path}/drive_module/index_ms.mjs`);
48
49
 
@@ -77,7 +78,7 @@ export const update_account_info = async function (req, job_id, headers) {
77
78
  }
78
79
  }
79
80
 
80
- const ret = await db_ms.get_couch_doc('xuda_accounts', uid);
81
+ const ret = await db_module.get_couch_doc('xuda_accounts', uid);
81
82
  var account_obj = ret.data;
82
83
  account_obj.ts = Date.now();
83
84
  var change = '';
@@ -170,7 +171,7 @@ export const update_account_info = async function (req, job_id, headers) {
170
171
  delete account_obj.account_info.job_id;
171
172
  delete account_obj.account_info.app_id;
172
173
 
173
- const save_ret = await db_ms.save_couch_doc('xuda_accounts', account_obj);
174
+ const save_ret = await db_module.save_couch_doc('xuda_accounts', account_obj);
174
175
 
175
176
  if (account_obj.account_info?.profile_picture) {
176
177
  if (!account_obj.account_info?.profile_avatar && account_obj.account_info.profile_avatar_stat !== 2) {
@@ -187,12 +188,12 @@ export const update_account_info = async function (req, job_id, headers) {
187
188
  export const update_account_preferences = async function (req) {
188
189
  const { uid, hide_create_button, default_dashboard, sidebar_style } = req;
189
190
  try {
190
- let account_doc = await db_ms.get_couch_doc_native('xuda_accounts', uid);
191
+ let account_doc = await db_module.get_couch_doc_native('xuda_accounts', uid);
191
192
  account_doc.ts = Date.now();
192
193
 
193
194
  account_doc.preferences = { hide_create_button, default_dashboard, sidebar_style };
194
195
 
195
- const save_ret = await db_ms.save_couch_doc('xuda_accounts', account_doc);
196
+ const save_ret = await db_module.save_couch_doc('xuda_accounts', account_doc);
196
197
  if (save_ret.code < 0) {
197
198
  throw new Error(save_ret.data);
198
199
  }
@@ -203,7 +204,7 @@ export const update_account_preferences = async function (req) {
203
204
  };
204
205
 
205
206
  export const save_admin_presets = async function (req) {
206
- const ret = await db_ms.get_couch_doc('xuda_master', req.app_id);
207
+ const ret = await db_module.get_couch_doc('xuda_master', req.app_id);
207
208
  if (ret.code < 0) {
208
209
  return ret;
209
210
  }
@@ -211,7 +212,7 @@ export const save_admin_presets = async function (req) {
211
212
  app_obj.deploy_data.admin_presets = req.admin_presets;
212
213
  app_obj.deploy_data.preset_id = req.preset_id;
213
214
 
214
- const ret3 = await db_ms.save_app_obj(app_obj, null, req.app_id);
215
+ const ret3 = await db_module.save_app_obj(app_obj, null, req.app_id);
215
216
  var obj = {};
216
217
  if (ret3.code > -1) {
217
218
  obj = {
@@ -233,7 +234,7 @@ export const increment_account_usage = async function (req) {
233
234
  if (uid) {
234
235
  opt.key = uid;
235
236
  }
236
- const accounts_ret = await db_ms.get_couch_view_raw('xuda_accounts', 'all_accounts', opt);
237
+ const accounts_ret = await db_module.get_couch_view_raw('xuda_accounts', 'all_accounts', opt);
237
238
 
238
239
  function bytesToGB(bytes) {
239
240
  return bytes / 1073741824; // or bytes / (1024 ** 3)
@@ -246,7 +247,7 @@ export const increment_account_usage = async function (req) {
246
247
  try {
247
248
  const usage_id = await _common.xuda_get_uuid('usage');
248
249
 
249
- const usage_ret = await db_ms.get_couch_view_raw('xuda_usage', 'open_drive_usage', {
250
+ const usage_ret = await db_module.get_couch_view_raw('xuda_usage', 'open_drive_usage', {
250
251
  key: account_data._id,
251
252
  });
252
253
 
@@ -280,7 +281,7 @@ export const increment_account_usage = async function (req) {
280
281
  doc.price += price_per_hour;
281
282
 
282
283
  doc.ts = Date.now();
283
- await db_ms.save_couch_doc('xuda_usage', doc);
284
+ await db_module.save_couch_doc('xuda_usage', doc);
284
285
  }
285
286
  }
286
287
  } catch (err) {
@@ -289,7 +290,7 @@ export const increment_account_usage = async function (req) {
289
290
  };
290
291
 
291
292
  const run_account_apps = async function (account_data) {
292
- const apps_ret = await db_ms.get_couch_view('xuda_master', 'user_apps', {
293
+ const apps_ret = await db_module.get_couch_view('xuda_master', 'user_apps', {
293
294
  startkey: [account_data._id, ''],
294
295
  endkey: [account_data._id, 'ZZZZZ'],
295
296
  include_docs: true,
@@ -323,13 +324,13 @@ export const increment_account_usage = async function (req) {
323
324
  if (size_ret.code > -1) account_data.plugins_drive_size += size_ret.data;
324
325
 
325
326
  // db data
326
- const couch_info_ret = await db_ms.get_couch_app_info(app.id);
327
+ const couch_info_ret = await db_module.get_couch_app_info(app.id);
327
328
  account_data.project_data_size += couch_info_ret?.data?.sizes?.active || 0;
328
329
  }
329
330
 
330
331
  const usage_id = await _common.xuda_get_uuid('usage');
331
332
 
332
- const usage_ret = await db_ms.get_couch_view_raw('xuda_usage', 'open_app_usage', {
333
+ const usage_ret = await db_module.get_couch_view_raw('xuda_usage', 'open_app_usage', {
333
334
  key: [app.id, app.doc?.app_hosting?.app_server_type || app.doc.app_type],
334
335
  });
335
336
 
@@ -382,7 +383,7 @@ export const increment_account_usage = async function (req) {
382
383
  } else {
383
384
  // backup of project
384
385
 
385
- const info_ret = await db_ms.get_couch_app_info(doc.app_id);
386
+ const info_ret = await db_module.get_couch_app_info(doc.app_id);
386
387
  if (info_ret.code > 0) {
387
388
  const size = info_ret.data.sizes.active / 1000 / 1000 / 1000; //gb
388
389
  price_per_hour = (size * _conf.PRICE_OBJ.price_per_gb_backup) / _conf.PRICE_OBJ.avg_hours_in_month;
@@ -391,7 +392,7 @@ export const increment_account_usage = async function (req) {
391
392
  break;
392
393
 
393
394
  case 'user_group': {
394
- const team_ret = await db_ms.get_couch_view('xuda_team', 'user_group_shares_count', {
395
+ const team_ret = await db_module.get_couch_view('xuda_team', 'user_group_shares_count', {
395
396
  key: app.id,
396
397
  reduce: true,
397
398
  group_level: 1,
@@ -421,7 +422,7 @@ export const increment_account_usage = async function (req) {
421
422
  }
422
423
 
423
424
  doc.ts = Date.now();
424
- await db_ms.save_couch_doc('xuda_usage', doc);
425
+ await db_module.save_couch_doc('xuda_usage', doc);
425
426
  }
426
427
  }
427
428
  } catch (err) {
@@ -449,7 +450,7 @@ export const increment_account_usage = async function (req) {
449
450
  await run_account_drive(account_data);
450
451
 
451
452
  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;
452
- await db_ms.save_couch_doc('xuda_accounts', account_data);
453
+ await db_module.save_couch_doc('xuda_accounts', account_data);
453
454
  }
454
455
  };
455
456
 
@@ -457,11 +458,11 @@ export const get_account_data = async function (req) {
457
458
  var { uid, enforce_usage } = req;
458
459
 
459
460
  try {
460
- const { data: acc_obj } = await db_ms.get_couch_doc('xuda_accounts', uid);
461
+ const { data: acc_obj } = await db_module.get_couch_doc('xuda_accounts', uid);
461
462
 
462
463
  // let _design;
463
464
  // if (acc_obj.account_project_id) {
464
- // _design = await db_ms.get_app_couch_doc_native(acc_obj.account_project_id, '_design/xuda');
465
+ // _design = await db_module.get_app_couch_doc_native(acc_obj.account_project_id, '_design/xuda');
465
466
  // }
466
467
 
467
468
  var ret = { code: 1 };
@@ -526,23 +527,23 @@ export const get_account_projects = async function (req) {
526
527
  opt = {};
527
528
  }
528
529
 
529
- var ret = await db_ms.get_couch_view('xuda_master', 'user_projects', opt);
530
+ var ret = await db_module.get_couch_view('xuda_master', 'user_projects', opt);
530
531
 
531
532
  return ret;
532
533
  };
533
534
 
534
535
  export const get_account_datacenters = async function (req) {
535
- return await db_ms.get_couch_view('xuda_master', 'user_datacenters', {
536
+ return await db_module.get_couch_view('xuda_master', 'user_datacenters', {
536
537
  key: req.uid,
537
538
  });
538
539
  };
539
540
  export const get_account_deployments = async function (req) {
540
- return await db_ms.get_couch_view('xuda_master', 'user_deployments', {
541
+ return await db_module.get_couch_view('xuda_master', 'user_deployments', {
541
542
  key: req.uid,
542
543
  });
543
544
  };
544
545
  export const get_account_instances = async function (req) {
545
- return await db_ms.get_couch_view('xuda_master', 'user_all_instances', {
546
+ return await db_module.get_couch_view('xuda_master', 'user_all_instances', {
546
547
  key: req.uid,
547
548
  });
548
549
  };
@@ -556,11 +557,11 @@ export const get_account_info = async function (req) {
556
557
 
557
558
  export const get_active_account_profile_info = async function (uid, profile_id) {
558
559
  try {
559
- const acc_obj = await db_ms.get_couch_doc_native('xuda_accounts', uid);
560
+ const acc_obj = await db_module.get_couch_doc_native('xuda_accounts', uid);
560
561
 
561
562
  let active_account_profile_id = profile_id || acc_obj.account_info.active_account_profile_id;
562
563
 
563
- const account_profile_obj = await db_ms.get_app_couch_doc_native(acc_obj.account_project_id, active_account_profile_id);
564
+ const account_profile_obj = await db_module.get_app_couch_doc_native(acc_obj.account_project_id, active_account_profile_id);
564
565
  if (account_profile_obj.share_item_id) {
565
566
  // set the original profile id if shared
566
567
  active_account_profile_id = account_profile_obj.share_item_id;
@@ -575,7 +576,7 @@ export const get_active_account_profile_info = async function (uid, profile_id)
575
576
  };
576
577
 
577
578
  export const get_account_name = async function (req) {
578
- const data = await db_ms.get_couch_doc('xuda_accounts', req.uid_query || req.uid);
579
+ const data = await db_module.get_couch_doc('xuda_accounts', req.uid_query || req.uid);
579
580
  if (data.code < 0) {
580
581
  return data;
581
582
  }
@@ -638,7 +639,7 @@ export const account_validate_username = async function (req) {
638
639
  limit: 1,
639
640
  };
640
641
 
641
- var ret = await db_ms.find_couch_query('xuda_accounts', opt);
642
+ var ret = await db_module.find_couch_query('xuda_accounts', opt);
642
643
 
643
644
  if (ret.docs.length) {
644
645
  return { code: -400, data: 'User exist' };
@@ -648,7 +649,7 @@ export const account_validate_username = async function (req) {
648
649
  };
649
650
 
650
651
  // export const get_account_activity = async function (req) {
651
- // return await db_ms.get_couch_view(
652
+ // return await db_module.get_couch_view(
652
653
  // "xuda_activity",
653
654
  // "activity_per_account",
654
655
  // {
@@ -663,14 +664,14 @@ export const verify_account = async function (req) {
663
664
  return { code: -10, data: 'Missing id' };
664
665
  }
665
666
 
666
- const ret = await db_ms.get_couch_doc('xuda_accounts', account_id);
667
+ const ret = await db_module.get_couch_doc('xuda_accounts', account_id);
667
668
 
668
669
  if (ret.code < 0) {
669
670
  return { code: -1, data: 'account not found' };
670
671
  }
671
672
  var obj = ret.data;
672
673
  obj.stat = 3;
673
- const ret_acc = await db_ms.save_couch_doc('xuda_accounts', obj);
674
+ const ret_acc = await db_module.save_couch_doc('xuda_accounts', obj);
674
675
  if (ret.code < 0) {
675
676
  return ret_acc;
676
677
  }
@@ -680,7 +681,7 @@ export const verify_account = async function (req) {
680
681
  export const validate_user_plan = async function (req) {
681
682
  const { account_id, app_obj } = req;
682
683
 
683
- const apps_ret = await db_ms.get_couch_view('xuda_master', 'user_apps_count', {
684
+ const apps_ret = await db_module.get_couch_view('xuda_master', 'user_apps_count', {
684
685
  startkey: [account_id, ''],
685
686
  endkey: [account_id, 'ZZZZZ'],
686
687
  reduce: true,
@@ -696,7 +697,7 @@ export const validate_user_plan = async function (req) {
696
697
  {},
697
698
  );
698
699
 
699
- const ret = await db_ms.get_couch_doc('xuda_accounts', account_id);
700
+ const ret = await db_module.get_couch_doc('xuda_accounts', account_id);
700
701
  if (ret.code < 0) {
701
702
  return ret;
702
703
  }
@@ -717,7 +718,7 @@ export const validate_user_plan = async function (req) {
717
718
 
718
719
  // validate number of team members
719
720
 
720
- const user_active_app_requests_count_ret = await db_ms.get_couch_view('xuda_team', 'user_active_app_requests_count', {
721
+ const user_active_app_requests_count_ret = await db_module.get_couch_view('xuda_team', 'user_active_app_requests_count', {
721
722
  key: account_id,
722
723
  reduce: true,
723
724
  // group_level: 2,
@@ -816,7 +817,7 @@ export const save_ssh_key = async function (req) {
816
817
  };
817
818
 
818
819
  if (_id) {
819
- const ret = await db_ms.get_couch_doc('xuda_ssh_keys', _id);
820
+ const ret = await db_module.get_couch_doc('xuda_ssh_keys', _id);
820
821
  if (ret.code < 0) {
821
822
  return ret;
822
823
  }
@@ -829,14 +830,14 @@ export const save_ssh_key = async function (req) {
829
830
  doc.ssh_key_name = ssh_key_name;
830
831
  doc.ssh_key_content = ssh_key_content;
831
832
 
832
- const save_ret = await db_ms.save_couch_doc('xuda_ssh_keys', doc);
833
+ const save_ret = await db_module.save_couch_doc('xuda_ssh_keys', doc);
833
834
 
834
835
  return save_ret;
835
836
  };
836
837
  export const delete_ssh_key = async function (req) {
837
838
  const { ssh_key_id } = req;
838
839
 
839
- const ret = await db_ms.get_couch_doc('xuda_ssh_keys', ssh_key_id);
840
+ const ret = await db_module.get_couch_doc('xuda_ssh_keys', ssh_key_id);
840
841
  if (ret.code < 0) {
841
842
  return ret;
842
843
  }
@@ -844,7 +845,7 @@ export const delete_ssh_key = async function (req) {
844
845
  doc.date_updated_ts = Date.now();
845
846
  doc.stat = 4;
846
847
 
847
- const save_ret = await db_ms.save_couch_doc('xuda_ssh_keys', doc);
848
+ const save_ret = await db_module.save_couch_doc('xuda_ssh_keys', doc);
848
849
 
849
850
  return save_ret;
850
851
  };
@@ -860,7 +861,7 @@ export const get_ssh_keys = async function (req) {
860
861
  opt.selector._id = _id;
861
862
  }
862
863
 
863
- var ret = await db_ms.find_couch_query('xuda_ssh_keys', opt);
864
+ var ret = await db_module.find_couch_query('xuda_ssh_keys', opt);
864
865
  return ret;
865
866
  };
866
867
 
@@ -943,7 +944,7 @@ export const search_users = async function (req) {
943
944
  }
944
945
 
945
946
  try {
946
- var ret = await db_ms.find_couch_query('xuda_accounts', opt);
947
+ var ret = await db_module.find_couch_query('xuda_accounts', opt);
947
948
 
948
949
  let docs = [];
949
950
  for await (let e of ret.docs) {
@@ -1273,7 +1274,7 @@ export const get_contact_info = async function (uid, contact_doc, _id) {
1273
1274
  const chat_conversation_count = async function () {
1274
1275
  const account_profile_info = await get_active_account_profile_info(uid);
1275
1276
 
1276
- const counts_ret = await db_ms.get_app_couch_view(account_profile_info.app_id, 'chat_conversation_item_counts', {
1277
+ const counts_ret = await db_module.get_app_couch_view(account_profile_info.app_id, 'chat_conversation_item_counts', {
1277
1278
  reduce: true,
1278
1279
  group_level: 1,
1279
1280
 
@@ -1286,7 +1287,7 @@ export const get_contact_info = async function (uid, contact_doc, _id) {
1286
1287
  const chat_conversation_read = async function () {
1287
1288
  const account_profile_info = await get_active_account_profile_info(uid);
1288
1289
 
1289
- const counts_ret = await db_ms.get_app_couch_view(account_profile_info.app_id, 'chat_conversation_item_read', {
1290
+ const counts_ret = await db_module.get_app_couch_view(account_profile_info.app_id, 'chat_conversation_item_read', {
1290
1291
  reduce: true,
1291
1292
  group_level: 2,
1292
1293
 
@@ -1321,7 +1322,7 @@ export const get_contact_info = async function (uid, contact_doc, _id) {
1321
1322
 
1322
1323
  if (doc.team_req_id) {
1323
1324
  try {
1324
- const req_doc = await db_ms.get_couch_doc_native('xuda_team', doc.team_req_id);
1325
+ const req_doc = await db_module.get_couch_doc_native('xuda_team', doc.team_req_id);
1325
1326
  doc.connection_stat = req_doc.team_req_stat;
1326
1327
  doc.connection_type = req_doc.access_type;
1327
1328
  doc.connection_uid = req_doc.team_req_from_uid === uid ? req_doc.team_req_to_uid : req_doc.team_req_from_uid;
@@ -1332,7 +1333,7 @@ export const get_contact_info = async function (uid, contact_doc, _id) {
1332
1333
  if (doc.connection_type === 'account_profile') {
1333
1334
  try {
1334
1335
  const app_id = await get_account_default_project_id(uid);
1335
- const account_profile_doc = await db_ms.get_app_couch_doc_native(app_id, req_doc.share_item_id);
1336
+ const account_profile_doc = await db_module.get_app_couch_doc_native(app_id, req_doc.share_item_id);
1336
1337
  doc.account_profile = account_profile_doc;
1337
1338
  } catch (error) {}
1338
1339
  }
@@ -1396,7 +1397,7 @@ export const get_pending_contact_out = async function (uid, _id) {
1396
1397
  if (_id) {
1397
1398
  to_opt.selector._id = _id;
1398
1399
  }
1399
- const requests_to_res = await db_ms.find_couch_query('xuda_team', to_opt);
1400
+ const requests_to_res = await db_module.find_couch_query('xuda_team', to_opt);
1400
1401
 
1401
1402
  for await (let e of requests_to_res.docs) {
1402
1403
  let doc = { docType: 'contact', date_created: e.team_req_date, email: [e.team_req_to_email], name: '', pending: true, contact_uid: e.team_req_to_uid, team_req_id: e._id, team_req_from_uid: uid };
@@ -1427,7 +1428,7 @@ export const get_pending_contact_in = async function (uid, _id) {
1427
1428
  to_opt.selector._id = _id;
1428
1429
  }
1429
1430
 
1430
- const requests_from_res = await db_ms.find_couch_query('xuda_team', from_opt);
1431
+ const requests_from_res = await db_module.find_couch_query('xuda_team', from_opt);
1431
1432
 
1432
1433
  for await (let e of requests_from_res.docs) {
1433
1434
  let doc = { docType: 'contact', date_created: e.team_req_date, email: [e.sender_account_info.email], name: '', pending: true, contact_uid: e.team_req_from_uid, team_req_id: e._id };
@@ -1456,7 +1457,7 @@ export const get_pending_share_contact_in = async function (uid, _id) {
1456
1457
  to_opt.selector._id = _id;
1457
1458
  }
1458
1459
 
1459
- const requests_from_res = await db_ms.find_couch_query('xuda_team', from_opt);
1460
+ const requests_from_res = await db_module.find_couch_query('xuda_team', from_opt);
1460
1461
 
1461
1462
  for await (let e of requests_from_res.docs) {
1462
1463
  let doc = await get_contact(uid, e.share_item_id);
@@ -1524,7 +1525,7 @@ export const update_contact = async function (req) {
1524
1525
  }
1525
1526
 
1526
1527
  if (contact_uid) {
1527
- const ret = await db_ms.get_couch_doc('xuda_accounts', contact_uid);
1528
+ const ret = await db_module.get_couch_doc('xuda_accounts', contact_uid);
1528
1529
  const account_obj = ret.data;
1529
1530
  doc.profile_picture = account_obj.account_info.profile_picture;
1530
1531
  doc.profile_avatar = account_obj.account_info.profile_avatar;
@@ -1536,7 +1537,7 @@ export const update_contact = async function (req) {
1536
1537
  };
1537
1538
 
1538
1539
  export const get_account_rt_info = async function (uid) {
1539
- const doc_ret = await db_ms.get_couch_doc('xuda_accounts', uid);
1540
+ const doc_ret = await db_module.get_couch_doc('xuda_accounts', uid);
1540
1541
  if (doc_ret.code < 0) {
1541
1542
  console.error('get_account_rt_info', doc_ret);
1542
1543
  return {};
@@ -1687,7 +1688,7 @@ export const create_account_oauth_link = async function (req, job_id) {
1687
1688
  };
1688
1689
 
1689
1690
  export const get_default_project_account_doc = async function (uid) {
1690
- const ret = await db_ms.get_couch_doc('xuda_accounts', uid);
1691
+ const ret = await db_module.get_couch_doc('xuda_accounts', uid);
1691
1692
  return ret.data;
1692
1693
  };
1693
1694
  export const get_account_default_project_id = async function (uid) {
@@ -1743,7 +1744,7 @@ export const onboarding_completed = async function (req, job_id, headers) {
1743
1744
  try {
1744
1745
  await update_account_info(req, job_id, headers);
1745
1746
 
1746
- let account_doc = await db_ms.get_couch_doc_native('xuda_accounts', uid);
1747
+ let account_doc = await db_module.get_couch_doc_native('xuda_accounts', uid);
1747
1748
 
1748
1749
  if (!account_doc.account_info?.profile_picture) {
1749
1750
  throw new Error(`missing profile_picture`);
@@ -1757,7 +1758,7 @@ export const onboarding_completed = async function (req, job_id, headers) {
1757
1758
 
1758
1759
  account_doc.boarded_info = { headers, date_ts: Date.now() };
1759
1760
  account_doc.isBoarded = true;
1760
- const save_ret = await db_ms.save_couch_doc('xuda_accounts', account_doc);
1761
+ const save_ret = await db_module.save_couch_doc('xuda_accounts', account_doc);
1761
1762
 
1762
1763
  return save_ret;
1763
1764
  } catch (err) {
@@ -1783,7 +1784,7 @@ export const get_account_ai_usage = async function (req, job_id, headers) {
1783
1784
  const curr_month = Number(String(d.getMonth() + 1).padStart(2, '0')); // months are 0-based
1784
1785
  const curr_year = d.getFullYear();
1785
1786
 
1786
- const ai_usage = await db_ms.get_couch_view('xuda_usage', 'ai_usage', {
1787
+ const ai_usage = await db_module.get_couch_view('xuda_usage', 'ai_usage', {
1787
1788
  startkey: [uid, year || curr_year, month || curr_month, day || 0],
1788
1789
  endkey: [uid, year || curr_year, month || curr_month, day || 99],
1789
1790
  reduce: true,
@@ -1822,7 +1823,7 @@ export const get_account_ai_usage = async function (req, job_id, headers) {
1822
1823
  // let contact_connection = 0;
1823
1824
  // let total_credits = membership_plan + ai_workspace_plan + contact_connection;
1824
1825
 
1825
- const ai_credits = await db_ms.get_couch_view('xuda_billing', 'ai_credits', {
1826
+ const ai_credits = await db_module.get_couch_view('xuda_billing', 'ai_credits', {
1826
1827
  startkey: [uid, ''],
1827
1828
  endkey: [uid, 'zzz'],
1828
1829
  reduce: true,
@@ -1858,7 +1859,7 @@ const set_account_profile_picture = async function (uid, account_uid, metadata,
1858
1859
  try {
1859
1860
  let profile_picture;
1860
1861
 
1861
- let { code: account_code, data: account_obj } = await db_ms.get_couch_doc('xuda_accounts', account_uid);
1862
+ let { code: account_code, data: account_obj } = await db_module.get_couch_doc('xuda_accounts', account_uid);
1862
1863
  let account_info = account_obj.account_info;
1863
1864
 
1864
1865
  if (account_code < 0) {
@@ -1871,7 +1872,7 @@ const set_account_profile_picture = async function (uid, account_uid, metadata,
1871
1872
  if (!account_info.profile_picture) {
1872
1873
  const file_ret = await ai_ms.get_profile_picture(uid, account_info.account_type, name, { email: account_info.email, metadata, account_info }, account_profile_info, job_id, headers);
1873
1874
 
1874
- let { code: account_code, data: account_obj2 } = await db_ms.get_couch_doc('xuda_accounts', account_uid);
1875
+ let { code: account_code, data: account_obj2 } = await db_module.get_couch_doc('xuda_accounts', account_uid);
1875
1876
  account_obj = account_obj2;
1876
1877
  account_info = account_obj.account_info;
1877
1878
 
@@ -1880,7 +1881,7 @@ const set_account_profile_picture = async function (uid, account_uid, metadata,
1880
1881
 
1881
1882
  account_info.profile_picture = account_info.profile_picture_obj.file_url;
1882
1883
  account_info.profile_picture_source = file_ret.profile_picture_source;
1883
- const account_save_ret = await db_ms.save_couch_doc('xuda_accounts', account_obj);
1884
+ const account_save_ret = await db_module.save_couch_doc('xuda_accounts', account_obj);
1884
1885
  profile_picture = account_info.profile_picture;
1885
1886
  }
1886
1887
 
@@ -1925,7 +1926,7 @@ const set_account_profile_picture = async function (uid, account_uid, metadata,
1925
1926
  if (file_ret.code < 0) {
1926
1927
  throw new Error(file_ret.data);
1927
1928
  }
1928
- let { code: account_code, data: account_obj3 } = await db_ms.get_couch_doc('xuda_accounts', account_uid);
1929
+ let { code: account_code, data: account_obj3 } = await db_module.get_couch_doc('xuda_accounts', account_uid);
1929
1930
  account_obj = account_obj3;
1930
1931
  account_info = account_obj.account_info;
1931
1932
 
@@ -1933,7 +1934,7 @@ const set_account_profile_picture = async function (uid, account_uid, metadata,
1933
1934
  account_info.profile_avatar_obj = file_ret.data;
1934
1935
  account_info.profile_avatar = account_info.profile_avatar_obj.file_url;
1935
1936
 
1936
- const account_save_ret = await db_ms.save_couch_doc('xuda_accounts', account_obj);
1937
+ const account_save_ret = await db_module.save_couch_doc('xuda_accounts', account_obj);
1937
1938
  await update_account_profile_picture_status(account_uid, 3);
1938
1939
  }
1939
1940
  } catch (err) {
@@ -1954,7 +1955,7 @@ setTimeout(async () => {
1954
1955
  // ret = await create_account_profile({ uid });
1955
1956
  // ret = await get_account_profiles({ uid });
1956
1957
 
1957
- // let _design = await db_ms.get_app_couch_doc_native(app_id, '_design/xuda');
1958
+ // let _design = await db_module.get_app_couch_doc_native(app_id, '_design/xuda');
1958
1959
  // ret = _design.views.ai_chat_usage;
1959
1960
  // console.log(_design.views.ai_chat_usage);
1960
1961
  // ret = await set_contact_profile_picture(uid, 'cnt_2d21d55f8f0cdaf65a7a69f69617f532', {}, null, {});
@@ -2023,7 +2024,7 @@ export const add_contact = async function (req, job_id, headers) {
2023
2024
  let cached_contact;
2024
2025
  let business_has_person;
2025
2026
  if (contact_uid) {
2026
- let { code: account_code, data: account_obj } = await db_ms.get_couch_doc('xuda_accounts', contact_uid);
2027
+ let { code: account_code, data: account_obj } = await db_module.get_couch_doc('xuda_accounts', contact_uid);
2027
2028
  // account_info = account_obj.account_info;
2028
2029
  // account_info_ret = await get_account_name({ uid_query: contact_uid });
2029
2030
  if (account_code < 0) {
@@ -2123,7 +2124,7 @@ const set_contact_profile_picture = async function (uid, contact_id, metadata, j
2123
2124
  try {
2124
2125
  // let profile_picture;
2125
2126
 
2126
- const contact_ret = await db_ms.get_app_couch_doc(account_profile_info.app_id, contact_id);
2127
+ const contact_ret = await db_module.get_app_couch_doc(account_profile_info.app_id, contact_id);
2127
2128
 
2128
2129
  if (contact_ret.code < 0) {
2129
2130
  throw new Error(`contact ${contact_id} not found`);
@@ -2163,13 +2164,13 @@ const set_contact_profile_picture = async function (uid, contact_id, metadata, j
2163
2164
  }
2164
2165
  }
2165
2166
  if (file_ret?.data?.file_url) {
2166
- let { code: contact_code, data: contact_obj2 } = await db_ms.get_app_couch_doc(account_profile_info.app_id, contact_id);
2167
+ let { code: contact_code, data: contact_obj2 } = await db_module.get_app_couch_doc(account_profile_info.app_id, contact_id);
2167
2168
  contact_obj = contact_obj2;
2168
2169
  contact_obj.profile_picture_obj = file_ret.data;
2169
2170
  contact_obj.profile_picture = contact_obj.profile_picture_obj.file_url;
2170
2171
  contact_obj.profile_picture_source = file_ret.profile_picture_source;
2171
2172
 
2172
- const contact_save_ret = await db_ms.save_app_couch_doc(account_profile_info.app_id, contact_obj);
2173
+ const contact_save_ret = await db_module.save_app_couch_doc(account_profile_info.app_id, contact_obj);
2173
2174
  // profile_picture = contact_obj.profile_picture;
2174
2175
  }
2175
2176
  }
@@ -2219,7 +2220,7 @@ const set_contact_profile_picture = async function (uid, contact_id, metadata, j
2219
2220
  if (file_ret.data === 'Input file contains unsupported image format') {
2220
2221
  contact_obj.profile_picture = null;
2221
2222
  contact_obj.profile_picture_obj = null;
2222
- const contact_save_ret = await db_ms.save_app_couch_doc(account_profile_info.app_id, contact_obj);
2223
+ const contact_save_ret = await db_module.save_app_couch_doc(account_profile_info.app_id, contact_obj);
2223
2224
  }
2224
2225
  throw new Error(file_ret.data);
2225
2226
  }
@@ -2248,12 +2249,12 @@ const set_contact_profile_picture = async function (uid, contact_id, metadata, j
2248
2249
  // save_xuda_cache(uid, 'profile_avatar', contact_obj.email, file_ret.data, { account_type: contact_obj.account_type, type: 'contact' });
2249
2250
  }
2250
2251
 
2251
- let { code: contact_code, data: contact_obj3 } = await db_ms.get_app_couch_doc(account_profile_info.app_id, contact_id);
2252
+ let { code: contact_code, data: contact_obj3 } = await db_module.get_app_couch_doc(account_profile_info.app_id, contact_id);
2252
2253
  contact_obj = contact_obj3;
2253
2254
  contact_obj.profile_avatar_obj = file_ret.data;
2254
2255
  contact_obj.profile_avatar = contact_obj?.profile_avatar_obj?.file_url;
2255
2256
  contact_obj.avatar_source = file_ret?.data?.avatar_source;
2256
- const contact_save_ret = await db_ms.save_app_couch_doc(account_profile_info.app_id, contact_obj);
2257
+ const contact_save_ret = await db_module.save_app_couch_doc(account_profile_info.app_id, contact_obj);
2257
2258
  }
2258
2259
  await update_contact_profile_picture_status(uid, contact_id, 3);
2259
2260
  } catch (err) {
@@ -2264,11 +2265,11 @@ const set_contact_profile_picture = async function (uid, contact_id, metadata, j
2264
2265
 
2265
2266
  const update_account_profile_picture_status = async function (uid, stat, error) {
2266
2267
  try {
2267
- let doc = await db_ms.get_couch_doc_native('xuda_accounts', uid);
2268
+ let doc = await db_module.get_couch_doc_native('xuda_accounts', uid);
2268
2269
  doc.account_info.profile_avatar_stat = stat;
2269
2270
  doc.account_info.profile_avatar_error = error;
2270
2271
  doc.account_info.profile_avatar_stat_ts = Date.now();
2271
- const save_ret = await db_ms.save_couch_doc_native('xuda_accounts', doc);
2272
+ const save_ret = await db_module.save_couch_doc_native('xuda_accounts', doc);
2272
2273
  } catch (err) {
2273
2274
  console.error(err);
2274
2275
  }
@@ -2277,12 +2278,12 @@ const update_account_profile_picture_status = async function (uid, stat, error)
2277
2278
  const update_contact_profile_picture_status = async function (uid, contact_id, stat, error) {
2278
2279
  const account_profile_info = await get_active_account_profile_info(uid);
2279
2280
  try {
2280
- let doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, contact_id);
2281
+ let doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, contact_id);
2281
2282
  doc.profile_avatar_stat = stat;
2282
2283
  doc.profile_avatar_error = error;
2283
2284
  doc.profile_avatar_stat_ts = Date.now();
2284
2285
 
2285
- const save_ret = await db_ms.save_app_couch_doc_native(account_profile_info.app_id, doc);
2286
+ const save_ret = await db_module.save_app_couch_doc_native(account_profile_info.app_id, doc);
2286
2287
  } catch (err) {
2287
2288
  console.error(err);
2288
2289
  }
@@ -2533,15 +2534,15 @@ export const unfriend_contact = async function (req) {
2533
2534
  if (!contact_doc.team_req_id) {
2534
2535
  throw new Error('no friend relationship found');
2535
2536
  }
2536
- let req_doc = await db_ms.get_couch_doc_native('xuda_team', contact_doc.team_req_id);
2537
+ let req_doc = await db_module.get_couch_doc_native('xuda_team', contact_doc.team_req_id);
2537
2538
  req_doc.team_req_stat = 4;
2538
2539
  req_doc.team_req_stat_desc = 'unfriend by the user';
2539
- await db_ms.get_couch_doc_native('xuda_team', contact_doc.team_req_id);
2540
+ await db_module.get_couch_doc_native('xuda_team', contact_doc.team_req_id);
2540
2541
 
2541
2542
  contact_doc.team_req_id = null;
2542
2543
  const contact_save_ret = await save_contact(uid, contact_doc);
2543
2544
 
2544
- const req_save_ret = await db_ms.save_couch_doc('xuda_team', req_doc);
2545
+ const req_save_ret = await db_module.save_couch_doc('xuda_team', req_doc);
2545
2546
 
2546
2547
  return req_save_ret;
2547
2548
  } catch (err) {
@@ -2625,7 +2626,7 @@ export const not_spam_contact = async function (req, job_id, headers) {
2625
2626
  var contact_doc = await get_contact(uid, contact_id);
2626
2627
 
2627
2628
  if (contact_doc.is_spam) {
2628
- await db_ms.save_couch_doc_native('xuda_accounts', {
2629
+ await db_module.save_couch_doc_native('xuda_accounts', {
2629
2630
  _id: await _common.xuda_get_uuid('spam_whitelist'),
2630
2631
  uid,
2631
2632
  docType: 'spam_whitelist',
@@ -2653,7 +2654,7 @@ export const not_spam_contact = async function (req, job_id, headers) {
2653
2654
  await delete_xuda_cache(contact_doc);
2654
2655
  set_contact_profile_picture(uid, contact_doc._id, {}, job_id, headers, account_profile_info, false);
2655
2656
 
2656
- const emails = await db_ms.find_app_couch_query(account_profile_info.app_id, {
2657
+ const emails = await db_module.find_app_couch_query(account_profile_info.app_id, {
2657
2658
  selector: {
2658
2659
  docType: 'email',
2659
2660
  contact_id,
@@ -2665,14 +2666,14 @@ export const not_spam_contact = async function (req, job_id, headers) {
2665
2666
  for await (let email of emails.docs) {
2666
2667
  const app_id = await get_account_default_project_id(uid);
2667
2668
  if (email.conversation_id) {
2668
- db_ms.delete_app_couch_doc(app_id, email.conversation_id);
2669
+ db_module.delete_app_couch_doc(app_id, email.conversation_id);
2669
2670
  email.conversation_id = null;
2670
2671
  }
2671
2672
  if (email.conversation_item_id) {
2672
- db_ms.delete_app_couch_doc(app_id, email.conversation_item_id);
2673
+ db_module.delete_app_couch_doc(app_id, email.conversation_item_id);
2673
2674
  email.conversation_item_id = null;
2674
2675
  }
2675
- const save_ret = await db_ms.save_app_couch_doc_native(app_id, email);
2676
+ const save_ret = await db_module.save_app_couch_doc_native(app_id, email);
2676
2677
 
2677
2678
  // save attachments
2678
2679
  // summarized_body
@@ -2740,7 +2741,7 @@ export const set_deep_research_contact = async function (req, job_id, headers) {
2740
2741
  const contact_save_ret = await save_contact(uid, contact_doc);
2741
2742
  await delete_xuda_cache(contact_doc);
2742
2743
 
2743
- const conversation_items = await db_ms.find_app_couch_query(account_profile_info.app_id, {
2744
+ const conversation_items = await db_module.find_app_couch_query(account_profile_info.app_id, {
2744
2745
  selector: {
2745
2746
  docType: 'chat_conversation_item',
2746
2747
  reference_id: contact_id,
@@ -2752,7 +2753,7 @@ export const set_deep_research_contact = async function (req, job_id, headers) {
2752
2753
  for await (let conversation_item of conversation_items.docs) {
2753
2754
  try {
2754
2755
  const account_profile_info = await get_active_account_profile_info(conversation_item.uid);
2755
- let conversation_doc = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, conversation_item.conversation_id);
2756
+ let conversation_doc = await db_module.get_app_couch_doc_native(account_profile_info.app_id, conversation_item.conversation_id);
2756
2757
 
2757
2758
  const transcript = await ai_ms.get_transcript(conversation_item.uid, conversation_item.app_id, conversation_item.conversation_id, account_profile_info, conversation_item.file.filename);
2758
2759
  let items;
@@ -2762,7 +2763,7 @@ export const set_deep_research_contact = async function (req, job_id, headers) {
2762
2763
 
2763
2764
  conversation_item.text = transcript?.data || transcript || '';
2764
2765
  conversation_item.process_stat = 'full';
2765
- const conversation_item_save_ret = await db_ms.save_app_couch_doc(account_profile_info.app_id, conversation_item);
2766
+ const conversation_item_save_ret = await db_module.save_app_couch_doc(account_profile_info.app_id, conversation_item);
2766
2767
  }
2767
2768
  } catch (err) {
2768
2769
  throw err;
@@ -2806,10 +2807,10 @@ export const get_pending_share_profile_in = async function (uid, _id) {
2806
2807
  to_opt.selector._id = _id;
2807
2808
  }
2808
2809
 
2809
- const requests_from_res = await db_ms.find_couch_query('xuda_team', from_opt);
2810
+ const requests_from_res = await db_module.find_couch_query('xuda_team', from_opt);
2810
2811
 
2811
2812
  for await (let e of requests_from_res.docs) {
2812
- let doc = await db_ms.get_couch_doc_native('xuda_accounts', e.share_item_id);
2813
+ let doc = await db_module.get_couch_doc_native('xuda_accounts', e.share_item_id);
2813
2814
 
2814
2815
  doc.shared_from_uid = e.team_req_from_uid;
2815
2816
  doc.pending = true;
@@ -2825,7 +2826,7 @@ export const get_account_profile_info = async function (uid, contact_profile_doc
2825
2826
  const app_id = await get_account_default_project_id(uid);
2826
2827
  let doc = contact_profile_doc;
2827
2828
  if (_id) {
2828
- doc = await db_ms.get_couch_doc_native('xuda_accounts', _id);
2829
+ doc = await db_module.get_couch_doc_native('xuda_accounts', _id);
2829
2830
  }
2830
2831
  doc.notifications = 0;
2831
2832
 
@@ -2850,7 +2851,7 @@ export const get_account_profile_info = async function (uid, contact_profile_doc
2850
2851
  let ret = `default-pattern.png`;
2851
2852
 
2852
2853
  if (doc.shared_from_uid) {
2853
- // const account_profile_doc = await db_ms.get_couch_doc_native('xuda_accounts', doc.share_item_id);
2854
+ // const account_profile_doc = await db_module.get_couch_doc_native('xuda_accounts', doc.share_item_id);
2854
2855
  const shared_from_uid_ret = await get_account_name({ uid_query: doc.shared_from_uid });
2855
2856
  ret = shared_from_uid_ret.data.profile_avatar;
2856
2857
  } else if (doc.is_spam) {
@@ -2883,7 +2884,7 @@ export const get_account_profile_info = async function (uid, contact_profile_doc
2883
2884
  const get_online = async () => {
2884
2885
  let online = false;
2885
2886
  let selector = { docType: 'team_request', team_req_stat: 3, share_item_id: doc._id, access_type: 'account_profile', team_req_from_uid: uid };
2886
- let ret = await db_ms.find_couch_query('xuda_team', { selector });
2887
+ let ret = await db_module.find_couch_query('xuda_team', { selector });
2887
2888
  for (const req_obj of ret.docs) {
2888
2889
  const account_info_ret = await get_account_name({ uid_query: req_obj.team_req_to_uid });
2889
2890
  online = account_info_ret.data.online;
@@ -2909,7 +2910,7 @@ export const get_account_profile_info = async function (uid, contact_profile_doc
2909
2910
  const get_members = async () => {
2910
2911
  let members = [];
2911
2912
  let selector = { docType: 'team_request', team_req_stat: 3, share_item_id: doc._id, access_type: 'account_profile', team_req_from_uid: uid };
2912
- let ret = await db_ms.find_couch_query('xuda_team', { selector });
2913
+ let ret = await db_module.find_couch_query('xuda_team', { selector });
2913
2914
  for (const req_obj of ret.docs) {
2914
2915
  const account_info_ret = await get_account_name({ uid_query: req_obj.team_req_to_uid });
2915
2916
  const info = await await get_contact_info(uid, account_info_ret.data);
@@ -2922,7 +2923,7 @@ export const get_account_profile_info = async function (uid, contact_profile_doc
2922
2923
  // const account_profile_info = await get_active_account_profile_info(uid);
2923
2924
 
2924
2925
  let selector = { docType: 'account_profile', stat: 3 };
2925
- let ret = await db_ms.find_app_couch_query(app_id, { selector });
2926
+ let ret = await db_module.find_app_couch_query(app_id, { selector });
2926
2927
 
2927
2928
  return ret.docs;
2928
2929
  };
@@ -2969,7 +2970,7 @@ export const get_account_profiles = async function (req) {
2969
2970
  const { _id, uid, profile_name, limit, skip, bookmark, search, filter_type = 'all', profile_id, active_tab } = req;
2970
2971
  const app_id = await get_account_default_project_id(uid);
2971
2972
  if (_id) {
2972
- let data = await db_ms.get_app_couch_doc(app_id, _id);
2973
+ let data = await db_module.get_app_couch_doc(app_id, _id);
2973
2974
  return data;
2974
2975
  }
2975
2976
 
@@ -3037,7 +3038,7 @@ export const get_account_profiles = async function (req) {
3037
3038
  let profiles = { docs: [], total_docs: 0 };
3038
3039
 
3039
3040
  if (filter_type !== 'pending') {
3040
- profiles = await db_ms.find_app_couch_query(app_id, opt);
3041
+ profiles = await db_module.find_app_couch_query(app_id, opt);
3041
3042
  if (!limit || profile_id) {
3042
3043
  profiles.total_docs = profiles.docs.length;
3043
3044
  } else {
@@ -3046,7 +3047,7 @@ export const get_account_profiles = async function (req) {
3046
3047
  opt.limit = 9999;
3047
3048
  opt.fields = ['_id'];
3048
3049
 
3049
- const counter = await db_ms.find_app_couch_query(app_id, opt);
3050
+ const counter = await db_module.find_app_couch_query(app_id, opt);
3050
3051
  profiles.total_docs = counter.docs.length;
3051
3052
  }
3052
3053
  }
@@ -3098,7 +3099,7 @@ export const archive_account_profile = async function (req) {
3098
3099
 
3099
3100
  await team_ms.validate_share_exist(uid, 'account_profile', profile_id);
3100
3101
 
3101
- var account_profile_doc = await db_ms.get_app_couch_doc_native(app_id, profile_id);
3102
+ var account_profile_doc = await db_module.get_app_couch_doc_native(app_id, profile_id);
3102
3103
 
3103
3104
  if (account_profile_doc.main) {
3104
3105
  throw new Error('reserve');
@@ -3108,7 +3109,7 @@ export const archive_account_profile = async function (req) {
3108
3109
  account_profile_doc.stat_ts = Date.now();
3109
3110
  account_profile_doc.stat_reason = 'archived by the user';
3110
3111
 
3111
- const account_profile_save_ret = await db_ms.save_app_couch_doc(app_id, account_profile_doc);
3112
+ const account_profile_save_ret = await db_module.save_app_couch_doc(app_id, account_profile_doc);
3112
3113
 
3113
3114
  return account_profile_save_ret;
3114
3115
  } catch (err) {
@@ -3123,7 +3124,7 @@ export const delete_account_profile = async function (req, job_id, headers) {
3123
3124
  const { profile_id, uid } = req;
3124
3125
  try {
3125
3126
  const app_id = await get_account_default_project_id(uid);
3126
- var account_profile_doc = await db_ms.get_app_couch_doc_native(app_id, profile_id);
3127
+ var account_profile_doc = await db_module.get_app_couch_doc_native(app_id, profile_id);
3127
3128
  if (account_profile_doc.stat !== 5) {
3128
3129
  throw new Error('A document can only be deleted after it has been archived');
3129
3130
  }
@@ -3131,7 +3132,7 @@ export const delete_account_profile = async function (req, job_id, headers) {
3131
3132
  account_profile_doc.stat_ts = Date.now();
3132
3133
  account_profile_doc.stat_reason = 'deleted by the user';
3133
3134
 
3134
- const account_profile_save_ret = await db_ms.save_app_couch_doc(app_id, account_profile_doc);
3135
+ const account_profile_save_ret = await db_module.save_app_couch_doc(app_id, account_profile_doc);
3135
3136
 
3136
3137
  ai_ms.delete_depended_chats(uid, profile_id);
3137
3138
  return account_profile_save_ret;
@@ -3148,7 +3149,7 @@ export const unarchive_account_profile = async function (req) {
3148
3149
  try {
3149
3150
  const app_id = await get_account_default_project_id(uid);
3150
3151
 
3151
- var account_profile_doc = await db_ms.get_app_couch_doc_native(app_id, profile_id);
3152
+ var account_profile_doc = await db_module.get_app_couch_doc_native(app_id, profile_id);
3152
3153
 
3153
3154
  if (account_profile_doc.stat !== 5) {
3154
3155
  throw new Error('account profile is not archived');
@@ -3158,7 +3159,7 @@ export const unarchive_account_profile = async function (req) {
3158
3159
  account_profile_doc.stat_ts = Date.now();
3159
3160
  account_profile_doc.stat_reason = 'archived by the user';
3160
3161
 
3161
- const account_profile_save_ret = await db_ms.save_app_couch_doc(app_id, account_profile_doc);
3162
+ const account_profile_save_ret = await db_module.save_app_couch_doc(app_id, account_profile_doc);
3162
3163
 
3163
3164
  return account_profile_save_ret;
3164
3165
  }
@@ -3174,7 +3175,7 @@ export const create_account_profile = async function (req, job_id, headers) {
3174
3175
  const { uid, profile_name, profile_signature, email_account_id, profile_picture, profile_avatar, profile_picture_obj, profile_avatar_obj, main, account_type } = req;
3175
3176
  try {
3176
3177
  const app_id = await get_account_default_project_id(uid);
3177
- // const { data: acc_obj } = await db_ms.get_couch_doc('xuda_accounts', uid);
3178
+ // const { data: acc_obj } = await db_module.get_couch_doc('xuda_accounts', uid);
3178
3179
 
3179
3180
  const d = Date.now();
3180
3181
  const doc = {
@@ -3194,7 +3195,7 @@ export const create_account_profile = async function (req, job_id, headers) {
3194
3195
  profile_avatar_obj,
3195
3196
  main,
3196
3197
  };
3197
- const save_ret = await db_ms.save_app_couch_doc(app_id, doc);
3198
+ const save_ret = await db_module.save_app_couch_doc(app_id, doc);
3198
3199
  // acc_obj.active_profile_id = save_ret.data.id;
3199
3200
 
3200
3201
  // return { code: 55, data: save_ret };
@@ -3209,7 +3210,7 @@ export const update_account_profile = async function (req, job_id, headers) {
3209
3210
  const app_id = await get_account_default_project_id(uid);
3210
3211
  const account_profile_properties = ['profile_name', 'profile_signature', 'email_account_id', 'profile_picture', 'profile_avatar', 'profile_picture_obj', 'profile_avatar_obj', 'auto_respond', 'auto_respond_mode', 'auto_respond_agents', 'account_type', 'active_ai_model'];
3211
3212
  try {
3212
- let { data: account_profile_doc } = await db_ms.get_app_couch_doc(app_id, _id);
3213
+ let { data: account_profile_doc } = await db_module.get_app_couch_doc(app_id, _id);
3213
3214
 
3214
3215
  if (account_profile_doc.docType !== 'account_profile') {
3215
3216
  throw new Error('not account profile doc');
@@ -3233,7 +3234,7 @@ export const update_account_profile = async function (req, job_id, headers) {
3233
3234
 
3234
3235
  account_profile_doc.ts = Date.now();
3235
3236
 
3236
- const save_ret = await db_ms.save_app_couch_doc(app_id, account_profile_doc);
3237
+ const save_ret = await db_module.save_app_couch_doc(app_id, account_profile_doc);
3237
3238
 
3238
3239
  return { code: 56, data: save_ret };
3239
3240
  } catch (err) {
@@ -3250,7 +3251,7 @@ export const update_entity_account_profiles = async function (req, job_id, heade
3250
3251
  throw new Error('account_profiles cannot be empty');
3251
3252
  }
3252
3253
 
3253
- let doc = await db_ms.get_app_couch_doc_native(app_id, _id);
3254
+ let doc = await db_module.get_app_couch_doc_native(app_id, _id);
3254
3255
 
3255
3256
  switch (doc.docType) {
3256
3257
  case 'chat_conversation':
@@ -3270,7 +3271,7 @@ export const update_entity_account_profiles = async function (req, job_id, heade
3270
3271
  break;
3271
3272
  }
3272
3273
 
3273
- const save_ret = await db_ms.save_app_couch_doc(app_id, doc);
3274
+ const save_ret = await db_module.save_app_couch_doc(app_id, doc);
3274
3275
 
3275
3276
  return { code: 57, data: save_ret };
3276
3277
  } catch (err) {
@@ -3280,24 +3281,24 @@ export const update_entity_account_profiles = async function (req, job_id, heade
3280
3281
 
3281
3282
  export const get_contact = async function (uid, contact_id) {
3282
3283
  const account_profile_info = await get_active_account_profile_info(uid);
3283
- const contact_ret = await db_ms.get_app_couch_doc_native(account_profile_info.app_id, contact_id);
3284
+ const contact_ret = await db_module.get_app_couch_doc_native(account_profile_info.app_id, contact_id);
3284
3285
  return contact_ret;
3285
3286
  };
3286
3287
 
3287
3288
  export const save_contact = async function (uid, contact_doc) {
3288
3289
  const account_profile_info = await get_active_account_profile_info(uid);
3289
- const contact_ret = await db_ms.save_app_couch_doc(account_profile_info.app_id, contact_doc);
3290
+ const contact_ret = await db_module.save_app_couch_doc(account_profile_info.app_id, contact_doc);
3290
3291
  return contact_ret;
3291
3292
  };
3292
3293
 
3293
3294
  export const find_contact_query = async function (uid, opt) {
3294
3295
  const app_id = await get_account_default_project_id(uid);
3295
- const ret = await db_ms.find_app_couch_query(app_id, opt);
3296
+ const ret = await db_module.find_app_couch_query(app_id, opt);
3296
3297
  return ret;
3297
3298
  };
3298
3299
 
3299
3300
  export const delete_xuda_cache = async function (contact_obj) {
3300
- const ret = await db_ms.find_couch_query('xuda_cache', {
3301
+ const ret = await db_module.find_couch_query('xuda_cache', {
3301
3302
  selector: {
3302
3303
  $or: [
3303
3304
  {
@@ -3312,7 +3313,7 @@ export const delete_xuda_cache = async function (contact_obj) {
3312
3313
 
3313
3314
  for (let doc of ret.docs) {
3314
3315
  doc._deleted = true;
3315
- const save_ret = await db_ms.save_couch_doc('xuda_cache', doc);
3316
+ const save_ret = await db_module.save_couch_doc('xuda_cache', doc);
3316
3317
  }
3317
3318
  };
3318
3319
 
@@ -3332,12 +3333,12 @@ export const save_xuda_cache = async function (uid, docType, key, file, metadata
3332
3333
  metadata,
3333
3334
  hits: 0,
3334
3335
  };
3335
- const save_ret = await db_ms.save_couch_doc('xuda_cache', doc);
3336
+ const save_ret = await db_module.save_couch_doc('xuda_cache', doc);
3336
3337
  return save_ret;
3337
3338
  };
3338
3339
 
3339
3340
  export const get_xuda_cache = async function (uid, docType, key, return_property) {
3340
- let ret = await db_ms.find_couch_query('xuda_cache', {
3341
+ let ret = await db_module.find_couch_query('xuda_cache', {
3341
3342
  selector: { docType, key: key.toLowerCase().trim() },
3342
3343
  limit: 1,
3343
3344
  });
@@ -3348,7 +3349,7 @@ export const get_xuda_cache = async function (uid, docType, key, return_property
3348
3349
 
3349
3350
  return null;
3350
3351
 
3351
- // ret = await db_ms.find_couch_query('xuda_cache', {
3352
+ // ret = await db_module.find_couch_query('xuda_cache', {
3352
3353
  // selector: { docType, name: name.trim() },
3353
3354
  // limit: 1,
3354
3355
  // });
@@ -3359,10 +3360,10 @@ export const get_xuda_cache = async function (uid, docType, key, return_property
3359
3360
  };
3360
3361
 
3361
3362
  export const save_cache_hit = async function (_id) {
3362
- let doc_ret = await db_ms.get_couch_doc('xuda_cache', _id);
3363
+ let doc_ret = await db_module.get_couch_doc('xuda_cache', _id);
3363
3364
  if (doc_ret.code > -1) {
3364
3365
  doc_ret.data.hits++;
3365
- await db_ms.save_couch_doc('xuda_cache', doc_ret.data);
3366
+ await db_module.save_couch_doc('xuda_cache', doc_ret.data);
3366
3367
  }
3367
3368
  };
3368
3369
 
@@ -3392,7 +3393,7 @@ export const record_ai_usage = async function (uid, input_tokens, output_tokens,
3392
3393
  account_profile_info,
3393
3394
  tools,
3394
3395
  };
3395
- const save_ret = await db_ms.save_couch_doc('xuda_usage', usage_doc);
3396
+ const save_ret = await db_module.save_couch_doc('xuda_usage', usage_doc);
3396
3397
  // console.log(save_ret);
3397
3398
  } catch (err) {
3398
3399
  console.error(err);
@@ -3401,7 +3402,7 @@ export const record_ai_usage = async function (uid, input_tokens, output_tokens,
3401
3402
 
3402
3403
  export const record_ai_credit = async function (uid, credits = 0, source, details, credited_uid) {
3403
3404
  try {
3404
- var dup = await db_ms.find_couch_query('xuda_billing', {
3405
+ var dup = await db_module.find_couch_query('xuda_billing', {
3405
3406
  selector: {
3406
3407
  docType: 'ai_credit',
3407
3408
  credited_uid,
@@ -3427,7 +3428,7 @@ export const record_ai_credit = async function (uid, credits = 0, source, detail
3427
3428
  credited_uid,
3428
3429
  details,
3429
3430
  };
3430
- const save_ret = await db_ms.save_couch_doc('xuda_billing', credit_doc);
3431
+ const save_ret = await db_module.save_couch_doc('xuda_billing', credit_doc);
3431
3432
  // console.log(save_ret);
3432
3433
  return save_ret;
3433
3434
  } catch (err) {
@@ -3446,13 +3447,13 @@ export const add_ai_credits_to_active_accounts = async function () {
3446
3447
  });
3447
3448
 
3448
3449
  const _24_hr_ms = 1000 * 60 * 60 * 24;
3449
- const active_accounts = await db_ms.find_couch_query('xuda_accounts', { selector: { stat: 3, docType: 'account', last_free_daily_ai_credit_ts: { $lt: Date.now() - _24_hr_ms }, ai_workspace_plan: 'free_ai_workspace' }, limit: 99999 });
3450
+ const active_accounts = await db_module.find_couch_query('xuda_accounts', { selector: { stat: 3, docType: 'account', last_free_daily_ai_credit_ts: { $lt: Date.now() - _24_hr_ms }, ai_workspace_plan: 'free_ai_workspace' }, limit: 99999 });
3450
3451
  for await (let account_doc of active_accounts.docs) {
3451
3452
  const ret = await record_ai_credit('system', 1, 'daily credit', 'free daily credit ' + date_str, account_doc._id);
3452
3453
  if (ret.code > -1) {
3453
3454
  account_doc.last_free_daily_ai_credit_ts = Date.now();
3454
3455
  account_doc.last_free_daily_ai_credit_id = ret.data.id;
3455
- const save_ret = await db_ms.save_couch_doc('xuda_accounts', account_doc);
3456
+ const save_ret = await db_module.save_couch_doc('xuda_accounts', account_doc);
3456
3457
  }
3457
3458
  }
3458
3459
  };
@@ -3461,17 +3462,17 @@ export const archive_expire_ai_credits = async function () {
3461
3462
  const _24_hr_ms = 1000 * 60 * 60 * 24;
3462
3463
  const _mo_ms = _24_hr_ms * 30;
3463
3464
 
3464
- const active_ai_credits = await db_ms.find_couch_query('xuda_billing', { selector: { stat: 3, docType: 'ai_credit', date_created_ts: { $lt: Date.now() - _mo_ms } }, limit: 99999 });
3465
+ const active_ai_credits = await db_module.find_couch_query('xuda_billing', { selector: { stat: 3, docType: 'ai_credit', date_created_ts: { $lt: Date.now() - _mo_ms } }, limit: 99999 });
3465
3466
  for await (let ai_credit_doc of active_ai_credits.docs) {
3466
3467
  ai_credit_doc.stat = 5;
3467
3468
  ai_credit_doc.stat_ts = Date.now();
3468
3469
  ai_credit_doc.stat_reason = 'expired';
3469
- const save_ret = await db_ms.save_couch_doc('xuda_billing', ai_credit_doc);
3470
+ const save_ret = await db_module.save_couch_doc('xuda_billing', ai_credit_doc);
3470
3471
  }
3471
3472
  };
3472
3473
 
3473
3474
  export const read_accounts_emails = async function () {
3474
- const active_accounts = await db_ms.find_couch_query('xuda_accounts', { selector: { stat: 3, docType: 'account' }, limit: 99999 });
3475
+ const active_accounts = await db_module.find_couch_query('xuda_accounts', { selector: { stat: 3, docType: 'account' }, limit: 99999 });
3475
3476
  for await (let account_doc of active_accounts.docs) {
3476
3477
  if (!account_doc?.account_info?.active_account_profile_id) continue;
3477
3478
  email_ms.refresh_mailboxes({ uid: account_doc._id });
@@ -3479,7 +3480,7 @@ export const read_accounts_emails = async function () {
3479
3480
  };
3480
3481
 
3481
3482
  export const process_accounts_emails = async function () {
3482
- const active_accounts = await db_ms.find_couch_query('xuda_accounts', { selector: { stat: 3, docType: 'account' }, limit: 99999 });
3483
+ const active_accounts = await db_module.find_couch_query('xuda_accounts', { selector: { stat: 3, docType: 'account' }, limit: 99999 });
3483
3484
  for await (let account_doc of active_accounts.docs) {
3484
3485
  if (!account_doc?.account_info?.active_account_profile_id) continue;
3485
3486
  email_ms.process_emails({ uid: account_doc._id });
@@ -3487,7 +3488,7 @@ export const process_accounts_emails = async function () {
3487
3488
  };
3488
3489
 
3489
3490
  const isLikelySpamEmail = async function (email) {
3490
- const spam_whitelists = await db_ms.find_couch_query('xuda_accounts', {
3491
+ const spam_whitelists = await db_module.find_couch_query('xuda_accounts', {
3491
3492
  selector: {
3492
3493
  docType: 'spam_whitelist',
3493
3494
  email,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/account_module",
3
- "version": "1.2.2072",
3
+ "version": "1.2.2074",
4
4
  "description": "Xuda Account Server Module",
5
5
  "main": "index.mjs",
6
6
  "dependencies": {