@xuda.io/account_module 1.2.1257 → 1.2.1259

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 +579 -646
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -1056,23 +1056,7 @@ export const read_emails = async function (req, job_id, headers) {
1056
1056
 
1057
1057
  for await (let [email, data] of Object.entries(is_sent ? recipient : senders)) {
1058
1058
  const { name, subject, is_sent } = data;
1059
- // const contact_ret = await db_module.get_couch_view_raw(
1060
- // "xuda_contacts",
1061
- // "contacts_by_email_address",
1062
- // { key: [uid, email] }
1063
- // );
1064
-
1065
- // if (!contact_ret.rows.length) {
1066
- // await db_module.save_couch_doc("xuda_contacts", {
1067
- // _id: await _common.xuda_get_uuid("contact"),
1068
- // email: [email],
1069
- // name,
1070
- // stat: 1,
1071
- // date_created: Date.now(),
1072
- // docType: "contact",
1073
- // uid,
1074
- // });
1075
- // }
1059
+
1076
1060
  await add_contact({ uid, name, email, metadata: { subject, is_sent }, source: 'read emails' }, job_id, headers);
1077
1061
  }
1078
1062
  }
@@ -1570,7 +1554,6 @@ export const get_pending_share_contact_in = async function (uid, _id) {
1570
1554
  const requests_from_res = await db_module.find_couch_query('xuda_team', from_opt);
1571
1555
 
1572
1556
  for await (let e of requests_from_res.docs) {
1573
- // 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 };
1574
1557
  let doc = await db_module.get_couch_doc_native('xuda_contacts', e.share_item_id);
1575
1558
  delete doc.contact_mood_level;
1576
1559
  doc.shared_from_uid = e.team_req_from_uid;
@@ -1601,108 +1584,6 @@ const get_user_contact = async function (uid, uid_query) {
1601
1584
  return doc;
1602
1585
  };
1603
1586
 
1604
- // invite user
1605
-
1606
- // const contact_ret = await db_module.get_couch_view_raw(
1607
- // "xuda_contacts",
1608
- // "contacts_by_name_count",
1609
- // {
1610
- // startkey: [uid, ""],
1611
- // endkey: [uid, "ZZZZZZZZZZ"],
1612
- // reduce: true,
1613
- // group_level: 2,
1614
- // }
1615
- // );
1616
-
1617
- export const add_contact = async function (req, job_id, headers) {
1618
- try {
1619
- const { uid, email, name, stat, contact_uid, context, source, team_req_id, metadata = {} } = req;
1620
- const account_profile_info = await get_active_account_profile_info(uid);
1621
-
1622
- const contact_ret = await db_module.get_couch_view_raw('xuda_contacts', 'contacts_by_user_email_address', { key: [account_profile_info.uid, email] });
1623
-
1624
- if (contact_ret.rows.length) {
1625
- throw new Error('contact already exist active or deleted');
1626
- }
1627
-
1628
- let is_spam = false;
1629
- let profile_avatar, profile_avatar_obj;
1630
- let account_info_ret;
1631
- if (contact_uid) {
1632
- account_info_ret = await get_account_name({ uid_query: contact_uid });
1633
- if (account_info_ret.code < 0) {
1634
- throw new Error(`account ${contact_uid} not found`);
1635
- }
1636
- } else {
1637
- // contact without use account
1638
- if (metadata.subject && !metadata.is_sent) {
1639
- is_spam_ret = await ai_module.is_spam_email(uid, email, metadata.subject);
1640
- try {
1641
- if (is_spam_ret.code > -1) {
1642
- const data = JSON.parse(is_spam_ret.data);
1643
- is_spam = data.is_spam;
1644
- }
1645
- } catch (error) {}
1646
- }
1647
-
1648
- const existing_contacts_ret = await db_module.get_couch_view_raw('xuda_contacts', 'contacts_by_email_address', { key: email });
1649
- for (const contact of existing_contacts_ret.rows) {
1650
- if (contact.value.profile_avatar) {
1651
- profile_avatar = contact.value.profile_avatar;
1652
- profile_avatar_obj == contact.value.profile_avatar_obj;
1653
- }
1654
- }
1655
- }
1656
- const d = Date.now();
1657
- const doc = {
1658
- _id: await _common.xuda_get_uuid('contact'),
1659
- email: [email],
1660
- name,
1661
- stat: stat || 1,
1662
- date_created: d,
1663
- ts: d,
1664
- docType: 'contact',
1665
- contact_uid,
1666
- uid: account_profile_info.uid,
1667
- uid_created: uid,
1668
- context,
1669
- headers,
1670
- source,
1671
- team_req_id,
1672
- metadata,
1673
- is_spam,
1674
- profile_avatar,
1675
- profile_avatar_obj,
1676
- account_profile_info,
1677
- };
1678
- const ret = await db_module.save_couch_doc('xuda_contacts', doc);
1679
-
1680
- if (!account_info_ret?.data?.profile_avatar) {
1681
- const app_id = await get_account_default_project_id(uid);
1682
- const ai_module = await import(`${module_path}/ai_module/index.mjs`);
1683
- if (account_info_ret?.data?.profile_avatar) {
1684
- // account_info_ret.data.profile_avatar_stat = 2;
1685
- ai_module.create_profile_avatar({ url: account_info_ret.data.profile_picture, uid, account_obj: { _id: contact_uid, account_info: account_info_ret.data }, account_profile_info }, job_id, headers);
1686
- } else {
1687
- if (!doc.profile_avatar) {
1688
- // create factual avatar
1689
- if (!is_spam) {
1690
- ai_module.update_thumbnail('contact', doc, account_profile_info.app_id, uid, job_id, headers, 'xuda_contacts', [email, doc._id, 'contact', account_profile_info.app_id], account_profile_info);
1691
- }
1692
- }
1693
- }
1694
- }
1695
- return ret;
1696
- } catch (err) {
1697
- return { code: -1, data: err.message };
1698
- }
1699
- };
1700
-
1701
- // const get_contact_id = async function (uid, contact_uid) {
1702
- // contact_ret = await db_module.get_couch_view_raw('xuda_contacts', 'contacts_by_contact_uid', { key: [uid, contact_uid] });
1703
- // return contact_ret?.rows?.[0]?.id;
1704
- // };
1705
-
1706
1587
  export const update_contact = async function (req) {
1707
1588
  const { uid, email, contact_uid, stat, team_req_id } = req;
1708
1589
 
@@ -2039,7 +1920,7 @@ export const update_contacts_with_account_changes = async function (uid, changes
2039
1920
  export const get_user_card = async function (uid, uid_query) {
2040
1921
  if (uid === uid_query) return await get_user_contact(uid, uid_query);
2041
1922
 
2042
- let user_contact_obj; // = { border, card_background, contact_uid, date_created, docType, icon_pattern, name, business_name, profile_avatar, connection_stat, username, bio, notifications, email, phone_number };
1923
+ let user_contact_obj;
2043
1924
 
2044
1925
  const contact_ret = await db_module.get_couch_view_raw('xuda_contacts', 'contacts_by_contact_uid', { key: [uid, uid_query] });
2045
1926
 
@@ -2203,76 +2084,6 @@ export const get_account_ai_usage = async function (req, job_id, headers) {
2203
2084
  }
2204
2085
  };
2205
2086
 
2206
- export const create_account_profile = async function (req, job_id, headers) {
2207
- const { uid, profile_name, profile_signature, profile_email, profile_picture, profile_avatar, profile_picture_obj, profile_avatar_obj, main } = req;
2208
- try {
2209
- // const app_id = await get_account_default_project_id(uid);
2210
- const { data: acc_obj } = await db_module.get_couch_doc('xuda_accounts', uid);
2211
-
2212
- const d = Date.now();
2213
- const doc = {
2214
- _id: await _common.xuda_get_uuid('account_profile'),
2215
- stat: 3,
2216
- date_created_ts: d,
2217
- ts: d,
2218
- docType: 'account_profile',
2219
-
2220
- uid,
2221
- profile_name, //: profile_name || acc_obj.account_info.business_name || acc_obj.account_info.name,
2222
- profile_signature,
2223
- profile_email,
2224
- profile_picture,
2225
- profile_avatar,
2226
- profile_picture_obj,
2227
- profile_avatar_obj,
2228
- main,
2229
- };
2230
- const save_ret = await db_module.save_couch_doc('xuda_accounts', doc);
2231
- // acc_obj.active_profile_id = save_ret.data.id;
2232
-
2233
- // return { code: 55, data: save_ret };
2234
- return save_ret;
2235
- } catch (err) {
2236
- return { code: -55, data: err.message };
2237
- }
2238
- };
2239
-
2240
- export const update_account_profile = async function (req, job_id, headers) {
2241
- const { uid, _id } = req;
2242
- const account_profile_properties = ['profile_name', 'profile_signature', 'profile_email', 'profile_picture', 'profile_avatar', 'profile_picture_obj', 'profile_avatar_obj', 'auto_respond', 'auto_respond_mode', 'auto_respond_agents'];
2243
- try {
2244
- let { data: account_profile_doc } = await db_module.get_couch_doc('xuda_accounts', _id);
2245
-
2246
- if (account_profile_doc.docType !== 'account_profile') {
2247
- throw new Error('not account profile doc');
2248
- }
2249
-
2250
- let changes_arr = [];
2251
-
2252
- for (const key of account_profile_properties) {
2253
- let val = req[key];
2254
- if (typeof val === 'undefined') continue;
2255
- if (account_profile_doc[key] !== val) {
2256
- changes_arr.push(key);
2257
- account_profile_doc[key] = val;
2258
- }
2259
- }
2260
-
2261
- if (!changes_arr.length) {
2262
- // throw new Error('no change');
2263
- return { code: 56, data: 'no change' };
2264
- }
2265
-
2266
- account_profile_doc.ts = Date.now();
2267
-
2268
- const save_ret = await db_module.save_couch_doc('xuda_accounts', account_profile_doc);
2269
-
2270
- return { code: 56, data: save_ret };
2271
- } catch (err) {
2272
- return { code: -56, data: err.message };
2273
- }
2274
- };
2275
-
2276
2087
  setTimeout(async () => {
2277
2088
  const app_id = 'prj712ffdf5aa8adce6cedef988f9c12392'; //'prj3937cb6f9a31c8c7dea25055bba845b1'; //
2278
2089
  const uid = 'd39126e0e2c51ffbd1aad10709fc8335';
@@ -2289,171 +2100,106 @@ setTimeout(async () => {
2289
2100
  console.log(ret);
2290
2101
  }, 1000);
2291
2102
 
2292
- export const get_pending_share_profile_in = async function (uid, _id) {
2293
- let docs = [];
2294
- const from_opt = { selector: { docType: 'team_request', access_type: 'account_profile', team_req_stat: 2, team_req_to_uid: uid }, fields: ['_id', 'team_req_date', 'sender_account_info.email', 'team_req_from_uid', 'share_item_id'] };
2295
-
2296
- if (_id) {
2297
- to_opt.selector._id = _id;
2298
- }
2299
-
2300
- const requests_from_res = await db_module.find_couch_query('xuda_team', from_opt);
2301
-
2302
- for await (let e of requests_from_res.docs) {
2303
- let doc = await db_module.get_couch_doc_native('xuda_accounts', e.share_item_id);
2304
-
2305
- doc.shared_from_uid = e.team_req_from_uid;
2306
- doc.pending = true;
2307
- doc = await get_account_profile_info(uid, doc);
2308
- doc.team_req_id = e._id;
2309
-
2310
- docs.push(doc);
2311
- }
2312
- return docs;
2313
- };
2103
+ //////// CONTACTS //////////////
2314
2104
 
2315
- export const get_account_profile_info = async function (uid, contact_profile_doc, _id) {
2316
- const ai_module = await import(`${module_path}/ai_module/index.mjs`);
2317
- let doc = contact_profile_doc;
2318
- if (_id) {
2319
- doc = await db_module.get_couch_doc_native('xuda_accounts', _id);
2320
- }
2321
- doc.notifications = 0;
2105
+ export const add_contact = async function (req, job_id, headers) {
2106
+ try {
2107
+ const { uid, email, name, stat, contact_uid, context, source, team_req_id, metadata = {} } = req;
2108
+ const account_profile_info = await get_active_account_profile_info(uid);
2322
2109
 
2323
- let uid_query = uid;
2324
- if (doc.shared_from_uid) uid_query = doc.shared_from_uid;
2325
- const account_info_ret = await get_account_name({ uid_query });
2326
- if (account_info_ret.code < 0) {
2327
- return;
2328
- }
2329
- if (!doc.profile_avatar) {
2330
- doc.profile_avatar = account_info_ret.data.profile_avatar;
2331
- }
2110
+ const contact_ret = await db_module.get_couch_view_raw('xuda_contacts', 'contacts_by_user_email_address', { key: [account_profile_info.uid, email] });
2332
2111
 
2333
- const get_pattern = async function (doc) {
2334
- let ret = `default-pattern.png`;
2112
+ if (contact_ret.rows.length) {
2113
+ throw new Error('contact already exist active or deleted');
2114
+ }
2335
2115
 
2336
- if (doc.shared_from_uid) {
2337
- // const account_profile_doc = await db_module.get_couch_doc_native('xuda_accounts', doc.share_item_id);
2338
- const shared_from_uid_ret = await get_account_name({ uid_query: doc.shared_from_uid });
2339
- ret = shared_from_uid_ret.data.profile_avatar;
2340
- } else if (doc.account_type === 'business') {
2341
- ret = doc.profile_picture;
2116
+ let is_spam = false;
2117
+ let profile_avatar, profile_avatar_obj;
2118
+ let account_info_ret;
2119
+ if (contact_uid) {
2120
+ account_info_ret = await get_account_name({ uid_query: contact_uid });
2121
+ if (account_info_ret.code < 0) {
2122
+ throw new Error(`account ${contact_uid} not found`);
2123
+ }
2342
2124
  } else {
2343
- switch (doc.avatar_source) {
2344
- case 'fictional':
2345
- ret = `fictional-avatar-pattern.png`;
2346
- break;
2347
-
2348
- case 'ai profile':
2349
- ret = `fictional-avatar-pattern.png`;
2350
- break;
2351
-
2352
- case 'authentic profile':
2353
- ret = `authentic-avatar-pattern.png`;
2354
- break;
2125
+ // contact without use account
2126
+ if (metadata.subject && !metadata.is_sent) {
2127
+ is_spam_ret = await ai_module.is_spam_email(uid, email, metadata.subject);
2128
+ try {
2129
+ if (is_spam_ret.code > -1) {
2130
+ const data = JSON.parse(is_spam_ret.data);
2131
+ is_spam = data.is_spam;
2132
+ }
2133
+ } catch (error) {}
2134
+ }
2355
2135
 
2356
- default:
2357
- ret = `xu-pattern.png`;
2358
- break;
2136
+ const existing_contacts_ret = await db_module.get_couch_view_raw('xuda_contacts', 'contacts_by_email_address', { key: email });
2137
+ for (const contact of existing_contacts_ret.rows) {
2138
+ if (contact.value.profile_avatar) {
2139
+ profile_avatar = contact.value.profile_avatar;
2140
+ profile_avatar_obj == contact.value.profile_avatar_obj;
2141
+ }
2359
2142
  }
2360
2143
  }
2144
+ const d = Date.now();
2145
+ const doc = {
2146
+ _id: await _common.xuda_get_uuid('contact'),
2147
+ email: [email],
2148
+ name,
2149
+ stat: stat || 1,
2150
+ date_created: d,
2151
+ ts: d,
2152
+ docType: 'contact',
2153
+ contact_uid,
2154
+ uid: account_profile_info.uid,
2155
+ uid_created: uid,
2156
+ context,
2157
+ headers,
2158
+ source,
2159
+ team_req_id,
2160
+ metadata,
2161
+ is_spam,
2162
+ profile_avatar,
2163
+ profile_avatar_obj,
2164
+ account_profile_info,
2165
+ };
2166
+ const ret = await db_module.save_couch_doc('xuda_contacts', doc);
2361
2167
 
2362
- return ret;
2363
- };
2364
-
2365
- const get_online = async () => {
2366
- let online = false;
2367
- let selector = { docType: 'team_request', team_req_stat: 3, share_item_id: doc._id, access_type: 'account_profile', team_req_from_uid: uid };
2368
- let ret = await db_module.find_couch_query('xuda_team', { selector });
2369
- for (const req_obj of ret.docs) {
2370
- const account_info_ret = await get_account_name({ uid_query: req_obj.team_req_to_uid });
2371
- online = account_info_ret.data.online;
2372
- if (online) break;
2373
- }
2374
- return online;
2375
- };
2376
-
2377
- const stringToColour = (str) => {
2378
- let hash = 0;
2379
- // Generate a numeric hash from the string
2380
- for (let i = 0; i < str.length; i++) {
2381
- hash = str.charCodeAt(i) + ((hash << 5) - hash);
2382
- }
2383
- let colour = '#';
2384
- // Convert the hash into a 6-digit hex color code
2385
- for (let i = 0; i < 3; i++) {
2386
- const value = (hash >> (i * 8)) & 0xff;
2387
- colour += ('00' + value.toString(16)).substr(-2);
2388
- }
2389
- return colour;
2390
- };
2391
- const get_members = async () => {
2392
- let members = [];
2393
- let selector = { docType: 'team_request', team_req_stat: 3, share_item_id: doc._id, access_type: 'account_profile', team_req_from_uid: uid };
2394
- let ret = await db_module.find_couch_query('xuda_team', { selector });
2395
- for (const req_obj of ret.docs) {
2396
- const account_info_ret = await get_account_name({ uid_query: req_obj.team_req_to_uid });
2397
- const info = await await get_contact_info(uid, account_info_ret.data);
2398
- members.push(info);
2168
+ if (!account_info_ret?.data?.profile_avatar) {
2169
+ const app_id = await get_account_default_project_id(uid);
2170
+ const ai_module = await import(`${module_path}/ai_module/index.mjs`);
2171
+ if (account_info_ret?.data?.profile_avatar) {
2172
+ // account_info_ret.data.profile_avatar_stat = 2;
2173
+ ai_module.create_profile_avatar({ url: account_info_ret.data.profile_picture, uid, account_obj: { _id: contact_uid, account_info: account_info_ret.data }, account_profile_info }, job_id, headers);
2174
+ } else {
2175
+ if (!doc.profile_avatar) {
2176
+ // create factual avatar
2177
+ if (!is_spam) {
2178
+ ai_module.update_thumbnail('contact', doc, account_profile_info.app_id, uid, job_id, headers, 'xuda_contacts', [email, doc._id, 'contact', account_profile_info.app_id], account_profile_info);
2179
+ }
2180
+ }
2181
+ }
2399
2182
  }
2400
- return members;
2401
- };
2402
-
2403
- const get_user_account_profiles = async () => {
2404
- const account_profile_info = await get_active_account_profile_info(uid);
2405
- let selector = { docType: 'account_profile', stat: 3, uid: uid };
2406
- let ret = await db_module.find_couch_query('xuda_accounts', { selector });
2407
-
2408
- return ret.docs;
2409
- };
2410
-
2411
- // Usage examples:
2412
- console.log(stringToColour('User A')); // -> Consistent color for "User A"
2413
- console.log(stringToColour('Project Z')); // -> Consistent color for "Project Z"
2414
-
2415
- doc.avatar_source = account_info_ret.data.avatar_source;
2416
- doc.icon_pattern = await get_pattern(doc);
2417
- doc.name = doc.profile_name;
2418
- doc.interactions = 0;
2419
- doc.chats = 0;
2420
-
2421
- doc.online = await get_online();
2422
-
2423
- // doc.border = account_info_ret.data.active_account_profile_id === doc._id ? 'yellow' : ''; //'#1a3557';
2424
- // doc.border = stringToColour(doc.shared_from_uid || doc.uid);
2425
-
2426
- const chats_ret = await ai_module.get_ai_chats({ uid, reference_id: doc._id });
2427
-
2428
- doc.card_background = `linear-gradient(145deg, #242480 0%, #060619 100%)`;
2429
- // doc.border = get_contact_border(doc);
2430
-
2431
- doc.members = await get_members();
2432
- const user_account_profiles = await get_user_account_profiles();
2433
- debugger;
2434
- const usage_ret = await get_account_ai_usage({ uid });
2435
- doc.max_credits = usage_ret.data.credits.total / user_account_profiles.length;
2436
- doc.used_credits = usage_ret.data.usage[doc._id] || 0;
2437
-
2438
- if (doc.shared_from_uid) {
2439
- doc.max_credits = doc.max_credits / doc.members.length;
2183
+ return ret;
2184
+ } catch (err) {
2185
+ return { code: -1, data: err.message };
2440
2186
  }
2441
-
2442
- return doc;
2443
2187
  };
2444
2188
 
2445
- export const get_account_profiles = async function (req) {
2446
- const { _id, uid, profile_name, limit, skip, bookmark, search, filter_type = 'all', profile_id } = req;
2189
+ export const get_contacts = async function (req) {
2190
+ const { _id, uid, name, limit, skip, bookmark, search, filter_type = 'all', contact_id } = req;
2447
2191
 
2192
+ const account_profile_info = await get_active_account_profile_info(uid);
2448
2193
  if (_id) {
2449
- let data = await db_module.get_couch_doc('xuda_accounts', _id);
2194
+ let data = await db_module.get_couch_doc('xuda_contacts', _id);
2450
2195
  return data;
2451
2196
  }
2452
2197
 
2198
+ let { with_requests } = req;
2453
2199
  var opt = {
2454
2200
  selector: {
2455
- docType: 'account_profile',
2456
- uid,
2201
+ docType: 'contact',
2202
+ uid: account_profile_info.uid,
2457
2203
  stat: { $lt: 4 },
2458
2204
  },
2459
2205
  limit: limit || 9999,
@@ -2471,7 +2217,18 @@ export const get_account_profiles = async function (req) {
2471
2217
  break;
2472
2218
  }
2473
2219
 
2220
+ case 'pinned': {
2221
+ opt.selector.pinned = true;
2222
+ break;
2223
+ }
2224
+
2225
+ case 'online': {
2226
+ opt.limit = 9999;
2227
+ break;
2228
+ }
2229
+
2474
2230
  default:
2231
+ with_requests = true;
2475
2232
  break;
2476
2233
  }
2477
2234
 
@@ -2479,23 +2236,23 @@ export const get_account_profiles = async function (req) {
2479
2236
  opt.selector._id = _id;
2480
2237
  }
2481
2238
 
2482
- if (profile_id) {
2483
- opt.selector._id = profile_id;
2239
+ if (contact_id) {
2240
+ opt.selector._id = contact_id;
2484
2241
  }
2485
2242
 
2486
- if (typeof profile_name !== 'undefined') {
2487
- opt.selector.name = { $regex: `(?i)${profile_name}` };
2243
+ if (typeof name !== 'undefined') {
2244
+ opt.selector.name = { $regex: `(?i)${name}` };
2488
2245
  }
2489
2246
 
2490
2247
  if (typeof search !== 'undefined') {
2491
2248
  opt.selector['$or'] = [
2492
2249
  {
2493
- profile_name: {
2250
+ name: {
2494
2251
  $regex: `(?i)${search}`,
2495
2252
  },
2496
2253
  },
2497
2254
  {
2498
- profile_email: {
2255
+ email: {
2499
2256
  $regex: `(?i)${search}`,
2500
2257
  },
2501
2258
  },
@@ -2506,39 +2263,58 @@ export const get_account_profiles = async function (req) {
2506
2263
  opt.bookmark = bookmark;
2507
2264
  }
2508
2265
 
2509
- let profiles = { docs: [], total_docs: 0 };
2266
+ let contacts = { docs: [], total_docs: 0 };
2510
2267
 
2511
2268
  if (filter_type !== 'pending') {
2512
- profiles = await db_module.find_couch_query('xuda_accounts', opt);
2513
- if (!limit || profile_id) {
2514
- profiles.total_docs = profiles.docs.length;
2269
+ contacts = await db_module.find_couch_query('xuda_contacts', opt);
2270
+ if (!limit || contact_id) {
2271
+ contacts.total_docs = contacts.docs.length;
2515
2272
  } else {
2516
2273
  delete opt.sort;
2517
2274
  delete opt.skip;
2518
2275
  opt.limit = 9999;
2519
2276
  opt.fields = ['_id'];
2520
2277
 
2521
- const counter = await db_module.find_couch_query('xuda_accounts', opt);
2522
- profiles.total_docs = counter.docs.length;
2278
+ const counter = await db_module.find_couch_query('xuda_contacts', opt);
2279
+ contacts.total_docs = counter.docs.length;
2523
2280
  }
2524
2281
  }
2525
2282
 
2283
+ let requests_to = { docs: [], total_docs: 0 };
2284
+ let requests_from = { docs: [], total_docs: 0 };
2526
2285
  let shared_from = { docs: [], total_docs: 0 };
2527
2286
 
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);
2287
+ let contact_docs = { docs: [], total_docs: contacts.total_docs };
2288
+ for await (let doc of contacts.docs) {
2289
+ const ret_to = requests_to.docs.find((e) => {
2290
+ return e.contact_uid === doc.contact_uid;
2291
+ });
2292
+ if (ret_to) continue;
2293
+
2294
+ const ret_from = requests_from.docs.find((e) => {
2295
+ return e.contact_uid === doc.contact_uid;
2296
+ });
2297
+ if (ret_from) continue;
2298
+
2299
+ doc = await get_contact_info(uid, doc);
2300
+
2301
+ if (filter_type === 'online') {
2302
+ if (!doc.online) continue;
2535
2303
  }
2304
+
2305
+ contact_docs.docs.push(doc);
2536
2306
  }
2537
2307
 
2538
- if (!profile_id) {
2308
+ if (!contact_id && with_requests) {
2539
2309
  if (filter_type === 'pending') {
2310
+ ////// TO - OUT
2311
+ requests_to.docs = await get_pending_contact_out(uid);
2312
+ requests_to.total_docs = requests_to.docs.length;
2313
+
2540
2314
  ////// FROM - IN
2541
- shared_from.docs = await get_pending_share_profile_in(uid);
2315
+ requests_from.docs = await get_pending_contact_in(uid);
2316
+ requests_from.total_docs = requests_from.docs.length;
2317
+ shared_from.docs = await get_pending_share_contact_in(uid);
2542
2318
  shared_from.total_docs = shared_from.docs.length;
2543
2319
  }
2544
2320
  }
@@ -2546,34 +2322,31 @@ export const get_account_profiles = async function (req) {
2546
2322
  return {
2547
2323
  code: 1,
2548
2324
  data: {
2549
- docs: [...shared_from.docs, ...profile_docs.docs],
2550
- total_docs: shared_from.total_docs + profile_docs.total_docs,
2325
+ docs: [...shared_from.docs, ...requests_from.docs, ...requests_to.docs, ...contact_docs.docs],
2326
+ total_docs: shared_from.total_docs + requests_from.total_docs + requests_to.total_docs + contact_docs.total_docs,
2551
2327
  },
2552
2328
  };
2553
2329
  };
2554
2330
 
2555
- export const archive_account_profile = async function (req) {
2556
- const { uid, profile_id } = req;
2331
+ export const archive_contact = async function (req) {
2332
+ const { contact_id } = req;
2557
2333
  try {
2558
2334
  const team_module = await import(`${module_path}/team_module/index.mjs`);
2559
- await team_module.validate_share_exist(uid, 'account_profile', profile_id);
2335
+ await team_module.validate_share_exist(uid, 'contact', contact_id);
2560
2336
 
2561
- var account_profile_doc = await db_module.get_couch_doc_native('xuda_accounts', profile_id);
2337
+ var contact_doc = await db_module.get_couch_doc_native('xuda_contacts', contact_id);
2562
2338
 
2563
- if (account_profile_doc.main) {
2564
- throw new Error('reserve');
2339
+ if (contact_doc.uid !== uid && contact_doc?.account_profile_info?.uid !== uid) {
2340
+ throw new Error('Operation not allowed');
2565
2341
  }
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
2342
 
2571
- const account_profile_save_ret = await db_module.save_couch_doc('xuda_accounts', account_profile_doc);
2343
+ contact_doc.stat = 5;
2344
+ contact_doc.stat_ts = Date.now();
2345
+ contact_doc.stat_reason = 'archived by the user';
2572
2346
 
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
- // }
2347
+ const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
2348
+
2349
+ return contact_save_ret;
2577
2350
  } catch (err) {
2578
2351
  return {
2579
2352
  code: -22,
@@ -2582,283 +2355,21 @@ export const archive_account_profile = async function (req) {
2582
2355
  }
2583
2356
  };
2584
2357
 
2585
- export const delete_account_profile = async function (req, job_id, headers) {
2586
- const { profile_id } = req;
2358
+ export const delete_contact = async function (req, job_id, headers) {
2359
+ const { contact_id } = req;
2587
2360
  try {
2588
- var account_profile_doc = await db_module.get_couch_doc_native('xuda_accounts', profile_id);
2589
- if (account_profile_doc.stat !== 5) {
2361
+ var contact_doc = await db_module.get_couch_doc_native('xuda_contacts', contact_id);
2362
+ if (contact_doc.stat !== 5) {
2590
2363
  throw new Error('A document can only be deleted after it has been archived');
2591
2364
  }
2592
- account_profile_doc.stat = 4;
2593
- account_profile_doc.stat_ts = Date.now();
2594
- account_profile_doc.stat_reason = 'deleted by the user';
2365
+ contact_doc.stat = 4;
2366
+ contact_doc.stat_ts = Date.now();
2367
+ contact_doc.stat_reason = 'deleted by the user';
2595
2368
 
2596
- const account_profile_save_ret = await db_module.save_couch_doc('xuda_accounts', account_profile_doc);
2369
+ const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
2597
2370
  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;
2761
- } else {
2762
- delete opt.sort;
2763
- delete opt.skip;
2764
- opt.limit = 9999;
2765
- opt.fields = ['_id'];
2766
-
2767
- const counter = await db_module.find_couch_query('xuda_contacts', opt);
2768
- contacts.total_docs = counter.docs.length;
2769
- }
2770
- }
2771
-
2772
- let requests_to = { docs: [], total_docs: 0 };
2773
- let requests_from = { docs: [], total_docs: 0 };
2774
- let shared_from = { docs: [], total_docs: 0 };
2775
-
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;
2792
- }
2793
-
2794
- contact_docs.docs.push(doc);
2795
- }
2796
-
2797
- if (!contact_id && with_requests) {
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
-
2803
- ////// FROM - IN
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);
2807
- shared_from.total_docs = shared_from.docs.length;
2808
- }
2809
- }
2810
-
2811
- return {
2812
- code: 1,
2813
- data: {
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,
2816
- },
2817
- };
2818
- };
2819
-
2820
- export const archive_contact = async function (req) {
2821
- const { contact_id } = req;
2822
- try {
2823
- const team_module = await import(`${module_path}/team_module/index.mjs`);
2824
- await team_module.validate_share_exist(uid, 'contact', contact_id);
2825
-
2826
- var contact_doc = await db_module.get_couch_doc_native('xuda_contacts', contact_id);
2827
-
2828
- if (contact_doc.uid !== uid && contact_doc?.account_profile_info?.uid !== uid) {
2829
- throw new Error('Operation not allowed');
2830
- }
2831
-
2832
- contact_doc.stat = 5;
2833
- contact_doc.stat_ts = Date.now();
2834
- contact_doc.stat_reason = 'archived by the user';
2835
-
2836
- const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
2837
-
2838
- return contact_save_ret;
2839
- } catch (err) {
2840
- return {
2841
- code: -22,
2842
- data: err.message,
2843
- };
2844
- }
2845
- };
2846
-
2847
- export const delete_contact = async function (req, job_id, headers) {
2848
- const { contact_id } = req;
2849
- try {
2850
- var contact_doc = await db_module.get_couch_doc_native('xuda_contacts', contact_id);
2851
- if (contact_doc.stat !== 5) {
2852
- throw new Error('A document can only be deleted after it has been archived');
2853
- }
2854
- contact_doc.stat = 4;
2855
- contact_doc.stat_ts = Date.now();
2856
- contact_doc.stat_reason = 'deleted by the user';
2857
-
2858
- const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
2859
- const ai_module = await import(`${module_path}/ai_module/index.mjs`);
2860
- ai_module.delete_depended_chats(uid, contact_id);
2861
- return contact_save_ret;
2371
+ ai_module.delete_depended_chats(uid, contact_id);
2372
+ return contact_save_ret;
2862
2373
  } catch (err) {
2863
2374
  return {
2864
2375
  code: -22,
@@ -2991,3 +2502,425 @@ export const unpin_contact = async function (req) {
2991
2502
  };
2992
2503
  }
2993
2504
  };
2505
+
2506
+ //////// PROFILES //////////////
2507
+
2508
+ export const get_pending_share_profile_in = async function (uid, _id) {
2509
+ let docs = [];
2510
+ const from_opt = { selector: { docType: 'team_request', access_type: 'account_profile', team_req_stat: 2, team_req_to_uid: uid }, fields: ['_id', 'team_req_date', 'sender_account_info.email', 'team_req_from_uid', 'share_item_id'] };
2511
+
2512
+ if (_id) {
2513
+ to_opt.selector._id = _id;
2514
+ }
2515
+
2516
+ const requests_from_res = await db_module.find_couch_query('xuda_team', from_opt);
2517
+
2518
+ for await (let e of requests_from_res.docs) {
2519
+ let doc = await db_module.get_couch_doc_native('xuda_accounts', e.share_item_id);
2520
+
2521
+ doc.shared_from_uid = e.team_req_from_uid;
2522
+ doc.pending = true;
2523
+ doc = await get_account_profile_info(uid, doc);
2524
+ doc.team_req_id = e._id;
2525
+
2526
+ docs.push(doc);
2527
+ }
2528
+ return docs;
2529
+ };
2530
+
2531
+ export const get_account_profile_info = async function (uid, contact_profile_doc, _id) {
2532
+ const ai_module = await import(`${module_path}/ai_module/index.mjs`);
2533
+ let doc = contact_profile_doc;
2534
+ if (_id) {
2535
+ doc = await db_module.get_couch_doc_native('xuda_accounts', _id);
2536
+ }
2537
+ doc.notifications = 0;
2538
+
2539
+ let uid_query = uid;
2540
+ if (doc.shared_from_uid) uid_query = doc.shared_from_uid;
2541
+ const account_info_ret = await get_account_name({ uid_query });
2542
+ if (account_info_ret.code < 0) {
2543
+ return;
2544
+ }
2545
+ if (!doc.profile_avatar) {
2546
+ doc.profile_avatar = account_info_ret.data.profile_avatar;
2547
+ }
2548
+
2549
+ const get_pattern = async function (doc) {
2550
+ let ret = `default-pattern.png`;
2551
+
2552
+ if (doc.shared_from_uid) {
2553
+ // const account_profile_doc = await db_module.get_couch_doc_native('xuda_accounts', doc.share_item_id);
2554
+ const shared_from_uid_ret = await get_account_name({ uid_query: doc.shared_from_uid });
2555
+ ret = shared_from_uid_ret.data.profile_avatar;
2556
+ } else if (doc.account_type === 'business') {
2557
+ ret = doc.profile_picture;
2558
+ } else {
2559
+ switch (doc.avatar_source) {
2560
+ case 'fictional':
2561
+ ret = `fictional-avatar-pattern.png`;
2562
+ break;
2563
+
2564
+ case 'ai profile':
2565
+ ret = `fictional-avatar-pattern.png`;
2566
+ break;
2567
+
2568
+ case 'authentic profile':
2569
+ ret = `authentic-avatar-pattern.png`;
2570
+ break;
2571
+
2572
+ default:
2573
+ ret = `xu-pattern.png`;
2574
+ break;
2575
+ }
2576
+ }
2577
+
2578
+ return ret;
2579
+ };
2580
+
2581
+ const get_online = async () => {
2582
+ let online = false;
2583
+ let selector = { docType: 'team_request', team_req_stat: 3, share_item_id: doc._id, access_type: 'account_profile', team_req_from_uid: uid };
2584
+ let ret = await db_module.find_couch_query('xuda_team', { selector });
2585
+ for (const req_obj of ret.docs) {
2586
+ const account_info_ret = await get_account_name({ uid_query: req_obj.team_req_to_uid });
2587
+ online = account_info_ret.data.online;
2588
+ if (online) break;
2589
+ }
2590
+ return online;
2591
+ };
2592
+
2593
+ const stringToColour = (str) => {
2594
+ let hash = 0;
2595
+ // Generate a numeric hash from the string
2596
+ for (let i = 0; i < str.length; i++) {
2597
+ hash = str.charCodeAt(i) + ((hash << 5) - hash);
2598
+ }
2599
+ let colour = '#';
2600
+ // Convert the hash into a 6-digit hex color code
2601
+ for (let i = 0; i < 3; i++) {
2602
+ const value = (hash >> (i * 8)) & 0xff;
2603
+ colour += ('00' + value.toString(16)).substr(-2);
2604
+ }
2605
+ return colour;
2606
+ };
2607
+ const get_members = async () => {
2608
+ let members = [];
2609
+ let selector = { docType: 'team_request', team_req_stat: 3, share_item_id: doc._id, access_type: 'account_profile', team_req_from_uid: uid };
2610
+ let ret = await db_module.find_couch_query('xuda_team', { selector });
2611
+ for (const req_obj of ret.docs) {
2612
+ const account_info_ret = await get_account_name({ uid_query: req_obj.team_req_to_uid });
2613
+ const info = await await get_contact_info(uid, account_info_ret.data);
2614
+ members.push(info);
2615
+ }
2616
+ return members;
2617
+ };
2618
+
2619
+ const get_user_account_profiles = async () => {
2620
+ const account_profile_info = await get_active_account_profile_info(uid);
2621
+ let selector = { docType: 'account_profile', stat: 3, uid: uid };
2622
+ let ret = await db_module.find_couch_query('xuda_accounts', { selector });
2623
+
2624
+ return ret.docs;
2625
+ };
2626
+
2627
+ // Usage examples:
2628
+ console.log(stringToColour('User A')); // -> Consistent color for "User A"
2629
+ console.log(stringToColour('Project Z')); // -> Consistent color for "Project Z"
2630
+
2631
+ doc.avatar_source = account_info_ret.data.avatar_source;
2632
+ doc.icon_pattern = await get_pattern(doc);
2633
+ doc.name = doc.profile_name;
2634
+ doc.interactions = 0;
2635
+ doc.chats = 0;
2636
+
2637
+ doc.online = await get_online();
2638
+
2639
+ // doc.border = account_info_ret.data.active_account_profile_id === doc._id ? 'yellow' : ''; //'#1a3557';
2640
+ // doc.border = stringToColour(doc.shared_from_uid || doc.uid);
2641
+
2642
+ const chats_ret = await ai_module.get_ai_chats({ uid, reference_id: doc._id });
2643
+
2644
+ doc.card_background = `linear-gradient(145deg, #242480 0%, #060619 100%)`;
2645
+ // doc.border = get_contact_border(doc);
2646
+
2647
+ doc.members = await get_members();
2648
+ const user_account_profiles = await get_user_account_profiles();
2649
+ debugger;
2650
+ const usage_ret = await get_account_ai_usage({ uid });
2651
+ doc.max_credits = usage_ret.data.credits.total / user_account_profiles.length;
2652
+ doc.used_credits = usage_ret.data.usage[doc._id] || 0;
2653
+
2654
+ if (doc.shared_from_uid) {
2655
+ doc.max_credits = doc.max_credits / doc.members.length;
2656
+ }
2657
+
2658
+ return doc;
2659
+ };
2660
+
2661
+ export const get_account_profiles = async function (req) {
2662
+ const { _id, uid, profile_name, limit, skip, bookmark, search, filter_type = 'all', profile_id } = req;
2663
+
2664
+ if (_id) {
2665
+ let data = await db_module.get_couch_doc('xuda_accounts', _id);
2666
+ return data;
2667
+ }
2668
+
2669
+ var opt = {
2670
+ selector: {
2671
+ docType: 'account_profile',
2672
+ uid,
2673
+ stat: { $lt: 4 },
2674
+ },
2675
+ limit: limit || 9999,
2676
+ skip: skip || 0,
2677
+ sort: [{ ts: 'desc' }],
2678
+ };
2679
+
2680
+ switch (filter_type) {
2681
+ case 'archived': {
2682
+ opt.selector.stat = 5;
2683
+ break;
2684
+ }
2685
+ case 'shared': {
2686
+ opt.selector.shared_from_uid = { $gt: null };
2687
+ break;
2688
+ }
2689
+
2690
+ default:
2691
+ break;
2692
+ }
2693
+
2694
+ if (_id) {
2695
+ opt.selector._id = _id;
2696
+ }
2697
+
2698
+ if (profile_id) {
2699
+ opt.selector._id = profile_id;
2700
+ }
2701
+
2702
+ if (typeof profile_name !== 'undefined') {
2703
+ opt.selector.name = { $regex: `(?i)${profile_name}` };
2704
+ }
2705
+
2706
+ if (typeof search !== 'undefined') {
2707
+ opt.selector['$or'] = [
2708
+ {
2709
+ profile_name: {
2710
+ $regex: `(?i)${search}`,
2711
+ },
2712
+ },
2713
+ {
2714
+ profile_email: {
2715
+ $regex: `(?i)${search}`,
2716
+ },
2717
+ },
2718
+ ];
2719
+ }
2720
+
2721
+ if (bookmark && bookmark !== 'nil') {
2722
+ opt.bookmark = bookmark;
2723
+ }
2724
+
2725
+ let profiles = { docs: [], total_docs: 0 };
2726
+
2727
+ if (filter_type !== 'pending') {
2728
+ profiles = await db_module.find_couch_query('xuda_accounts', opt);
2729
+ if (!limit || profile_id) {
2730
+ profiles.total_docs = profiles.docs.length;
2731
+ } else {
2732
+ delete opt.sort;
2733
+ delete opt.skip;
2734
+ opt.limit = 9999;
2735
+ opt.fields = ['_id'];
2736
+
2737
+ const counter = await db_module.find_couch_query('xuda_accounts', opt);
2738
+ profiles.total_docs = counter.docs.length;
2739
+ }
2740
+ }
2741
+
2742
+ let shared_from = { docs: [], total_docs: 0 };
2743
+
2744
+ let profile_docs = { docs: [], total_docs: profiles.total_docs };
2745
+ for await (let doc of profiles.docs) {
2746
+ doc = await get_account_profile_info(uid, doc);
2747
+ if (doc.main) {
2748
+ profile_docs.docs.unshift(doc);
2749
+ } else {
2750
+ profile_docs.docs.push(doc);
2751
+ }
2752
+ }
2753
+
2754
+ if (!profile_id) {
2755
+ if (filter_type === 'pending') {
2756
+ ////// FROM - IN
2757
+ shared_from.docs = await get_pending_share_profile_in(uid);
2758
+ shared_from.total_docs = shared_from.docs.length;
2759
+ }
2760
+ }
2761
+
2762
+ return {
2763
+ code: 1,
2764
+ data: {
2765
+ docs: [...shared_from.docs, ...profile_docs.docs],
2766
+ total_docs: shared_from.total_docs + profile_docs.total_docs,
2767
+ },
2768
+ };
2769
+ };
2770
+
2771
+ export const archive_account_profile = async function (req) {
2772
+ const { uid, profile_id } = req;
2773
+ try {
2774
+ const team_module = await import(`${module_path}/team_module/index.mjs`);
2775
+ await team_module.validate_share_exist(uid, 'account_profile', profile_id);
2776
+
2777
+ var account_profile_doc = await db_module.get_couch_doc_native('xuda_accounts', profile_id);
2778
+
2779
+ if (account_profile_doc.main) {
2780
+ throw new Error('reserve');
2781
+ }
2782
+ // if (!account_profile_doc.team_req_id) {
2783
+ account_profile_doc.stat = 5;
2784
+ account_profile_doc.stat_ts = Date.now();
2785
+ account_profile_doc.stat_reason = 'archived by the user';
2786
+
2787
+ const account_profile_save_ret = await db_module.save_couch_doc('xuda_accounts', account_profile_doc);
2788
+
2789
+ return account_profile_save_ret;
2790
+ // } else {
2791
+ // return await team_module.change_account_profile_dependencies_status(account_profile_doc.team_req_id, 5);
2792
+ // }
2793
+ } catch (err) {
2794
+ return {
2795
+ code: -22,
2796
+ data: err.message,
2797
+ };
2798
+ }
2799
+ };
2800
+
2801
+ export const delete_account_profile = async function (req, job_id, headers) {
2802
+ const { profile_id } = req;
2803
+ try {
2804
+ var account_profile_doc = await db_module.get_couch_doc_native('xuda_accounts', profile_id);
2805
+ if (account_profile_doc.stat !== 5) {
2806
+ throw new Error('A document can only be deleted after it has been archived');
2807
+ }
2808
+ account_profile_doc.stat = 4;
2809
+ account_profile_doc.stat_ts = Date.now();
2810
+ account_profile_doc.stat_reason = 'deleted by the user';
2811
+
2812
+ const account_profile_save_ret = await db_module.save_couch_doc('xuda_accounts', account_profile_doc);
2813
+ const ai_module = await import(`${module_path}/ai_module/index.mjs`);
2814
+ ai_module.delete_depended_chats(uid, profile_id);
2815
+ return account_profile_save_ret;
2816
+ } catch (err) {
2817
+ return {
2818
+ code: -22,
2819
+ data: err.message,
2820
+ };
2821
+ }
2822
+ };
2823
+
2824
+ export const unarchive_account_profile = async function (req) {
2825
+ const { profile_id } = req;
2826
+ try {
2827
+ const team_module = await import(`${module_path}/team_module/index.mjs`);
2828
+ var account_profile_doc = await db_module.get_couch_doc_native('xuda_accounts', profile_id);
2829
+
2830
+ if (account_profile_doc.stat !== 5) {
2831
+ throw new Error('account profile is not archived');
2832
+ }
2833
+ if (!account_profile_doc.team_req_id) {
2834
+ account_profile_doc.stat = 3;
2835
+ account_profile_doc.stat_ts = Date.now();
2836
+ account_profile_doc.stat_reason = 'archived by the user';
2837
+
2838
+ const account_profile_save_ret = await db_module.save_couch_doc('xuda_accounts', account_profile_doc);
2839
+
2840
+ return account_profile_save_ret;
2841
+ } else {
2842
+ return await team_module.change_account_profile_dependencies_status(account_profile_doc.team_req_id, 3);
2843
+ }
2844
+ } catch (err) {
2845
+ return {
2846
+ code: -22,
2847
+ data: err.message,
2848
+ };
2849
+ }
2850
+ };
2851
+
2852
+ export const create_account_profile = async function (req, job_id, headers) {
2853
+ const { uid, profile_name, profile_signature, profile_email, profile_picture, profile_avatar, profile_picture_obj, profile_avatar_obj, main } = req;
2854
+ try {
2855
+ // const app_id = await get_account_default_project_id(uid);
2856
+ const { data: acc_obj } = await db_module.get_couch_doc('xuda_accounts', uid);
2857
+
2858
+ const d = Date.now();
2859
+ const doc = {
2860
+ _id: await _common.xuda_get_uuid('account_profile'),
2861
+ stat: 3,
2862
+ date_created_ts: d,
2863
+ ts: d,
2864
+ docType: 'account_profile',
2865
+
2866
+ uid,
2867
+ profile_name, //: profile_name || acc_obj.account_info.business_name || acc_obj.account_info.name,
2868
+ profile_signature,
2869
+ profile_email,
2870
+ profile_picture,
2871
+ profile_avatar,
2872
+ profile_picture_obj,
2873
+ profile_avatar_obj,
2874
+ main,
2875
+ };
2876
+ const save_ret = await db_module.save_couch_doc('xuda_accounts', doc);
2877
+ // acc_obj.active_profile_id = save_ret.data.id;
2878
+
2879
+ // return { code: 55, data: save_ret };
2880
+ return save_ret;
2881
+ } catch (err) {
2882
+ return { code: -55, data: err.message };
2883
+ }
2884
+ };
2885
+
2886
+ export const update_account_profile = async function (req, job_id, headers) {
2887
+ const { uid, _id } = req;
2888
+ const account_profile_properties = ['profile_name', 'profile_signature', 'profile_email', 'profile_picture', 'profile_avatar', 'profile_picture_obj', 'profile_avatar_obj', 'auto_respond', 'auto_respond_mode', 'auto_respond_agents'];
2889
+ try {
2890
+ let { data: account_profile_doc } = await db_module.get_couch_doc('xuda_accounts', _id);
2891
+
2892
+ if (account_profile_doc.docType !== 'account_profile') {
2893
+ throw new Error('not account profile doc');
2894
+ }
2895
+
2896
+ let changes_arr = [];
2897
+
2898
+ for (const key of account_profile_properties) {
2899
+ let val = req[key];
2900
+ if (typeof val === 'undefined') continue;
2901
+ if (account_profile_doc[key] !== val) {
2902
+ changes_arr.push(key);
2903
+ account_profile_doc[key] = val;
2904
+ }
2905
+ }
2906
+
2907
+ if (!changes_arr.length) {
2908
+ // throw new Error('no change');
2909
+ return { code: 56, data: 'no change' };
2910
+ }
2911
+
2912
+ account_profile_doc.ts = Date.now();
2913
+
2914
+ const save_ret = await db_module.save_couch_doc('xuda_accounts', account_profile_doc);
2915
+
2916
+ return { code: 56, data: save_ret };
2917
+ } catch (err) {
2918
+ return { code: -56, data: err.message };
2919
+ }
2920
+ };
2921
+
2922
+ const get_contact = async function (uid, contact_id) {
2923
+ const account_profile_info = await get_active_account_profile_info(uid);
2924
+
2925
+ const contact_ret = await db_module.get_couch_doc('xuda_contacts', contact_id);
2926
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/account_module",
3
- "version": "1.2.1257",
3
+ "version": "1.2.1259",
4
4
  "description": "Xuda Account Server Module",
5
5
  "main": "index.mjs",
6
6
  "dependencies": {