tango-app-api-trax 3.3.1-beta-9 → 3.3.1-beta-11

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tango-app-api-trax",
3
- "version": "3.3.1-beta-9",
3
+ "version": "3.3.1-beta-11",
4
4
  "description": "Trax",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -859,11 +859,11 @@ export const assignedUserDetails = async ( req, res ) => {
859
859
  if ( !req.params.checklistId ) {
860
860
  return res.sendError( { message: 'checklist Id is required' }, 400 );
861
861
  }
862
- let limit = parseInt( req.query.limit ) || 10;
862
+ let limit = parseInt( req.query.limit ) || 5;
863
863
  let page = parseInt( req.query.offset ) || 0;
864
864
  let skip = limit * page;
865
865
 
866
- let query = [ { $match: { checkListId: new ObjectId( req.params.checklistId ) } } ];
866
+ let query = [ { $match: { checkListId: new ObjectId( req.params.checklistId ), coverage: req.body.coverage } } ];
867
867
  if ( req.query.search.trim() && req.query.search.trim() != '' ) {
868
868
  let searchValue = req.query.search;
869
869
  searchValue = searchValue.split( ',' ).map( ( item ) => item.trim().toLowerCase() );
@@ -874,52 +874,89 @@ export const assignedUserDetails = async ( req, res ) => {
874
874
  query.push( { $match: { storeName: { $regex: req.query.search.trim(), $options: 'i' } } } );
875
875
  }
876
876
  }
877
- if ( req.query.sortColumn && req.query.sortBy ) {
878
- query.push( { $sort: { [req.query.sortColumn]: parseInt( req.query.sortBy ) } } );
879
- } else {
880
- query.push( { $sort: { _id: -1 } } );
881
- }
882
877
 
883
- query.push( {
884
- $facet: {
885
- data: [
886
- { $skip: skip }, { $limit: limit },
887
- ],
888
- count: [
889
- { $count: 'total' },
890
- ],
891
- },
892
- } );
878
+ query.push( { $sort: { _id: -1 } } );
893
879
 
894
880
  let checklistDetails = await assignedService.aggregate( query );
895
-
896
- if ( !checklistDetails[0].data.length ) {
881
+ if ( !checklistDetails.length ) {
897
882
  return res.sendError( 'no data found', 204 );
898
883
  }
899
884
 
900
885
  let userDetails = [];
901
886
  let storeList = [];
902
887
  let userList = [];
903
- checklistDetails[0].data.forEach( ( item ) => {
904
- if ( item?.store_id ) {
905
- storeList.push( item.assignId );
888
+ await Promise.all( checklistDetails.map( async ( ele ) => {
889
+ if ( ele?.store_id || ele?.clusterName ) {
890
+ storeList.push( ele.assignId );
906
891
  } else {
907
- userList.push( item.assignId );
892
+ userList.push( ele.assignId );
893
+ }
894
+
895
+ if ( ele?.clusterName ) {
896
+ let clusterDetails = await clusterServices.findOneCluster( { _id: ele.assignId }, { stores: 1 } );
897
+ if ( clusterDetails ) {
898
+ let storeDetails = await storeService.find( { _id: { $in: clusterDetails.stores.map( ( item ) => item.store ) } } );
899
+ storeDetails.forEach( ( item ) => {
900
+ userDetails.push( {
901
+ id: ele._id,
902
+ userName: item.spocDetails?.[0]?.name,
903
+ userEmail: item.spocDetails?.[0]?.email,
904
+ store_id: item?.storeId,
905
+ storeName: item?.storeName,
906
+ userPhone: item.spocDetails?.[0]?.phone,
907
+ city: ele?.city,
908
+ checkFlag: ele.checkFlag,
909
+ clusterName: ele?.clusterName || '',
910
+ teamName: ele?.teamName || '',
911
+ } );
912
+ } );
913
+ }
914
+ } else if ( ele?.teamName ) {
915
+ let teamDetails = await teamsServices.findOneTeams( { _id: ele.assignId }, { users: 1, teamName: 1 } );
916
+ if ( teamDetails ) {
917
+ let teamUserDetails = await userService.find( { _id: { $in: teamDetails.users.map( ( item ) => item.userId ) } } );
918
+ teamUserDetails.forEach( ( item ) => {
919
+ userDetails.push( {
920
+ id: ele._id,
921
+ userName: item.userName,
922
+ userEmail: item.email,
923
+ store_id: ele?.storeId,
924
+ storeName: ele?.storeName,
925
+ userPhone: item?.mobileNumber,
926
+ city: ele?.city,
927
+ checkFlag: ele.checkFlag,
928
+ clusterName: ele?.clusterName || '',
929
+ teamName: ele?.teamName || '',
930
+ } );
931
+ } );
932
+ }
933
+ } else {
934
+ userDetails.push( {
935
+ id: ele._id,
936
+ userName: ele.userName,
937
+ userEmail: ele.userEmail,
938
+ store_id: ele?.store_id,
939
+ storeName: ele?.storeName,
940
+ userPhone: ele.userPhone,
941
+ city: ele.city,
942
+ checkFlag: ele.checkFlag,
943
+ clusterName: ele?.clusterName || '',
944
+ teamName: ele?.teamName || '',
945
+ } );
908
946
  }
909
- userDetails.push( {
910
- id: item._id,
911
- userName: item.userName,
912
- userEmail: item.userEmail,
913
- store_id: item?.store_id,
914
- storeName: item?.storeName,
915
- userPhone: item.userPhone,
916
- city: item.city,
917
- checkFlag: item.checkFlag,
918
- clusterName: item?.clusterName,
919
- teamName: item?.teamName,
947
+ } ) );
948
+ if ( req.query.sortColumn && req.query.sortBy ) {
949
+ const sortColumn = req.query.sortColumn;
950
+ const sortBy = parseInt( req.query.sortBy, 10 ) === -1 ? -1 : 1;
951
+ userDetails.sort( ( a, b ) => {
952
+ if ( a[sortColumn] == null || b[sortColumn] == null ) return 0;
953
+ if ( typeof a[sortColumn] === 'string' && typeof b[sortColumn] === 'string' ) {
954
+ return sortBy * a[sortColumn].localeCompare( b[sortColumn] );
955
+ }
920
956
  } );
921
- } );
922
- return res.sendSuccess( { users: userDetails, count: checklistDetails[0].count[0].total, storeList, userList } );
957
+ }
958
+ let limitedData = userDetails.slice( skip, limit + skip );
959
+ return res.sendSuccess( { count: userDetails.length, storeList, userList, users: limitedData } );
923
960
  } catch ( e ) {
924
961
  logger.error( 'assignedUserDetails =>', e );
925
962
  return res.sendError( e, 500 );
@@ -1266,6 +1303,226 @@ export const updateConfigure =async ( req, res ) => {
1266
1303
  }
1267
1304
  };
1268
1305
 
1306
+ export const updateConfigurev1 =async ( req, res ) => {
1307
+ try {
1308
+ let inputBody = req.body;
1309
+ let id;
1310
+ let checklistDetails;
1311
+ if ( !inputBody.checkListDetails._id ) {
1312
+ return res.sendError( 'checkListId is Required', 400 );
1313
+ }
1314
+
1315
+ if ( inputBody.checkListDetails.checkListType == 'mobileusagedetection' && inputBody.submitType == 'publish' ) {
1316
+ if ( !inputBody?.checkListDetails?.alert?.usageExceeds ) {
1317
+ return res.sendError( 'Please Enter usage exceeds', 400 );
1318
+ }
1319
+
1320
+ if ( !inputBody?.checkListDetails?.alert?.alertsTo.length ) {
1321
+ return res.sendError( 'Please select users to send alert', 400 );
1322
+ }
1323
+
1324
+ if ( !inputBody?.checkListDetails?.detectionArea.length ) {
1325
+ return res.sendError( 'Please select detection area', 400 );
1326
+ }
1327
+ }
1328
+
1329
+ if ( !inputBody.checkListDetails.assignedUsers.length && inputBody.submitType == 'publish' ) {
1330
+ return res.sendError( 'Please Assigned a user', 400 );
1331
+ }
1332
+
1333
+ if ( !inputBody?.checkListDetails?.approver.length && inputBody.submitType == 'publish' ) {
1334
+ return res.sendError( 'Please assign approver', 400 );
1335
+ }
1336
+
1337
+
1338
+ let logInsertData = {
1339
+ action: inputBody.submitType == 'publish' ? 'checklistPublishUsingConfigPage' : 'checklistConfigDraft',
1340
+ checklistId: inputBody.checkListDetails._id,
1341
+ checkListName: inputBody?.checkListDetails.checkListName,
1342
+ createdBy: req.user._id,
1343
+ createdByName: req.user.userName,
1344
+ client_id: req.body.clientId,
1345
+ createdByEmail: req.user.email,
1346
+ approver: inputBody?.checkListDetails?.approver || [],
1347
+ };
1348
+ await checklistLogs.create( logInsertData );
1349
+
1350
+ checklistDetails = await checklistService.findOne( { _id: inputBody.checkListDetails._id, type: 'checklist', isdeleted: false } );
1351
+
1352
+ if ( [ 'mobileusagedetection', 'storeopenandclose', 'uniformdetection' ].includes( inputBody.checkListDetails.checkListType ) && inputBody.uploadUser ) {
1353
+ checklistDetails = await checklistService.findOne( { _id: inputBody.checkListDetails._id, type: 'checklist' } );
1354
+ }
1355
+
1356
+ if ( !checklistDetails ) {
1357
+ return res.sendError( 'no data found', 204 );
1358
+ }
1359
+
1360
+ let currentDate = dayjs.utc().format();
1361
+ let updatedscheduleEndTimeISO = dayjs.utc( inputBody?.checkListDetails?.scheduleEndTime, 'hh:mm A' ).format( 'HH:mm:ss' );
1362
+ let newUpdatedDate = dayjs.utc( updatedscheduleEndTimeISO, 'HH:mm:ss' ).format();
1363
+
1364
+ if ( inputBody.submitType == 'publish' && inputBody?.showEdit && typeof inputBody?.editSubmit == 'undefined' && newUpdatedDate > currentDate ) {
1365
+ let checkSubmitDetails = await processedchecklist.findOne( { sourceCheckList_id: inputBody.checkListDetails._id, date_string: dayjs().format( 'YYYY-MM-DD' ), checklistStatus: 'submit' } );
1366
+ if ( checkSubmitDetails ) {
1367
+ return res.sendError( 'Checklist got submitted', 400 );
1368
+ }
1369
+ }
1370
+
1371
+ if ( checklistDetails?.publishDate && inputBody.submitType == 'publish' ) {
1372
+ let date = dayjs();
1373
+ let diff = date.diff( dayjs.utc( checklistDetails?.publishDate ), 'minutes' );
1374
+ if ( diff < 5 ) {
1375
+ let mins = ( 5 - diff ) > 1 ? 'minutes' : 'minute';
1376
+ return res.sendError( `Please try after ${5 - diff} ${mins}`, 400 );
1377
+ }
1378
+ }
1379
+
1380
+ if ( inputBody?.checkListDetails?.scheduleRepeatedMonthSetup == 'date' ) {
1381
+ inputBody.checkListDetails.scheduleRepeatedDay = inputBody?.checkListDetails?.scheduleRepeatedDay;
1382
+ }
1383
+
1384
+ if ( inputBody?.checkListDetails?.scheduleDate ) {
1385
+ inputBody.checkListDetails.scheduleDate = new Date( inputBody?.checkListDetails?.scheduleDate );
1386
+ }
1387
+ let configDetails = {
1388
+ ...inputBody?.checkListDetails,
1389
+ publishDate: inputBody.checkListDetails.publish ? new Date() : '',
1390
+ scheduleStartTimeISO: dayjs.utc( inputBody?.checkListDetails?.scheduleStartTime, 'hh:mm A' ).format(),
1391
+ scheduleEndTimeISO: dayjs.utc( inputBody?.checkListDetails?.scheduleEndTime, 'hh:mm A' ).format(),
1392
+ scheduleDate: inputBody?.checkListDetails?.scheduleDate ? dayjs( inputBody.checkListDetails.scheduleDate ).format() : '',
1393
+ approver: inputBody?.checkListDetails?.approver.length ? inputBody?.checkListDetails?.approver.map( ( item ) => {
1394
+ return { name: item.name, value: item.value };
1395
+ } ) : [],
1396
+ };
1397
+
1398
+ if ( inputBody?.checkListDetails?.approver.length ) {
1399
+ let data = [];
1400
+ let existEmail = await traxApprover.find( { checkListId: inputBody.checkListDetails._id, isDeleted: false } );
1401
+ let mailList = existEmail.map( ( item ) => item.userEmail );
1402
+ existEmail = inputBody?.checkListDetails?.approver.filter( ( item ) => mailList.includes( item.value ) ).map( ( item ) => item.value );
1403
+ let userMailList = inputBody?.checkListDetails?.approver.map( ( ele ) => ele.value );
1404
+ inputBody?.checkListDetails?.approver.forEach( ( ele ) => {
1405
+ if ( !existEmail.includes( ele.value ) ) {
1406
+ data.push( {
1407
+ userEmail: ele.value,
1408
+ checkListId: inputBody.checkListDetails._id,
1409
+ type: 'checklist',
1410
+ checkListName: inputBody?.checkListDetails.checkListName,
1411
+ client_id: inputBody?.clientId,
1412
+ } );
1413
+ }
1414
+ } );
1415
+
1416
+ await traxApprover.updateMany( { checkListId: inputBody.checkListDetails._id, userEmail: { $nin: userMailList } }, { isDeleted: true } );
1417
+ if ( data.length ) {
1418
+ await traxApprover.insertMany( data );
1419
+ }
1420
+ }
1421
+
1422
+ if ( checklistDetails.checkListType != 'custom' && !checklistDetails.client_id ) {
1423
+ let getchecklistNumber = await checklistService.findOne( { client_id: req.body.clientId, type: 'checklist' }, { checkListNumber: 1 } );
1424
+ let name = checklistDetails.checkListName.split( '(' )[0];
1425
+ let checkListNameDetails = await checklistService.find( { checkListName: { $regex: name }, client_id: req.body.clientId, type: 'checklist', isdeleted: false } );
1426
+ if ( checkListNameDetails.length ) {
1427
+ let nameLength = ( checkListNameDetails.length-1 ) + 1;
1428
+ checklistDetails.checkListName = checklistDetails.checkListName.split( '(' )[0] + '(' + nameLength + ')';
1429
+ } else {
1430
+ checklistDetails.checkListName = checklistDetails.checkListName;
1431
+ }
1432
+ configDetails.type = 'checklist';
1433
+ configDetails.checkListName = checklistDetails.checkListName;
1434
+ configDetails.checkListDescription = checklistDetails.checkListDescription;
1435
+ if ( getchecklistNumber ) {
1436
+ configDetails.checkListNumber = getchecklistNumber.checkListNumber + 1;
1437
+ } else {
1438
+ configDetails.checkListNumber = 1;
1439
+ }
1440
+ configDetails.createdBy= req.user._id;
1441
+ configDetails.createdByName=req.user.userName;
1442
+ delete inputBody.checkListDetails['_id'];
1443
+ if ( [ 'storeopenandclose', 'uniformdetection' ].includes( checklistDetails.checkListType ) ) {
1444
+ let query = { client_id: req.body.clientId, type: 'checklist', isdeleted: false };
1445
+ if ( checklistDetails.checkListType == 'storeopenandclose' ) {
1446
+ query.checkListType= 'storeopenandclose';
1447
+ } else {
1448
+ query.checkListType= 'uniformdetection';
1449
+ }
1450
+ let storeChecklisDetails = await checklistService.findOne( query );
1451
+ if ( storeChecklisDetails ) {
1452
+ inputBody.checkListDetails._id = storeChecklisDetails._id;
1453
+ }
1454
+ }
1455
+ }
1456
+ let query = {
1457
+ client_id: req.body.clientId,
1458
+ checkListType: checklistDetails.checkListType,
1459
+ _id: inputBody.checkListDetails._id,
1460
+ };
1461
+ let response;
1462
+ if ( inputBody.checkListDetails._id ) {
1463
+ if ( [ 'mobileusagedetection', 'storeopenandclose', 'uniformdetection' ].includes( inputBody.checkListDetails.checkListType ) && inputBody.uploadUser ) {
1464
+ configDetails.isdeleted = false;
1465
+ }
1466
+ id = inputBody.checkListDetails._id;
1467
+ response = await checklistService.updateOne( query, configDetails );
1468
+ } else {
1469
+ configDetails.client_id = req.body.clientId;
1470
+ configDetails.checkListType = checklistDetails.checkListType;
1471
+ response = await checklistService.create( configDetails );
1472
+ id = response._id;
1473
+ }
1474
+ // if ( inputBody.checkListDetails.assignedUsers.length ) {
1475
+ // await assignedService.deleteMany( { checkListId: inputBody.checkListDetails._id } );
1476
+ // await Promise.all( inputBody.checkListDetails.assignedUsers.map( async ( user ) => {
1477
+ // let data = {
1478
+ // ...user,
1479
+ // clientId: req.body.clientId,
1480
+ // checkListName: checklistDetails.checkListName,
1481
+ // checklistId: checklistDetails._id,
1482
+ // };
1483
+ // await assignUsers( data );
1484
+ // } ) );
1485
+ // }
1486
+ if ( inputBody.submitType == 'publish' ) {
1487
+ if ( inputBody.checkListDetails.checkListType == 'custom' ) {
1488
+ let currentDate = dayjs.utc().format();
1489
+ let updatedscheduleEndTimeISO = dayjs.utc( configDetails.scheduleEndTimeISO ).format( 'HH:mm:ss' );
1490
+ let newUpdatedDate = dayjs.utc( updatedscheduleEndTimeISO, 'HH:mm:ss' ).format();
1491
+ if ( newUpdatedDate > currentDate ) {
1492
+ let deleteQuery = {
1493
+ $and: [
1494
+ { date_string: dayjs().format( 'YYYY-MM-DD' ) },
1495
+ { sourceCheckList_id: new ObjectId( req.params.checklistId ) },
1496
+ { scheduleEndTime_iso: { $gt: currentDate } },
1497
+ ],
1498
+ };
1499
+ deleteQuery.$and.push( { checklistStatus: { $ne: 'submit' } } );
1500
+ let PClicklist = await processedchecklist.deleteMany( deleteQuery );
1501
+ logger.info( { function: 'updateConfigure', query: deleteQuery } );
1502
+ if ( PClicklist.acknowledged || inputBody.updateConfigure ) {
1503
+ await insertSingleProcessData( inputBody.checkListDetails._id, 0, inputBody.updateConfigure, inputBody?.editSubmit, inputBody?.showEdit );
1504
+ } else {
1505
+ res.sendError( 'something went wrong, please try again', 500 );
1506
+ }
1507
+ } else {
1508
+ logger.info( `Schudled End Time Breached Checklist publish true => Checklist Name: ${checklistDetails.checkListName}` );
1509
+ }
1510
+
1511
+ futureDaysDataRemove( currentDate, req.params.checklistId, checklistDetails.checkListName, '333' );
1512
+ }
1513
+ }
1514
+ if ( response?.modifiedCount || response?.matchedCount || response?.upsertedCount ) {
1515
+ return res.sendSuccess( { id, message: 'Configured Updated Successfully' } );
1516
+ }
1517
+ if ( response._id ) {
1518
+ return res.sendSuccess( { id, message: 'Configured Added Successfully' } );
1519
+ }
1520
+ } catch ( e ) {
1521
+ logger.error( 'updateConfigure =>', e );
1522
+ return res.sendError( e, 500 );
1523
+ }
1524
+ };
1525
+
1269
1526
  export const updatePublish = async ( req, res ) => {
1270
1527
  try {
1271
1528
  if ( typeof req?.body?.checklistId == 'undefined' && typeof req.body.type == 'undefined' ) {
@@ -1594,6 +1851,147 @@ export const validateUser = async ( req, res ) => {
1594
1851
  }
1595
1852
  };
1596
1853
 
1854
+ export const validateUserv1 = async ( req, res ) => {
1855
+ try {
1856
+ if ( !req.body.assignedUsers.length ) {
1857
+ return res.sendError( 'Please Enter user Details', 400 );
1858
+ }
1859
+
1860
+ let assignDetails = req.body.assignedUsers;
1861
+
1862
+ const duplicateStore = assignDetails.reduce( ( acc, obj ) => {
1863
+ if ( !acc[obj.storeName.toLowerCase()] ) {
1864
+ acc[obj.storeName.toLowerCase()] = {
1865
+ email: [ obj.userEmail.toLowerCase() ],
1866
+ count: 1,
1867
+ };
1868
+ } else {
1869
+ if ( acc[obj.storeName.toLowerCase()] ) {
1870
+ if ( acc[obj.storeName.toLowerCase()].email.includes( obj.userEmail.toLowerCase() ) ) {
1871
+ acc[obj.storeName.toLowerCase()].count++;
1872
+ } else {
1873
+ acc[obj.storeName.toLowerCase()].email.push( obj.userEmail.toLowerCase() );
1874
+ }
1875
+ }
1876
+ }
1877
+ return acc;
1878
+ }, {} );
1879
+
1880
+ const duplicateStores = Object.keys( duplicateStore ).filter( ( storeName ) => duplicateStore[storeName].count > 1 );
1881
+ if ( duplicateStores.length ) {
1882
+ return res.sendSuccess( { validate: false, ExistsEmail: duplicateStores, message: 'store and email is Duplicated' } );
1883
+ }
1884
+
1885
+ if ( req.body.hasOwnProperty( 'delete' ) ) {
1886
+ let checkExists = await assignedService.findOne( { userEmail: assignDetails[0].userEmail, storeName: assignDetails[0].storeName, checkListId: req.body.id } );
1887
+ if ( checkExists ) {
1888
+ return res.sendError( 'User already Exists', 400 );
1889
+ }
1890
+ }
1891
+ // let userChunk =await chunkArray( users, 10 );
1892
+
1893
+ await Promise.all( users.map( async ( chunk ) => {
1894
+ await processUser( [ chunk ] );
1895
+ } ) );
1896
+
1897
+ let userEmailList = assignDetails.map( ( item ) => item.userEmail.toLowerCase() );
1898
+ let storeList = assignDetails.map( ( item ) => item.storeName.toLowerCase() );
1899
+
1900
+ let userQuery = [
1901
+ {
1902
+ $project: {
1903
+ newEmail: { $toLower: '$email' },
1904
+ isActive: 1,
1905
+ clientId: 1,
1906
+ },
1907
+ },
1908
+ {
1909
+ $match: {
1910
+ newEmail: { $in: userEmailList },
1911
+ isActive: true,
1912
+ clientId: req.body.clientId,
1913
+ },
1914
+ },
1915
+ ];
1916
+
1917
+ let storeQuery = [
1918
+ {
1919
+ $project: {
1920
+ 'storeName': { $toLower: '$storeName' },
1921
+ 'clientId': 1,
1922
+ 'storeProfile.city': 1,
1923
+ 'status': 1,
1924
+ },
1925
+ },
1926
+ {
1927
+ $match: {
1928
+ clientId: req.body.clientId,
1929
+ storeName: { $in: storeList },
1930
+ },
1931
+ },
1932
+ ];
1933
+
1934
+ let [ userDetails, storeDetails ] = await Promise.all[
1935
+ await userService.aggregate( userQuery ),
1936
+ await storeService.aggregate( storeQuery )
1937
+ ];
1938
+
1939
+ let existUSerEmail = userDetails.map( ( ele ) => ele.email );
1940
+
1941
+ userDetails = userEmailList.filter( ( ele ) => !existUSerEmail.includes( ele ) );
1942
+
1943
+ let existsStore = storeDetails.map( ( ele ) => ele.storeName );
1944
+ storeDetails = storeList.filter( ( ele ) => !existsStore.includes( ele ) );
1945
+
1946
+ if ( userDetails.length || storeDetails.length ) {
1947
+ return res.sendError( { validate: false, user: userDetails, store: storeDetails }, 400 );
1948
+ }
1949
+
1950
+ return res.sendSuccess( { validate: true } );
1951
+ } catch ( e ) {
1952
+ logger.error( 'validateUser 2=>', e );
1953
+ return res.sendError( e, 500 );
1954
+ }
1955
+ };
1956
+
1957
+ export async function assignChecklistUser( req, res ) {
1958
+ try {
1959
+ let inputBody = req.body;
1960
+ let assignDetails = inputBody.assignUsers;
1961
+ let newUsers = inputBody.newUsers;
1962
+ if ( inputBody.type == 'upload' ) {
1963
+ await assignedService.deleteMany( { checkListId: inputBody.checklistId } );
1964
+ }
1965
+ let checklistDetails = await checklistService.findOne( { _id: inputBody.checklistId } );
1966
+ if ( !checklistDetails ) {
1967
+ return res.sendError( 'No data found', 204 );
1968
+ }
1969
+ await Promise.all( newUsers.map( async ( user ) => {
1970
+ let getUser = inputBody.assignUsers.find( ( ele ) => ele.userEmail.toLowerCase() == user.toLowerCase() );
1971
+ let userData = {
1972
+ userName: getUser.userName,
1973
+ email: user,
1974
+ mobileNumber: getUser?.phone,
1975
+ clientId: inputBody.clientId,
1976
+ };
1977
+ await createUser( userData );
1978
+ } ) );
1979
+ await Promise.all( assignDetails.map( async ( assign ) => {
1980
+ assign.checklistId = inputBody.checklistId;
1981
+ assign.checkListName = checklistDetails.checkListName;
1982
+ assign.coverage = inputBody.coverage;
1983
+ assign.clientId = inputBody.clientId;
1984
+ assign.upload = inputBody.type;
1985
+ await assignUsers( assign );
1986
+ } ) );
1987
+ return res.sendSuccess( 'Details updated successfully' );
1988
+ } catch ( e ) {
1989
+ logger.error( { functionName: 'assignUser', error: e } );
1990
+ return res.sendError( e, 500 );
1991
+ }
1992
+ }
1993
+
1994
+
1597
1995
  async function uploadUser( req, res ) {
1598
1996
  try {
1599
1997
  let inputBody = req.body;
@@ -2726,168 +3124,114 @@ export const checklistV2 = async ( req, res ) => {
2726
3124
  }
2727
3125
  };
2728
3126
 
2729
- export async function checklistAssign( req, res ) {
2730
- try {
2731
- if ( !req.body.checklistId ) {
2732
- return res.sendError( 'Checklist id is required', 400 );
2733
- }
2734
- if ( !req.body.coverage ) {
2735
- return res.sendError( 'Coverage is required', 400 );
2736
- }
2737
- if ( !req.body.id ) {
2738
- return res.sendError( 'Store/user id is required', 400 );
2739
- }
2740
-
2741
- let checklistDetails = await checklistService.findOne( { _id: req.body.checklistId } );
2742
- if ( !checklistDetails ) {
2743
- return res.sendError( 'No data found', 204 );
2744
- }
2745
- checklistDetails.coverage = req.body.coverage;
2746
- checklistDetails.save();
2747
- let idList;
2748
- if ( req.body.coverage == 'store' ) {
2749
- let clusterDetails = await clusterServices.findOneCluster( { _id: req.body.id }, { stores: 1, clusterName: 1 } );
3127
+ async function assignUsers( data ) {
3128
+ let assignedData;
3129
+ if ( data.coverage == 'store' ) {
3130
+ if ( data?.type == 'cluster' ) {
3131
+ let query = !data?.upload ? { _id: data.id } : { clusterName: data.clusterName, clientId: data.clientId };
3132
+ let clusterDetails = await clusterServices.findOneCluster( query, { stores: 1, clusterName: 1 } );
2750
3133
  if ( clusterDetails ) {
2751
- idList = clusterDetails.stores.map( ( item ) => item.store );
2752
- } else {
2753
- idList = [ new ObjectId( req.body.id ) ];
2754
- }
2755
- let getStoreDetails = await storeService.find( { _id: { $in: idList } } );
2756
- if ( !getStoreDetails.length ) {
2757
- return res.sendError( 'No data found', 204 );
3134
+ assignedData = {
3135
+ checkFlag: true,
3136
+ checkListId: data.checklistId,
3137
+ checkListName: data.checkListName,
3138
+ client_id: data?.clientId,
3139
+ clusterName: clusterDetails?.clusterName,
3140
+ assignId: data?.id || clusterDetails?._id,
3141
+ };
2758
3142
  }
2759
- let assignList = await Promise.all( getStoreDetails.map( async ( store ) => {
2760
- let userDetails = await userService.findOne( { userEmail: store?.spocDetails?.email, clientId: store.clientId } );
3143
+ } else {
3144
+ let query = !data?.upload ? { _id: data.id } : { storeName: data.storeName };
3145
+ let storeDetails = await storeService.findOne( query );
3146
+ if ( storeDetails ) {
3147
+ let email = data?.upload ? data.userEmail : storeDetails?.spocDetails?.email;
3148
+ let userDetails = await userService.findOne( { email: email, clientId: data.clientId } );
2761
3149
  if ( !userDetails ) {
2762
- let data = {
2763
- clientId: store.clientId,
2764
- userName: store.spocDetails?.[0]?.name,
2765
- mobileNumber: store.spocDetails?.[0]?.phone || '',
2766
- email: store.spocDetails[0].email,
2767
- password: '5dqFKAJj29PsV6P+kL+3Dw==',
2768
- role: 'user',
2769
- userType: 'client',
2770
- rolespermission: [
2771
- {
2772
- featureName: 'Global',
2773
- modules: [
2774
- {
2775
- name: 'Store',
2776
- isAdd: false,
2777
- isEdit: false,
2778
-
2779
- },
2780
- {
2781
- name: 'User',
2782
- isAdd: false,
2783
- isEdit: false,
2784
-
2785
- },
2786
- {
2787
- name: 'Camera',
2788
- isAdd: false,
2789
- isEdit: false,
2790
-
2791
- },
2792
- {
2793
- name: 'Configuration',
2794
- isAdd: false,
2795
- isEdit: false,
2796
-
2797
- },
2798
- {
2799
- name: 'Subscription',
2800
- isAdd: false,
2801
- isEdit: false,
2802
-
2803
- },
2804
- {
2805
- name: 'Billing',
2806
- isAdd: false,
2807
- isEdit: false,
2808
-
2809
- },
2810
- ],
2811
- },
2812
- {
2813
- featurName: 'TangoEye',
2814
- modules: [
2815
- {
2816
- name: 'ZoneTag',
2817
- isAdd: false,
2818
- isEdit: false,
2819
-
2820
- },
2821
- ],
2822
- },
2823
- {
2824
- featurName: 'TangoTrax',
2825
- modules: [
2826
- {
2827
- name: 'checklist',
2828
- isAdd: false,
2829
- isEdit: false,
2830
-
2831
- },
2832
- {
2833
- name: 'Task',
2834
- isAdd: false,
2835
- isEdit: false,
2836
-
2837
- },
2838
- ],
2839
- },
2840
- ],
3150
+ let userData = {
3151
+ userName: storeDetails.spocDetails?.[0]?.name,
3152
+ email: storeDetails.spocDetails[0].email,
3153
+ mobileNumber: storeDetails.spocDetails?.[0]?.phone,
3154
+ clientId: data.clientId,
2841
3155
  };
2842
- userDetails = await userService.create( data );
3156
+ userDetails = createUser( userData );
2843
3157
  }
2844
- let data = {
2845
- store_id: store.storeId,
2846
- storeName: store.storeName,
3158
+ assignedData = {
3159
+ store_id: storeDetails.storeId,
3160
+ storeName: storeDetails.storeName,
2847
3161
  userId: userDetails._id,
2848
3162
  userName: userDetails.userName,
2849
3163
  userEmail: userDetails.email,
2850
3164
  userPhone: userDetails?.mobileNumber,
2851
- city: store?.storeProfile?.city,
2852
- country: store?.storeprofile?.country,
3165
+ city: storeDetails?.storeProfile?.city,
3166
+ country: storeDetails?.storeProfile?.country,
2853
3167
  checkFlag: true,
2854
- checkListId: req.body.checklistId,
2855
- checkListName: checklistDetails.checkListName,
2856
- client_id: store.clientId,
2857
- clusterName: clusterDetails?.clusterName,
2858
- assignId: req.body.id,
3168
+ checkListId: data.checklistId,
3169
+ checkListName: data.checkListName,
3170
+ client_id: data.clientId,
3171
+ assignId: data?.id || storeDetails._id,
2859
3172
  };
2860
- return data;
2861
- } ) );
2862
- await assignedService.insertMany( assignList );
2863
- } else {
2864
- let teamDetails = await teamsServices.findOneTeams( { _id: req.body.id }, { users: 1, teamName: 1 } );
3173
+ }
3174
+ }
3175
+ } else {
3176
+ if ( data.type == 'teams' ) {
3177
+ let query = !data?.upload ? { _id: data.id } : { teamName: data.teamName, clientId: data.clientId };
3178
+ let teamDetails = await teamsServices.findOneTeams( query, { users: 1, teamName: 1 } );
2865
3179
  if ( teamDetails ) {
2866
- idList = teamDetails.users.map( ( item ) => item.userId );
2867
- } else {
2868
- idList = [ new ObjectId( req.body.id ) ];
2869
- }
2870
- let userDetails = await userService.find( { _id: { $in: idList } } );
2871
- if ( !userDetails.length ) {
2872
- return res.sendError( 'No data found', 204 );
2873
- }
2874
- let assignList = [];
2875
- userDetails.forEach( ( user ) => {
2876
- assignList.push( {
2877
- userId: user._id,
2878
- userName: user.userName,
2879
- userEmail: user.email,
2880
- userPhone: user?.mobileNumber,
3180
+ assignedData = {
2881
3181
  checkFlag: true,
2882
- checkListId: req.body.checklistId,
2883
- checkListName: checklistDetails.checkListName,
2884
- client_id: user.clientId,
3182
+ checkListId: data.checklistId,
3183
+ checkListName: data.checkListName,
3184
+ client_id: data.clientId,
2885
3185
  teamName: teamDetails?.teamName,
2886
- assignId: req.body.id,
2887
- } );
2888
- } );
2889
- await assignedService.insertMany( assignList );
3186
+ assignId: data?.id || teamDetails?._id,
3187
+ };
3188
+ }
3189
+ } else {
3190
+ let query = !data?.upload ? { _id: data.id } : { email: data.email, clientId: data.clientId };
3191
+ let userDetails = await userService.findOne( query );
3192
+ if ( userDetails ) {
3193
+ assignedData = {
3194
+ userId: userDetails._id,
3195
+ userName: userDetails.userName,
3196
+ userEmail: userDetails.email,
3197
+ userPhone: userDetails?.mobileNumber,
3198
+ checkFlag: true,
3199
+ checkListId: data.checklistId,
3200
+ checkListName: data.checkListName,
3201
+ client_id: data.clientId,
3202
+ assignId: data?.id || userDetails?._id,
3203
+ };
3204
+ }
3205
+ }
3206
+ }
3207
+ if ( assignedData ) {
3208
+ await assignedService.create( assignedData );
3209
+ }
3210
+ }
3211
+
3212
+ export async function checklistAssign( req, res ) {
3213
+ try {
3214
+ if ( !req.body.coverage ) {
3215
+ return res.sendError( 'coverage is required', 400 );
3216
+ }
3217
+ if ( !req.body.idList ) {
3218
+ return res.sendError( 'Id list is required', 400 );
3219
+ }
3220
+ if ( !req.body.checklistId ) {
3221
+ return res.sendError( 'Checklist id is required', 400 );
3222
+ }
3223
+ if ( !req.body.clientId ) {
3224
+ return res.sendError( 'Client id is required', 400 );
3225
+ }
3226
+ let checklistDetails = await checklistService.findOne( { _id: req.body.checklistId } );
3227
+ if ( !checklistDetails ) {
3228
+ return res.sendError( 'No data found', 204 );
2890
3229
  }
3230
+ req.body.checkListName = checklistDetails.checkListName;
3231
+ await Promise.all( req.body.idList.map( async ( ele ) => {
3232
+ let storeData = { ...req.body, type: ele.type, id: ele.id };
3233
+ await assignUsers( storeData );
3234
+ } ) );
2891
3235
  return res.sendSuccess( 'Details updated successfully' );
2892
3236
  } catch ( e ) {
2893
3237
  logger.error( { functionName: 'checklistAssign', error: e } );
@@ -2914,3 +3258,20 @@ export async function removeAssign( req, res ) {
2914
3258
  return res.sendError( e, 500 );
2915
3259
  }
2916
3260
  }
3261
+
3262
+ export async function deleteAssignList( req, res ) {
3263
+ try {
3264
+ if ( !req.body.checkListId ) {
3265
+ return res.sendError( 'Checklist id is required', 400 );
3266
+ }
3267
+ let checklistDetails = await checklistService.findOne( { _id: req.body.checkListId, isdeleted: false } );
3268
+ if ( !checklistDetails ) {
3269
+ return res.sendError( 'No data found', 204 );
3270
+ }
3271
+ await assignedService.updateMany( { checkListId: req.body.checkListId }, { isdeleted: true } );
3272
+ return res.sendSuccess( 'Assign details removed successfully' );
3273
+ } catch ( e ) {
3274
+ logger.error( { functionName: 'deleteAssignList', error: e } );
3275
+ return res.sendError( e, 500 );
3276
+ }
3277
+ }
@@ -13,9 +13,10 @@ traxRouter
13
13
  .post( '/runAIInsert', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ 'isEdit' ] } ] } ), validate( runaiValidation ), traxController.runAIInsert )
14
14
  .get( '/duplicateChecklist/:checklistId', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ 'isEdit' ] } ] } ), validate( duplicateValidation ), traxController.duplicateChecklist )
15
15
  .put( '/checklist/update/:checklistId', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ 'isEdit' ] } ] } ), validate( updateChecklistValidation ), traxController.update )
