tango-app-api-trax 3.3.1-beta-49 → 3.3.1-beta-51

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-49",
3
+ "version": "3.3.1-beta-51",
4
4
  "description": "Trax",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -26,7 +26,7 @@
26
26
  "mongodb": "^6.8.0",
27
27
  "nodemon": "^3.1.4",
28
28
  "path": "^0.12.7",
29
- "tango-api-schema": "^2.2.58",
29
+ "tango-api-schema": "^2.2.61",
30
30
  "tango-app-api-middleware": "^3.1.55",
31
31
  "url": "^0.11.4",
32
32
  "winston": "^3.13.1",
@@ -227,6 +227,9 @@ export async function startChecklist( req, res ) {
227
227
  checkListName: getBeforeChecklist[0].checkListName,
228
228
  type: getBeforeChecklist[0].checkListType,
229
229
  client_id: req.user.clientId,
230
+ userEmail: getBeforeChecklist[0].userEmail || '',
231
+ userName: getBeforeChecklist[0].userName || '',
232
+ coverage: getBeforeChecklist[0].coverage || '',
230
233
  };
231
234
  await checklistLogs.create( logInsertData );
232
235
  let getchecklist = getupdatedchecklist;
@@ -1825,8 +1828,11 @@ export async function submitChecklist( req, res ) {
1825
1828
  type: getchecklist[0].checkListType,
1826
1829
  client_id: req.user.clientId,
1827
1830
  redoStatus: requestData?.redoStatus ? true : false,
1831
+ userEmail: getchecklist[0].userEmail || '',
1832
+ userName: getchecklist[0].userName | '',
1833
+ coverage: getchecklist[0].coverage || '',
1828
1834
  };
1829
- console.log( 'logInsertData=>', logInsertData );
1835
+ // console.log( 'logInsertData=>', logInsertData );
1830
1836
  await checklistLogs.create( logInsertData );
1831
1837
  // let time = dayjs().format( 'HH:mm:ss' );
1832
1838
  // let [ hours, minutes ] = time.split( ':' ).map( Number );
@@ -2062,6 +2068,9 @@ async function updateOpenSearch( user, data ) {
2062
2068
  checkListName: getchecklist[0].checkListName,
2063
2069
  type: getchecklist[0].checkListType,
2064
2070
  client_id: user.clientId,
2071
+ userEmail: getchecklist[0].userEmail || '',
2072
+ userName: getchecklist[0].userName || '',
2073
+ coverage: getchecklist[0].coverage || '',
2065
2074
  };
2066
2075
  await checklistLogs.create( logInsertData );
2067
2076
  const requestOptions = {
@@ -2083,6 +2092,9 @@ async function updateOpenSearch( user, data ) {
2083
2092
  checkListName: getchecklist[0].checkListName,
2084
2093
  type: getchecklist[0].checkListType,
2085
2094
  client_id: user.clientId,
2095
+ userEmail: getchecklist[0].userEmail || '',
2096
+ userName: getchecklist[0].userName || '',
2097
+ coverage: getchecklist[0].coverage || '',
2086
2098
  };
2087
2099
  await checklistLogs.create( logInsertData );
2088
2100
  }
@@ -2111,6 +2123,9 @@ async function updateOpenSearchTask( user, data ) {
2111
2123
  checkListName: getchecklist[0].checkListName,
2112
2124
  type: getchecklist[0].checkListType,
2113
2125
  client_id: user.clientId,
2126
+ userEmail: getchecklist[0].userEmail || '',
2127
+ userName: getchecklist[0].userName || '',
2128
+ coverage: getchecklist[0].coverage || '',
2114
2129
  };
2115
2130
  await checklistLogs.create( logInsertData );
2116
2131
  const requestOptions = {
@@ -2132,6 +2147,9 @@ async function updateOpenSearchTask( user, data ) {
2132
2147
  checkListName: getchecklist[0].checkListName,
2133
2148
  type: getchecklist[0].checkListType,
2134
2149
  client_id: user.clientId,
2150
+ userEmail: getchecklist[0].userEmail || '',
2151
+ userName: getchecklist[0].userName || '',
2152
+ coverage: getchecklist[0].coverage || '',
2135
2153
  };
2136
2154
  await checklistLogs.create( logInsertData );
2137
2155
  }
