@xuda.io/account_module 1.2.1255 → 1.2.1257

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 +414 -408
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -1601,323 +1601,6 @@ const get_user_contact = async function (uid, uid_query) {
1601
1601
  return doc;
1602
1602
  };
1603
1603
 
1604
- export const get_contacts = async function (req) {
1605
- const { _id, uid, name, limit, skip, bookmark, search, filter_type = 'all', contact_id } = req;
1606
-
1607
- const account_profile_info = await get_active_account_profile_info(uid);
1608
- if (_id) {
1609
- let data = await db_module.get_couch_doc('xuda_contacts', _id);
1610
- return data;
1611
- }
1612
-
1613
- let { with_requests } = req;
1614
- var opt = {
1615
- selector: {
1616
- docType: 'contact',
1617
- uid: account_profile_info.uid,
1618
- stat: { $lt: 4 },
1619
- },
1620
- limit: limit || 9999,
1621
- skip: skip || 0,
1622
- sort: [{ ts: 'desc' }],
1623
- };
1624
-
1625
- switch (filter_type) {
1626
- case 'archived': {
1627
- opt.selector.stat = 5;
1628
- break;
1629
- }
1630
- case 'shared': {
1631
- opt.selector.shared_from_uid = { $gt: null };
1632
- break;
1633
- }
1634
-
1635
- case 'pinned': {
1636
- opt.selector.pinned = true;
1637
- break;
1638
- }
1639
-
1640
- case 'online': {
1641
- opt.limit = 9999;
1642
- break;
1643
- }
1644
-
1645
- default:
1646
- with_requests = true;
1647
- break;
1648
- }
1649
-
1650
- if (_id) {
1651
- opt.selector._id = _id;
1652
- }
1653
-
1654
- if (contact_id) {
1655
- opt.selector._id = contact_id;
1656
- }
1657
-
1658
- if (typeof name !== 'undefined') {
1659
- opt.selector.name = { $regex: `(?i)${name}` };
1660
- }
1661
-
1662
- if (typeof search !== 'undefined') {
1663
- opt.selector['$or'] = [
1664
- {
1665
- name: {
1666
- $regex: `(?i)${search}`,
1667
- },
1668
- },
1669
- {
1670
- email: {
1671
- $regex: `(?i)${search}`,
1672
- },
1673
- },
1674
- ];
1675
- }
1676
-
1677
- if (bookmark && bookmark !== 'nil') {
1678
- opt.bookmark = bookmark;
1679
- }
1680
-
1681
- let contacts = { docs: [], total_docs: 0 };
1682
-
1683
- if (filter_type !== 'pending') {
1684
- contacts = await db_module.find_couch_query('xuda_contacts', opt);
1685
- if (!limit || contact_id) {
1686
- contacts.total_docs = contacts.docs.length;
1687
- } else {
1688
- delete opt.sort;
1689
- delete opt.skip;
1690
- opt.limit = 9999;
1691
- opt.fields = ['_id'];
1692
-
1693
- const counter = await db_module.find_couch_query('xuda_contacts', opt);
1694
- contacts.total_docs = counter.docs.length;
1695
- }
1696
- }
1697
-
1698
- let requests_to = { docs: [], total_docs: 0 };
1699
- let requests_from = { docs: [], total_docs: 0 };
1700
- let shared_from = { docs: [], total_docs: 0 };
1701
-
1702
- let contact_docs = { docs: [], total_docs: contacts.total_docs };
1703
- for await (let doc of contacts.docs) {
1704
- const ret_to = requests_to.docs.find((e) => {
1705
- return e.contact_uid === doc.contact_uid;
1706
- });
1707
- if (ret_to) continue;
1708
-
1709
- const ret_from = requests_from.docs.find((e) => {
1710
- return e.contact_uid === doc.contact_uid;
1711
- });
1712
- if (ret_from) continue;
1713
-
1714
- doc = await get_contact_info(uid, doc);
1715
-
1716
- if (filter_type === 'online') {
1717
- if (!doc.online) continue;
1718
- }
1719
-
1720
- contact_docs.docs.push(doc);
1721
- }
1722
-
1723
- if (!contact_id && with_requests) {
1724
- if (filter_type === 'pending') {
1725
- ////// TO - OUT
1726
- requests_to.docs = await get_pending_contact_out(uid);
1727
- requests_to.total_docs = requests_to.docs.length;
1728
-
1729
- ////// FROM - IN
1730
- requests_from.docs = await get_pending_contact_in(uid);
1731
- requests_from.total_docs = requests_from.docs.length;
1732
- shared_from.docs = await get_pending_share_contact_in(uid);
1733
- shared_from.total_docs = shared_from.docs.length;
1734
- }
1735
- }
1736
-
1737
- return {
1738
- code: 1,
1739
- data: {
1740
- docs: [...shared_from.docs, ...requests_from.docs, ...requests_to.docs, ...contact_docs.docs],
1741
- total_docs: shared_from.total_docs + requests_from.total_docs + requests_to.total_docs + contact_docs.total_docs,
1742
- },
1743
- };
1744
- };
1745
-
1746
- export const archive_contact = async function (req) {
1747
- const { contact_id } = req;
1748
- try {
1749
- const team_module = await import(`${module_path}/team_module/index.mjs`);
1750
- await team_module.validate_share_exist(uid, 'contact', contact_id);
1751
-
1752
- var contact_doc = await db_module.get_couch_doc_native('xuda_contacts', contact_id);
1753
-
1754
- if (contact_doc.uid !== uid && contact_doc?.account_profile_info?.uid !== uid) {
1755
- throw new Error('Operation not allowed');
1756
- }
1757
-
1758
- contact_doc.stat = 5;
1759
- contact_doc.stat_ts = Date.now();
1760
- contact_doc.stat_reason = 'archived by the user';
1761
-
1762
- const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
1763
-
1764
- return contact_save_ret;
1765
- } catch (err) {
1766
- return {
1767
- code: -22,
1768
- data: err.message,
1769
- };
1770
- }
1771
- };
1772
-
1773
- export const delete_contact = async function (req, job_id, headers) {
1774
- const { contact_id } = req;
1775
- try {
1776
- var contact_doc = await db_module.get_couch_doc_native('xuda_contacts', contact_id);
1777
- if (contact_doc.stat !== 5) {
1778
- throw new Error('A document can only be deleted after it has been archived');
1779
- }
1780
- contact_doc.stat = 4;
1781
- contact_doc.stat_ts = Date.now();
1782
- contact_doc.stat_reason = 'deleted by the user';
1783
-
1784
- const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
1785
- const ai_module = await import(`${module_path}/ai_module/index.mjs`);
1786
- ai_module.delete_depended_chats(uid, contact_id);
1787
- return contact_save_ret;
1788
- } catch (err) {
1789
- return {
1790
- code: -22,
1791
- data: err.message,
1792
- };
1793
- }
1794
- };
1795
-
1796
- export const unarchive_contact = async function (req) {
1797
- const { contact_id } = req;
1798
- try {
1799
- var contact_doc = await db_module.get_couch_doc_native('xuda_contacts', contact_id);
1800
-
1801
- if (contact_doc.stat !== 5) {
1802
- throw new Error('contact is not archived');
1803
- }
1804
-
1805
- contact_doc.stat = 3;
1806
- contact_doc.stat_ts = Date.now();
1807
- contact_doc.stat_reason = 'archived by the user';
1808
-
1809
- const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
1810
-
1811
- return contact_save_ret;
1812
- } catch (err) {
1813
- return {
1814
- code: -22,
1815
- data: err.message,
1816
- };
1817
- }
1818
- };
1819
-
1820
- export const unfriend_contact = async function (req) {
1821
- const { contact_id } = req;
1822
- try {
1823
- const contact_ret = await db_module.get_couch_doc('xuda_contacts', contact_id);
1824
- if (contact_ret.code < 0) {
1825
- throw new Error(contact_ret.data);
1826
- }
1827
- var contact_doc = contact_ret.data;
1828
-
1829
- if (!contact_doc.team_req_id) {
1830
- throw new Error('no friend relationship found');
1831
- }
1832
- let req_doc = await db_module.get_couch_doc_native('xuda_team', contact_doc.team_req_id);
1833
- req_doc.team_req_stat = 4;
1834
- req_doc.team_req_stat_desc = 'unfriend by the user';
1835
- await db_module.get_couch_doc_native('xuda_team', contact_doc.team_req_id);
1836
-
1837
- contact_doc.team_req_id = null;
1838
- const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
1839
-
1840
- const req_save_ret = await db_module.save_couch_doc('xuda_team', req_doc);
1841
-
1842
- return req_save_ret;
1843
- } catch (err) {
1844
- return {
1845
- code: -21,
1846
- data: err.message,
1847
- };
1848
- }
1849
- };
1850
-
1851
- export const pin_contact = async function (req) {
1852
- const { uid, contact_id } = req;
1853
- try {
1854
- const contact_ret = await db_module.get_couch_doc('xuda_contacts', contact_id);
1855
- if (contact_ret.code < 0) {
1856
- return contact_ret;
1857
- }
1858
- var contact_doc = contact_ret.data;
1859
- contact_doc.pinned = true;
1860
-
1861
- const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
1862
-
1863
- global[`_account_module_ch`].sendToQueue(
1864
- 'ws_dashboard_module',
1865
- Buffer.from(
1866
- JSON.stringify({
1867
- method: 'emit_message_to_dashboard',
1868
- data: {
1869
- service: 'contact_pinned',
1870
- to: [uid],
1871
- data: await get_contact_info(uid, null, contact_id),
1872
- },
1873
- }),
1874
- ),
1875
- );
1876
-
1877
- return contact_save_ret;
1878
- } catch (err) {
1879
- return {
1880
- code: -24,
1881
- data: err.message,
1882
- };
1883
- }
1884
- };
1885
-
1886
- export const unpin_contact = async function (req) {
1887
- const { contact_id, uid } = req;
1888
- try {
1889
- const contact_ret = await db_module.get_couch_doc('xuda_contacts', contact_id);
1890
- if (contact_ret.code < 0) {
1891
- return contact_ret;
1892
- }
1893
- var contact_doc = contact_ret.data;
1894
- contact_doc.pinned = false;
1895
-
1896
- const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
1897
-
1898
- global[`_account_module_ch`].sendToQueue(
1899
- 'ws_dashboard_module',
1900
- Buffer.from(
1901
- JSON.stringify({
1902
- method: 'emit_message_to_dashboard',
1903
- data: {
1904
- service: 'contact_unpinned',
1905
- to: [uid],
1906
- data: await get_contact_info(uid, null, contact_id),
1907
- },
1908
- }),
1909
- ),
1910
- );
1911
-
1912
- return contact_save_ret;
1913
- } catch (err) {
1914
- return {
1915
- code: -24,
1916
- data: err.message,
1917
- };
1918
- }
1919
- };
1920
-
1921
1604
  // invite user