16
- .post( '/validateUser', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ 'isEdit' ] } ] } ), validate( uploadUserValidation ), traxController.validateUser )
16
+ .post( '/validateUser', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ 'isEdit' ] } ] } ), validate( uploadUserValidation ), traxController.validateUserv1 )
17
17
  .get( '/userDetails/:checklistId', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ ] } ] } ), validate( duplicateValidation ), traxController.assignedUserDetails )
18
18
  .post( '/checklistConfigure', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ 'isEdit' ] } ] } ), traxController.updateConfigure )
19
+ .post( '/checklistConfigurev1', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ 'isEdit' ] } ] } ), traxController.updateConfigurev1 )
19
20
  .delete( '/deleteChecklist/:checklistId', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ 'isEdit' ] } ] } ), validate( duplicateValidation ), traxController.deleteChecklist )
20
21
  .put( '/publish', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ 'isEdit' ] } ] } ), validate( publishValidation ), traxController.updatePublish )
21
22
  .get( '/userList', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ ] } ] } ), traxController.userlist )
@@ -25,6 +26,8 @@ traxRouter
25
26
  .post( '/selectAssign', validate( selectAssign ), traxController.selectAssign )
26
27
  .get( '/checklistV2', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ ] } ] } ), validate( checklistPageSchema ), traxController.checklistV2 )
27
28
  .post( '/assign', isAllowedSessionHandler, traxController.checklistAssign )
28
- .post( '/remove', isAllowedSessionHandler, traxController.removeAssign );
29
+ .post( '/remove', isAllowedSessionHandler, traxController.removeAssign )
30
+ .post( '/removeAssignList', isAllowedSessionHandler, traxController.deleteAssignList )
31
+ .post( '/assignUpload', isAllowedSessionHandler, traxController.assignChecklistUser );
29
32
 
30
33
  // isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ ] } ] } ),
@@ -28,4 +28,8 @@ export const aggregate = async ( query = {} ) => {
28
28
  return model.checklistassignconfigModel.aggregate( query );
29
29
  };
30
30
 
31
+ export const count = async ( query = {} ) => {
32
+ return model.checklistassignconfigModel.countDocuments( query );
33
+ };
34
+
31
35