@@ -961,7 +961,12 @@ export const flagTablesV1 = async ( req, res ) => {
961
961
  $project: {
962
962
  checkListName: 1,
963
963
  checkListChar: 1,
964
- coverage: 1,
964
+ coverage: {
965
+ $concat: [
966
+ { $toUpper: { $substr: [ { $ifNull: [ '$coverage', '' ] }, 0, 1 ] } },
967
+ { $substr: [ { $ifNull: [ '$coverage', '' ] }, 1, { $strLenCP: { $ifNull: [ '$coverage', '' ] } } ] },
968
+ ],
969
+ },
965
970
  sourceCheckList_id: 1,
966
971
  checkListType: 1,
967
972
  flagType: 1,
@@ -396,17 +396,25 @@ export const getConfigDetails = async ( req, res ) => {
396
396
  }
397
397
  let query = {
398
398
  checkListId: storechecklistdetails._id,
399
- ...( storechecklistdetails.coverage == 'store' ) ? { $or: [ { store_id: { $exists: true } }, { clusterName: { $exists: true } } ] } : { $or: [ { userEmail: { $exists: true } }, { teamsName: { $exists: true } } ] },
399
+ ...( storechecklistdetails.coverage == 'store' ) ? { $or: [ { store_id: { $exists: true } }, { clusterName: { $exists: true } } ] } : { $or: [ { userEmail: { $exists: true } }, { teamName: { $exists: true } } ] },
400
400
  };
401
401
  let assignList = await assignedService.find( query, { _id: 1, store_id: 1, clusterName: 1, userEmail: 1, teamName: 1, assignId: 1 } );
402
402
  let singleAssign;
403
403
  let groupAssign;
404
404
  if ( storechecklistdetails.coverage == 'store' ) {
405
- singleAssign = assignList.filter( ( item ) => typeof item?.store_id != 'undefined' && item?.store_id != '' ).map( ( item ) => item.assignId );
406
- groupAssign = assignList.filter( ( item ) => typeof item?.clusterName != 'undefined' && item?.clusterName != '' ).map( ( item ) => item.assignId );
405
+ singleAssign = assignList.filter( ( item ) => typeof item?.store_id != 'undefined' && item?.store_id != '' ).map( ( item ) => {
406
+ return { id: item.assignId, type: 'store' };
407
+ } );
408
+ groupAssign = assignList.filter( ( item ) => typeof item?.clusterName != 'undefined' && item?.clusterName != '' ).map( ( item ) => {
409
+ return { id: item.assignId, type: 'cluster' };
410
+ } );
407
411
  } else {
408
- singleAssign = assignList.filter( ( item ) => typeof item?.userEmail != 'undefined' && item?.userEmail !='' ).map( ( item ) => item.assignId );
409
- groupAssign = assignList.filter( ( item ) => typeof item?.teamName != 'undefined' && item?.teamName != '' ).map( ( item ) => item.assignId );
412
+ singleAssign = assignList.filter( ( item ) => typeof item?.userEmail != 'undefined' && item?.userEmail !='' ).map( ( item ) => {
413
+ return { id: item.assignId, type: 'user' };
414
+ } );
415
+ groupAssign = assignList.filter( ( item ) => typeof item?.teamName != 'undefined' && item?.teamName != '' ).map( ( item ) => {
416
+ return { id: item.assignId, type: 'teams' };
417
+ } );
410
418
  }
411
419
  result.checkListDetails = { ...result.checkListDetails, ...{ sections: sectionList }, singleAssign, groupAssign };
412
420
  }
@@ -1585,20 +1593,6 @@ export const updateConfigurev1 =async ( req, res ) => {
1585
1593
  response = await checklistService.create( configDetails );
1586
1594
  id = response._id;
1587
1595
  }
1588
- if ( inputBody.checkListDetails.assignedUsers.length ) {
1589
- await assignedService.deleteMany( { checkListId: inputBody.checkListDetails._id } );
1590
- await Promise.all( inputBody.checkListDetails.assignedUsers.map( async ( user ) => {
1591
- let data = {
1592
- ...user,
1593
- clientId: req.body.clientId,
1594
- checkListName: checklistDetails.checkListName,
1595
- checklistId: checklistDetails._id,
1596
- coverage: inputBody?.checkListDetails.coverage,
1597
- insert: true,
1598
- };
1599
- await assignUsers( data );
1600
- } ) );
1601
- }
1602
1596
  if ( inputBody.submitType == 'publish' ) {
1603
1597
  if ( inputBody.checkListDetails.checkListType == 'custom' ) {
1604
1598
  let currentDate = dayjs.utc().format();
@@ -1975,6 +1969,54 @@ export const validateUserv1 = async ( req, res ) => {
1975
1969
 
1976
1970
  let assignDetails = req.body.assignedUsers;
1977
1971
 
1972
+ if ( req.body.coverage == 'store' ) {
1973
+ const duplicateStore = assignDetails.reduce( ( acc, obj ) => {
1974
+ if ( !acc[obj.storeName.toLowerCase()] ) {
1975
+ acc[obj.storeName.toLowerCase()] = {
1976
+ email: [ obj.userEmail.toLowerCase() ],
1977
+ count: 1,
1978
+ };
1979
+ } else {
1980
+ if ( acc[obj.storeName.toLowerCase()] ) {
1981
+ if ( acc[obj.storeName.toLowerCase()].email.includes( obj.userEmail.toLowerCase() ) ) {
1982
+ acc[obj.storeName.toLowerCase()].count++;
1983
+ } else {
1984
+ acc[obj.storeName.toLowerCase()].email.push( obj.userEmail.toLowerCase() );
1985
+ }
1986
+ }
1987
+ }
1988
+ return acc;
1989
+ }, {} );
1990
+
1991
+ const duplicateStores = Object.keys( duplicateStore ).filter( ( storeName ) => duplicateStore[storeName].count > 1 );
1992
+ if ( duplicateStores.length ) {
1993
+ return res.sendSuccess( { validate: false, ExistsEmail: duplicateStores, message: 'Store and email is duplicated' } );
1994
+ }
1995
+ } else {
1996
+ const duplicateUser = assignDetails.reduce( ( acc, obj ) => {
1997
+ if ( !acc[obj.userEmail.toLowerCase()] ) {
1998
+ acc[obj.userEmail.toLowerCase()] = {
1999
+ name: [ obj.userName.toLowerCase() ],
2000
+ count: 1,
2001
+ };
2002
+ } else {
2003
+ if ( acc[obj.userEmail.toLowerCase()] ) {
2004
+ if ( acc[obj.userEmail.toLowerCase()].name.includes( obj.userName.toLowerCase() ) ) {
2005
+ acc[obj.userEmail.toLowerCase()].count++;
2006
+ } else {
2007
+ acc[obj.userEmail.toLowerCase()].name.push( obj.userEmail.toLowerCase() );
2008
+ }
2009
+ }
2010
+ }
2011
+ return acc;
2012
+ }, {} );
2013
+
2014
+ const duplicateUsers = Object.keys( duplicateUser ).filter( ( storeName ) => duplicateUser[storeName].count > 1 );
2015
+ if ( duplicateUsers.length ) {
2016
+ return res.sendSuccess( { validate: false, ExistsEmail: duplicateUsers, message: 'Email is Duplicated' } );
2017
+ }
2018
+ }
2019
+
1978
2020
  if ( req.body.hasOwnProperty( 'addSingle' ) ) {
1979
2021
  let checkExists = await assignedService.findOne( { userEmail: assignDetails[0]?.userEmail, storeName: assignDetails[0]?.storeName, checkListId: req.body.id } );
1980
2022
  if ( checkExists ) {
@@ -2036,73 +2078,87 @@ export const validateUserv1 = async ( req, res ) => {
2036
2078
  },
2037
2079
  ];
2038
2080
 
2039
- console.log( storeQuery );
2040
-
2041
2081
  let [ userDetails, storeDetails, existEmail ] = await Promise.all( [
2042
2082
  await userService.aggregate( userQuery ),
2043
2083
  await storeService.aggregate( storeQuery ),
2044
2084
  await userService.aggregate( emailCheckQuery ),
2045
2085
  ] );
2046
2086
 
2047
- let existUSerEmail = userDetails.map( ( ele ) => ele.email );
2048
2087
 
2049
- userDetails = userEmailList.filter( ( ele ) => !existUSerEmail.includes( ele ) );
2088
+ let existUserEmail = userDetails.map( ( ele ) => ele.newEmail );
2089
+ let newUserList = userEmailList.filter( ( ele ) => !existUserEmail.includes( ele ) );
2050
2090
 
2051
- let existsStore = storeDetails.map( ( ele ) => ele.storeName && ele.storeName != null );
2052
- storeDetails = storeList.filter( ( ele ) => ele != null && !existsStore.includes( ele ) );
2091
+ let existsStore = storeDetails.map( ( ele ) => ele?.storeName );
2092
+ let newStoreList = storeList.filter( ( ele ) => ele != null && !existsStore.includes( ele.toLowerCase() ) );
2093
+ if ( req.body.coverage == 'store' ) {
2094
+ assignDetails.forEach( ( item ) => {
2095
+ let getStoreId = storeDetails.find( ( store ) => store.storeName.toLowerCase() == item.storeName.toLowerCase() );
2096
+ if ( getStoreId ) {
2097
+ item._id = getStoreId._id;
2098
+ }
2099
+ } );
2100
+ } else {
2101
+ assignDetails.forEach( ( item ) => {
2102
+ let getUserId = userDetails.find( ( user ) => user.newEmail.toLowerCase() == item.userEmail.toLowerCase() );
2103
+ if ( getUserId ) {
2104
+ item._id = getUserId._id;
2105
+ }
2106
+ } );
2107
+ }
2053
2108
 
2054
- if ( userDetails.length || storeDetails.length || existEmail.length ) {
2055
- return res.sendError( { validate: false, user: userDetails, store: storeDetails, existEmail }, 400 );
2109
+ if ( newUserList.length || newStoreList.length || existEmail.length ) {
2110
+ return res.sendError( { validate: false, user: newUserList, store: newStoreList, existEmail, data: assignDetails }, 400 );
2056
2111
  }
2057
- return res.sendSuccess( { validate: true } );
2112
+ return res.sendSuccess( { validate: true, data: assignDetails } );
2058
2113
  } catch ( e ) {
2059
2114
  logger.error( 'validateUser 2=>', e );
2060
2115
  return res.sendError( e, 500 );
2061
2116
  }
2062
2117
  };
2063
2118
 
2064
- export async function assignChecklistUser( req, res ) {
2065
- try {
2066
- let inputBody = req.body;
2067
- let assignDetails = inputBody.assignUsers;
2068
- let newUsers = inputBody.newUsers;
2069
- let checklistDetails = await checklistService.findOne( { _id: inputBody.checklistId } );
2070
- if ( !checklistDetails ) {
2071
- return res.sendError( 'No data found', 204 );
2072
- }
2073
- await Promise.all( newUsers.map( async ( user ) => {
2074
- let getUser = inputBody.assignUsers.find( ( ele ) => ele.userEmail.toLowerCase() == user.toLowerCase() );
2075
- let userData = {
2076
- userName: getUser.userName,
2077
- email: user,
2078
- mobileNumber: getUser?.phone,
2079
- clientId: inputBody.clientId,
2080
- };
2081
- await createUser( userData );
2082
- } ) );
2083
- let assignData = [];
2084
- await Promise.all( assignDetails.map( async ( assign ) => {
2085
- assign.checklistId = inputBody.taskId;
2086
- assign.checkListName = checklistDetails.checkListName;
2087
- assign.coverage = inputBody.coverage;
2088
- assign.clientId = inputBody.clientId;
2089
- assign.upload = inputBody.type;
2090
- let uploadData = await assignUsers( assign );
2091
- if ( uploadData ) {
2092
- if ( Array.isArray( uploadData ) ) {
2093
- assignData.push( ...uploadData );
2094
- } else {
2095
- assignData.push( uploadData );
2096
- }
2097
- }
2098
- } ) );
2099
- return res.sendSuccess( { idList: [ ...new Set( assignData.map( ( item ) => item?.assignId?.toString() ) ) ], assignData } );
2100
- } catch ( e ) {
2101
- logger.error( { functionName: 'assignUser', error: e } );
2102
- return res.sendError( e, 500 );
2103
- }
2104
- }
2105
-
2119
+ // export async function assignChecklistUser( req, res ) {
2120
+ // try {
2121
+ // let inputBody = req.body;
2122
+ // let assignDetails = inputBody.assignUsers;
2123
+ // let newUsers = inputBody.newUsers;
2124
+ // let checklistDetails = await checklistService.findOne( { _id: inputBody.checklistId } );
2125
+ // if ( !checklistDetails ) {
2126
+ // return res.sendError( 'No data found', 204 );
2127
+ // }
2128
+ // await Promise.all( newUsers.map( async ( user ) => {
2129
+ // let getUser = inputBody.assignUsers.find( ( ele ) => ele.userEmail.toLowerCase() == user.toLowerCase() );
2130
+ // let userData = {
2131
+ // userName: getUser.userName,
2132
+ // email: user,
2133
+ // mobileNumber: getUser?.phone,
2134
+ // clientId: inputBody.clientId,
2135
+ // };
2136
+ // await createUser( userData );
2137
+ // } ) );
2138
+ // let assignData = [];
2139
+ // await Promise.all( assignDetails.map( async ( assign ) => {
2140
+ // assign.checklistId = inputBody.taskId;
2141
+ // assign.checkListName = checklistDetails.checkListName;
2142
+ // assign.coverage = inputBody.coverage;
2143
+ // assign.clientId = inputBody.clientId;
2144
+ // assign.upload = inputBody.type;
2145
+ // let uploadData = await assignUsers( assign );
2146
+ // if ( uploadData ) {
2147
+ // if ( Array.isArray( uploadData ) ) {
2148
+ // assignData.push( ...uploadData );
2149
+ // } else {
2150
+ // assignData.push( uploadData );
2151
+ // }
2152
+ // }
2153
+ // } ) );
2154
+ // return res.sendSuccess( { idList: [ ...new Set( assignData.map( ( item ) => {
2155
+ // return { id: item?.assignId?.toString(), type: inputBody.coverage == 'store' ? typeof item.clusterName == 'undefined' ? 'store' : 'cluster' : typeof item.teamsName == 'undefined' ? 'user' : 'teams' };
2156
+ // } ) ) ], assignData } );
2157
+ // } catch ( e ) {
2158
+ // logger.error( { functionName: 'assignUser', error: e } );
2159
+ // return res.sendError( e, 500 );
2160
+ // }
2161
+ // }
2106
2162
 
2107
2163
  async function uploadUser( req, res ) {
2108
2164
  try {
@@ -3482,7 +3538,18 @@ export const selectAssign = async ( req, res ) => {
3482
3538
  if ( requestData.assignType == 'store' ) {
3483
3539
  let storeQuery = [
3484
3540
  { $match: { clientId: requestData.clientId, status: 'active' } },
3485
- { $project: { 'storeName': 1, 'storeId': 1, 'type': 'store' } },
3541
+ {
3542
+ $project: {
3543
+ storeName: 1,
3544
+ storeId: 1,
3545
+ type: 'store',
3546
+ userName: { $arrayElemAt: [ '$spocDetails.name', 0 ] },
3547
+ userEmail: { $arrayElemAt: [ '$spocDetails.email', 0 ] },
3548
+ contact: { $arrayElemAt: [ '$spocDetails.contact', 0 ] },
3549
+ city: '$storeProfile.city',
3550
+ },
3551
+ },
3552
+
3486
3553
  ];
3487
3554
  resuldData = await storeService.aggregate( storeQuery );
3488
3555
  } else {
@@ -3499,7 +3566,7 @@ export const selectAssign = async ( req, res ) => {
3499
3566
  if ( requestData.assignType == 'user' ) {
3500
3567
  let userQuery = [
3501
3568
  { $match: { clientId: requestData.clientId } },
3502
- { $project: { 'userEmail': 1, 'userName': 1, 'type': 'user' } },
3569
+ { $project: { 'email': 1, 'userName': 1, 'type': 'user' } },
3503
3570
  ];
3504
3571
  resuldData = await userService.aggregate( userQuery );
3505
3572
  } else {
@@ -3525,213 +3592,40 @@ export const selectAssign = async ( req, res ) => {
3525
3592
 
3526
3593
  async function assignUsers( data ) {
3527
3594
  let assignedData;
3528
- if ( data.coverage == 'store' ) {
3529
- if ( data?.type == 'cluster' ) {
3530
- let query = [
3531
- {
3532
- $addFields: {
3533
- cluster: { $toLower: '$clusterName' },
3534
- },
3535
- },
3536
- {
3537
- $match: {
3538
- clientId: data.clientId,
3539
- ...( data?.id ) ? { _id: new mongoose.Types.ObjectId( data.id ) } : { cluster: data.clusterName.toLowerCase() },
3540
- },
3541
- },
3542
- {
3543
- $project: {
3544
- stores: 1,
3545
- clusterName: 1,
3546
- },
3547
- },
3548
- ];
3549
- let clusterDetails = await clusterServices.aggregateCluster( query );
3550
- if ( clusterDetails.length ) {
3551
- if ( data.insert ) {
3552
- assignedData = {
3553
- checkFlag: true,
3554
- checkListId: data.checklistId,
3555
- checkListName: data.checkListName,
3556
- client_id: data?.clientId,
3557
- clusterName: clusterDetails?.[0]?.clusterName,
3558
- assignId: data?.id || clusterDetails?.[0]?._id,
3559
- coverage: 'store',
3560
- };
3561
- } else {
3562
- let clusterList = clusterDetails[0].stores.map( ( ele ) => ele.store );
3563
- let storeDetails = await storeService.find( { _id: { $in: clusterList }, status: 'active' } );
3564
- assignedData = await Promise.all( storeDetails.map( async ( store ) => {
3565
- let userDetails = await userService.findOne( { email: store.spocDetails?.[0]?.email, clientId: data.clientId } );
3566
- if ( !userDetails && store?.spocDetails?.length ) {
3567
- let userData = {
3568
- userName: store.spocDetails?.[0]?.name,
3569
- email: store.spocDetails[0]?.email,
3570
- mobileNumber: store.spocDetails?.[0]?.phone,
3571
- clientId: data.clientId,
3572
- };
3573
- userDetails = await createUser( userData );
3574
- }
3575
- let userData = {
3576
- userId: userDetails?._id,
3577
- userName: userDetails?.userName,
3578
- userEmail: userDetails?.email,
3579
- userPhone: userDetails?.mobileNumber,
3580
- checkFlag: true,
3581
- checkListId: data.checklistId,
3582
- checkListName: data.checkListName,
3583
- client_id: data.clientId,
3584
- assignId: data?.id || clusterDetails?.[0]?._id,
3585
- coverage: 'store',
3586
- clusterName: clusterDetails?.[0]?.clusterName,
3587
- storeId: store.storeId,
3588
- storeName: store.storeName,
3589
- };
3590
- return userData;
3591
- } ) );
3592
- }
3593
- }
3594
- } else {
3595
- let query = [
3596
- {
3597
- $addFields: {
3598
- store: { $toLower: '$storeName' },
3599
- },
3600
- },
3601
- {
3602
- $match: {
3603
- clientId: data.clientId,
3604
- ...( data?.id ) ? { _id: new mongoose.Types.ObjectId( data.id ) } : { store: data.storeName.toLowerCase() },
3605
- },
3606
- },
3607
- ];
3608
- let storeDetails = await storeService.aggregate( query );
3609
- if ( storeDetails.length ) {
3610
- let email = data?.userEmail ? data.userEmail : storeDetails?.[0]?.spocDetails?.[0]?.email;
3611
- let userDetails = await userService.findOne( { email: email, clientId: data.clientId } );
3612
- if ( !userDetails ) {
3613
- let userData = {
3614
- userName: storeDetails?.[0]?.spocDetails?.[0]?.name,
3615
- email: data?.userEmail || storeDetails?.[0]?.spocDetails[0].email,
3616
- mobileNumber: storeDetails?.[0]?.spocDetails?.[0]?.phone,
3617
- clientId: data.clientId,
3618
- };
3619
- userDetails = await createUser( userData );
3620
- }
3621
- assignedData = {
3622
- store_id: storeDetails?.[0]?.storeId,
3623
- storeName: storeDetails?.[0]?.storeName,
3624
- userId: userDetails._id,
3625
- userName: userDetails.userName,
3626
- userEmail: userDetails.email,
3627
- userPhone: userDetails?.mobileNumber,
3628
- city: storeDetails?.[0]?.storeProfile?.city,
3629
- country: storeDetails?.[0]?.storeProfile?.country,
3630
- checkFlag: true,
3631
- checkListId: data.checklistId,
3632
- checkListName: data.checkListName,
3633
- client_id: data.clientId,
3634
- assignId: data?.id || storeDetails?.[0]?._id,
3635
- coverage: 'store',
3595
+ if ( data?.type == 'cluster' ) {
3596
+ let clusterDetails = await clusterServices.findcluster( { clientId: data.clientId, _id: new mongoose.Types.ObjectId( data.id ) } );
3597
+ if ( clusterDetails.length ) {
3598
+ let clusterList = clusterDetails[0].stores.map( ( ele ) => ele.store );
3599
+ let storeDetails = await storeService.find( { _id: { $in: clusterList }, status: 'active' } );
3600
+ assignedData = await Promise.all( storeDetails.map( async ( store ) => {
3601
+ let userData = {
3602
+ storeId: store.storeId,
3603
+ storeName: store.storeName,
3604
+ userName: store.spocDetails?.[0]?.name,
3605
+ userEmail: store.spocDetails?.[0]?.email,
3606
+ clusterName: clusterDetails?.[0]?.clusterName,
3607
+ id: clusterDetails?.[0]?._id,
3636
3608
  };
3637
- }
3609
+ return userData;
3610
+ } ) );
3638
3611
  }
3639
3612
  } else {
3640
- if ( data.type == 'teams' ) {
3641
- let query = [
3642
- {
3643
- $addFields: {
3644
- team: { $toLower: '$teamName' },
3645
- },
3646
- },
3647
- {
3648
- $match: {
3649
- clientId: data.clientId,
3650
- ...( data?.id ) ? { _id: new mongoose.Types.ObjectId( data.id ) } : { team: data.teamName.toLowerCase() },
3651
- },
3652
- },
3653
- ];
3654
- let teamDetails = await teamsServices.aggregateTeams( query );
3655
- if ( teamDetails.length ) {
3656
- if ( data.insert ) {
3657
- assignedData = {
3658
- checkFlag: true,
3659
- checkListId: data.checklistId,
3660
- checkListName: data.checkListName,
3661
- client_id: data.clientId,
3662
- teamName: teamDetails?.[0]?.teamName,
3663
- assignId: data?.id || teamDetails?.[0]?._id,
3664
- coverage: 'user',
3665
- };
3666
- } else {
3667
- let userIdList = teamDetails[0].users.map( ( ele ) => ele.userId );
3668
- let userDetails = await userService.find( { _id: userIdList } );
3669
- assignedData = userDetails.map( ( user ) => {
3670
- let userData = {
3671
- userId: user._id,
3672
- userName: user.userName,
3673
- userEmail: user.email,
3674
- userPhone: user.mobileNumber,
3675
- checkFlag: true,
3676
- checkListId: data.checklistId,
3677
- checkListName: data.checkListName,
3678
- client_id: data.clientId,
3679
- assignId: data?.id || teamDetails?.[0]?._id,
3680
- coverage: 'user',
3681
- teamName: teamDetails?.[0]?.teamName,
3682
- };
3683
- return userData;
3684
- } );
3685
- }
3686
- }
3687
- } else {
3688
- let query = [
3689
- {
3690
- $addFields: {
3691
- userEmail: { $toLower: '$email' },
3692
- },
3693
- },
3694
- {
3695
- $match: {
3696
- clientId: data.clientId,
3697
- ...( data?.id ) ? { _id: new mongoose.Types.ObjectId( data.id ) } : { userEmail: data.userEmail.toLowerCase() },
3698
- },
3699
- },
3700
- ];
3701
- // console.log( 'data?.userEmail=>', data.userEmail );
3702
- let userDetails = await userService.aggregate( query );
3703
- if ( !userDetails.length ) {
3613
+ let teamDetails = await teamsServices.findteams( { clientId: data.clientId, _id: new mongoose.Types.ObjectId( data.id ) } );
3614
+ if ( teamDetails.length ) {
3615
+ let userIdList = teamDetails[0].users.map( ( ele ) => ele.userId );
3616
+ let userDetails = await userService.find( { _id: userIdList } );
3617
+ assignedData = userDetails.map( ( user ) => {
3704
3618
  let userData = {
3705
- userName: data?.userEmail?.split( '@' )[0],
3706
- email: data.userEmail,
3707
- clientId: data.clientId,
3708
- };
3709
- // console.log( userData, 'userData', data );
3710
- let details = await createUser( userData );
3711
- userDetails = [ details ];
3712
- }
3713
- if ( userDetails.length ) {
3714
- assignedData = {
3715
- userId: userDetails?.[0]?._id,
3716
- userName: userDetails?.[0]?.userName,
3717
- userEmail: userDetails?.[0]?.email,
3718
- userPhone: userDetails?.[0]?.mobileNumber,
3719
- checkFlag: true,
3720
- checkListId: data.checklistId,
3721
- checkListName: data.checkListName,
3722
- client_id: data.clientId,
3723
- assignId: data?.id || userDetails?.[0]?._id,
3724
- coverage: 'user',
3619
+ userName: user.userName,
3620
+ userEmail: user.email,
3621
+ teamName: teamDetails?.[0]?.teamName,
3622
+ id: teamDetails?.[0]?._id,
3725
3623
  };
3726
- }
3624
+ return userData;
3625
+ } );
3727
3626
  }
3728
3627
  }
3729
-
3730
- if ( assignedData && data?.insert ) {
3731
- await assignedService.updateOne( { checkListId: assignedData.checkListId, assignId: assignedData.assignId }, assignedData );
3732
- } else {
3733
- return assignedData;
3734
- }
3628
+ return assignedData;
3735
3629
  }
3736
3630
 
3737
3631
  export async function checklistAssign( req, res ) {
@@ -3757,20 +3651,9 @@ export async function checklistAssign( req, res ) {
3757
3651
  let assignedData = {
3758
3652
  ...data,
3759
3653
  clientId: req.body.clientId,
3760
- checkListName: checklistDetails.checkListName,
3761
- checklistId: checklistDetails._id,
3762
- coverage: req.body.coverage,
3763
- insert: false,
3764
3654
  };
3765
- // console.log( assignedData, 'daterthj' );
3766
3655
  let uploadData = await assignUsers( assignedData );
3767
- if ( uploadData ) {
3768
- if ( Array.isArray( uploadData ) ) {
3769
- uniqueArr.push( ...uploadData );
3770
- } else {
3771
- uniqueArr.push( uploadData );
3772
- }
3773
- }
3656
+ uniqueArr.push( ...uploadData );
3774
3657
  } ) );
3775
3658
 
3776
3659
  return res.sendSuccess( { count: uniqueArr.length, uniqueArr } );
@@ -3780,3 +3663,78 @@ export async function checklistAssign( req, res ) {
3780
3663
  }
3781
3664
  }
3782
3665
 
3666
+ export async function updateAssign( req, res ) {
3667
+ try {
3668
+ console.log( req.body );
3669
+ if ( !req.body.assignedList.length && !req.body.assignedGroup.length ) {
3670
+ return res.sendError( 'No data found', 204 );
3671
+ }
3672
+ let checklistDetails = await checklistService.findOne( { _id: req.body.checkListId, client_id: req.body.clientId } );
3673
+ if ( !checklistDetails ) {
3674
+ return res.sendError( 'No data found', 204 );
3675
+ }
3676
+ await assignedService.deleteMany( { checkListId: req.body.checkListId } );
3677
+ let assignedUserList = [];
3678
+ await Promise.all( req.body.assignedList.map( async ( assign ) => {
3679
+ delete assign.id;
3680
+ let query = [
3681
+ {
3682
+ $addFields: {
3683
+ emailToLower: { $toLower: '$email' },
3684
+ },
3685
+ },
3686
+ {
3687
+ $match: {
3688
+ clientId: req.body.clientId,
3689
+ emailToLower: assign.userEmail.toLowerCase(),
3690
+ },
3691
+ },
3692
+ ];
3693
+ let userDetails = await userService.aggregate( query );
3694
+ if ( !userDetails.length ) {
3695
+ let userData = {
3696
+ userName: assign.userName,
3697
+ email: assign.userEmail,
3698
+ mobileNumber: assign?.phone || '',
3699
+ clientId: req.body.clientId,
3700
+ };
3701
+ userDetails = await createUser( userData );
3702
+ }
3703
+ let data = {
3704
+ ...assign,
3705
+ store_id: assign.storeId,
3706
+ client_id: req.body.clientId,
3707
+ checkListId: req.body.checkListId,
3708
+ coverage: req.body.coverage,
3709
+ assignId: assign._id,
3710
+ userId: userDetails._id,
3711
+ checkListName: checklistDetails.checkListName,
3712
+ };
3713
+ assignedUserList.push( data );
3714
+ } ) );
3715
+ await Promise.all( req.body.assignedGroup.map( async ( assign ) => {
3716
+ let groupDetails;
3717
+ if ( req.body.coverage == 'store' ) {
3718
+ groupDetails = await clusterServices.findOneCluster( { _id: assign.id } );
3719
+ } else {
3720
+ groupDetails = await teamsServices.findOneTeams( { _id: assign.id } );
3721
+ }
3722
+ if ( groupDetails ) {
3723
+ let groupData = {
3724
+ client_id: req.body.clientId,
3725
+ checkListId: req.body.checkListId,
3726
+ coverage: req.body.coverage,
3727
+ assignId: assign.id,
3728
+ checkListName: checklistDetails.checkListName,
3729
+ ...( req.body.coverage == 'store' ) ? { clusterName: groupDetails?.clusterName } : { teamName: groupDetails?.teamName },
3730
+ };
3731
+ assignedUserList.push( groupData );
3732
+ }
3733
+ } ) );
3734
+ await assignedService.insertMany( assignedUserList );
3735
+ return res.sendSuccess( 'Assign details updated successfully' );
3736
+ } catch ( e ) {
3737
+ logger.error( { functionName: 'updateAssign', error: e } );
3738
+ return res.sendError( e, 500 );
3739
+ }
3740
+ }
@@ -27,6 +27,8 @@ traxRouter
27
27
  .get( '/predefinedChecklist', isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ ] } ] } ), validate( aichecklistValidation ), traxController.preDefinedChecklist )
28
28
  .post( '/selectAssign', validate( selectAssign ), traxController.selectAssign )
29
29
  .post( '/assign', isAllowedSessionHandler, traxController.checklistAssign )
30
- .post( '/assignUpload', isAllowedSessionHandler, traxController.assignChecklistUser );
30
+ // .post( '/assignUpload', isAllowedSessionHandler, traxController.assignChecklistUser )
31
+ .post( '/updateAssign', isAllowedSessionHandler, traxController.updateAssign );
32
+ ;
31
33
 
32
34
  // isAllowedSessionHandler, isAllowedClient, accessVerification( { userType: [ 'tango', 'client' ], access: [ { featureName: 'TangoTrax', name: 'checklist', permissions: [ ] } ] } ),