@xuda.io/account_module 1.2.1257 → 1.2.1258
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.mjs +576 -614
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -1614,90 +1614,6 @@ const get_user_contact = async function (uid, uid_query) {
|
|
|
1614
1614
|
// }
|
|
1615
1615
|
// );
|
|
1616
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
1617
|
// const get_contact_id = async function (uid, contact_uid) {
|
|
1702
1618
|
// contact_ret = await db_module.get_couch_view_raw('xuda_contacts', 'contacts_by_contact_uid', { key: [uid, contact_uid] });
|
|
1703
1619
|
// return contact_ret?.rows?.[0]?.id;
|
|
@@ -2203,76 +2119,6 @@ export const get_account_ai_usage = async function (req, job_id, headers) {
|
|
|
2203
2119
|
}
|
|
2204
2120
|
};
|
|
2205
2121
|
|
|
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
2122
|
setTimeout(async () => {
|
|
2277
2123
|
const app_id = 'prj712ffdf5aa8adce6cedef988f9c12392'; //'prj3937cb6f9a31c8c7dea25055bba845b1'; //
|
|
2278
2124
|
const uid = 'd39126e0e2c51ffbd1aad10709fc8335';
|
|
@@ -2289,171 +2135,106 @@ setTimeout(async () => {
|
|
|
2289
2135
|
console.log(ret);
|
|
2290
2136
|
}, 1000);
|
|
2291
2137
|
|
|
2292
|
-
|
|
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'] };
|
|
2138
|
+
//////// CONTACTS //////////////
|
|
2295
2139
|
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2140
|
+
export const add_contact = async function (req, job_id, headers) {
|
|
2141
|
+
try {
|
|
2142
|
+
const { uid, email, name, stat, contact_uid, context, source, team_req_id, metadata = {} } = req;
|
|
2143
|
+
const account_profile_info = await get_active_account_profile_info(uid);
|
|
2299
2144
|
|
|
2300
|
-
|
|
2145
|
+
const contact_ret = await db_module.get_couch_view_raw('xuda_contacts', 'contacts_by_user_email_address', { key: [account_profile_info.uid, email] });
|
|
2301
2146
|
|
|
2302
|
-
|
|
2303
|
-
|
|
2147
|
+
if (contact_ret.rows.length) {
|
|
2148
|
+
throw new Error('contact already exist active or deleted');
|
|
2149
|
+
}
|
|
2304
2150
|
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2151
|
+
let is_spam = false;
|
|
2152
|
+
let profile_avatar, profile_avatar_obj;
|
|
2153
|
+
let account_info_ret;
|
|
2154
|
+
if (contact_uid) {
|
|
2155
|
+
account_info_ret = await get_account_name({ uid_query: contact_uid });
|
|
2156
|
+
if (account_info_ret.code < 0) {
|
|
2157
|
+
throw new Error(`account ${contact_uid} not found`);
|
|
2158
|
+
}
|
|
2159
|
+
} else {
|
|
2160
|
+
// contact without use account
|
|
2161
|
+
if (metadata.subject && !metadata.is_sent) {
|
|
2162
|
+
is_spam_ret = await ai_module.is_spam_email(uid, email, metadata.subject);
|
|
2163
|
+
try {
|
|
2164
|
+
if (is_spam_ret.code > -1) {
|
|
2165
|
+
const data = JSON.parse(is_spam_ret.data);
|
|
2166
|
+
is_spam = data.is_spam;
|
|
2167
|
+
}
|
|
2168
|
+
} catch (error) {}
|
|
2169
|
+
}
|
|
2309
2170
|
|
|
2310
|
-
|
|
2171
|
+
const existing_contacts_ret = await db_module.get_couch_view_raw('xuda_contacts', 'contacts_by_email_address', { key: email });
|
|
2172
|
+
for (const contact of existing_contacts_ret.rows) {
|
|
2173
|
+
if (contact.value.profile_avatar) {
|
|
2174
|
+
profile_avatar = contact.value.profile_avatar;
|
|
2175
|
+
profile_avatar_obj == contact.value.profile_avatar_obj;
|
|
2176
|
+
}
|
|
2177
|
+
}
|
|
2178
|
+
}
|
|
2179
|
+
const d = Date.now();
|
|
2180
|
+
const doc = {
|
|
2181
|
+
_id: await _common.xuda_get_uuid('contact'),
|
|
2182
|
+
email: [email],
|
|
2183
|
+
name,
|
|
2184
|
+
stat: stat || 1,
|
|
2185
|
+
date_created: d,
|
|
2186
|
+
ts: d,
|
|
2187
|
+
docType: 'contact',
|
|
2188
|
+
contact_uid,
|
|
2189
|
+
uid: account_profile_info.uid,
|
|
2190
|
+
uid_created: uid,
|
|
2191
|
+
context,
|
|
2192
|
+
headers,
|
|
2193
|
+
source,
|
|
2194
|
+
team_req_id,
|
|
2195
|
+
metadata,
|
|
2196
|
+
is_spam,
|
|
2197
|
+
profile_avatar,
|
|
2198
|
+
profile_avatar_obj,
|
|
2199
|
+
account_profile_info,
|
|
2200
|
+
};
|
|
2201
|
+
const ret = await db_module.save_couch_doc('xuda_contacts', doc);
|
|
2202
|
+
|
|
2203
|
+
if (!account_info_ret?.data?.profile_avatar) {
|
|
2204
|
+
const app_id = await get_account_default_project_id(uid);
|
|
2205
|
+
const ai_module = await import(`${module_path}/ai_module/index.mjs`);
|
|
2206
|
+
if (account_info_ret?.data?.profile_avatar) {
|
|
2207
|
+
// account_info_ret.data.profile_avatar_stat = 2;
|
|
2208
|
+
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);
|
|
2209
|
+
} else {
|
|
2210
|
+
if (!doc.profile_avatar) {
|
|
2211
|
+
// create factual avatar
|
|
2212
|
+
if (!is_spam) {
|
|
2213
|
+
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);
|
|
2214
|
+
}
|
|
2215
|
+
}
|
|
2216
|
+
}
|
|
2217
|
+
}
|
|
2218
|
+
return ret;
|
|
2219
|
+
} catch (err) {
|
|
2220
|
+
return { code: -1, data: err.message };
|
|
2311
2221
|
}
|
|
2312
|
-
return docs;
|
|
2313
2222
|
};
|
|
2314
2223
|
|
|
2315
|
-
export const
|
|
2316
|
-
const
|
|
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;
|
|
2322
|
-
|
|
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
|
-
}
|
|
2332
|
-
|
|
2333
|
-
const get_pattern = async function (doc) {
|
|
2334
|
-
let ret = `default-pattern.png`;
|
|
2335
|
-
|
|
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;
|
|
2342
|
-
} 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;
|
|
2355
|
-
|
|
2356
|
-
default:
|
|
2357
|
-
ret = `xu-pattern.png`;
|
|
2358
|
-
break;
|
|
2359
|
-
}
|
|
2360
|
-
}
|
|
2361
|
-
|
|
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);
|
|
2399
|
-
}
|
|
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;
|
|
2440
|
-
}
|
|
2441
|
-
|
|
2442
|
-
return doc;
|
|
2443
|
-
};
|
|
2444
|
-
|
|
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;
|
|
2224
|
+
export const get_contacts = async function (req) {
|
|
2225
|
+
const { _id, uid, name, limit, skip, bookmark, search, filter_type = 'all', contact_id } = req;
|
|
2447
2226
|
|
|
2227
|
+
const account_profile_info = await get_active_account_profile_info(uid);
|
|
2448
2228
|
if (_id) {
|
|
2449
|
-
let data = await db_module.get_couch_doc('
|
|
2229
|
+
let data = await db_module.get_couch_doc('xuda_contacts', _id);
|
|
2450
2230
|
return data;
|
|
2451
2231
|
}
|
|
2452
2232
|
|
|
2233
|
+
let { with_requests } = req;
|
|
2453
2234
|
var opt = {
|
|
2454
2235
|
selector: {
|
|
2455
|
-
docType: '
|
|
2456
|
-
uid,
|
|
2236
|
+
docType: 'contact',
|
|
2237
|
+
uid: account_profile_info.uid,
|
|
2457
2238
|
stat: { $lt: 4 },
|
|
2458
2239
|
},
|
|
2459
2240
|
limit: limit || 9999,
|
|
@@ -2471,7 +2252,18 @@ export const get_account_profiles = async function (req) {
|
|
|
2471
2252
|
break;
|
|
2472
2253
|
}
|
|
2473
2254
|
|
|
2255
|
+
case 'pinned': {
|
|
2256
|
+
opt.selector.pinned = true;
|
|
2257
|
+
break;
|
|
2258
|
+
}
|
|
2259
|
+
|
|
2260
|
+
case 'online': {
|
|
2261
|
+
opt.limit = 9999;
|
|
2262
|
+
break;
|
|
2263
|
+
}
|
|
2264
|
+
|
|
2474
2265
|
default:
|
|
2266
|
+
with_requests = true;
|
|
2475
2267
|
break;
|
|
2476
2268
|
}
|
|
2477
2269
|
|
|
@@ -2479,23 +2271,23 @@ export const get_account_profiles = async function (req) {
|
|
|
2479
2271
|
opt.selector._id = _id;
|
|
2480
2272
|
}
|
|
2481
2273
|
|
|
2482
|
-
if (
|
|
2483
|
-
opt.selector._id =
|
|
2274
|
+
if (contact_id) {
|
|
2275
|
+
opt.selector._id = contact_id;
|
|
2484
2276
|
}
|
|
2485
2277
|
|
|
2486
|
-
if (typeof
|
|
2487
|
-
opt.selector.name = { $regex: `(?i)${
|
|
2278
|
+
if (typeof name !== 'undefined') {
|
|
2279
|
+
opt.selector.name = { $regex: `(?i)${name}` };
|
|
2488
2280
|
}
|
|
2489
2281
|
|
|
2490
2282
|
if (typeof search !== 'undefined') {
|
|
2491
2283
|
opt.selector['$or'] = [
|
|
2492
2284
|
{
|
|
2493
|
-
|
|
2285
|
+
name: {
|
|
2494
2286
|
$regex: `(?i)${search}`,
|
|
2495
2287
|
},
|
|
2496
2288
|
},
|
|
2497
2289
|
{
|
|
2498
|
-
|
|
2290
|
+
email: {
|
|
2499
2291
|
$regex: `(?i)${search}`,
|
|
2500
2292
|
},
|
|
2501
2293
|
},
|
|
@@ -2506,39 +2298,58 @@ export const get_account_profiles = async function (req) {
|
|
|
2506
2298
|
opt.bookmark = bookmark;
|
|
2507
2299
|
}
|
|
2508
2300
|
|
|
2509
|
-
let
|
|
2301
|
+
let contacts = { docs: [], total_docs: 0 };
|
|
2510
2302
|
|
|
2511
2303
|
if (filter_type !== 'pending') {
|
|
2512
|
-
|
|
2513
|
-
if (!limit ||
|
|
2514
|
-
|
|
2304
|
+
contacts = await db_module.find_couch_query('xuda_contacts', opt);
|
|
2305
|
+
if (!limit || contact_id) {
|
|
2306
|
+
contacts.total_docs = contacts.docs.length;
|
|
2515
2307
|
} else {
|
|
2516
2308
|
delete opt.sort;
|
|
2517
2309
|
delete opt.skip;
|
|
2518
2310
|
opt.limit = 9999;
|
|
2519
2311
|
opt.fields = ['_id'];
|
|
2520
2312
|
|
|
2521
|
-
const counter = await db_module.find_couch_query('
|
|
2522
|
-
|
|
2313
|
+
const counter = await db_module.find_couch_query('xuda_contacts', opt);
|
|
2314
|
+
contacts.total_docs = counter.docs.length;
|
|
2523
2315
|
}
|
|
2524
2316
|
}
|
|
2525
2317
|
|
|
2318
|
+
let requests_to = { docs: [], total_docs: 0 };
|
|
2319
|
+
let requests_from = { docs: [], total_docs: 0 };
|
|
2526
2320
|
let shared_from = { docs: [], total_docs: 0 };
|
|
2527
2321
|
|
|
2528
|
-
let
|
|
2529
|
-
for await (let doc of
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2322
|
+
let contact_docs = { docs: [], total_docs: contacts.total_docs };
|
|
2323
|
+
for await (let doc of contacts.docs) {
|
|
2324
|
+
const ret_to = requests_to.docs.find((e) => {
|
|
2325
|
+
return e.contact_uid === doc.contact_uid;
|
|
2326
|
+
});
|
|
2327
|
+
if (ret_to) continue;
|
|
2328
|
+
|
|
2329
|
+
const ret_from = requests_from.docs.find((e) => {
|
|
2330
|
+
return e.contact_uid === doc.contact_uid;
|
|
2331
|
+
});
|
|
2332
|
+
if (ret_from) continue;
|
|
2333
|
+
|
|
2334
|
+
doc = await get_contact_info(uid, doc);
|
|
2335
|
+
|
|
2336
|
+
if (filter_type === 'online') {
|
|
2337
|
+
if (!doc.online) continue;
|
|
2535
2338
|
}
|
|
2339
|
+
|
|
2340
|
+
contact_docs.docs.push(doc);
|
|
2536
2341
|
}
|
|
2537
2342
|
|
|
2538
|
-
if (!
|
|
2343
|
+
if (!contact_id && with_requests) {
|
|
2539
2344
|
if (filter_type === 'pending') {
|
|
2345
|
+
////// TO - OUT
|
|
2346
|
+
requests_to.docs = await get_pending_contact_out(uid);
|
|
2347
|
+
requests_to.total_docs = requests_to.docs.length;
|
|
2348
|
+
|
|
2540
2349
|
////// FROM - IN
|
|
2541
|
-
|
|
2350
|
+
requests_from.docs = await get_pending_contact_in(uid);
|
|
2351
|
+
requests_from.total_docs = requests_from.docs.length;
|
|
2352
|
+
shared_from.docs = await get_pending_share_contact_in(uid);
|
|
2542
2353
|
shared_from.total_docs = shared_from.docs.length;
|
|
2543
2354
|
}
|
|
2544
2355
|
}
|
|
@@ -2546,34 +2357,31 @@ export const get_account_profiles = async function (req) {
|
|
|
2546
2357
|
return {
|
|
2547
2358
|
code: 1,
|
|
2548
2359
|
data: {
|
|
2549
|
-
docs: [...shared_from.docs, ...
|
|
2550
|
-
total_docs: shared_from.total_docs +
|
|
2360
|
+
docs: [...shared_from.docs, ...requests_from.docs, ...requests_to.docs, ...contact_docs.docs],
|
|
2361
|
+
total_docs: shared_from.total_docs + requests_from.total_docs + requests_to.total_docs + contact_docs.total_docs,
|
|
2551
2362
|
},
|
|
2552
2363
|
};
|
|
2553
2364
|
};
|
|
2554
2365
|
|
|
2555
|
-
export const
|
|
2556
|
-
const {
|
|
2366
|
+
export const archive_contact = async function (req) {
|
|
2367
|
+
const { contact_id } = req;
|
|
2557
2368
|
try {
|
|
2558
2369
|
const team_module = await import(`${module_path}/team_module/index.mjs`);
|
|
2559
|
-
await team_module.validate_share_exist(uid, '
|
|
2370
|
+
await team_module.validate_share_exist(uid, 'contact', contact_id);
|
|
2560
2371
|
|
|
2561
|
-
var
|
|
2372
|
+
var contact_doc = await db_module.get_couch_doc_native('xuda_contacts', contact_id);
|
|
2562
2373
|
|
|
2563
|
-
if (
|
|
2564
|
-
throw new Error('
|
|
2374
|
+
if (contact_doc.uid !== uid && contact_doc?.account_profile_info?.uid !== uid) {
|
|
2375
|
+
throw new Error('Operation not allowed');
|
|
2565
2376
|
}
|
|
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
2377
|
|
|
2571
|
-
|
|
2378
|
+
contact_doc.stat = 5;
|
|
2379
|
+
contact_doc.stat_ts = Date.now();
|
|
2380
|
+
contact_doc.stat_reason = 'archived by the user';
|
|
2572
2381
|
|
|
2573
|
-
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
// }
|
|
2382
|
+
const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
|
|
2383
|
+
|
|
2384
|
+
return contact_save_ret;
|
|
2577
2385
|
} catch (err) {
|
|
2578
2386
|
return {
|
|
2579
2387
|
code: -22,
|
|
@@ -2582,283 +2390,21 @@ export const archive_account_profile = async function (req) {
|
|
|
2582
2390
|
}
|
|
2583
2391
|
};
|
|
2584
2392
|
|
|
2585
|
-
export const
|
|
2586
|
-
const {
|
|
2393
|
+
export const delete_contact = async function (req, job_id, headers) {
|
|
2394
|
+
const { contact_id } = req;
|
|
2587
2395
|
try {
|
|
2588
|
-
var
|
|
2589
|
-
if (
|
|
2396
|
+
var contact_doc = await db_module.get_couch_doc_native('xuda_contacts', contact_id);
|
|
2397
|
+
if (contact_doc.stat !== 5) {
|
|
2590
2398
|
throw new Error('A document can only be deleted after it has been archived');
|
|
2591
2399
|
}
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2400
|
+
contact_doc.stat = 4;
|
|
2401
|
+
contact_doc.stat_ts = Date.now();
|
|
2402
|
+
contact_doc.stat_reason = 'deleted by the user';
|
|
2595
2403
|
|
|
2596
|
-
const
|
|
2404
|
+
const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
|
|
2597
2405
|
const ai_module = await import(`${module_path}/ai_module/index.mjs`);
|
|
2598
|
-
ai_module.delete_depended_chats(uid,
|
|
2599
|
-
return
|
|
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;
|
|
2406
|
+
ai_module.delete_depended_chats(uid, contact_id);
|
|
2407
|
+
return contact_save_ret;
|
|
2862
2408
|
} catch (err) {
|
|
2863
2409
|
return {
|
|
2864
2410
|
code: -22,
|
|
@@ -2991,3 +2537,419 @@ export const unpin_contact = async function (req) {
|
|
|
2991
2537
|
};
|
|
2992
2538
|
}
|
|
2993
2539
|
};
|
|
2540
|
+
|
|
2541
|
+
//////// PROFILES //////////////
|
|
2542
|
+
|
|
2543
|
+
export const get_pending_share_profile_in = async function (uid, _id) {
|
|
2544
|
+
let docs = [];
|
|
2545
|
+
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'] };
|
|
2546
|
+
|
|
2547
|
+
if (_id) {
|
|
2548
|
+
to_opt.selector._id = _id;
|
|
2549
|
+
}
|
|
2550
|
+
|
|
2551
|
+
const requests_from_res = await db_module.find_couch_query('xuda_team', from_opt);
|
|
2552
|
+
|
|
2553
|
+
for await (let e of requests_from_res.docs) {
|
|
2554
|
+
let doc = await db_module.get_couch_doc_native('xuda_accounts', e.share_item_id);
|
|
2555
|
+
|
|
2556
|
+
doc.shared_from_uid = e.team_req_from_uid;
|
|
2557
|
+
doc.pending = true;
|
|
2558
|
+
doc = await get_account_profile_info(uid, doc);
|
|
2559
|
+
doc.team_req_id = e._id;
|
|
2560
|
+
|
|
2561
|
+
docs.push(doc);
|
|
2562
|
+
}
|
|
2563
|
+
return docs;
|
|
2564
|
+
};
|
|
2565
|
+
|
|
2566
|
+
export const get_account_profile_info = async function (uid, contact_profile_doc, _id) {
|
|
2567
|
+
const ai_module = await import(`${module_path}/ai_module/index.mjs`);
|
|
2568
|
+
let doc = contact_profile_doc;
|
|
2569
|
+
if (_id) {
|
|
2570
|
+
doc = await db_module.get_couch_doc_native('xuda_accounts', _id);
|
|
2571
|
+
}
|
|
2572
|
+
doc.notifications = 0;
|
|
2573
|
+
|
|
2574
|
+
let uid_query = uid;
|
|
2575
|
+
if (doc.shared_from_uid) uid_query = doc.shared_from_uid;
|
|
2576
|
+
const account_info_ret = await get_account_name({ uid_query });
|
|
2577
|
+
if (account_info_ret.code < 0) {
|
|
2578
|
+
return;
|
|
2579
|
+
}
|
|
2580
|
+
if (!doc.profile_avatar) {
|
|
2581
|
+
doc.profile_avatar = account_info_ret.data.profile_avatar;
|
|
2582
|
+
}
|
|
2583
|
+
|
|
2584
|
+
const get_pattern = async function (doc) {
|
|
2585
|
+
let ret = `default-pattern.png`;
|
|
2586
|
+
|
|
2587
|
+
if (doc.shared_from_uid) {
|
|
2588
|
+
// const account_profile_doc = await db_module.get_couch_doc_native('xuda_accounts', doc.share_item_id);
|
|
2589
|
+
const shared_from_uid_ret = await get_account_name({ uid_query: doc.shared_from_uid });
|
|
2590
|
+
ret = shared_from_uid_ret.data.profile_avatar;
|
|
2591
|
+
} else if (doc.account_type === 'business') {
|
|
2592
|
+
ret = doc.profile_picture;
|
|
2593
|
+
} else {
|
|
2594
|
+
switch (doc.avatar_source) {
|
|
2595
|
+
case 'fictional':
|
|
2596
|
+
ret = `fictional-avatar-pattern.png`;
|
|
2597
|
+
break;
|
|
2598
|
+
|
|
2599
|
+
case 'ai profile':
|
|
2600
|
+
ret = `fictional-avatar-pattern.png`;
|
|
2601
|
+
break;
|
|
2602
|
+
|
|
2603
|
+
case 'authentic profile':
|
|
2604
|
+
ret = `authentic-avatar-pattern.png`;
|
|
2605
|
+
break;
|
|
2606
|
+
|
|
2607
|
+
default:
|
|
2608
|
+
ret = `xu-pattern.png`;
|
|
2609
|
+
break;
|
|
2610
|
+
}
|
|
2611
|
+
}
|
|
2612
|
+
|
|
2613
|
+
return ret;
|
|
2614
|
+
};
|
|
2615
|
+
|
|
2616
|
+
const get_online = async () => {
|
|
2617
|
+
let online = false;
|
|
2618
|
+
let selector = { docType: 'team_request', team_req_stat: 3, share_item_id: doc._id, access_type: 'account_profile', team_req_from_uid: uid };
|
|
2619
|
+
let ret = await db_module.find_couch_query('xuda_team', { selector });
|
|
2620
|
+
for (const req_obj of ret.docs) {
|
|
2621
|
+
const account_info_ret = await get_account_name({ uid_query: req_obj.team_req_to_uid });
|
|
2622
|
+
online = account_info_ret.data.online;
|
|
2623
|
+
if (online) break;
|
|
2624
|
+
}
|
|
2625
|
+
return online;
|
|
2626
|
+
};
|
|
2627
|
+
|
|
2628
|
+
const stringToColour = (str) => {
|
|
2629
|
+
let hash = 0;
|
|
2630
|
+
// Generate a numeric hash from the string
|
|
2631
|
+
for (let i = 0; i < str.length; i++) {
|
|
2632
|
+
hash = str.charCodeAt(i) + ((hash << 5) - hash);
|
|
2633
|
+
}
|
|
2634
|
+
let colour = '#';
|
|
2635
|
+
// Convert the hash into a 6-digit hex color code
|
|
2636
|
+
for (let i = 0; i < 3; i++) {
|
|
2637
|
+
const value = (hash >> (i * 8)) & 0xff;
|
|
2638
|
+
colour += ('00' + value.toString(16)).substr(-2);
|
|
2639
|
+
}
|
|
2640
|
+
return colour;
|
|
2641
|
+
};
|
|
2642
|
+
const get_members = async () => {
|
|
2643
|
+
let members = [];
|
|
2644
|
+
let selector = { docType: 'team_request', team_req_stat: 3, share_item_id: doc._id, access_type: 'account_profile', team_req_from_uid: uid };
|
|
2645
|
+
let ret = await db_module.find_couch_query('xuda_team', { selector });
|
|
2646
|
+
for (const req_obj of ret.docs) {
|
|
2647
|
+
const account_info_ret = await get_account_name({ uid_query: req_obj.team_req_to_uid });
|
|
2648
|
+
const info = await await get_contact_info(uid, account_info_ret.data);
|
|
2649
|
+
members.push(info);
|
|
2650
|
+
}
|
|
2651
|
+
return members;
|
|
2652
|
+
};
|
|
2653
|
+
|
|
2654
|
+
const get_user_account_profiles = async () => {
|
|
2655
|
+
const account_profile_info = await get_active_account_profile_info(uid);
|
|
2656
|
+
let selector = { docType: 'account_profile', stat: 3, uid: uid };
|
|
2657
|
+
let ret = await db_module.find_couch_query('xuda_accounts', { selector });
|
|
2658
|
+
|
|
2659
|
+
return ret.docs;
|
|
2660
|
+
};
|
|
2661
|
+
|
|
2662
|
+
// Usage examples:
|
|
2663
|
+
console.log(stringToColour('User A')); // -> Consistent color for "User A"
|
|
2664
|
+
console.log(stringToColour('Project Z')); // -> Consistent color for "Project Z"
|
|
2665
|
+
|
|
2666
|
+
doc.avatar_source = account_info_ret.data.avatar_source;
|
|
2667
|
+
doc.icon_pattern = await get_pattern(doc);
|
|
2668
|
+
doc.name = doc.profile_name;
|
|
2669
|
+
doc.interactions = 0;
|
|
2670
|
+
doc.chats = 0;
|
|
2671
|
+
|
|
2672
|
+
doc.online = await get_online();
|
|
2673
|
+
|
|
2674
|
+
// doc.border = account_info_ret.data.active_account_profile_id === doc._id ? 'yellow' : ''; //'#1a3557';
|
|
2675
|
+
// doc.border = stringToColour(doc.shared_from_uid || doc.uid);
|
|
2676
|
+
|
|
2677
|
+
const chats_ret = await ai_module.get_ai_chats({ uid, reference_id: doc._id });
|
|
2678
|
+
|
|
2679
|
+
doc.card_background = `linear-gradient(145deg, #242480 0%, #060619 100%)`;
|
|
2680
|
+
// doc.border = get_contact_border(doc);
|
|
2681
|
+
|
|
2682
|
+
doc.members = await get_members();
|
|
2683
|
+
const user_account_profiles = await get_user_account_profiles();
|
|
2684
|
+
debugger;
|
|
2685
|
+
const usage_ret = await get_account_ai_usage({ uid });
|
|
2686
|
+
doc.max_credits = usage_ret.data.credits.total / user_account_profiles.length;
|
|
2687
|
+
doc.used_credits = usage_ret.data.usage[doc._id] || 0;
|
|
2688
|
+
|
|
2689
|
+
if (doc.shared_from_uid) {
|
|
2690
|
+
doc.max_credits = doc.max_credits / doc.members.length;
|
|
2691
|
+
}
|
|
2692
|
+
|
|
2693
|
+
return doc;
|
|
2694
|
+
};
|
|
2695
|
+
|
|
2696
|
+
export const get_account_profiles = async function (req) {
|
|
2697
|
+
const { _id, uid, profile_name, limit, skip, bookmark, search, filter_type = 'all', profile_id } = req;
|
|
2698
|
+
|
|
2699
|
+
if (_id) {
|
|
2700
|
+
let data = await db_module.get_couch_doc('xuda_accounts', _id);
|
|
2701
|
+
return data;
|
|
2702
|
+
}
|
|
2703
|
+
|
|
2704
|
+
var opt = {
|
|
2705
|
+
selector: {
|
|
2706
|
+
docType: 'account_profile',
|
|
2707
|
+
uid,
|
|
2708
|
+
stat: { $lt: 4 },
|
|
2709
|
+
},
|
|
2710
|
+
limit: limit || 9999,
|
|
2711
|
+
skip: skip || 0,
|
|
2712
|
+
sort: [{ ts: 'desc' }],
|
|
2713
|
+
};
|
|
2714
|
+
|
|
2715
|
+
switch (filter_type) {
|
|
2716
|
+
case 'archived': {
|
|
2717
|
+
opt.selector.stat = 5;
|
|
2718
|
+
break;
|
|
2719
|
+
}
|
|
2720
|
+
case 'shared': {
|
|
2721
|
+
opt.selector.shared_from_uid = { $gt: null };
|
|
2722
|
+
break;
|
|
2723
|
+
}
|
|
2724
|
+
|
|
2725
|
+
default:
|
|
2726
|
+
break;
|
|
2727
|
+
}
|
|
2728
|
+
|
|
2729
|
+
if (_id) {
|
|
2730
|
+
opt.selector._id = _id;
|
|
2731
|
+
}
|
|
2732
|
+
|
|
2733
|
+
if (profile_id) {
|
|
2734
|
+
opt.selector._id = profile_id;
|
|
2735
|
+
}
|
|
2736
|
+
|
|
2737
|
+
if (typeof profile_name !== 'undefined') {
|
|
2738
|
+
opt.selector.name = { $regex: `(?i)${profile_name}` };
|
|
2739
|
+
}
|
|
2740
|
+
|
|
2741
|
+
if (typeof search !== 'undefined') {
|
|
2742
|
+
opt.selector['$or'] = [
|
|
2743
|
+
{
|
|
2744
|
+
profile_name: {
|
|
2745
|
+
$regex: `(?i)${search}`,
|
|
2746
|
+
},
|
|
2747
|
+
},
|
|
2748
|
+
{
|
|
2749
|
+
profile_email: {
|
|
2750
|
+
$regex: `(?i)${search}`,
|
|
2751
|
+
},
|
|
2752
|
+
},
|
|
2753
|
+
];
|
|
2754
|
+
}
|
|
2755
|
+
|
|
2756
|
+
if (bookmark && bookmark !== 'nil') {
|
|
2757
|
+
opt.bookmark = bookmark;
|
|
2758
|
+
}
|
|
2759
|
+
|
|
2760
|
+
let profiles = { docs: [], total_docs: 0 };
|
|
2761
|
+
|
|
2762
|
+
if (filter_type !== 'pending') {
|
|
2763
|
+
profiles = await db_module.find_couch_query('xuda_accounts', opt);
|
|
2764
|
+
if (!limit || profile_id) {
|
|
2765
|
+
profiles.total_docs = profiles.docs.length;
|
|
2766
|
+
} else {
|
|
2767
|
+
delete opt.sort;
|
|
2768
|
+
delete opt.skip;
|
|
2769
|
+
opt.limit = 9999;
|
|
2770
|
+
opt.fields = ['_id'];
|
|
2771
|
+
|
|
2772
|
+
const counter = await db_module.find_couch_query('xuda_accounts', opt);
|
|
2773
|
+
profiles.total_docs = counter.docs.length;
|
|
2774
|
+
}
|
|
2775
|
+
}
|
|
2776
|
+
|
|
2777
|
+
let shared_from = { docs: [], total_docs: 0 };
|
|
2778
|
+
|
|
2779
|
+
let profile_docs = { docs: [], total_docs: profiles.total_docs };
|
|
2780
|
+
for await (let doc of profiles.docs) {
|
|
2781
|
+
doc = await get_account_profile_info(uid, doc);
|
|
2782
|
+
if (doc.main) {
|
|
2783
|
+
profile_docs.docs.unshift(doc);
|
|
2784
|
+
} else {
|
|
2785
|
+
profile_docs.docs.push(doc);
|
|
2786
|
+
}
|
|
2787
|
+
}
|
|
2788
|
+
|
|
2789
|
+
if (!profile_id) {
|
|
2790
|
+
if (filter_type === 'pending') {
|
|
2791
|
+
////// FROM - IN
|
|
2792
|
+
shared_from.docs = await get_pending_share_profile_in(uid);
|
|
2793
|
+
shared_from.total_docs = shared_from.docs.length;
|
|
2794
|
+
}
|
|
2795
|
+
}
|
|
2796
|
+
|
|
2797
|
+
return {
|
|
2798
|
+
code: 1,
|
|
2799
|
+
data: {
|
|
2800
|
+
docs: [...shared_from.docs, ...profile_docs.docs],
|
|
2801
|
+
total_docs: shared_from.total_docs + profile_docs.total_docs,
|
|
2802
|
+
},
|
|
2803
|
+
};
|
|
2804
|
+
};
|
|
2805
|
+
|
|
2806
|
+
export const archive_account_profile = async function (req) {
|
|
2807
|
+
const { uid, profile_id } = req;
|
|
2808
|
+
try {
|
|
2809
|
+
const team_module = await import(`${module_path}/team_module/index.mjs`);
|
|
2810
|
+
await team_module.validate_share_exist(uid, 'account_profile', profile_id);
|
|
2811
|
+
|
|
2812
|
+
var account_profile_doc = await db_module.get_couch_doc_native('xuda_accounts', profile_id);
|
|
2813
|
+
|
|
2814
|
+
if (account_profile_doc.main) {
|
|
2815
|
+
throw new Error('reserve');
|
|
2816
|
+
}
|
|
2817
|
+
// if (!account_profile_doc.team_req_id) {
|
|
2818
|
+
account_profile_doc.stat = 5;
|
|
2819
|
+
account_profile_doc.stat_ts = Date.now();
|
|
2820
|
+
account_profile_doc.stat_reason = 'archived by the user';
|
|
2821
|
+
|
|
2822
|
+
const account_profile_save_ret = await db_module.save_couch_doc('xuda_accounts', account_profile_doc);
|
|
2823
|
+
|
|
2824
|
+
return account_profile_save_ret;
|
|
2825
|
+
// } else {
|
|
2826
|
+
// return await team_module.change_account_profile_dependencies_status(account_profile_doc.team_req_id, 5);
|
|
2827
|
+
// }
|
|
2828
|
+
} catch (err) {
|
|
2829
|
+
return {
|
|
2830
|
+
code: -22,
|
|
2831
|
+
data: err.message,
|
|
2832
|
+
};
|
|
2833
|
+
}
|
|
2834
|
+
};
|
|
2835
|
+
|
|
2836
|
+
export const delete_account_profile = async function (req, job_id, headers) {
|
|
2837
|
+
const { profile_id } = req;
|
|
2838
|
+
try {
|
|
2839
|
+
var account_profile_doc = await db_module.get_couch_doc_native('xuda_accounts', profile_id);
|
|
2840
|
+
if (account_profile_doc.stat !== 5) {
|
|
2841
|
+
throw new Error('A document can only be deleted after it has been archived');
|
|
2842
|
+
}
|
|
2843
|
+
account_profile_doc.stat = 4;
|
|
2844
|
+
account_profile_doc.stat_ts = Date.now();
|
|
2845
|
+
account_profile_doc.stat_reason = 'deleted by the user';
|
|
2846
|
+
|
|
2847
|
+
const account_profile_save_ret = await db_module.save_couch_doc('xuda_accounts', account_profile_doc);
|
|
2848
|
+
const ai_module = await import(`${module_path}/ai_module/index.mjs`);
|
|
2849
|
+
ai_module.delete_depended_chats(uid, profile_id);
|
|
2850
|
+
return account_profile_save_ret;
|
|
2851
|
+
} catch (err) {
|
|
2852
|
+
return {
|
|
2853
|
+
code: -22,
|
|
2854
|
+
data: err.message,
|
|
2855
|
+
};
|
|
2856
|
+
}
|
|
2857
|
+
};
|
|
2858
|
+
|
|
2859
|
+
export const unarchive_account_profile = async function (req) {
|
|
2860
|
+
const { profile_id } = req;
|
|
2861
|
+
try {
|
|
2862
|
+
const team_module = await import(`${module_path}/team_module/index.mjs`);
|
|
2863
|
+
var account_profile_doc = await db_module.get_couch_doc_native('xuda_accounts', profile_id);
|
|
2864
|
+
|
|
2865
|
+
if (account_profile_doc.stat !== 5) {
|
|
2866
|
+
throw new Error('account profile is not archived');
|
|
2867
|
+
}
|
|
2868
|
+
if (!account_profile_doc.team_req_id) {
|
|
2869
|
+
account_profile_doc.stat = 3;
|
|
2870
|
+
account_profile_doc.stat_ts = Date.now();
|
|
2871
|
+
account_profile_doc.stat_reason = 'archived by the user';
|
|
2872
|
+
|
|
2873
|
+
const account_profile_save_ret = await db_module.save_couch_doc('xuda_accounts', account_profile_doc);
|
|
2874
|
+
|
|
2875
|
+
return account_profile_save_ret;
|
|
2876
|
+
} else {
|
|
2877
|
+
return await team_module.change_account_profile_dependencies_status(account_profile_doc.team_req_id, 3);
|
|
2878
|
+
}
|
|
2879
|
+
} catch (err) {
|
|
2880
|
+
return {
|
|
2881
|
+
code: -22,
|
|
2882
|
+
data: err.message,
|
|
2883
|
+
};
|
|
2884
|
+
}
|
|
2885
|
+
};
|
|
2886
|
+
|
|
2887
|
+
export const create_account_profile = async function (req, job_id, headers) {
|
|
2888
|
+
const { uid, profile_name, profile_signature, profile_email, profile_picture, profile_avatar, profile_picture_obj, profile_avatar_obj, main } = req;
|
|
2889
|
+
try {
|
|
2890
|
+
// const app_id = await get_account_default_project_id(uid);
|
|
2891
|
+
const { data: acc_obj } = await db_module.get_couch_doc('xuda_accounts', uid);
|
|
2892
|
+
|
|
2893
|
+
const d = Date.now();
|
|
2894
|
+
const doc = {
|
|
2895
|
+
_id: await _common.xuda_get_uuid('account_profile'),
|
|
2896
|
+
stat: 3,
|
|
2897
|
+
date_created_ts: d,
|
|
2898
|
+
ts: d,
|
|
2899
|
+
docType: 'account_profile',
|
|
2900
|
+
|
|
2901
|
+
uid,
|
|
2902
|
+
profile_name, //: profile_name || acc_obj.account_info.business_name || acc_obj.account_info.name,
|
|
2903
|
+
profile_signature,
|
|
2904
|
+
profile_email,
|
|
2905
|
+
profile_picture,
|
|
2906
|
+
profile_avatar,
|
|
2907
|
+
profile_picture_obj,
|
|
2908
|
+
profile_avatar_obj,
|
|
2909
|
+
main,
|
|
2910
|
+
};
|
|
2911
|
+
const save_ret = await db_module.save_couch_doc('xuda_accounts', doc);
|
|
2912
|
+
// acc_obj.active_profile_id = save_ret.data.id;
|
|
2913
|
+
|
|
2914
|
+
// return { code: 55, data: save_ret };
|
|
2915
|
+
return save_ret;
|
|
2916
|
+
} catch (err) {
|
|
2917
|
+
return { code: -55, data: err.message };
|
|
2918
|
+
}
|
|
2919
|
+
};
|
|
2920
|
+
|
|
2921
|
+
export const update_account_profile = async function (req, job_id, headers) {
|
|
2922
|
+
const { uid, _id } = req;
|
|
2923
|
+
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'];
|
|
2924
|
+
try {
|
|
2925
|
+
let { data: account_profile_doc } = await db_module.get_couch_doc('xuda_accounts', _id);
|
|
2926
|
+
|
|
2927
|
+
if (account_profile_doc.docType !== 'account_profile') {
|
|
2928
|
+
throw new Error('not account profile doc');
|
|
2929
|
+
}
|
|
2930
|
+
|
|
2931
|
+
let changes_arr = [];
|
|
2932
|
+
|
|
2933
|
+
for (const key of account_profile_properties) {
|
|
2934
|
+
let val = req[key];
|
|
2935
|
+
if (typeof val === 'undefined') continue;
|
|
2936
|
+
if (account_profile_doc[key] !== val) {
|
|
2937
|
+
changes_arr.push(key);
|
|
2938
|
+
account_profile_doc[key] = val;
|
|
2939
|
+
}
|
|
2940
|
+
}
|
|
2941
|
+
|
|
2942
|
+
if (!changes_arr.length) {
|
|
2943
|
+
// throw new Error('no change');
|
|
2944
|
+
return { code: 56, data: 'no change' };
|
|
2945
|
+
}
|
|
2946
|
+
|
|
2947
|
+
account_profile_doc.ts = Date.now();
|
|
2948
|
+
|
|
2949
|
+
const save_ret = await db_module.save_couch_doc('xuda_accounts', account_profile_doc);
|
|
2950
|
+
|
|
2951
|
+
return { code: 56, data: save_ret };
|
|
2952
|
+
} catch (err) {
|
|
2953
|
+
return { code: -56, data: err.message };
|
|
2954
|
+
}
|
|
2955
|
+
};
|