@xuda.io/account_module 1.2.523 → 1.2.750

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 +456 -136
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -6,9 +6,30 @@ 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);
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'];
9
+ // const execAsync = util.promisify(exec);
10
+
11
+ const account_info_properties = [
12
+ 'account_type',
13
+ 'business_name',
14
+ 'bio',
15
+ 'phone_number',
16
+ 'address',
17
+ 'city',
18
+ 'state',
19
+ 'zip',
20
+ 'country',
21
+ 'email',
22
+ 'first_name',
23
+ 'last_name',
24
+ 'mainCategory',
25
+ 'subCategory',
26
+ 'profile_picture',
27
+ 'username',
28
+ 'website',
29
+ 'auto_respond',
30
+ 'auto_respond_mode',
31
+ 'auto_respond_agents',
32
+ ];
12
33
 
13
34
  global._conf = (
14
35
  await import(path.join(process.env.XUDA_HOME, process.env.XUDA_CONFIG), {
@@ -31,10 +52,6 @@ export const update_account_info = async function (req, job_id, headers) {
31
52
  const { uid } = req;
32
53
  const data = req;
33
54
 
34
- // delete data.gtp_token;
35
- // delete data.uid;
36
- // delete data.token_ret;
37
- // delete data.job_id;
38
55
  delete data.profile_avatar;
39
56
 
40
57
  function validate_input(string, max_length, is_mandatory, is_pass) {
@@ -60,9 +77,9 @@ export const update_account_info = async function (req, job_id, headers) {
60
77
 
61
78
  await marketplace_module.marketplace_save_user({ uid });
62
79
 
63
- _.forEach(account_info_properties, function (key) {
80
+ for (const key of account_info_properties) {
64
81
  let val = data[key];
65
-
82
+ if (typeof val === 'undefined') continue;
66
83
  if (account_obj.account_info[key] !== val) {
67
84
  change += ' from ' + account_obj.account_info[key] + ' to ' + val;
68
85
  account_info_changes_arr.push(key);
@@ -87,8 +104,8 @@ export const update_account_info = async function (req, job_id, headers) {
87
104
  account_obj.account_info[key] = val;
88
105
 
89
106
  if (account_obj.account_info.account_type === 'business') {
90
- if (key === 'company_name' && validate_input(val, 150, true, false) < 0) {
91
- error[key] = 'Invalid company';
107
+ if (key === 'business_name' && validate_input(val, 150, true, false) < 0) {
108
+ error[key] = 'Invalid business_name';
92
109
  }
93
110
  }
94
111
  if (key === 'first_name' && !validate_input(val, 35, true, false)) {
@@ -102,10 +119,10 @@ export const update_account_info = async function (req, job_id, headers) {
102
119
  if (key === 'email' && (!validator.isEmail(val) || !val || val.length < 2)) {
103
120
  error[key] = 'Invalid email';
104
121
  }
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
- }
122
+ // if (key === 'phone_number' && (!/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/.test(val) || !val || val.length < 2)) {
123
+ // // if (key === "phone_number" && (!preg_match("/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/", $val) || !$val || count($val) < 2)) {
124
+ // error[key] = 'Invalid phone number';
125
+ // }
109
126
  if (key === 'address' && !/^[a-z0-9- ]+$/i.test(val)) {
110
127
  // if (key === "address" && !preg_match('/^[a-z0-9- ]+$/i', $val)) {
111
128
  error[key] = 'Invalid address';
@@ -123,7 +140,7 @@ export const update_account_info = async function (req, job_id, headers) {
123
140
  error[key] = 'Invalid country';
124
141
  }
125
142
  }
126
- });
143
+ }
127
144
 
128
145
  if (!_.isEmpty(error)) {
129
146
  return { code: -1310, data: error };
@@ -144,19 +161,19 @@ export const update_account_info = async function (req, job_id, headers) {
144
161
  delete account_obj.account_info.job_id;
145
162
  delete account_obj.account_info.app_id;
146
163
 
147
- if (!account_obj.account_project_id) {
148
- const app_module = await import(`${module_path}/app_module/index.mjs`);
164
+ // if (!account_obj.account_project_id) {
165
+ // const app_module = await import(`${module_path}/app_module/index.mjs`);
149
166
 
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;
167
+ // 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);
168
+ // if (ret.code > -1) {
169
+ // account_obj.account_project_id = ret.data;
153
170
 
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
- }
171
+ // let drive_req = { app_id: account_obj.account_project_id, uid, path: '/' };
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);
175
+ // }
176
+ // }
160
177
 
161
178
  const ai_module = await import(`${module_path}/ai_module/index.mjs`);
162
179
  if (account_obj.account_info?.profile_picture) {
@@ -169,13 +186,13 @@ export const update_account_info = async function (req, job_id, headers) {
169
186
 
170
187
  // temp remove later on
171
188
 
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);
189
+ // let drive_req = { app_id: account_obj.account_project_id, uid, path: '/' };
190
+ // await drive_module.create_drive_folder_workspace({ ...drive_req, ...{ folder_name: 'Chat Images' } }, job_id, headers);
191
+ // await drive_module.create_drive_folder_studio({ ...drive_req, ...{ folder_name: 'Progs Thumbnails', is_system: true } }, job_id, headers);
192
+ // await drive_module.create_drive_folder_studio({ ...drive_req, ...{ folder_name: 'Screenshots', is_system: true } }, job_id, headers);
193
+ // await drive_module.create_drive_folder_user({ ...drive_req, ...{ folder_name: 'Profile Images', is_system: true } }, job_id, headers);
194
+ // await drive_module.create_drive_folder_user({ ...drive_req, ...{ folder_name: 'Chat Thumbnails', is_system: true } }, job_id, headers);
195
+ // await drive_module.create_drive_folder_user({ ...drive_req, ...{ folder_name: 'Chat Attachments', is_system: true } }, job_id, headers);
179
196
 
180
197
  const ret2 = await db_module.save_couch_doc('xuda_accounts', account_obj);
181
198
 
@@ -558,7 +575,7 @@ export const get_account_name = async function (req) {
558
575
  first_name: '',
559
576
  last_name: '',
560
577
  email: '',
561
- phone: '',
578
+ phone_number: '',
562
579
  profile_picture: '',
563
580
  username: '',
564
581
  membership_plan,
@@ -567,13 +584,14 @@ export const get_account_name = async function (req) {
567
584
  date_created_ts,
568
585
  };
569
586
  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;
587
+ const { first_name, last_name, email, phone_number, 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
588
 
572
589
  obj = {
590
+ _id: data.data._id,
573
591
  first_name,
574
592
  last_name,
575
593
  email,
576
- phone,
594
+ phone_number,
577
595
  profile_picture,
578
596
  profile_avatar,
579
597
  username,
@@ -585,9 +603,20 @@ export const get_account_name = async function (req) {
585
603
  support_plan,
586
604
  storage_plan,
587
605
  date_created_ts,
606
+ account_type,
607
+ address,
608
+ city,
609
+ state,
610
+ zip,
611
+ business_name,
612
+ online: data?.data?.socket_id ? true : false,
613
+ auto_respond,
614
+ auto_respond_mode,
615
+ auto_respond_agents,
616
+ primary_email: email?.[0] || '',
588
617
  };
589
618
  }
590
- return { code: 1, data: obj };
619
+ return { code: 1, data: obj }; //is_online: data?.data?.socket_id ? true : false
591
620
  };
592
621
 
593
622
  export const account_validate_username = async function (req) {
@@ -825,57 +854,63 @@ export const get_ssh_keys = async function (req) {
825
854
  };
826
855
 
827
856
  export const search_users = async function (req) {
857
+ const { _id, search, limit, skip, bookmark, uid } = req;
858
+
828
859
  // search
829
860
  // skip
830
861
  // limit
831
862
  // bookmark
832
863
  try {
833
- if (!_conf.superuser_account_ids.includes(req.uid)) {
834
- throw new Error('user is not authorized for this method');
835
- }
864
+ // if (!_conf.superuser_account_ids.includes(req.uid)) {
865
+ // throw new Error('user is not authorized for this method');
866
+ // }
836
867
 
837
868
  let selector = {
838
869
  docType: 'account',
839
870
  stat: 3,
840
871
  };
841
872
 
842
- if (req.search) {
873
+ if (_id) {
874
+ selector._id = _id;
875
+ }
876
+
877
+ if (search) {
843
878
  selector = {
844
879
  ...selector,
845
880
  $or: [
846
881
  {
847
882
  'account_info.first_name': {
848
- $regex: `(?i)${req.search}`,
883
+ $regex: `(?i)${search}`,
849
884
  },
850
885
  },
851
886
  {
852
887
  'account_info.last_name': {
853
- $regex: `(?i)${req.search}`,
888
+ $regex: `(?i)${search}`,
854
889
  },
855
890
  },
856
891
  {
857
- 'account_info.company_name': {
858
- $regex: `(?i)${req.search}`,
892
+ 'account_info.business_name': {
893
+ $regex: `(?i)${search}`,
859
894
  },
860
895
  },
861
896
  {
862
- 'account_info.tel': {
863
- $regex: `(?i)${req.search}`,
897
+ 'account_info.phone_number': {
898
+ $regex: `(?i)${search}`,
864
899
  },
865
900
  },
866
901
  {
867
902
  'account_info.email': {
868
- $regex: `(?i)${req.search}`,
903
+ $regex: `(?i)${search}`,
869
904
  },
870
905
  },
871
906
  {
872
907
  'account_info.address': {
873
- $regex: `(?i)${req.search}`,
908
+ $regex: `(?i)${search}`,
874
909
  },
875
910
  },
876
911
  {
877
912
  _id: {
878
- $regex: `(?i)${req.search}`,
913
+ $regex: `(?i)${search}`,
879
914
  },
880
915
  },
881
916
  ],
@@ -884,24 +919,36 @@ export const search_users = async function (req) {
884
919
 
885
920
  const opt = {
886
921
  selector,
887
-
888
- limit: req.limit ? req.limit : 99999,
922
+ 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.business_name'],
923
+ limit: limit ? limit : 99999,
889
924
  };
890
925
 
891
- if (req.skip) {
892
- opt.skip = req.skip;
926
+ if (skip) {
927
+ opt.skip = skip;
893
928
  }
894
929
 
895
930
  if (req?.bookmark !== 'nil') {
896
- opt.bookmark = req.bookmark;
931
+ opt.bookmark = bookmark;
897
932
  }
898
933
 
899
934
  try {
900
935
  var ret = await db_module.find_couch_query('xuda_accounts', opt);
901
936
 
937
+ let docs = [];
938
+ for await (let e of ret.docs) {
939
+ let doc = await get_user_card(uid, e._id); //await get_contact_info(uid, e);
940
+ docs.push(doc);
941
+ }
942
+
943
+ // debugger
902
944
  return {
903
945
  code: 1,
904
- data: ret,
946
+ data: docs,
947
+ // data: ret.docs.map((e) => {
948
+ // const { socket_id, ...rest } = e;
949
+
950
+ // return { _id: e._id, ...rest.account_info, online: e.socket_id ? true : false };
951
+ // }),
905
952
  };
906
953
  } catch (e) {
907
954
  return {
@@ -1169,22 +1216,24 @@ const get_contact_background = function (doc) {
1169
1216
 
1170
1217
  if (doc.pending) {
1171
1218
  ret = `linear-gradient(145deg, #009ec2 0%, #002c74 100%)`;
1219
+ } else if (doc.my_contact) {
1220
+ ret = `linear-gradient(145deg, #242480 0%, #060619 100%)`;
1172
1221
  } else {
1173
- switch (doc.contact_mood) {
1222
+ switch (doc.contact_mood_level) {
1174
1223
  case -2: {
1175
- ret = `linear-gradient(145deg, #f60404d8 0%, #7f98e344 100%)`;
1224
+ ret = `linear-gradient(145deg, #f6040494 0%, #f6040476 100%)`;
1176
1225
  break;
1177
1226
  }
1178
1227
  case -1: {
1179
- ret = `linear-gradient(145deg, #f6040452 0%, #7f98e344 100%)`;
1228
+ ret = `linear-gradient(145deg, #f604048b 0%, #7f98e344 100%)`;
1180
1229
  break;
1181
1230
  }
1182
1231
  case 1: {
1183
- ret = `linear-gradient(145deg, #04f61452 0%, #7f98e344 100%)`;
1232
+ ret = `linear-gradient(145deg, #04f61447 0%, #7f98e344 100%)`;
1184
1233
  break;
1185
1234
  }
1186
1235
  case 2: {
1187
- ret = `linear-gradient(145deg, #30f604c9 0%, #7f98e344 100%)`;
1236
+ ret = `linear-gradient(145deg, #30f60468 0%, #04f61447 100%)`;
1188
1237
  break;
1189
1238
  }
1190
1239
  default:
@@ -1194,10 +1243,15 @@ const get_contact_background = function (doc) {
1194
1243
  return ret;
1195
1244
  };
1196
1245
 
1197
- const get_contact_pattern = function (doc) {
1246
+ const get_contact_pattern = async function (doc) {
1198
1247
  let ret = `default-pattern.png`;
1199
1248
 
1200
- if (doc.contact_uid) {
1249
+ if (doc.my_contact) {
1250
+ ret = `my-pattern.png`;
1251
+ } else if (doc.shared_from_uid) {
1252
+ const shared_from_uid_ret = await get_account_name({ uid_query: doc.shared_from_uid });
1253
+ ret = shared_from_uid_ret.data.profile_avatar;
1254
+ } else if (doc.contact_uid) {
1201
1255
  ret = `xu-pattern.png`;
1202
1256
  } else {
1203
1257
  switch (doc.source) {
@@ -1218,14 +1272,17 @@ const get_contact_pattern = function (doc) {
1218
1272
  };
1219
1273
 
1220
1274
  const get_contact_border = function (doc) {
1221
- let ret = `transparent`;
1275
+ const disable_color = `#1a3557`;
1276
+ let ret;
1222
1277
 
1223
1278
  if (doc.pending) {
1224
- ret = `#6f6c67`;
1279
+ ret = `#6f6c67`; // gray
1280
+ } else if (doc.my_contact) {
1281
+ ret = `transparent`; // gray
1225
1282
  } else {
1226
1283
  switch (doc.connection_stat) {
1227
1284
  case 0: {
1228
- ret = `transparent`;
1285
+ ret = disable_color;
1229
1286
  break;
1230
1287
  }
1231
1288
  case 1: {
@@ -1239,7 +1296,7 @@ const get_contact_border = function (doc) {
1239
1296
  }
1240
1297
 
1241
1298
  case 3: {
1242
- ret = `#0070ff`;
1299
+ ret = `#164a28`; //`#0070ff`;
1243
1300
  break;
1244
1301
  }
1245
1302
 
@@ -1249,15 +1306,17 @@ const get_contact_border = function (doc) {
1249
1306
  }
1250
1307
 
1251
1308
  default:
1309
+ ret = disable_color;
1252
1310
  break;
1253
1311
  }
1254
1312
  }
1255
1313
  return ret;
1256
1314
  };
1257
1315
 
1258
- function formatPhoneWithFlag(phoneString) {
1316
+ function formatPhoneWithFlag(phoneString, country = 'US') {
1259
1317
  // try {
1260
- const phoneNumber = parsePhoneNumber(phoneString);
1318
+
1319
+ const phoneNumber = parsePhoneNumber(phoneString, country);
1261
1320
  if (phoneNumber && phoneNumber.country) {
1262
1321
  const flag = countryCodeEmoji(phoneNumber.country);
1263
1322
  const formatted = phoneNumber.formatInternational(); // e.g., +1 213 373 4253
@@ -1282,45 +1341,155 @@ export const get_contact_info = async function (uid, contact_doc, _id) {
1282
1341
  }
1283
1342
 
1284
1343
  doc.profile_avatar = account_info_ret.data.profile_avatar;
1285
- doc.card_background = get_contact_background(doc);
1286
1344
 
1287
1345
  //membership_plan
1288
1346
  doc.name = `${account_info_ret.data.first_name} ${account_info_ret.data.last_name}`;
1289
1347
 
1290
- if (account_info_ret.data.phone) {
1348
+ if (account_info_ret.data.phone_number) {
1291
1349
  try {
1292
- doc.telephone = formatPhoneWithFlag(account_info_ret.data.phone);
1293
- } catch (error) {
1294
- doc.telephone = account_info_ret.data.phone;
1350
+ doc.phone_number = formatPhoneWithFlag(account_info_ret.data.phone_number, account_info_ret.data.country);
1351
+ } catch (err) {
1352
+ console.error(err);
1353
+ doc.phone_number = account_info_ret.data.phone_number;
1295
1354
  }
1296
1355
  }
1297
1356
 
1298
- // doc.online = true;
1299
1357
  if (doc.team_req_id) {
1300
1358
  try {
1301
1359
  const req_doc = await db_module.get_couch_doc_native('xuda_team', doc.team_req_id);
1302
1360
  doc.connection_stat = req_doc.team_req_stat;
1361
+ doc.friend_since = req_doc.team_req_date;
1303
1362
  } catch (error) {
1304
1363
  doc.connection_stat = 0;
1305
1364
  }
1306
1365
  }
1307
1366
 
1308
- doc.icon_pattern = get_contact_pattern(doc);
1367
+ doc.icon_pattern = await get_contact_pattern(doc);
1368
+ doc.username = account_info_ret.data.username;
1369
+ doc.business_name = account_info_ret.data.business_name;
1370
+ doc.address = account_info_ret.data.address;
1371
+ doc.city = account_info_ret.data.city;
1372
+ doc.state = account_info_ret.data.state;
1373
+ doc.zip = account_info_ret.data.zip;
1374
+ } else {
1375
+ doc.email = doc.email?.[0];
1309
1376
  }
1310
1377
 
1378
+ doc.interactions = 0;
1379
+ doc.chats = 0;
1380
+
1311
1381
  const chats_ret = await ai_module.get_ai_chats({ uid, reference_id: doc._id });
1312
1382
  for (let chat_doc of chats_ret.data.docs) {
1313
1383
  doc.notifications += chat_doc.notifications;
1384
+
1385
+ doc.chats++;
1386
+ if (chat_doc.interactions) {
1387
+ doc.interactions += chat_doc.interactions;
1388
+ }
1389
+ doc.contact_mood_level = chat_doc.mood_level || 0;
1314
1390
  }
1315
1391
 
1392
+ doc.card_background = get_contact_background(doc);
1393
+
1316
1394
  doc.border = get_contact_border(doc);
1317
- if (!doc.name) {
1318
- doc.name = doc.email;
1395
+
1396
+ return doc;
1397
+ };
1398
+
1399
+ export const get_pending_contact_out = async function (uid, _id) {
1400
+ let docs = [];
1401
+ const to_opt = {
1402
+ selector: { docType: 'team_request', access_type: 'contact_connection', team_req_stat: 2, team_req_from_uid: uid },
1403
+ 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'],
1404
+ };
1405
+
1406
+ if (_id) {
1407
+ to_opt.selector._id = _id;
1319
1408
  }
1409
+ const requests_to_res = await db_module.find_couch_query('xuda_team', to_opt);
1320
1410
 
1321
- doc.email = '';
1411
+ for await (let e of requests_to_res.docs) {
1412
+ 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 };
1322
1413
 
1323
- return doc;
1414
+ if (e.team_req_to_uid) {
1415
+ const account_info_ret = await get_account_name({ uid_query: e.team_req_to_uid });
1416
+ if (account_info_ret.code < 0) {
1417
+ continue; // throw new Error(`account ${contact_uid} not found`);
1418
+ }
1419
+ doc.profile_avatar = account_info_ret.data.profile_avatar;
1420
+ doc.name = `${account_info_ret.data.first_name} ${account_info_ret.data.last_name}`;
1421
+ }
1422
+ doc.icon_pattern = await get_contact_pattern(doc);
1423
+ doc.card_background = get_contact_background({ pending: true });
1424
+ docs.push(doc);
1425
+ }
1426
+
1427
+ return docs;
1428
+ };
1429
+
1430
+ export const get_pending_contact_in = async function (uid, _id) {
1431
+ let docs = [];
1432
+ 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'] };
1433
+
1434
+ if (_id) {
1435
+ to_opt.selector._id = _id;
1436
+ }
1437
+
1438
+ const requests_from_res = await db_module.find_couch_query('xuda_team', from_opt);
1439
+
1440
+ for await (let e of requests_from_res.docs) {
1441
+ 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 };
1442
+
1443
+ if (e.team_req_from_uid) {
1444
+ const account_info_ret = await get_account_name({ uid_query: e.team_req_from_uid });
1445
+ if (account_info_ret.code < 0) {
1446
+ continue; // throw new Error(`account ${contact_uid} not found`);
1447
+ }
1448
+ doc.profile_avatar = account_info_ret.data.profile_avatar;
1449
+ doc.name = `${account_info_ret.data.first_name} ${account_info_ret.data.last_name}`;
1450
+ }
1451
+ doc.card_background = get_contact_background({ pending: true });
1452
+ doc.icon_pattern = await get_contact_pattern(doc);
1453
+ docs.push(doc);
1454
+ }
1455
+ return docs;
1456
+ };
1457
+ export const get_pending_share_contact_in = async function (uid, _id) {
1458
+ let docs = [];
1459
+ 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'] };
1460
+
1461
+ if (_id) {
1462
+ to_opt.selector._id = _id;
1463
+ }
1464
+
1465
+ const requests_from_res = await db_module.find_couch_query('xuda_team', from_opt);
1466
+
1467
+ for await (let e of requests_from_res.docs) {
1468
+ // 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 };
1469
+ let doc = await db_module.get_couch_doc_native('xuda_contacts', e.share_item_id);
1470
+ delete doc.contact_mood_level;
1471
+ doc.shared_from_uid = e.team_req_from_uid;
1472
+ doc.pending = true;
1473
+ doc = await get_contact_info(uid, doc);
1474
+ doc.team_req_id = e._id;
1475
+
1476
+ docs.push(doc);
1477
+ }
1478
+ return docs;
1479
+ };
1480
+
1481
+ const get_user_contact = async function (uid, uid_query) {
1482
+ const my_contact_ret = await get_account_name({ uid_query });
1483
+ const my_account_info = my_contact_ret.data;
1484
+ 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 };
1485
+ my_contact_doc.profile_avatar = my_account_info.profile_avatar;
1486
+ my_contact_doc.card_background = get_contact_background(my_contact_doc);
1487
+ my_contact_doc.icon_pattern = await get_contact_pattern(my_contact_doc);
1488
+ my_contact_doc.border = get_contact_border(my_contact_doc);
1489
+ my_contact_doc.auto_respond = my_account_info.auto_respond;
1490
+ my_contact_doc.auto_respond_mode = my_account_info.auto_respond_mode;
1491
+ my_contact_doc.auto_respond_agents = my_account_info.auto_respond_agents;
1492
+ return my_contact_doc;
1324
1493
  };
1325
1494
 
1326
1495
  export const get_contacts = async function (req) {
@@ -1379,80 +1548,151 @@ export const get_contacts = async function (req) {
1379
1548
 
1380
1549
  const contacts = await db_module.find_couch_query('xuda_contacts', opt);
1381
1550
 
1382
- for await (let doc of contacts.docs) {
1383
- doc = await get_contact_info(uid, doc);
1384
- }
1385
-
1386
1551
  let requests_to = { docs: [] };
1387
1552
  let requests_from = { docs: [] };
1388
-
1553
+ let shared_from = { docs: [] };
1554
+ let my_contact = { docs: [] };
1389
1555
  if (with_requests) {
1390
- ////// TO
1391
- const requests_to_res = await db_module.find_couch_query('xuda_team', {
1392
- selector: { docType: 'team_request', access_type: 'contact', team_req_stat: 2, team_req_from_uid: uid },
1393
- 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'],
1556
+ ////// TO - OUT
1557
+ requests_to.docs = await get_pending_contact_out(uid);
1558
+
1559
+ ////// FROM - IN
1560
+ requests_from.docs = await get_pending_contact_in(uid);
1561
+ shared_from.docs = await get_pending_share_contact_in(uid);
1562
+ // my contact
1563
+ // const my_contact_ret = await get_account_name({ uid_query: uid }); //await db_module.get_couch_doc_native('xuda_contacts', sender_contact_id);
1564
+ // const my_account_info = my_contact_ret.data;
1565
+ // 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 };
1566
+ // my_contact_doc.profile_avatar = my_account_info.profile_avatar;
1567
+ // my_contact_doc.card_background = get_contact_background(my_contact_doc);
1568
+ // my_contact_doc.icon_pattern = await get_contact_pattern(my_contact_doc);
1569
+ // my_contact_doc.border = get_contact_border(my_contact_doc);
1570
+ // my_contact_doc.auto_respond = my_account_info.auto_respond;
1571
+ // my_contact_doc.auto_respond_mode = my_account_info.auto_respond_mode;
1572
+ // my_contact_doc.auto_respond_agents = my_account_info.auto_respond_agents;
1573
+ const my_contact_doc = await get_user_contact(uid, uid);
1574
+ my_contact.docs = [my_contact_doc];
1575
+ }
1576
+ let contact_docs = { docs: [] };
1577
+ for await (let doc of contacts.docs) {
1578
+ const ret_to = requests_to.docs.find((e) => {
1579
+ return e.contact_uid === doc.contact_uid;
1394
1580
  });
1581
+ if (ret_to) continue;
1395
1582
 
1396
- for await (let e of requests_to_res.docs) {
1397
- 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 };
1398
-
1399
- if (e.team_req_to_uid) {
1400
- const account_info_ret = await get_account_name({ uid_query: e.team_req_to_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.icon_pattern = get_contact_pattern(doc);
1408
- doc.card_background = get_contact_background({ pending: true });
1409
- requests_to.docs.push(doc);
1410
- }
1411
-
1412
- ////// FROM
1413
- 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'] });
1414
-
1415
- for await (let e of requests_from_res.docs) {
1416
- 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 };
1583
+ const ret_from = requests_from.docs.find((e) => {
1584
+ return e.contact_uid === doc.contact_uid;
1585
+ });
1586
+ if (ret_from) continue;
1417
1587
 
1418
- if (e.team_req_from_uid) {
1419
- const account_info_ret = await get_account_name({ uid_query: e.team_req_from_uid });
1420
- if (account_info_ret.code < 0) {
1421
- continue; // throw new Error(`account ${contact_uid} not found`);
1422
- }
1423
- doc.profile_avatar = account_info_ret.data.profile_avatar;
1424
- doc.name = `${account_info_ret.data.first_name} ${account_info_ret.data.last_name}`;
1425
- }
1426
- doc.card_background = get_contact_background({ pending: true });
1427
- doc.icon_pattern = get_contact_pattern(doc);
1428
- requests_from.docs.push(doc);
1429
- }
1588
+ doc = await get_contact_info(uid, doc);
1589
+ contact_docs.docs.push(doc);
1430
1590
  }
1431
1591
 
1432
1592
  return {
1433
1593
  code: 1,
1434
1594
  data: {
1435
- docs: [...requests_to.docs, ...requests_from.docs, ...contacts.docs],
1595
+ docs: [...my_contact.docs, ...shared_from.docs, ...requests_from.docs, ...requests_to.docs, ...contact_docs.docs],
1436
1596
  },
1437
1597
  };
1438
1598
  };
1439
1599
 
1440
1600
  export const archive_contact = async function (req) {
1441
- const { _id } = req;
1601
+ const { contact_id } = req;
1602
+ try {
1603
+ const contact_ret = await db_module.get_couch_doc('xuda_contacts', contact_id);
1604
+ if (contact_ret.code < 0) {
1605
+ return contact_ret;
1606
+ }
1607
+ var contact_doc = contact_ret.data;
1608
+ contact_doc.stat = 2;
1609
+ contact_doc.stat_ts = Date.now();
1610
+ contact_doc.stat_reason = 'archived by the user';
1611
+
1612
+ const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
1613
+
1614
+ return contact_save_ret;
1615
+ } catch (err) {
1616
+ return {
1617
+ code: -22,
1618
+ data: err.message,
1619
+ };
1620
+ }
1621
+ };
1622
+
1623
+ export const unfriend_contact = async function (req) {
1624
+ const { contact_id } = req;
1625
+ try {
1626
+ const contact_ret = await db_module.get_couch_doc('xuda_contacts', contact_id);
1627
+ if (contact_ret.code < 0) {
1628
+ throw new Error(contact_ret.data);
1629
+ }
1630
+ var contact_doc = contact_ret.data;
1631
+
1632
+ if (!contact_doc.team_req_id) {
1633
+ throw new Error('no friend relationship found');
1634
+ }
1635
+ let req_doc = await db_module.get_couch_doc_native('xuda_team', contact_doc.team_req_id);
1636
+ req_doc.team_req_stat = 4;
1637
+ req_doc.team_req_stat_desc = 'unfriend by the user';
1638
+ await db_module.get_couch_doc_native('xuda_team', contact_doc.team_req_id);
1639
+
1640
+ contact_doc.team_req_id = null;
1641
+ const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
1442
1642
 
1443
- const contact_ret = await db_module.get_couch_doc('xuda_contacts', _id);
1444
- if (contact_ret.code < 0) {
1445
- return contact_ret;
1643
+ const req_save_ret = await db_module.save_couch_doc('xuda_team', req_doc);
1644
+
1645
+ return req_save_ret;
1646
+ } catch (err) {
1647
+ return {
1648
+ code: -21,
1649
+ data: err.message,
1650
+ };
1651
+ }
1652
+ };
1653
+
1654
+ export const pin_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 = true;
1663
+
1664
+ const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
1665
+
1666
+ return contact_save_ret;
1667
+ } catch (err) {
1668
+ return {
1669
+ code: -24,
1670
+ data: err.message,
1671
+ };
1446
1672
  }
1447
- var contact_doc = contact_ret.data;
1448
- contact_doc.stat = 4;
1449
- contact_doc.stat_ts = Date.now();
1450
- contact_doc.stat_reason = 'deleted';
1673
+ };
1674
+
1675
+ export const unpin_contact = async function (req) {
1676
+ const { contact_id } = req;
1677
+ try {
1678
+ const contact_ret = await db_module.get_couch_doc('xuda_contacts', contact_id);
1679
+ if (contact_ret.code < 0) {
1680
+ return contact_ret;
1681
+ }
1682
+ var contact_doc = contact_ret.data;
1683
+ contact_doc.pin = false;
1451
1684
 
1452
- const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
1685
+ const contact_save_ret = await db_module.save_couch_doc('xuda_contacts', contact_doc);
1453
1686
 
1454
- return contact_save_ret;
1687
+ return contact_save_ret;
1688
+ } catch (err) {
1689
+ return {
1690
+ code: -24,
1691
+ data: err.message,
1692
+ };
1693
+ }
1455
1694
  };
1695
+
1456
1696
  // invite user
1457
1697
 
1458
1698
  // const contact_ret = await db_module.get_couch_view_raw(
@@ -1468,7 +1708,7 @@ export const archive_contact = async function (req) {
1468
1708
 
1469
1709
  export const add_contact = async function (req, job_id, headers) {
1470
1710
  try {
1471
- const { uid, email, name, stat, contact_uid, context, source, team_req_id, data } = req;
1711
+ const { uid, email, name, stat, contact_uid, context, source, team_req_id, metadata } = req;
1472
1712
  const contact_ret = await db_module.get_couch_view_raw('xuda_contacts', 'contacts_by_email_address', { key: [uid, email] });
1473
1713
 
1474
1714
  if (contact_ret.rows.length) {
@@ -1496,7 +1736,7 @@ export const add_contact = async function (req, job_id, headers) {
1496
1736
  headers,
1497
1737
  source,
1498
1738
  team_req_id,
1499
- data,
1739
+ metadata,
1500
1740
  };
1501
1741
  const ret = await db_module.save_couch_doc('xuda_contacts', doc);
1502
1742
 
@@ -1515,6 +1755,12 @@ export const add_contact = async function (req, job_id, headers) {
1515
1755
  return { code: -1, data: err.message };
1516
1756
  }
1517
1757
  };
1758
+
1759
+ const get_contact_id = async function (uid, contact_uid) {
1760
+ contact_ret = await db_module.get_couch_view_raw('xuda_contacts', 'contacts_by_contact_uid', { key: [uid, contact_uid] });
1761
+ return contact_ret?.rows?.[0]?.id;
1762
+ };
1763
+
1518
1764
  export const update_contact = async function (req) {
1519
1765
  const { uid, email, contact_uid, stat, team_req_id } = req;
1520
1766
  var contact_ret;
@@ -1844,3 +2090,77 @@ export const update_contacts_with_account_changes = async function (uid, changes
1844
2090
  return 'unknown';
1845
2091
  }
1846
2092
  };
2093
+
2094
+ export const get_user_card = async function (uid, uid_query) {
2095
+ if (uid === uid_query) return await get_user_contact(uid, uid_query);
2096
+
2097
+ 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 };
2098
+
2099
+ const contact_ret = await db_module.get_couch_view_raw('xuda_contacts', 'contacts_by_contact_uid', { key: [uid, uid_query] });
2100
+
2101
+ const contact_id = contact_ret?.rows?.[0]?.id;
2102
+ if (contact_id) {
2103
+ user_contact_obj = await get_contact_info(uid, null, contact_id);
2104
+ } else {
2105
+ user_contact_obj = await get_user_contact(uid, uid_query);
2106
+ }
2107
+ return user_contact_obj;
2108
+ };
2109
+
2110
+ // export const generate_user_avatar = async function (req, job_id, headers) {
2111
+ // const { uid, account_info } = req;
2112
+ // try {
2113
+ // const ai_module = await import(`${module_path}/ai_module/index.mjs`);
2114
+
2115
+ // ai_module.convert_profile_image_to_avatar({ url: account_info?.profile_picture, uid, account_obj }, job_id, headers);
2116
+
2117
+ // // const account_info = await get_account_name({ uid });
2118
+ // // let username = 'unknown';
2119
+ // // if (account_info) {
2120
+ // // username = account_info.first_name + ' ' + account_info.last_name;
2121
+
2122
+ // // if (!username) {
2123
+ // // username = +account_info.email;
2124
+ // // }
2125
+ // // }
2126
+
2127
+ // // return username;
2128
+ // } catch (err) {
2129
+ // return 'unknown';
2130
+ // }
2131
+ // };
2132
+
2133
+ export const onboarding_completed = async function (req, job_id, headers) {
2134
+ const { uid } = req;
2135
+ try {
2136
+ let account_doc = await db_module.get_couch_doc_native('xuda_accounts', uid);
2137
+
2138
+ if (!account_doc.account_info?.profile_picture) {
2139
+ throw new Error(`missing profile_picture`);
2140
+ }
2141
+
2142
+ if (!account_doc.account_info?.profile_avatar) {
2143
+ throw new Error(`missing profile_avatar`);
2144
+ }
2145
+
2146
+ if (!account_doc.account_project_id) {
2147
+ const app_module = await import(`${module_path}/app_module/index.mjs`);
2148
+
2149
+ 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);
2150
+ if (ret.code > -1) {
2151
+ account_doc.account_project_id = ret.data;
2152
+ const drive_module = await import(`${module_path}/drive_module/index.mjs`);
2153
+ let drive_req = { app_id: account_doc.account_project_id, uid, path: '/' };
2154
+ await drive_module.create_drive_folder_user({ ...drive_req, ...{ folder_name: 'Profile Images', is_system: true } }, job_id, headers);
2155
+ await drive_module.create_drive_folder_user({ ...drive_req, ...{ folder_name: 'Chat Thumbnails', is_system: true } }, job_id, headers);
2156
+ await drive_module.create_drive_folder_user({ ...drive_req, ...{ folder_name: 'Chat Attachments', is_system: true } }, job_id, headers);
2157
+ }
2158
+ }
2159
+
2160
+ account_doc.onboarding = { headers, date_ts: Date.now() };
2161
+ const save_ret = await db_module.save_couch_doc('xuda_accounts', account_doc);
2162
+ return save_ret;
2163
+ } catch (err) {
2164
+ return { code: -44, data: err.message };
2165
+ }
2166
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xuda.io/account_module",
3
- "version": "1.2.523",
3
+ "version": "1.2.750",
4
4
  "description": "Xuda Account Server Module",
5
5
  "main": "index.mjs",
6
6
  "dependencies": {