1922
1605
 
1923
1606
  // const contact_ret = await db_module.get_couch_view_raw(
@@ -2752,6 +2435,10 @@ export const get_account_profile_info = async function (uid, contact_profile_doc
2752
2435
  doc.max_credits = usage_ret.data.credits.total / user_account_profiles.length;
2753
2436
  doc.used_credits = usage_ret.data.usage[doc._id] || 0;
2754
2437
 
2438
+ if (doc.shared_from_uid) {
2439
+ doc.max_credits = doc.max_credits / doc.members.length;
2440
+ }
2441
+
2755
2442
  return doc;
2756
2443
  };
2757
2444
 
@@ -2822,36 +2509,301 @@ export const get_account_profiles = async function (req) {
2822
2509
  let profiles = { docs: [], total_docs: 0 };
2823
2510
 
2824
2511
  if (filter_type !== 'pending') {
2825
- profiles = await db_module.find_couch_query('xuda_accounts', opt);
2826
- if (!limit || profile_id) {
2827
- profiles.total_docs = profiles.docs.length;
2512
+ profiles = await db_module.find_couch_query('xuda_accounts', opt);
2513
+ if (!limit || profile_id) {
2514
+ profiles.total_docs = profiles.docs.length;
2515
+ } else {
2516
+ delete opt.sort;
2517
+ delete opt.skip;
2518
+ opt.limit = 9999;
2519
+ opt.fields = ['_id'];
2520
+
2521
+ const counter = await db_module.find_couch_query('xuda_accounts', opt);
2522
+ profiles.total_docs = counter.docs.length;
2523
+ }
2524
+ }
2525
+
2526
+ let shared_from = { docs: [], total_docs: 0 };
2527
+
2528
+ let profile_docs = { docs: [], total_docs: profiles.total_docs };
2529
+ for await (let doc of profiles.docs) {
2530
+ doc = await get_account_profile_info(uid, doc);
2531
+ if (doc.main) {
2532
+ profile_docs.docs.unshift(doc);
2533
+ } else {
2534
+ profile_docs.docs.push(doc);
2535
+ }
2536
+ }
2537
+
2538
+ if (!profile_id) {
2539
+ if (filter_type === 'pending') {
2540
+ ////// FROM - IN
2541
+ shared_from.docs = await get_pending_share_profile_in(uid);
2542
+ shared_from.total_docs = shared_from.docs.length;
2543
+ }
2544
+ }
2545
+
2546
+ return {
2547
+ code: 1,
2548
+ data: {
2549
+ docs: [...shared_from.docs, ...profile_docs.docs],
2550
+ total_docs: shared_from.total_docs + profile_docs.total_docs,
2551
+ },
2552
+ };
2553
+ };
2554
+
2555
+ export const archive_account_profile = async function (req) {
2556
+ const { uid, profile_id } = req;
2557
+ try {
2558
+ const team_module = await import(`${module_path}/team_module/index.mjs`);
2559
+ await team_module.validate_share_exist(uid, 'account_profile', profile_id);
2560
+
2561
+ var account_profile_doc = await db_module.get_couch_doc_native('xuda_accounts', profile_id);
2562
+
2563
+ if (account_profile_doc.main) {
2564
+ throw new Error('reserve');
2565
+ }
2566
+ // if (!account_profile_doc.team_req_id) {
2567
+ account_profile_doc.stat = 5;
2568
+ account_profile_doc.stat_ts = Date.now();
2569
+ account_profile_doc.stat_reason = 'archived by the user';
2570
+
2571
+ const account_profile_save_ret = await db_module.save_couch_doc('xuda_accounts', account_profile_doc);
2572
+
2573
+ return account_profile_save_ret;
2574
+ // } else {
2575
+ // return await team_module.change_account_profile_dependencies_status(account_profile_doc.team_req_id, 5);
2576
+ // }
2577
+ } catch (err) {
2578
+ return {
2579
+ code: -22,
2580
+ data: err.message,
2581
+ };
2582
+ }
2583
+ };
2584
+
2585
+ export const delete_account_profile = async function (req, job_id, headers) {
2586
+ const { profile_id } = req;
2587
+ try {
2588
+ var account_profile_doc = await db_module.get_couch_doc_native('xuda_accounts', profile_id);
2589
+ if (account_profile_doc.stat !== 5) {
2590
+ throw new Error('A document can only be deleted after it has been archived');
2591
+ }
2592
+ account_profile_doc.stat = 4;
2593
+ account_profile_doc.stat_ts = Date.now();
2594
+ account_profile_doc.stat_reason = 'deleted by the user';
2595
+
2596
+ const account_profile_save_ret = await db_module.save_couch_doc('xuda_accounts', account_profile_doc);
2597
+ const ai_module = await import(`${module_path}/ai_module/index.mjs`);
2598
+ ai_module.delete_depended_chats(uid, profile_id);
2599
+ return account_profile_save_ret;
2600
+ } catch (err) {
2601
+ return {
2602
+ code: -22,
2603
+ data: err.message,
2604
+ };
2605
+ }
2606
+ };
2607
+
2608
+ export const unarchive_account_profile = async function (req) {
2609
+ const { profile_id } = req;
2610
+ try {
2611
+ const team_module = await import(`${module_path}/team_module/index.mjs`);
2612
+ var account_profile_doc = await db_module.get_couch_doc_native('xuda_accounts', profile_id);
2613
+
2614
+ if (account_profile_doc.stat !== 5) {
2615
+ throw new Error('account profile is not archived');
2616
+ }
2617
+ if (!account_profile_doc.team_req_id) {
2618
+ account_profile_doc.stat = 3;
2619
+ account_profile_doc.stat_ts = Date.now();
2620
+ account_profile_doc.stat_reason = 'archived by the user';
2621
+
2622
+ const account_profile_save_ret = await db_module.save_couch_doc('xuda_accounts', account_profile_doc);
2623
+
2624
+ return account_profile_save_ret;
2625
+ } else {
2626
+ return await team_module.change_account_profile_dependencies_status(account_profile_doc.team_req_id, 3);
2627
+ }
2628
+ } catch (err) {
2629
+ return {
2630
+ code: -22,
2631
+ data: err.message,
2632
+ };
2633
+ }
2634
+ };
2635
+
2636
+ // export const unfriend_account_profile = async function (req) {
2637
+ // const { profile_id } = req;
2638
+ // try {
2639
+ //
2640
+ // } catch (err) {
2641
+ // return {
2642
+ // code: -21,
2643
+ // data: err.message,
2644
+ // };
2645
+ // }
2646
+ // };
2647
+
2648
+ // export const validate_account_asset_change = async function (req) {
2649
+ // const { profile_id } = req;
2650
+ // try {
2651
+ // const team_module = await import(`${module_path}/team_module/index.mjs`);
2652
+ // var account_profile_doc = await db_module.get_couch_doc_native('xuda_accounts', profile_id);
2653
+
2654
+ // if (account_profile_doc.stat !== 5) {
2655
+ // throw new Error('account profile is not archived');
2656
+ // }
2657
+ // if (!account_profile_doc.team_req_id) {
2658
+ // account_profile_doc.stat = 3;
2659
+ // account_profile_doc.stat_ts = Date.now();
2660
+ // account_profile_doc.stat_reason = 'archived by the user';
2661
+
2662
+ // const account_profile_save_ret = await db_module.save_couch_doc('xuda_accounts', account_profile_doc);
2663
+
2664
+ // return account_profile_save_ret;
2665
+ // } else {
2666
+ // return await team_module.change_account_profile_dependencies_status(account_profile_doc.team_req_id, 3);
2667
+ // }
2668
+ // } catch (err) {
2669
+ // return {
2670
+ // code: -22,
2671
+ // data: err.message,
2672
+ // };
2673
+ // }
2674
+ // };
2675
+
2676
+ //////// CONTACTS //////////////
2677
+
2678
+ export const get_contacts = async function (req) {
2679
+ const { _id, uid, name, limit, skip, bookmark, search, filter_type = 'all', contact_id } = req;
2680
+
2681
+ const account_profile_info = await get_active_account_profile_info(uid);
2682
+ if (_id) {
2683
+ let data = await db_module.get_couch_doc('xuda_contacts', _id);
2684
+ return data;
2685
+ }
2686
+
2687
+ let { with_requests } = req;
2688
+ var opt = {
2689
+ selector: {
2690
+ docType: 'contact',
2691
+ uid: account_profile_info.uid,
2692
+ stat: { $lt: 4 },
2693
+ },
2694
+ limit: limit || 9999,
2695
+ skip: skip || 0,
2696
+ sort: [{ ts: 'desc' }],
2697
+ };
2698
+
2699
+ switch (filter_type) {
2700
+ case 'archived': {
2701
+ opt.selector.stat = 5;
2702
+ break;
2703
+ }
2704
+ case 'shared': {
2705
+ opt.selector.shared_from_uid = { $gt: null };
2706
+ break;
2707
+ }
2708
+
2709
+ case 'pinned': {
2710
+ opt.selector.pinned = true;
2711
+ break;
2712
+ }
2713
+
2714
+ case 'online': {
2715
+ opt.limit = 9999;
2716
+ break;
2717
+ }
2718
+
2719
+ default:
2720
+ with_requests = true;
2721
+ break;
2722
+ }
2723
+
2724
+ if (_id) {
2725
+ opt.selector._id = _id;
2726
+ }
2727
+
2728
+ if (contact_id) {
2729
+ opt.selector._id = contact_id;
2730
+ }
2731
+
2732
+ if (typeof name !== 'undefined') {
2733
+ opt.selector.name = { $regex: `(?i)${name}` };
2734
+ }
2735
+
2736
+ if (typeof search !== 'undefined') {
2737
+ opt.selector['$or'] = [
2738
+ {
2739
+ name: {
2740
+ $regex: `(?i)${search}`,
2741
+ },
2742
+ },
2743
+ {
2744
+ email: {
2745
+ $regex: `(?i)${search}`,
2746
+ },
2747
+ },
2748
+ ];
2749
+ }
2750
+
2751
+ if (bookmark && bookmark !== 'nil') {
2752
+ opt.bookmark = bookmark;
2753
+ }
2754
+
2755
+ let contacts = { docs: [], total_docs: 0 };
2756
+
2757
+ if (filter_type !== 'pending') {
2758
+ contacts = await db_module.find_couch_query('xuda_contacts', opt);
2759
+ if (!limit || contact_id) {
2760
+ contacts.total_docs = contacts.docs.length;
2828
2761
  } else {
2829
2762
  delete opt.sort;
2830
2763
  delete opt.skip;
2831
2764
  opt.limit = 9999;
2832
2765
  opt.fields = ['_id'];
2833
2766
 
2834
- const counter = await db_module.find_couch_query('xuda_accounts', opt);
2835
- profiles.total_docs = counter.docs.length;
2767
+ const counter = await db_module.find_couch_query('xuda_contacts', opt);
2768
+ contacts.total_docs = counter.docs.length;
2836
2769
  }
2837
2770
  }
2838
2771
 
2772
+ let requests_to = { docs: [], total_docs: 0 };
2773
+ let requests_from = { docs: [], total_docs: 0 };
2839
2774
  let shared_from = { docs: [], total_docs: 0 };
2840
2775
 
2841
- let profile_docs = { docs: [], total_docs: profiles.total_docs };
2842
- for await (let doc of profiles.docs) {
2843
- doc = await get_account_profile_info(uid, doc);
2844
- if (doc.main) {
2845
- profile_docs.docs.unshift(doc);
2846
- } else {
2847
- profile_docs.docs.push(doc);
2776
+ let contact_docs = { docs: [], total_docs: contacts.total_docs };
2777
+ for await (let doc of contacts.docs) {
2778
+ const ret_to = requests_to.docs.find((e) => {
2779
+ return e.contact_uid === doc.contact_uid;
2780
+ });
2781
+ if (ret_to) continue;
2782
+
2783
+ const ret_from = requests_from.docs.find((e) => {
2784
+ return e.contact_uid === doc.contact_uid;
2785
+ });
2786
+ if (ret_from) continue;
2787
+
2788
+ doc = await get_contact_info(uid, doc);
2789
+
2790
+ if (filter_type === 'online') {
2791
+ if (!doc.online) continue;
2848
2792
  }
2793
+
2794
+ contact_docs.docs.push(doc);
2849
2795
  }
2850
2796
 
2851
- if (!profile_id) {
2797
+ if (!contact_id && with_requests) {
2852
2798
  if (filter_type === 'pending') {
2799
+ ////// TO - OUT
2800
+ requests_to.docs = await get_pending_contact_out(uid);
2801
+ requests_to.total_docs = requests_to.docs.length;
2802
+
2853
2803
  ////// FROM - IN
2854
- shared_from.docs = await get_pending_share_profile_in(uid);
2804
+ requests_from.docs = await get_pending_contact_in(uid);
2805
+ requests_from.total_docs = requests_from.docs.length;
2806
+ shared_from.docs = await get_pending_share_contact_in(uid);
2855
2807
  shared_from.total_docs = shared_from.docs.length;
2856
2808
  }
2857
2809
  }
@@ -2859,34 +2811,31 @@ export const get_account_profiles = async function (req) {
2859
2811
  return {
2860
2812
  code: 1,
2861
2813
  data: {
2862
- docs: [...shared_from.docs, ...profile_docs.docs],
2863
- total_docs: shared_from.total_docs + profile_docs.total_docs,
2814
+ docs: [...shared_from.docs, ...requests_from.docs, ...requests_to.docs, ...contact_docs.docs],
2815
+ total_docs: shared_from.total_docs + requests_from.total_docs + requests_to.total_docs + contact_docs.total_docs,
2864
2816
  },
2865
2817
  };
2866
2818
  };
2867
2819
 
2868
- export const archive_account_profile = async function (req) {
2869
- const { uid, profile_id } = req;
2820
+ export const archive_contact = async function (req) {
2821
+ const { contact_id } = req;
2870
2822
  try {
2871
2823
  const team_module = await import(`${module_path}/team_module/index.mjs`);
2872
- await team_module.validate_share_exist(uid, 'account_profile', profile_id);
2824
+ await team_module.validate_share_exist(uid, 'contact', contact_id);
2873
2825
 
2874
- var account_profile_doc = await db_module.get_couch_doc_native('xuda_accounts', profile_id);
2826
+ var contact_doc = await db_module.get_couch_doc_native('xuda_contacts', contact_id);
2875
2827
 
2876
- if (account_profile_doc.main) {
2877
- throw new Error('reserve');
2828
+ if (contact_doc.uid !== uid && contact_doc?.account_profile_info?.uid !== uid) {
2829
+ throw new Error('Operation not allowed');
2878
2830
  }
2879
- // if (!account_profile_doc.team_req_id) {
2880
- account_profile_doc.stat = 5;
2881
- account_profile_doc.stat_ts = Date.now();
2882
- account_profile_doc.stat_reason = 'archived by the user';
2883
2831
 
2884
- const account_profile_save_ret = await db_module.save_couch_doc('xuda_accounts', account_profile_doc);
2832
+ contact_doc.stat = 5;
2833
+ contact_doc.stat_ts = Date.now();
2834
+ contact_doc.stat_reason = 'archived by the user';
2885
2835
 
2886
- return account_profile_save_ret;
2887
- // } else {
2888
- // return await team_module.change_account_profile_dependencies_status(account_profile_doc.team_req_id, 5);
2889
- // }
2836
+ const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
2837
+
2838
+ return contact_save_ret;
2890
2839
  } catch (err) {
2891
2840
  return {
2892
2841
  code: -22,
@@ -2895,21 +2844,21 @@ export const archive_account_profile = async function (req) {
2895
2844
  }
2896
2845
  };
2897
2846
 
2898
- export const delete_account_profile = async function (req, job_id, headers) {
2899
- const { profile_id } = req;
2847
+ export const delete_contact = async function (req, job_id, headers) {
2848
+ const { contact_id } = req;
2900
2849
  try {
2901
- var account_profile_doc = await db_module.get_couch_doc_native('xuda_accounts', profile_id);
2902
- if (account_profile_doc.stat !== 5) {
2850
+ var contact_doc = await db_module.get_couch_doc_native('xuda_contacts', contact_id);
2851
+ if (contact_doc.stat !== 5) {
2903
2852
  throw new Error('A document can only be deleted after it has been archived');
2904
2853
  }
2905
- account_profile_doc.stat = 4;
2906
- account_profile_doc.stat_ts = Date.now();
2907
- account_profile_doc.stat_reason = 'deleted by the user';
2854
+ contact_doc.stat = 4;
2855
+ contact_doc.stat_ts = Date.now();
2856
+ contact_doc.stat_reason = 'deleted by the user';
2908
2857
 
2909
- const account_profile_save_ret = await db_module.save_couch_doc('xuda_accounts', account_profile_doc);
2858
+ const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
2910
2859
  const ai_module = await import(`${module_path}/ai_module/index.mjs`);
2911
- ai_module.delete_depended_chats(uid, profile_id);
2912
- return account_profile_save_ret;
2860
+ ai_module.delete_depended_chats(uid, contact_id);
2861
+ return contact_save_ret;
2913
2862
  } catch (err) {
2914
2863
  return {
2915
2864
  code: -22,
@@ -2918,26 +2867,22 @@ export const delete_account_profile = async function (req, job_id, headers) {
2918
2867
  }
2919
2868
  };
2920
2869
 
2921
- export const unarchive_account_profile = async function (req) {
2922
- const { profile_id } = req;
2870
+ export const unarchive_contact = async function (req) {
2871
+ const { contact_id } = req;
2923
2872
  try {
2924
- const team_module = await import(`${module_path}/team_module/index.mjs`);
2925
- var account_profile_doc = await db_module.get_couch_doc_native('xuda_accounts', profile_id);
2873
+ var contact_doc = await db_module.get_couch_doc_native('xuda_contacts', contact_id);
2926
2874
 
2927
- if (account_profile_doc.stat !== 5) {
2928
- throw new Error('account profile is not archived');
2875
+ if (contact_doc.stat !== 5) {
2876
+ throw new Error('contact is not archived');
2929
2877
  }
2930
- if (!account_profile_doc.team_req_id) {
2931
- account_profile_doc.stat = 3;
2932
- account_profile_doc.stat_ts = Date.now();
2933
- account_profile_doc.stat_reason = 'archived by the user';
2934
2878
 
2935
- const account_profile_save_ret = await db_module.save_couch_doc('xuda_accounts', account_profile_doc);
2879
+ contact_doc.stat = 3;
2880
+ contact_doc.stat_ts = Date.now();
2881
+ contact_doc.stat_reason = 'archived by the user';
2936
2882
 
2937
- return account_profile_save_ret;
2938
- } else {
2939
- return await team_module.change_account_profile_dependencies_status(account_profile_doc.team_req_id, 3);
2940
- }
2883
+ const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
2884
+
2885
+ return contact_save_ret;
2941
2886
  } catch (err) {
2942
2887
  return {
2943
2888
  code: -22,
@@ -2946,42 +2891,103 @@ export const unarchive_account_profile = async function (req) {
2946
2891
  }
2947
2892
  };
2948
2893
 
2949
- // export const unfriend_account_profile = async function (req) {
2950
- // const { profile_id } = req;
2951
- // try {
2952
- //
2953
- // } catch (err) {
2954
- // return {
2955
- // code: -21,
2956
- // data: err.message,
2957
- // };
2958
- // }
2959
- // };
2894
+ export const unfriend_contact = async function (req) {
2895
+ const { contact_id } = req;
2896
+ try {
2897
+ const contact_ret = await db_module.get_couch_doc('xuda_contacts', contact_id);
2898
+ if (contact_ret.code < 0) {
2899
+ throw new Error(contact_ret.data);
2900
+ }
2901
+ var contact_doc = contact_ret.data;
2960
2902
 
2961
- // export const validate_account_asset_change = async function (req) {
2962
- // const { profile_id } = req;
2963
- // try {
2964
- // const team_module = await import(`${module_path}/team_module/index.mjs`);
2965
- // var account_profile_doc = await db_module.get_couch_doc_native('xuda_accounts', profile_id);
2903
+ if (!contact_doc.team_req_id) {
2904
+ throw new Error('no friend relationship found');
2905
+ }
2906
+ let req_doc = await db_module.get_couch_doc_native('xuda_team', contact_doc.team_req_id);
2907
+ req_doc.team_req_stat = 4;
2908
+ req_doc.team_req_stat_desc = 'unfriend by the user';
2909
+ await db_module.get_couch_doc_native('xuda_team', contact_doc.team_req_id);
2966
2910
 
2967
- // if (account_profile_doc.stat !== 5) {
2968
- // throw new Error('account profile is not archived');
2969
- // }
2970
- // if (!account_profile_doc.team_req_id) {
2971
- // account_profile_doc.stat = 3;
2972
- // account_profile_doc.stat_ts = Date.now();
2973
- // account_profile_doc.stat_reason = 'archived by the user';
2911
+ contact_doc.team_req_id = null;
2912
+ const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
2974
2913
 
2975
- // const account_profile_save_ret = await db_module.save_couch_doc('xuda_accounts', account_profile_doc);
2914
+ const req_save_ret = await db_module.save_couch_doc('xuda_team', req_doc);
2976
2915
 
2977
- // return account_profile_save_ret;
2978
- // } else {
2979
- // return await team_module.change_account_profile_dependencies_status(account_profile_doc.team_req_id, 3);
2980
- // }
2981
- // } catch (err) {
2982
- // return {
2983
- // code: -22,
2984
- // data: err.message,
2985
- // };
2986
- // }
2987
- // };
2916
+ return req_save_ret;
2917
+ } catch (err) {
2918
+ return {
2919
+ code: -21,
2920
+ data: err.message,
2921
+ };
2922
+ }
2923
+ };
2924
+
2925
+ export const pin_contact = async function (req) {
2926
+ const { uid, contact_id } = req;
2927
+ try {
2928
+ const contact_ret = await db_module.get_couch_doc('xuda_contacts', contact_id);
2929
+ if (contact_ret.code < 0) {
2930
+ return contact_ret;
2931
+ }
2932
+ var contact_doc = contact_ret.data;
2933
+ contact_doc.pinned = true;
2934
+
2935
+ const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
2936
+
2937
+ global[`_account_module_ch`].sendToQueue(
2938
+ 'ws_dashboard_module',
2939
+ Buffer.from(
2940
+ JSON.stringify({
2941
+ method: 'emit_message_to_dashboard',
2942
+ data: {
2943
+ service: 'contact_pinned',
2944
+ to: [uid],
2945
+ data: await get_contact_info(uid, null, contact_id),
2946
+ },
2947
+ }),
2948
+ ),
2949
+ );
2950
+
2951
+ return contact_save_ret;
2952
+ } catch (err) {
2953
+ return {
2954
+ code: -24,
2955
+ data: err.message,
2956
+ };
2957
+ }
2958
+ };
2959
+
2960
+ export const unpin_contact = async function (req) {
2961
+ const { contact_id, uid } = req;
2962
+ try {
2963
+ const contact_ret = await db_module.get_couch_doc('xuda_contacts', contact_id);
2964
+ if (contact_ret.code < 0) {
2965
+ return contact_ret;
2966
+ }
2967
+ var contact_doc = contact_ret.data;
2968
+ contact_doc.pinned = false;
2969
+
2970
+ const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
2971
+
2972
+ global[`_account_module_ch`].sendToQueue(
2973
+ 'ws_dashboard_module',
2974
+ Buffer.from(
2975
+ JSON.stringify({
2976
+ method: 'emit_message_to_dashboard',
2977
+ data: {
2978
+ service: 'contact_unpinned',
2979
+ to: [uid],
2980
+ data: await get_contact_info(uid, null, contact_id),
2981
+ },
2982
+ }),
2983
+ ),
2984
+ );
2985
+
2986
+ return contact_save_ret;
2987
+ } catch (err) {
2988
+ return {
2989
+ code: -24,
2990
+ data: err.message,
2991
+ };
2992
+ }
2993
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/account_module",
3
- "version": "1.2.1255",
3
+ "version": "1.2.1257",
4
4
  "description": "Xuda Account Server Module",
5
5
  "main": "index.mjs",
6
6
  "dependencies": {