tango-app-api-trax 3.3.1-beta-50 → 3.3.1-beta-52

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-50",
3
+ "version": "3.3.1-beta-52",
4
4
  "description": "Trax",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -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,7 +396,7 @@ 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;
@@ -1593,20 +1593,6 @@ export const updateConfigurev1 =async ( req, res ) => {
1593
1593
  response = await checklistService.create( configDetails );
1594
1594
  id = response._id;
1595
1595
  }
1596
- if ( inputBody.checkListDetails.assignedUsers.length ) {
1597
- await assignedService.deleteMany( { checkListId: inputBody.checkListDetails._id } );
1598
- await Promise.all( inputBody.checkListDetails.assignedUsers.map( async ( user ) => {
1599
- let data = {
1600
- ...user,
1601
- clientId: req.body.clientId,
1602
- checkListName: checklistDetails.checkListName,
1603
- checklistId: checklistDetails._id,
1604
- coverage: inputBody?.checkListDetails.coverage,
1605
- insert: true,
1606
- };
1607
- await assignUsers( data );
1608
- } ) );
1609
- }
1610
1596
  if ( inputBody.submitType == 'publish' ) {
1611
1597
  if ( inputBody.checkListDetails.checkListType == 'custom' ) {
1612
1598
  let currentDate = dayjs.utc().format();
@@ -1983,6 +1969,54 @@ export const validateUserv1 = async ( req, res ) => {
1983
1969
 
1984
1970
  let assignDetails = req.body.assignedUsers;
1985
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
+
1986
2020
  if ( req.body.hasOwnProperty( 'addSingle' ) ) {
1987
2021
  let checkExists = await assignedService.findOne( { userEmail: assignDetails[0]?.userEmail, storeName: assignDetails[0]?.storeName, checkListId: req.body.id } );
1988
2022
  if ( checkExists ) {
@@ -2034,6 +2068,7 @@ export const validateUserv1 = async ( req, res ) => {
2034
2068
  'clientId': 1,
2035
2069
  'storeProfile.city': 1,
2036
2070
  'status': 1,
2071
+ 'storeId': 1,
2037
2072
  },
2038
2073
  },
2039
2074
  {
@@ -2044,75 +2079,88 @@ export const validateUserv1 = async ( req, res ) => {
2044
2079
  },
2045
2080
  ];
2046
2081
 
2047
- console.log( storeQuery );
2048
-
2049
2082
  let [ userDetails, storeDetails, existEmail ] = await Promise.all( [
2050
2083
  await userService.aggregate( userQuery ),
2051
2084
  await storeService.aggregate( storeQuery ),
2052
2085
  await userService.aggregate( emailCheckQuery ),
2053
2086
  ] );
2054
2087
 
2055
- let existUSerEmail = userDetails.map( ( ele ) => ele.email );
2056
2088
 
2057
- userDetails = userEmailList.filter( ( ele ) => !existUSerEmail.includes( ele ) );
2089
+ let existUserEmail = userDetails.map( ( ele ) => ele.newEmail );
2090
+ let newUserList = userEmailList.filter( ( ele ) => !existUserEmail.includes( ele ) );
2058
2091
 
2059
- let existsStore = storeDetails.map( ( ele ) => ele.storeName && ele.storeName != null );
2060
- storeDetails = storeList.filter( ( ele ) => ele != null && !existsStore.includes( ele ) );
2092
+ let existsStore = storeDetails.map( ( ele ) => ele?.storeName );
2093
+ let newStoreList = storeList.filter( ( ele ) => ele != null && !existsStore.includes( ele.toLowerCase() ) );
2094
+ if ( req.body.coverage == 'store' ) {
2095
+ assignDetails.forEach( ( item ) => {
2096
+ let getStoreId = storeDetails.find( ( store ) => store.storeName.toLowerCase() == item.storeName.toLowerCase() );
2097
+ if ( getStoreId ) {
2098
+ item._id = getStoreId._id;
2099
+ item.storeId = getStoreId.storeId;
2100
+ }
2101
+ } );
2102
+ } else {
2103
+ assignDetails.forEach( ( item ) => {
2104
+ let getUserId = userDetails.find( ( user ) => user.newEmail.toLowerCase() == item.userEmail.toLowerCase() );
2105
+ if ( getUserId ) {
2106
+ item._id = getUserId._id;
2107
+ }
2108
+ } );
2109
+ }
2061
2110
 
2062
- if ( userDetails.length || storeDetails.length || existEmail.length ) {
2063
- return res.sendError( { validate: false, user: userDetails, store: storeDetails, existEmail }, 400 );
2111
+ if ( newUserList.length || newStoreList.length || existEmail.length ) {
2112
+ return res.sendError( { validate: false, user: newUserList, store: newStoreList, existEmail, data: assignDetails }, 400 );
2064
2113
  }
2065
- return res.sendSuccess( { validate: true } );
2114
+ return res.sendSuccess( { validate: true, data: assignDetails } );
2066
2115
  } catch ( e ) {
2067
2116
  logger.error( 'validateUser 2=>', e );
2068
2117
  return res.sendError( e, 500 );
2069
2118
  }
2070
2119
  };
2071
2120
 
2072
- export async function assignChecklistUser( req, res ) {
2073
- try {
2074
- let inputBody = req.body;
2075
- let assignDetails = inputBody.assignUsers;
2076
- let newUsers = inputBody.newUsers;
2077
- let checklistDetails = await checklistService.findOne( { _id: inputBody.checklistId } );
2078
- if ( !checklistDetails ) {
2079
- return res.sendError( 'No data found', 204 );
2080
- }
2081
- await Promise.all( newUsers.map( async ( user ) => {
2082
- let getUser = inputBody.assignUsers.find( ( ele ) => ele.userEmail.toLowerCase() == user.toLowerCase() );
2083
- let userData = {
2084
- userName: getUser.userName,
2085
- email: user,
2086
- mobileNumber: getUser?.phone,
2087
- clientId: inputBody.clientId,
2088
- };
2089
- await createUser( userData );
2090
- } ) );
2091
- let assignData = [];
2092
- await Promise.all( assignDetails.map( async ( assign ) => {
2093
- assign.checklistId = inputBody.taskId;
2094
- assign.checkListName = checklistDetails.checkListName;
2095
- assign.coverage = inputBody.coverage;
2096
- assign.clientId = inputBody.clientId;
2097
- assign.upload = inputBody.type;
2098
- let uploadData = await assignUsers( assign );
2099
- if ( uploadData ) {
2100
- if ( Array.isArray( uploadData ) ) {
2101
- assignData.push( ...uploadData );
2102
- } else {
2103
- assignData.push( uploadData );
2104
- }
2105
- }
2106
- } ) );
2107
- return res.sendSuccess( { idList: [ ...new Set( assignData.map( ( item ) => {
2108
- return { id: item?.assignId?.toString(), type: inputBody.coverage == 'store' ? typeof item.clusterName == 'undefined' ? 'store' : 'cluster' : typeof item.teamsName == 'undefined' ? 'user' : 'teams' };
2109
- } ) ) ], assignData } );
2110
- } catch ( e ) {
2111
- logger.error( { functionName: 'assignUser', error: e } );
2112
- return res.sendError( e, 500 );
2113
- }
2114
- }
2115
-
2121
+ // export async function assignChecklistUser( req, res ) {
2122
+ // try {
2123
+ // let inputBody = req.body;
2124
+ // let assignDetails = inputBody.assignUsers;
2125
+ // let newUsers = inputBody.newUsers;
2126
+ // let checklistDetails = await checklistService.findOne( { _id: inputBody.checklistId } );
2127
+ // if ( !checklistDetails ) {
2128
+ // return res.sendError( 'No data found', 204 );
2129
+ // }
2130
+ // await Promise.all( newUsers.map( async ( user ) => {
2131
+ // let getUser = inputBody.assignUsers.find( ( ele ) => ele.userEmail.toLowerCase() == user.toLowerCase() );
2132
+ // let userData = {
2133
+ // userName: getUser.userName,
2134
+ // email: user,
2135
+ // mobileNumber: getUser?.phone,
2136
+ // clientId: inputBody.clientId,
2137
+ // };
2138
+ // await createUser( userData );
2139
+ // } ) );
2140
+ // let assignData = [];
2141
+ // await Promise.all( assignDetails.map( async ( assign ) => {
2142
+ // assign.checklistId = inputBody.taskId;
2143
+ // assign.checkListName = checklistDetails.checkListName;
2144
+ // assign.coverage = inputBody.coverage;
2145
+ // assign.clientId = inputBody.clientId;
2146
+ // assign.upload = inputBody.type;
2147
+ // let uploadData = await assignUsers( assign );
2148
+ // if ( uploadData ) {
2149
+ // if ( Array.isArray( uploadData ) ) {
2150
+ // assignData.push( ...uploadData );
2151
+ // } else {
2152
+ // assignData.push( uploadData );
2153
+ // }
2154
+ // }
2155
+ // } ) );
2156
+ // return res.sendSuccess( { idList: [ ...new Set( assignData.map( ( item ) => {
2157
+ // return { id: item?.assignId?.toString(), type: inputBody.coverage == 'store' ? typeof item.clusterName == 'undefined' ? 'store' : 'cluster' : typeof item.teamsName == 'undefined' ? 'user' : 'teams' };
2158
+ // } ) ) ], assignData } );
2159
+ // } catch ( e ) {
2160
+ // logger.error( { functionName: 'assignUser', error: e } );
2161
+ // return res.sendError( e, 500 );
2162
+ // }
2163
+ // }
2116
2164
 
2117
2165
  async function uploadUser( req, res ) {
2118
2166
  try {
@@ -3492,7 +3540,18 @@ export const selectAssign = async ( req, res ) => {
3492
3540
  if ( requestData.assignType == 'store' ) {
3493
3541
  let storeQuery = [
3494
3542
  { $match: { clientId: requestData.clientId, status: 'active' } },
3495
- { $project: { 'storeName': 1, 'storeId': 1, 'type': 'store' } },
3543
+ {
3544
+ $project: {
3545
+ storeName: 1,
3546
+ storeId: 1,
3547
+ type: 'store',
3548
+ userName: { $arrayElemAt: [ '$spocDetails.name', 0 ] },
3549
+ userEmail: { $arrayElemAt: [ '$spocDetails.email', 0 ] },
3550
+ contact: { $arrayElemAt: [ '$spocDetails.contact', 0 ] },
3551
+ city: '$storeProfile.city',
3552
+ },
3553
+ },
3554
+
3496
3555
  ];
3497
3556
  resuldData = await storeService.aggregate( storeQuery );
3498
3557
  } else {
@@ -3509,7 +3568,7 @@ export const selectAssign = async ( req, res ) => {
3509
3568
  if ( requestData.assignType == 'user' ) {
3510
3569
  let userQuery = [
3511
3570
  { $match: { clientId: requestData.clientId } },
3512
- { $project: { 'userEmail': 1, 'userName': 1, 'type': 'user' } },
3571
+ { $project: { 'email': 1, 'userName': 1, 'type': 'user' } },
3513
3572
  ];
3514
3573
  resuldData = await userService.aggregate( userQuery );
3515
3574
  } else {
@@ -3535,213 +3594,40 @@ export const selectAssign = async ( req, res ) => {
3535
3594
 
3536
3595
  async function assignUsers( data ) {
3537
3596
  let assignedData;
3538
- if ( data.coverage == 'store' ) {
3539
- if ( data?.type == 'cluster' ) {
3540
- let query = [
3541
- {
3542
- $addFields: {
3543
- cluster: { $toLower: '$clusterName' },
3544
- },
3545
- },
3546
- {
3547
- $match: {
3548
- clientId: data.clientId,
3549
- ...( data?.id ) ? { _id: new mongoose.Types.ObjectId( data.id ) } : { cluster: data.clusterName.toLowerCase() },
3550
- },
3551
- },
3552
- {
3553
- $project: {
3554
- stores: 1,
3555
- clusterName: 1,
3556
- },
3557
- },
3558
- ];
3559
- let clusterDetails = await clusterServices.aggregateCluster( query );
3560
- if ( clusterDetails.length ) {
3561
- if ( data.insert ) {
3562
- assignedData = {
3563
- checkFlag: true,
3564
- checkListId: data.checklistId,
3565
- checkListName: data.checkListName,
3566
- client_id: data?.clientId,
3567
- clusterName: clusterDetails?.[0]?.clusterName,
3568
- assignId: data?.id || clusterDetails?.[0]?._id,
3569
- coverage: 'store',
3570
- };
3571
- } else {
3572
- let clusterList = clusterDetails[0].stores.map( ( ele ) => ele.store );
3573
- let storeDetails = await storeService.find( { _id: { $in: clusterList }, status: 'active' } );
3574
- assignedData = await Promise.all( storeDetails.map( async ( store ) => {
3575
- let userDetails = await userService.findOne( { email: store.spocDetails?.[0]?.email, clientId: data.clientId } );
3576
- if ( !userDetails && store?.spocDetails?.length ) {
3577
- let userData = {
3578
- userName: store.spocDetails?.[0]?.name,
3579
- email: store.spocDetails[0]?.email,
3580
- mobileNumber: store.spocDetails?.[0]?.phone,
3581
- clientId: data.clientId,
3582
- };
3583
- userDetails = await createUser( userData );
3584
- }
3585
- let userData = {
3586
- userId: userDetails?._id,
3587
- userName: userDetails?.userName,
3588
- userEmail: userDetails?.email,
3589
- userPhone: userDetails?.mobileNumber,
3590
- checkFlag: true,
3591
- checkListId: data.checklistId,
3592
- checkListName: data.checkListName,
3593
- client_id: data.clientId,
3594
- assignId: data?.id || clusterDetails?.[0]?._id,
3595
- coverage: 'store',
3596
- clusterName: clusterDetails?.[0]?.clusterName,
3597
- storeId: store.storeId,
3598
- storeName: store.storeName,
3599
- };
3600
- return userData;
3601
- } ) );
3602
- }
3603
- }
3604
- } else {
3605
- let query = [
3606
- {
3607
- $addFields: {
3608
- store: { $toLower: '$storeName' },
3609
- },
3610
- },
3611
- {
3612
- $match: {
3613
- clientId: data.clientId,
3614
- ...( data?.id ) ? { _id: new mongoose.Types.ObjectId( data.id ) } : { store: data.storeName.toLowerCase() },
3615
- },
3616
- },
3617
- ];
3618
- let storeDetails = await storeService.aggregate( query );
3619
- if ( storeDetails.length ) {
3620
- let email = data?.userEmail ? data.userEmail : storeDetails?.[0]?.spocDetails?.[0]?.email;
3621
- let userDetails = await userService.findOne( { email: email, clientId: data.clientId } );
3622
- if ( !userDetails ) {
3623
- let userData = {
3624
- userName: storeDetails?.[0]?.spocDetails?.[0]?.name,
3625
- email: data?.userEmail || storeDetails?.[0]?.spocDetails[0].email,
3626
- mobileNumber: storeDetails?.[0]?.spocDetails?.[0]?.phone,
3627
- clientId: data.clientId,
3628
- };
3629
- userDetails = await createUser( userData );
3630
- }
3631
- assignedData = {
3632
- store_id: storeDetails?.[0]?.storeId,
3633
- storeName: storeDetails?.[0]?.storeName,
3634
- userId: userDetails._id,
3635
- userName: userDetails.userName,
3636
- userEmail: userDetails.email,
3637
- userPhone: userDetails?.mobileNumber,
3638
- city: storeDetails?.[0]?.storeProfile?.city,
3639
- country: storeDetails?.[0]?.storeProfile?.country,
3640
- checkFlag: true,
3641
- checkListId: data.checklistId,
3642
- checkListName: data.checkListName,
3643
- client_id: data.clientId,
3644
- assignId: data?.id || storeDetails?.[0]?._id,
3645
- coverage: 'store',
3597
+ if ( data?.type == 'cluster' ) {
3598
+ let clusterDetails = await clusterServices.findcluster( { clientId: data.clientId, _id: new mongoose.Types.ObjectId( data.id ) } );
3599
+ if ( clusterDetails.length ) {
3600
+ let clusterList = clusterDetails[0].stores.map( ( ele ) => ele.store );
3601
+ let storeDetails = await storeService.find( { _id: { $in: clusterList }, status: 'active' } );
3602
+ assignedData = await Promise.all( storeDetails.map( async ( store ) => {
3603
+ let userData = {
3604
+ storeId: store.storeId,
3605
+ storeName: store.storeName,
3606
+ userName: store.spocDetails?.[0]?.name,
3607
+ userEmail: store.spocDetails?.[0]?.email,
3608
+ clusterName: clusterDetails?.[0]?.clusterName,
3609
+ id: clusterDetails?.[0]?._id,
3646
3610
  };
3647
- }
3611
+ return userData;
3612
+ } ) );
3648
3613
  }
3649
3614
  } else {
3650
- if ( data.type == 'teams' ) {
3651
- let query = [
3652
- {
3653
- $addFields: {
3654
- team: { $toLower: '$teamName' },
3655
- },
3656
- },
3657
- {
3658
- $match: {
3659
- clientId: data.clientId,
3660
- ...( data?.id ) ? { _id: new mongoose.Types.ObjectId( data.id ) } : { team: data.teamName.toLowerCase() },
3661
- },
3662
- },
3663
- ];
3664
- let teamDetails = await teamsServices.aggregateTeams( query );
3665
- if ( teamDetails.length ) {
3666
- if ( data.insert ) {
3667
- assignedData = {
3668
- checkFlag: true,
3669
- checkListId: data.checklistId,
3670
- checkListName: data.checkListName,
3671
- client_id: data.clientId,
3672
- teamName: teamDetails?.[0]?.teamName,
3673
- assignId: data?.id || teamDetails?.[0]?._id,
3674
- coverage: 'user',
3675
- };
3676
- } else {
3677
- let userIdList = teamDetails[0].users.map( ( ele ) => ele.userId );
3678
- let userDetails = await userService.find( { _id: userIdList } );
3679
- assignedData = userDetails.map( ( user ) => {
3680
- let userData = {
3681
- userId: user._id,
3682
- userName: user.userName,
3683
- userEmail: user.email,
3684
- userPhone: user.mobileNumber,
3685
- checkFlag: true,
3686
- checkListId: data.checklistId,
3687
- checkListName: data.checkListName,
3688
- client_id: data.clientId,
3689
- assignId: data?.id || teamDetails?.[0]?._id,
3690
- coverage: 'user',
3691
- teamName: teamDetails?.[0]?.teamName,
3692
- };
3693
- return userData;
3694
- } );
3695
- }
3696
- }
3697
- } else {
3698
- let query = [
3699
- {
3700
- $addFields: {
3701
- userEmail: { $toLower: '$email' },
3702
- },
3703
- },
3704
- {
3705
- $match: {
3706
- clientId: data.clientId,
3707
- ...( data?.id ) ? { _id: new mongoose.Types.ObjectId( data.id ) } : { userEmail: data.userEmail.toLowerCase() },
3708
- },
3709
- },
3710
- ];
3711
- // console.log( 'data?.userEmail=>', data.userEmail );
3712
- let userDetails = await userService.aggregate( query );
3713
- if ( !userDetails.length ) {
3615
+ let teamDetails = await teamsServices.findteams( { clientId: data.clientId, _id: new mongoose.Types.ObjectId( data.id ) } );
3616
+ if ( teamDetails.length ) {
3617
+ let userIdList = teamDetails[0].users.map( ( ele ) => ele.userId );
3618
+ let userDetails = await userService.find( { _id: userIdList } );
3619
+ assignedData = userDetails.map( ( user ) => {
3714
3620
  let userData = {
3715
- userName: data?.userEmail?.split( '@' )[0],
3716
- email: data.userEmail,
3717
- clientId: data.clientId,
3718
- };
3719
- // console.log( userData, 'userData', data );
3720
- let details = await createUser( userData );
3721
- userDetails = [ details ];
3722
- }
3723
- if ( userDetails.length ) {
3724
- assignedData = {
3725
- userId: userDetails?.[0]?._id,
3726
- userName: userDetails?.[0]?.userName,
3727
- userEmail: userDetails?.[0]?.email,
3728
- userPhone: userDetails?.[0]?.mobileNumber,
3729
- checkFlag: true,
3730
- checkListId: data.checklistId,
3731
- checkListName: data.checkListName,
3732
- client_id: data.clientId,
3733
- assignId: data?.id || userDetails?.[0]?._id,
3734
- coverage: 'user',
3621
+ userName: user.userName,
3622
+ userEmail: user.email,
3623
+ teamName: teamDetails?.[0]?.teamName,
3624
+ id: teamDetails?.[0]?._id,
3735
3625
  };
3736
- }
3626
+ return userData;
3627
+ } );
3737
3628
  }
3738
3629
  }
3739
-
3740
- if ( assignedData && data?.insert ) {
3741
- await assignedService.updateOne( { checkListId: assignedData.checkListId, assignId: assignedData.assignId }, assignedData );
3742
- } else {
3743
- return assignedData;
3744
- }
3630
+ return assignedData;
3745
3631
  }
3746
3632
 
3747
3633
  export async function checklistAssign( req, res ) {
@@ -3767,20 +3653,9 @@ export async function checklistAssign( req, res ) {
3767
3653
  let assignedData = {
3768
3654
  ...data,
3769
3655
  clientId: req.body.clientId,
3770
- checkListName: checklistDetails.checkListName,
3771
- checklistId: checklistDetails._id,
3772
- coverage: req.body.coverage,
3773
- insert: false,
3774
3656
  };
3775
- // console.log( assignedData, 'daterthj' );
3776
3657
  let uploadData = await assignUsers( assignedData );
3777
- if ( uploadData ) {
3778
- if ( Array.isArray( uploadData ) ) {
3779
- uniqueArr.push( ...uploadData );
3780
- } else {
3781
- uniqueArr.push( uploadData );
3782
- }
3783
- }
3658
+ uniqueArr.push( ...uploadData );
3784
3659
  } ) );
3785
3660
 
3786
3661
  return res.sendSuccess( { count: uniqueArr.length, uniqueArr } );
@@ -3790,3 +3665,82 @@ export async function checklistAssign( req, res ) {
3790
3665
  }
3791
3666
  }
3792
3667
 
3668
+ export async function updateAssign( req, res ) {
3669
+ try {
3670
+ if ( !req.body.assignedList.length && !req.body.assignedGroup.length ) {
3671
+ return res.sendError( 'No data found', 204 );
3672
+ }
3673
+ let checklistDetails = await checklistService.findOne( { _id: req.body.checkListId, client_id: req.body.clientId } );
3674
+ if ( !checklistDetails ) {
3675
+ return res.sendError( 'No data found', 204 );
3676
+ }
3677
+ req.body.assignedGroup = [ ...new Set( req.body.assignedGroup.map( ( item ) => item.id ) ) ];
3678
+ req.body.assignedGroup = req.body.assignedGroup.map( ( ele ) => {
3679
+ return { id: ele };
3680
+ } );
3681
+ await assignedService.deleteMany( { checkListId: req.body.checkListId } );
3682
+ let assignedUserList = [];
3683
+ await Promise.all( req.body.assignedList.map( async ( assign ) => {
3684
+ delete assign._id;
3685
+ delete assign.id;
3686
+ let query = [
3687
+ {
3688
+ $addFields: {
3689
+ emailToLower: { $toLower: '$email' },
3690
+ },
3691
+ },
3692
+ {
3693
+ $match: {
3694
+ clientId: req.body.clientId,
3695
+ emailToLower: assign.userEmail.toLowerCase(),
3696
+ },
3697
+ },
3698
+ ];
3699
+ let userDetails = await userService.aggregate( query );
3700
+ if ( !userDetails.length ) {
3701
+ let userData = {
3702
+ userName: assign.userName,
3703
+ email: assign.userEmail,
3704
+ mobileNumber: assign?.phone || '',
3705
+ clientId: req.body.clientId,
3706
+ };
3707
+ userDetails = await createUser( userData );
3708
+ }
3709
+ let data = {
3710
+ ...assign,
3711
+ store_id: assign.storeId,
3712
+ client_id: req.body.clientId,
3713
+ checkListId: req.body.checkListId,
3714
+ coverage: req.body.coverage,
3715
+ assignId: assign._id,
3716
+ userId: userDetails._id,
3717
+ checkListName: checklistDetails.checkListName,
3718
+ };
3719
+ assignedUserList.push( data );
3720
+ } ) );
3721
+ await Promise.all( req.body.assignedGroup.map( async ( assign ) => {
3722
+ let groupDetails;
3723
+ if ( req.body.coverage == 'store' ) {
3724
+ groupDetails = await clusterServices.findOneCluster( { _id: assign.id } );
3725
+ } else {
3726
+ groupDetails = await teamsServices.findOneTeams( { _id: assign.id } );
3727
+ }
3728
+ if ( groupDetails ) {
3729
+ let groupData = {
3730
+ client_id: req.body.clientId,
3731
+ checkListId: req.body.checkListId,
3732
+ coverage: req.body.coverage,
3733
+ assignId: assign.id,
3734
+ checkListName: checklistDetails.checkListName,
3735
+ ...( req.body.coverage == 'store' ) ? { clusterName: groupDetails?.clusterName } : { teamName: groupDetails?.teamName },
3736
+ };
3737
+ assignedUserList.push( groupData );
3738
+ }
3739
+ } ) );
3740
+ await assignedService.insertMany( assignedUserList );
3741
+ return res.sendSuccess( 'Assign details updated successfully' );
3742
+ } catch ( e ) {
3743
+ logger.error( { functionName: 'updateAssign', error: e } );
3744
+ return res.sendError( e, 500 );
3745
+ }
3746
+ }