@xuda.io/account_module 1.2.522 → 1.2.749

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 +444 -126
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -6,9 +6,9 @@ import { parsePhoneNumber } from 'libphonenumber-js';
6
6
  import { countryCodeEmoji } from 'country-code-emoji';
7
7
 
8
8
  // Convert exec to a promise for cleaner async/await usage
9
- const execAsync = util.promisify(exec);
9
+ // const execAsync = util.promisify(exec);
10
10
 
11
- const account_info_properties = ['account_type', 'company_name', 'bio', 'tel', 'address', 'city', 'state', 'zip', 'country', 'email', 'first_name', 'last_name', 'mainCategory', 'subCategory', 'profile_picture', 'username', 'website'];
11
+ const account_info_properties = ['account_type', 'business_name', 'bio', 'tel', 'address', 'city', 'state', 'zip', 'country', 'email', 'first_name', 'last_name', 'mainCategory', 'subCategory', 'profile_picture', 'username', 'website', 'auto_respond', 'auto_respond_mode', 'auto_respond_agents'];
12
12
 
13
13
  global._conf = (
14
14
  await import(path.join(process.env.XUDA_HOME, process.env.XUDA_CONFIG), {
@@ -31,10 +31,6 @@ export const update_account_info = async function (req, job_id, headers) {
31
31
  const { uid } = req;
32
32
  const data = req;
33
33
 
34
- // delete data.gtp_token;
35
- // delete data.uid;
36
- // delete data.token_ret;
37
- // delete data.job_id;
38
34
  delete data.profile_avatar;
39
35
 
40
36
  function validate_input(string, max_length, is_mandatory, is_pass) {
@@ -60,9 +56,9 @@ export const update_account_info = async function (req, job_id, headers) {
60
56
 
61
57
  await marketplace_module.marketplace_save_user({ uid });
62
58
 
63
- _.forEach(account_info_properties, function (key) {
59
+ for (const key of account_info_properties) {
64
60
  let val = data[key];
65
-
61
+ if (typeof val === 'undefined') continue;
66
62
  if (account_obj.account_info[key] !== val) {
67
63
  change += ' from ' + account_obj.account_info[key] + ' to ' + val;
68
64
  account_info_changes_arr.push(key);
@@ -87,7 +83,7 @@ export const update_account_info = async function (req, job_id, headers) {
87
83
  account_obj.account_info[key] = val;
88
84
 
89
85
  if (account_obj.account_info.account_type === 'business') {
90
- if (key === 'company_name' && validate_input(val, 150, true, false) < 0) {
86
+ if (key === 'business_name' && validate_input(val, 150, true, false) < 0) {
91
87
  error[key] = 'Invalid company';
92
88
  }
93
89
  }
@@ -102,10 +98,10 @@ export const update_account_info = async function (req, job_id, headers) {
102
98
  if (key === 'email' && (!validator.isEmail(val) || !val || val.length < 2)) {
103
99
  error[key] = 'Invalid email';
104
100
  }
105
- if (key === 'tel' && (!/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/.test(val) || !val || val.length < 2)) {
106
- // if (key === "tel" && (!preg_match("/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/", $val) || !$val || count($val) < 2)) {
107
- error[key] = 'Invalid phone number';
108
- }
101
+ // if (key === 'tel' && (!/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/.test(val) || !val || val.length < 2)) {
102
+ // // if (key === "tel" && (!preg_match("/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/", $val) || !$val || count($val) < 2)) {
103
+ // error[key] = 'Invalid phone number';
104
+ // }
109
105
  if (key === 'address' && !/^[a-z0-9- ]+$/i.test(val)) {
110
106
  // if (key === "address" && !preg_match('/^[a-z0-9- ]+$/i', $val)) {
111
107
  error[key] = 'Invalid address';
@@ -123,7 +119,7 @@ export const update_account_info = async function (req, job_id, headers) {
123
119
  error[key] = 'Invalid country';
124
120
  }
125
121
  }
126
- });
122
+ }
127
123
 
128
124
  if (!_.isEmpty(error)) {
129
125
  return { code: -1310, data: error };
@@ -144,19 +140,19 @@ export const update_account_info = async function (req, job_id, headers) {
144
140
  delete account_obj.account_info.job_id;
145
141
  delete account_obj.account_info.app_id;
146
142
 
147
- if (!account_obj.account_project_id) {
148
- const app_module = await import(`${module_path}/app_module/index.mjs`);
143
+ // if (!account_obj.account_project_id) {
144
+ // const app_module = await import(`${module_path}/app_module/index.mjs`);
149
145
 
150
- const ret = await app_module.create_project({ token_ret: req.token_ret, uid, data: { app_name: `Account ${uid} main project`, is_account_project: true, app_plugins_purchased: [' @xuda.io/xuda-dbs-plugin-xuda', '@xuda.io/xuda-framework-plugin-tailwind'] } }, job_id, headers);
151
- if (ret.code > -1) {
152
- account_obj.account_project_id = ret.data;
146
+ // const ret = await app_module.create_project({ token_ret: req.token_ret, uid, data: { app_name: `Account ${uid} main project`, is_account_project: true, app_plugins_purchased: [' @xuda.io/xuda-dbs-plugin-xuda', '@xuda.io/xuda-framework-plugin-tailwind'] } }, job_id, headers);
147
+ // if (ret.code > -1) {
148
+ // account_obj.account_project_id = ret.data;
153
149
 
154
- let drive_req = { app_id: account_obj.account_project_id, uid, path: '/' };
155
- await drive_module.create_drive_folder_user({ ...drive_req, ...{ folder_name: 'Profile Images', is_system: true } }, job_id, headers);
156
- await drive_module.create_drive_folder_user({ ...drive_req, ...{ folder_name: 'Chat Thumbnails', is_system: true } }, job_id, headers);
157
- await drive_module.create_drive_folder_user({ ...drive_req, ...{ folder_name: 'Chat Attachments', is_system: true } }, job_id, headers);
158
- }
159
- }
150
+ // let drive_req = { app_id: account_obj.account_project_id, uid, path: '/' };
151
+ // await drive_module.create_drive_folder_user({ ...drive_req, ...{ folder_name: 'Profile Images', is_system: true } }, job_id, headers);
152
+ // await drive_module.create_drive_folder_user({ ...drive_req, ...{ folder_name: 'Chat Thumbnails', is_system: true } }, job_id, headers);
153
+ // await drive_module.create_drive_folder_user({ ...drive_req, ...{ folder_name: 'Chat Attachments', is_system: true } }, job_id, headers);
154
+ // }
155
+ // }
160
156
 
161
157
  const ai_module = await import(`${module_path}/ai_module/index.mjs`);
162
158
  if (account_obj.account_info?.profile_picture) {
@@ -169,13 +165,13 @@ export const update_account_info = async function (req, job_id, headers) {
169
165
 
170
166
  // temp remove later on
171
167
 
172
- let drive_req = { app_id: account_obj.account_project_id, uid, path: '/' };
173
- await drive_module.create_drive_folder_workspace({ ...drive_req, ...{ folder_name: 'Chat Images' } }, job_id, headers);
174
- await drive_module.create_drive_folder_studio({ ...drive_req, ...{ folder_name: 'Progs Thumbnails', is_system: true } }, job_id, headers);
175
- await drive_module.create_drive_folder_studio({ ...drive_req, ...{ folder_name: 'Screenshots', is_system: true } }, job_id, headers);
176
- await drive_module.create_drive_folder_user({ ...drive_req, ...{ folder_name: 'Profile Images', is_system: true } }, job_id, headers);
177
- await drive_module.create_drive_folder_user({ ...drive_req, ...{ folder_name: 'Chat Thumbnails', is_system: true } }, job_id, headers);
178
- await drive_module.create_drive_folder_user({ ...drive_req, ...{ folder_name: 'Chat Attachments', is_system: true } }, job_id, headers);
168
+ // let drive_req = { app_id: account_obj.account_project_id, uid, path: '/' };
169
+ // await drive_module.create_drive_folder_workspace({ ...drive_req, ...{ folder_name: 'Chat Images' } }, job_id, headers);
170
+ // await drive_module.create_drive_folder_studio({ ...drive_req, ...{ folder_name: 'Progs Thumbnails', is_system: true } }, job_id, headers);
171
+ // await drive_module.create_drive_folder_studio({ ...drive_req, ...{ folder_name: 'Screenshots', is_system: true } }, job_id, headers);
172
+ // await drive_module.create_drive_folder_user({ ...drive_req, ...{ folder_name: 'Profile Images', is_system: true } }, job_id, headers);
173
+ // await drive_module.create_drive_folder_user({ ...drive_req, ...{ folder_name: 'Chat Thumbnails', is_system: true } }, job_id, headers);
174
+ // await drive_module.create_drive_folder_user({ ...drive_req, ...{ folder_name: 'Chat Attachments', is_system: true } }, job_id, headers);
179
175
 
180
176
  const ret2 = await db_module.save_couch_doc('xuda_accounts', account_obj);
181
177
 
@@ -567,9 +563,10 @@ export const get_account_name = async function (req) {
567
563
  date_created_ts,
568
564
  };
569
565
  if (data.data.account_info) {
570
- const { first_name, last_name, email, tel: phone, profile_picture, profile_avatar, username, website, country, bio, industry } = data.data.account_info;
566
+ const { first_name, last_name, email, tel: phone, profile_picture, profile_avatar, username, website, country, bio, industry, account_type, address, city, state, zip, business_name, auto_respond, auto_respond_mode, auto_respond_agents } = data.data.account_info;
571
567
 
572
568
  obj = {
569
+ _id: data.data._id,
573
570
  first_name,
574
571
  last_name,
575
572
  email,
@@ -585,9 +582,20 @@ export const get_account_name = async function (req) {
585
582
  support_plan,
586
583
  storage_plan,
587
584
  date_created_ts,
585
+ account_type,
586
+ address,
587
+ city,
588
+ state,
589
+ zip,
590
+ business_name,
591
+ online: data?.data?.socket_id ? true : false,
592
+ auto_respond,
593
+ auto_respond_mode,
594
+ auto_respond_agents,
595
+ primary_email: email?.[0] || '',
588
596
  };
589
597
  }
590
- return { code: 1, data: obj };
598
+ return { code: 1, data: obj }; //is_online: data?.data?.socket_id ? true : false
591
599
  };
592
600
 
593
601
  export const account_validate_username = async function (req) {
@@ -825,57 +833,63 @@ export const get_ssh_keys = async function (req) {
825
833
  };
826
834
 
827
835
  export const search_users = async function (req) {
836
+ const { _id, search, limit, skip, bookmark, uid } = req;
837
+
828
838
  // search
829
839
  // skip
830
840
  // limit
831
841
  // bookmark
832
842
  try {
833
- if (!_conf.superuser_account_ids.includes(req.uid)) {
834
- throw new Error('user is not authorized for this method');
835
- }
843
+ // if (!_conf.superuser_account_ids.includes(req.uid)) {
844
+ // throw new Error('user is not authorized for this method');
845
+ // }
836
846
 
837
847
  let selector = {
838
848
  docType: 'account',
839
849
  stat: 3,
840
850
  };
841
851
 
842
- if (req.search) {
852
+ if (_id) {
853
+ selector._id = _id;
854
+ }
855
+
856
+ if (search) {
843
857
  selector = {
844
858
  ...selector,
845
859
  $or: [
846
860
  {
847
861
  'account_info.first_name': {
848
- $regex: `(?i)${req.search}`,
862
+ $regex: `(?i)${search}`,
849
863
  },
850
864
  },
851
865
  {
852
866
  'account_info.last_name': {
853
- $regex: `(?i)${req.search}`,
867
+ $regex: `(?i)${search}`,
854
868
  },
855
869
  },
856
870
  {
857
- 'account_info.company_name': {
858
- $regex: `(?i)${req.search}`,
871
+ 'account_info.business_name': {
872
+ $regex: `(?i)${search}`,
859
873
  },
860
874
  },
861
875
  {
862
876
  'account_info.tel': {
863
- $regex: `(?i)${req.search}`,
877
+ $regex: `(?i)${search}`,
864
878
  },
865
879
  },
866
880
  {
867
881
  'account_info.email': {
868
- $regex: `(?i)${req.search}`,
882
+ $regex: `(?i)${search}`,
869
883
  },
870
884
  },
871
885
  {
872
886
  'account_info.address': {
873
- $regex: `(?i)${req.search}`,
887
+ $regex: `(?i)${search}`,
874
888
  },
875
889
  },
876
890
  {
877
891
  _id: {
878
- $regex: `(?i)${req.search}`,
892
+ $regex: `(?i)${search}`,
879
893
  },
880
894
  },
881
895
  ],
@@ -884,24 +898,36 @@ export const search_users = async function (req) {
884
898
 
885
899
  const opt = {
886
900
  selector,
887
-
888
- limit: req.limit ? req.limit : 99999,
901
+ fields: ['_id', 'account_info.first_name', 'account_info.last_name', 'account_info.username', 'account_info.profile_avatar', 'account_info.profile_picture', 'socket_id', 'account_info.country', 'account_info.bio', 'account_info.company'],
902
+ limit: limit ? limit : 99999,
889
903
  };
890
904
 
891
- if (req.skip) {
892
- opt.skip = req.skip;
905
+ if (skip) {
906
+ opt.skip = skip;
893
907
  }
894
908
 
895
909
  if (req?.bookmark !== 'nil') {
896
- opt.bookmark = req.bookmark;
910
+ opt.bookmark = bookmark;
897
911
  }
898
912
 
899
913
  try {
900
914
  var ret = await db_module.find_couch_query('xuda_accounts', opt);
901
915
 
916
+ let docs = [];
917
+ for await (let e of ret.docs) {
918
+ let doc = await get_user_card(uid, e._id); //await get_contact_info(uid, e);
919
+ docs.push(doc);
920
+ }
921
+
922
+ // debugger
902
923
  return {
903
924
  code: 1,
904
- data: ret,
925
+ data: docs,
926
+ // data: ret.docs.map((e) => {
927
+ // const { socket_id, ...rest } = e;
928
+
929
+ // return { _id: e._id, ...rest.account_info, online: e.socket_id ? true : false };
930
+ // }),
905
931
  };
906
932
  } catch (e) {
907
933
  return {
@@ -1169,22 +1195,24 @@ const get_contact_background = function (doc) {
1169
1195
 
1170
1196
  if (doc.pending) {
1171
1197
  ret = `linear-gradient(145deg, #009ec2 0%, #002c74 100%)`;
1198
+ } else if (doc.my_contact) {
1199
+ ret = `linear-gradient(145deg, #242480 0%, #060619 100%)`;
1172
1200
  } else {
1173
- switch (doc.contact_mood) {
1201
+ switch (doc.contact_mood_level) {
1174
1202
  case -2: {
1175
- ret = `linear-gradient(145deg, #f60404d8 0%, #7f98e344 100%)`;
1203
+ ret = `linear-gradient(145deg, #f6040494 0%, #f6040476 100%)`;
1176
1204
  break;
1177
1205
  }
1178
1206
  case -1: {
1179
- ret = `linear-gradient(145deg, #f6040452 0%, #7f98e344 100%)`;
1207
+ ret = `linear-gradient(145deg, #f604048b 0%, #7f98e344 100%)`;
1180
1208
  break;
1181
1209
  }
1182
1210
  case 1: {
1183
- ret = `linear-gradient(145deg, #04f61452 0%, #7f98e344 100%)`;
1211
+ ret = `linear-gradient(145deg, #04f61447 0%, #7f98e344 100%)`;
1184
1212
  break;
1185
1213
  }
1186
1214
  case 2: {
1187
- ret = `linear-gradient(145deg, #30f604c9 0%, #7f98e344 100%)`;
1215
+ ret = `linear-gradient(145deg, #30f60468 0%, #04f61447 100%)`;
1188
1216
  break;
1189
1217
  }
1190
1218
  default:
@@ -1194,10 +1222,15 @@ const get_contact_background = function (doc) {
1194
1222
  return ret;
1195
1223
  };
1196
1224
 
1197
- const get_contact_pattern = function (doc) {
1225
+ const get_contact_pattern = async function (doc) {
1198
1226
  let ret = `default-pattern.png`;
1199
1227
 
1200
- if (doc.contact_uid) {
1228
+ if (doc.my_contact) {
1229
+ ret = `my-pattern.png`;
1230
+ } else if (doc.shared_from_uid) {
1231
+ const shared_from_uid_ret = await get_account_name({ uid_query: doc.shared_from_uid });
1232
+ ret = shared_from_uid_ret.data.profile_avatar;
1233
+ } else if (doc.contact_uid) {
1201
1234
  ret = `xu-pattern.png`;
1202
1235
  } else {
1203
1236
  switch (doc.source) {
@@ -1218,14 +1251,17 @@ const get_contact_pattern = function (doc) {
1218
1251
  };
1219
1252
 
1220
1253
  const get_contact_border = function (doc) {
1221
- let ret = `transparent`;
1254
+ const disable_color = `#1a3557`;
1255
+ let ret;
1222
1256
 
1223
1257
  if (doc.pending) {
1224
- ret = `#6f6c67`;
1258
+ ret = `#6f6c67`; // gray
1259
+ } else if (doc.my_contact) {
1260
+ ret = `transparent`; // gray
1225
1261
  } else {
1226
1262
  switch (doc.connection_stat) {
1227
1263
  case 0: {
1228
- ret = `transparent`;
1264
+ ret = disable_color;
1229
1265
  break;
1230
1266
  }
1231
1267
  case 1: {
@@ -1239,7 +1275,7 @@ const get_contact_border = function (doc) {
1239
1275
  }
1240
1276
 
1241
1277
  case 3: {
1242
- ret = `#0070ff`;
1278
+ ret = `#164a28`; //`#0070ff`;
1243
1279
  break;
1244
1280
  }
1245
1281
 
@@ -1249,12 +1285,27 @@ const get_contact_border = function (doc) {
1249
1285
  }
1250
1286
 
1251
1287
  default:
1288
+ ret = disable_color;
1252
1289
  break;
1253
1290
  }
1254
1291
  }
1255
1292
  return ret;
1256
1293
  };
1257
1294
 
1295
+ function formatPhoneWithFlag(phoneString, country = 'US') {
1296
+ // try {
1297
+
1298
+ const phoneNumber = parsePhoneNumber(phoneString, country);
1299
+ if (phoneNumber && phoneNumber.country) {
1300
+ const flag = countryCodeEmoji(phoneNumber.country);
1301
+ const formatted = phoneNumber.formatInternational(); // e.g., +1 213 373 4253
1302
+ return `${flag} ${formatted}`;
1303
+ }
1304
+ // } catch (error) {
1305
+ // return 'Invalid phone number';
1306
+ // }
1307
+ }
1308
+
1258
1309
  export const get_contact_info = async function (uid, contact_doc, _id) {
1259
1310
  const ai_module = await import(`${module_path}/ai_module/index.mjs`);
1260
1311
  let doc = contact_doc;
@@ -1269,39 +1320,155 @@ export const get_contact_info = async function (uid, contact_doc, _id) {
1269
1320
  }
1270
1321
 
1271
1322
  doc.profile_avatar = account_info_ret.data.profile_avatar;
1272
- doc.card_background = get_contact_background(doc);
1273
1323
 
1274
1324
  //membership_plan
1275
1325
  doc.name = `${account_info_ret.data.first_name} ${account_info_ret.data.last_name}`;
1276
1326
 
1277
- doc.telephone = account_info_ret.data.phone;
1327
+ if (account_info_ret.data.phone) {
1328
+ try {
1329
+ doc.telephone = formatPhoneWithFlag(account_info_ret.data.phone, account_info_ret.data.country);
1330
+ } catch (err) {
1331
+ console.error(err);
1332
+ doc.telephone = account_info_ret.data.phone;
1333
+ }
1334
+ }
1278
1335
 
1279
- // doc.online = true;
1280
1336
  if (doc.team_req_id) {
1281
1337
  try {
1282
1338
  const req_doc = await db_module.get_couch_doc_native('xuda_team', doc.team_req_id);
1283
1339
  doc.connection_stat = req_doc.team_req_stat;
1340
+ doc.friend_since = req_doc.team_req_date;
1284
1341
  } catch (error) {
1285
1342
  doc.connection_stat = 0;
1286
1343
  }
1287
1344
  }
1288
1345
 
1289
- doc.icon_pattern = get_contact_pattern(doc);
1346
+ doc.icon_pattern = await get_contact_pattern(doc);
1347
+ doc.username = account_info_ret.data.username;
1348
+ doc.company = account_info_ret.data.business_name;
1349
+ doc.address = account_info_ret.data.address;
1350
+ doc.city = account_info_ret.data.city;
1351
+ doc.state = account_info_ret.data.state;
1352
+ doc.zip = account_info_ret.data.zip;
1353
+ } else {
1354
+ doc.email = doc.email?.[0];
1290
1355
  }
1291
1356
 
1357
+ doc.interactions = 0;
1358
+ doc.chats = 0;
1359
+
1292
1360
  const chats_ret = await ai_module.get_ai_chats({ uid, reference_id: doc._id });
1293
1361
  for (let chat_doc of chats_ret.data.docs) {
1294
1362
  doc.notifications += chat_doc.notifications;
1363
+
1364
+ doc.chats++;
1365
+ if (chat_doc.interactions) {
1366
+ doc.interactions += chat_doc.interactions;
1367
+ }
1368
+ doc.contact_mood_level = chat_doc.mood_level || 0;
1295
1369
  }
1296
1370
 
1371
+ doc.card_background = get_contact_background(doc);
1372
+
1297
1373
  doc.border = get_contact_border(doc);
1298
- if (!doc.name) {
1299
- doc.name = doc.email;
1374
+
1375
+ return doc;
1376
+ };
1377
+
1378
+ export const get_pending_contact_out = async function (uid, _id) {
1379
+ let docs = [];
1380
+ const to_opt = {
1381
+ selector: { docType: 'team_request', access_type: 'contact_connection', team_req_stat: 2, team_req_from_uid: uid },
1382
+ fields: ['_id', 'team_req_date', 'team_req_to_uid', 'team_req_to_email', 'sender_account_info.email', 'sender_account_info.first_name', 'sender_account_info.last_name', 'sender_account_info.profile_avatar', 'team_req_from_uid'],
1383
+ };
1384
+
1385
+ if (_id) {
1386
+ to_opt.selector._id = _id;
1300
1387
  }
1388
+ const requests_to_res = await db_module.find_couch_query('xuda_team', to_opt);
1301
1389
 
1302
- doc.email = '';
1390
+ for await (let e of requests_to_res.docs) {
1391
+ let doc = { docType: 'contact', date_created: e.team_req_date, email: [e.team_req_to_email], name: '', pending: true, contact_uid: e.team_req_to_uid, team_req_id: e._id, team_req_from_uid: uid };
1303
1392
 
1304
- return doc;
1393
+ if (e.team_req_to_uid) {
1394
+ const account_info_ret = await get_account_name({ uid_query: e.team_req_to_uid });
1395
+ if (account_info_ret.code < 0) {
1396
+ continue; // throw new Error(`account ${contact_uid} not found`);
1397
+ }
1398
+ doc.profile_avatar = account_info_ret.data.profile_avatar;
1399
+ doc.name = `${account_info_ret.data.first_name} ${account_info_ret.data.last_name}`;
1400
+ }
1401
+ doc.icon_pattern = await get_contact_pattern(doc);
1402
+ doc.card_background = get_contact_background({ pending: true });
1403
+ docs.push(doc);
1404
+ }
1405
+
1406
+ return docs;
1407
+ };
1408
+
1409
+ export const get_pending_contact_in = async function (uid, _id) {
1410
+ let docs = [];
1411
+ const from_opt = { selector: { docType: 'team_request', access_type: 'contact_connection', team_req_stat: 2, team_req_to_uid: uid }, fields: ['_id', 'team_req_date', 'sender_account_info.email', 'team_req_from_uid'] };
1412
+
1413
+ if (_id) {
1414
+ to_opt.selector._id = _id;
1415
+ }
1416
+
1417
+ const requests_from_res = await db_module.find_couch_query('xuda_team', from_opt);
1418
+
1419
+ for await (let e of requests_from_res.docs) {
1420
+ 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 };
1421
+
1422
+ if (e.team_req_from_uid) {
1423
+ const account_info_ret = await get_account_name({ uid_query: e.team_req_from_uid });
1424
+ if (account_info_ret.code < 0) {
1425
+ continue; // throw new Error(`account ${contact_uid} not found`);
1426
+ }
1427
+ doc.profile_avatar = account_info_ret.data.profile_avatar;
1428
+ doc.name = `${account_info_ret.data.first_name} ${account_info_ret.data.last_name}`;
1429
+ }
1430
+ doc.card_background = get_contact_background({ pending: true });
1431
+ doc.icon_pattern = await get_contact_pattern(doc);
1432
+ docs.push(doc);
1433
+ }
1434
+ return docs;
1435
+ };
1436
+ export const get_pending_share_contact_in = async function (uid, _id) {
1437
+ let docs = [];
1438
+ const from_opt = { selector: { docType: 'team_request', access_type: 'contact', 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'] };
1439
+
1440
+ if (_id) {
1441
+ to_opt.selector._id = _id;
1442
+ }
1443
+
1444
+ const requests_from_res = await db_module.find_couch_query('xuda_team', from_opt);
1445
+
1446
+ for await (let e of requests_from_res.docs) {
1447
+ // 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 };
1448
+ let doc = await db_module.get_couch_doc_native('xuda_contacts', e.share_item_id);
1449
+ delete doc.contact_mood_level;
1450
+ doc.shared_from_uid = e.team_req_from_uid;
1451
+ doc.pending = true;
1452
+ doc = await get_contact_info(uid, doc);
1453
+ doc.team_req_id = e._id;
1454
+
1455
+ docs.push(doc);
1456
+ }
1457
+ return docs;
1458
+ };
1459
+
1460
+ const get_user_contact = async function (uid, uid_query) {
1461
+ const my_contact_ret = await get_account_name({ uid_query });
1462
+ const my_account_info = my_contact_ret.data;
1463
+ let my_contact_doc = { docType: 'contact', date_created: Date.now(), email: [my_account_info.email], name: `${my_account_info.first_name} ${my_account_info.last_name}`, my_contact: uid === uid_query, contact_uid: uid_query };
1464
+ my_contact_doc.profile_avatar = my_account_info.profile_avatar;
1465
+ my_contact_doc.card_background = get_contact_background(my_contact_doc);
1466
+ my_contact_doc.icon_pattern = await get_contact_pattern(my_contact_doc);
1467
+ my_contact_doc.border = get_contact_border(my_contact_doc);
1468
+ my_contact_doc.auto_respond = my_account_info.auto_respond;
1469
+ my_contact_doc.auto_respond_mode = my_account_info.auto_respond_mode;
1470
+ my_contact_doc.auto_respond_agents = my_account_info.auto_respond_agents;
1471
+ return my_contact_doc;
1305
1472
  };
1306
1473
 
1307
1474
  export const get_contacts = async function (req) {
@@ -1360,80 +1527,151 @@ export const get_contacts = async function (req) {
1360
1527
 
1361
1528
  const contacts = await db_module.find_couch_query('xuda_contacts', opt);
1362
1529
 
1363
- for await (let doc of contacts.docs) {
1364
- doc = await get_contact_info(uid, doc);
1365
- }
1366
-
1367
1530
  let requests_to = { docs: [] };
1368
1531
  let requests_from = { docs: [] };
1369
-
1532
+ let shared_from = { docs: [] };
1533
+ let my_contact = { docs: [] };
1370
1534
  if (with_requests) {
1371
- ////// TO
1372
- const requests_to_res = await db_module.find_couch_query('xuda_team', {
1373
- selector: { docType: 'team_request', access_type: 'contact', team_req_stat: 2, team_req_from_uid: uid },
1374
- fields: ['_id', 'team_req_date', 'team_req_to_uid', 'team_req_to_email', 'sender_account_info.email', 'sender_account_info.first_name', 'sender_account_info.last_name', 'sender_account_info.profile_avatar', 'team_req_from_uid'],
1535
+ ////// TO - OUT
1536
+ requests_to.docs = await get_pending_contact_out(uid);
1537
+
1538
+ ////// FROM - IN
1539
+ requests_from.docs = await get_pending_contact_in(uid);
1540
+ shared_from.docs = await get_pending_share_contact_in(uid);
1541
+ // my contact
1542
+ // const my_contact_ret = await get_account_name({ uid_query: uid }); //await db_module.get_couch_doc_native('xuda_contacts', sender_contact_id);
1543
+ // const my_account_info = my_contact_ret.data;
1544
+ // let my_contact_doc = { docType: 'contact', date_created: Date.now(), email: [my_account_info.email], name: `${my_account_info.first_name} ${my_account_info.last_name}`, my_contact: true, contact_uid: uid };
1545
+ // my_contact_doc.profile_avatar = my_account_info.profile_avatar;
1546
+ // my_contact_doc.card_background = get_contact_background(my_contact_doc);
1547
+ // my_contact_doc.icon_pattern = await get_contact_pattern(my_contact_doc);
1548
+ // my_contact_doc.border = get_contact_border(my_contact_doc);
1549
+ // my_contact_doc.auto_respond = my_account_info.auto_respond;
1550
+ // my_contact_doc.auto_respond_mode = my_account_info.auto_respond_mode;
1551
+ // my_contact_doc.auto_respond_agents = my_account_info.auto_respond_agents;
1552
+ const my_contact_doc = await get_user_contact(uid, uid);
1553
+ my_contact.docs = [my_contact_doc];
1554
+ }
1555
+ let contact_docs = { docs: [] };
1556
+ for await (let doc of contacts.docs) {
1557
+ const ret_to = requests_to.docs.find((e) => {
1558
+ return e.contact_uid === doc.contact_uid;
1375
1559
  });
1560
+ if (ret_to) continue;
1376
1561
 
1377
- for await (let e of requests_to_res.docs) {
1378
- let doc = { docType: 'contact', date_created: e.team_req_date, email: [e.team_req_to_email], name: '', pending: true, contact_uid: e.team_req_to_uid, team_req_id: e._id, team_req_from_uid: uid };
1379
-
1380
- if (e.team_req_to_uid) {
1381
- const account_info_ret = await get_account_name({ uid_query: e.team_req_to_uid });
1382
- if (account_info_ret.code < 0) {
1383
- continue; // throw new Error(`account ${contact_uid} not found`);
1384
- }
1385
- doc.profile_avatar = account_info_ret.data.profile_avatar;
1386
- doc.name = `${account_info_ret.data.first_name} ${account_info_ret.data.last_name}`;
1387
- }
1388
- doc.icon_pattern = get_contact_pattern(doc);
1389
- doc.card_background = get_contact_background({ pending: true });
1390
- requests_to.docs.push(doc);
1391
- }
1392
-
1393
- ////// FROM
1394
- const requests_from_res = await db_module.find_couch_query('xuda_team', { selector: { docType: 'team_request', access_type: 'contact', team_req_stat: 2, team_req_to_uid: uid }, fields: ['_id', 'team_req_date', 'sender_account_info.email', 'team_req_from_uid'] });
1395
-
1396
- for await (let e of requests_from_res.docs) {
1397
- 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 };
1562
+ const ret_from = requests_from.docs.find((e) => {
1563
+ return e.contact_uid === doc.contact_uid;
1564
+ });
1565
+ if (ret_from) continue;
1398
1566
 
1399
- if (e.team_req_from_uid) {
1400
- const account_info_ret = await get_account_name({ uid_query: e.team_req_from_uid });
1401
- if (account_info_ret.code < 0) {
1402
- continue; // throw new Error(`account ${contact_uid} not found`);
1403
- }
1404
- doc.profile_avatar = account_info_ret.data.profile_avatar;
1405
- doc.name = `${account_info_ret.data.first_name} ${account_info_ret.data.last_name}`;
1406
- }
1407
- doc.card_background = get_contact_background({ pending: true });
1408
- doc.icon_pattern = get_contact_pattern(doc);
1409
- requests_from.docs.push(doc);
1410
- }
1567
+ doc = await get_contact_info(uid, doc);
1568
+ contact_docs.docs.push(doc);
1411
1569
  }
1412
1570
 
1413
1571
  return {
1414
1572
  code: 1,
1415
1573
  data: {
1416
- docs: [...requests_to.docs, ...requests_from.docs, ...contacts.docs],
1574
+ docs: [...my_contact.docs, ...shared_from.docs, ...requests_from.docs, ...requests_to.docs, ...contact_docs.docs],
1417
1575
  },
1418
1576
  };
1419
1577
  };
1420
1578
 
1421
1579
  export const archive_contact = async function (req) {
1422
- const { _id } = req;
1580
+ const { contact_id } = req;
1581
+ try {
1582
+ const contact_ret = await db_module.get_couch_doc('xuda_contacts', contact_id);
1583
+ if (contact_ret.code < 0) {
1584
+ return contact_ret;
1585
+ }
1586
+ var contact_doc = contact_ret.data;
1587
+ contact_doc.stat = 2;
1588
+ contact_doc.stat_ts = Date.now();
1589
+ contact_doc.stat_reason = 'archived by the user';
1423
1590
 
1424
- const contact_ret = await db_module.get_couch_doc('xuda_contacts', _id);
1425
- if (contact_ret.code < 0) {
1426
- return contact_ret;
1591
+ const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
1592
+
1593
+ return contact_save_ret;
1594
+ } catch (err) {
1595
+ return {
1596
+ code: -22,
1597
+ data: err.message,
1598
+ };
1427
1599
  }
1428
- var contact_doc = contact_ret.data;
1429
- contact_doc.stat = 4;
1430
- contact_doc.stat_ts = Date.now();
1431
- contact_doc.stat_reason = 'deleted';
1600
+ };
1601
+
1602
+ export const unfriend_contact = async function (req) {
1603
+ const { contact_id } = req;
1604
+ try {
1605
+ const contact_ret = await db_module.get_couch_doc('xuda_contacts', contact_id);
1606
+ if (contact_ret.code < 0) {
1607
+ throw new Error(contact_ret.data);
1608
+ }
1609
+ var contact_doc = contact_ret.data;
1610
+
1611
+ if (!contact_doc.team_req_id) {
1612
+ throw new Error('no friend relationship found');
1613
+ }
1614
+ let req_doc = await db_module.get_couch_doc_native('xuda_team', contact_doc.team_req_id);
1615
+ req_doc.team_req_stat = 4;
1616
+ req_doc.team_req_stat_desc = 'unfriend by the user';
1617
+ await db_module.get_couch_doc_native('xuda_team', contact_doc.team_req_id);
1618
+
1619
+ contact_doc.team_req_id = null;
1620
+ const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
1621
+
1622
+ const req_save_ret = await db_module.save_couch_doc('xuda_team', req_doc);
1623
+
1624
+ return req_save_ret;
1625
+ } catch (err) {
1626
+ return {
1627
+ code: -21,
1628
+ data: err.message,
1629
+ };
1630
+ }
1631
+ };
1632
+
1633
+ export const pin_contact = async function (req) {
1634
+ const { contact_id } = req;
1635
+ try {
1636
+ const contact_ret = await db_module.get_couch_doc('xuda_contacts', contact_id);
1637
+ if (contact_ret.code < 0) {
1638
+ return contact_ret;
1639
+ }
1640
+ var contact_doc = contact_ret.data;
1641
+ contact_doc.pin = true;
1642
+
1643
+ const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
1644
+
1645
+ return contact_save_ret;
1646
+ } catch (err) {
1647
+ return {
1648
+ code: -24,
1649
+ data: err.message,
1650
+ };
1651
+ }
1652
+ };
1653
+
1654
+ export const unpin_contact = async function (req) {
1655
+ const { contact_id } = req;
1656
+ try {
1657
+ const contact_ret = await db_module.get_couch_doc('xuda_contacts', contact_id);
1658
+ if (contact_ret.code < 0) {
1659
+ return contact_ret;
1660
+ }
1661
+ var contact_doc = contact_ret.data;
1662
+ contact_doc.pin = false;
1432
1663
 
1433
- const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
1664
+ const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
1434
1665
 
1435
- return contact_save_ret;
1666
+ return contact_save_ret;
1667
+ } catch (err) {
1668
+ return {
1669
+ code: -24,
1670
+ data: err.message,
1671
+ };
1672
+ }
1436
1673
  };
1674
+
1437
1675
  // invite user
1438
1676
 
1439
1677
  // const contact_ret = await db_module.get_couch_view_raw(
@@ -1449,7 +1687,7 @@ export const archive_contact = async function (req) {
1449
1687
 
1450
1688
  export const add_contact = async function (req, job_id, headers) {
1451
1689
  try {
1452
- const { uid, email, name, stat, contact_uid, context, source, team_req_id, data } = req;
1690
+ const { uid, email, name, stat, contact_uid, context, source, team_req_id, metadata } = req;
1453
1691
  const contact_ret = await db_module.get_couch_view_raw('xuda_contacts', 'contacts_by_email_address', { key: [uid, email] });
1454
1692
 
1455
1693
  if (contact_ret.rows.length) {
@@ -1477,7 +1715,7 @@ export const add_contact = async function (req, job_id, headers) {
1477
1715
  headers,
1478
1716
  source,
1479
1717
  team_req_id,
1480
- data,
1718
+ metadata,
1481
1719
  };
1482
1720
  const ret = await db_module.save_couch_doc('xuda_contacts', doc);
1483
1721
 
@@ -1496,6 +1734,12 @@ export const add_contact = async function (req, job_id, headers) {
1496
1734
  return { code: -1, data: err.message };
1497
1735
  }
1498
1736
  };
1737
+
1738
+ const get_contact_id = async function (uid, contact_uid) {
1739
+ contact_ret = await db_module.get_couch_view_raw('xuda_contacts', 'contacts_by_contact_uid', { key: [uid, contact_uid] });
1740
+ return contact_ret?.rows?.[0]?.id;
1741
+ };
1742
+
1499
1743
  export const update_contact = async function (req) {
1500
1744
  const { uid, email, contact_uid, stat, team_req_id } = req;
1501
1745
  var contact_ret;
@@ -1825,3 +2069,77 @@ export const update_contacts_with_account_changes = async function (uid, changes
1825
2069
  return 'unknown';
1826
2070
  }
1827
2071
  };
2072
+
2073
+ export const get_user_card = async function (uid, uid_query) {
2074
+ if (uid === uid_query) return await get_user_contact(uid, uid_query);
2075
+
2076
+ let user_contact_obj; // = { border, card_background, contact_uid, date_created, docType, icon_pattern, name, company, profile_avatar, connection_stat, username, bio, notifications, email, phone };
2077
+
2078
+ const contact_ret = await db_module.get_couch_view_raw('xuda_contacts', 'contacts_by_contact_uid', { key: [uid, uid_query] });
2079
+
2080
+ const contact_id = contact_ret?.rows?.[0]?.id;
2081
+ if (contact_id) {
2082
+ user_contact_obj = await get_contact_info(uid, null, contact_id);
2083
+ } else {
2084
+ user_contact_obj = await get_user_contact(uid, uid_query);
2085
+ }
2086
+ return user_contact_obj;
2087
+ };
2088
+
2089
+ // export const generate_user_avatar = async function (req, job_id, headers) {
2090
+ // const { uid, account_info } = req;
2091
+ // try {
2092
+ // const ai_module = await import(`${module_path}/ai_module/index.mjs`);
2093
+
2094
+ // ai_module.convert_profile_image_to_avatar({ url: account_info?.profile_picture, uid, account_obj }, job_id, headers);
2095
+
2096
+ // // const account_info = await get_account_name({ uid });
2097
+ // // let username = 'unknown';
2098
+ // // if (account_info) {
2099
+ // // username = account_info.first_name + ' ' + account_info.last_name;
2100
+
2101
+ // // if (!username) {
2102
+ // // username = +account_info.email;
2103
+ // // }
2104
+ // // }
2105
+
2106
+ // // return username;
2107
+ // } catch (err) {
2108
+ // return 'unknown';
2109
+ // }
2110
+ // };
2111
+
2112
+ export const onboarding_completed = async function (req, job_id, headers) {
2113
+ const { uid } = req;
2114
+ try {
2115
+ let account_doc = await db_module.get_couch_doc_native('xuda_accounts', uid);
2116
+
2117
+ if (!account_doc.account_info?.profile_picture) {
2118
+ throw new Error(`missing profile_picture`);
2119
+ }
2120
+
2121
+ if (!account_doc.account_info?.profile_avatar) {
2122
+ throw new Error(`missing profile_avatar`);
2123
+ }
2124
+
2125
+ if (!account_doc.account_project_id) {
2126
+ const app_module = await import(`${module_path}/app_module/index.mjs`);
2127
+
2128
+ const ret = await app_module.create_project({ token_ret: req.token_ret, uid, data: { app_name: `Account ${uid} main project`, is_account_project: true, app_plugins_purchased: [' @xuda.io/xuda-dbs-plugin-xuda', '@xuda.io/xuda-framework-plugin-tailwind'] } }, job_id, headers);
2129
+ if (ret.code > -1) {
2130
+ account_doc.account_project_id = ret.data;
2131
+ const drive_module = await import(`${module_path}/drive_module/index.mjs`);
2132
+ let drive_req = { app_id: account_doc.account_project_id, uid, path: '/' };
2133
+ await drive_module.create_drive_folder_user({ ...drive_req, ...{ folder_name: 'Profile Images', is_system: true } }, job_id, headers);
2134
+ await drive_module.create_drive_folder_user({ ...drive_req, ...{ folder_name: 'Chat Thumbnails', is_system: true } }, job_id, headers);
2135
+ await drive_module.create_drive_folder_user({ ...drive_req, ...{ folder_name: 'Chat Attachments', is_system: true } }, job_id, headers);
2136
+ }
2137
+ }
2138
+
2139
+ account_doc.onboarding = { headers, date_ts: Date.now() };
2140
+ const save_ret = await db_module.save_couch_doc('xuda_accounts', account_doc);
2141
+ return save_ret;
2142
+ } catch (err) {
2143
+ return { code: -44, data: err.message };
2144
+ }
2145
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/account_module",
3
- "version": "1.2.522",
3
+ "version": "1.2.749",
4
4
  "description": "Xuda Account Server Module",
5
5
  "main": "index.mjs",
6
6
  "dependencies": {