tango-app-api-task 3.2.1-beta-20 → 3.2.1-beta-21

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-task",
3
- "version": "3.2.1-beta-20",
3
+ "version": "3.2.1-beta-21",
4
4
  "description": "Task",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -24,8 +24,8 @@
24
24
  "mongodb": "^6.10.0",
25
25
  "nodemon": "^3.1.7",
26
26
  "npm": "^10.9.2",
27
- "tango-api-schema": "^2.2.49",
28
- "tango-app-api-middleware": "^3.1.55",
27
+ "tango-api-schema": "^2.2.52",
28
+ "tango-app-api-middleware": "^3.1.58",
29
29
  "winston": "^3.17.0",
30
30
  "winston-daily-rotate-file": "^5.0.0"
31
31
  },
@@ -8,7 +8,9 @@ import * as storeService from '../service/store.service.js';
8
8
  import * as traxApprover from '../service/approver.service.js';
9
9
  import * as processedChecklist from '../service/processedChecklist.service.js';
10
10
  import * as checklistassignconfigModel from '../service/checklistAssign.service.js';
11
- import * as clusterModel from '../service/cluster.service.js';
11
+ import * as clusterServices from '../service/cluster.service.js';
12
+ import * as teamsServices from '../service/teams.service.js';
13
+ // import * as clusterModel from '../service/cluster.service.js';
12
14
  // import * as domainService from '../service/domain.service.js';
13
15
  import mongoose from 'mongoose';
14
16
  const ObjectId = mongoose.Types.ObjectId;
@@ -125,38 +127,6 @@ export async function createUpdateTask( req, res ) {
125
127
  } );
126
128
  } );
127
129
 
128
- for ( let [ index, question ] of section.questions.entries() ) {
129
- await processNested( index, question );
130
- }
131
-
132
- async function processNested( qIdx, question, nestedIndex = -1 ) {
133
- if ( question?.answers?.length ) {
134
- for ( let [ index, answer ] of question?.answers?.entries() ) {
135
- if ( !section.questions[qIdx].answers[index]?.nestedQuestion && nestedIndex == -1 ) {
136
- section.questions[qIdx].answers[index].nestedQuestion = [];
137
- }
138
- if ( answer.showLinked ) {
139
- if ( nestedIndex != -1 ) {
140
- section.questions[qIdx].answers[nestedIndex].nestedQuestion.push( answer.linkedQuestion );
141
- } else {
142
- section.questions[qIdx].answers[index].nestedQuestion.push( answer.linkedQuestion );
143
- }
144
- let nestedLinkedQuestion = section.questions.find( ( item ) => item.qno == answer.linkedQuestion );
145
- if ( nestedLinkedQuestion ) {
146
- let findNestedAnswers = nestedLinkedQuestion.answers.find( ( item ) => item.showLinked );
147
- if ( findNestedAnswers ) {
148
- if ( nestedIndex != -1 ) {
149
- await processNested( qIdx, nestedLinkedQuestion, nestedIndex );
150
- } else {
151
- await processNested( qIdx, nestedLinkedQuestion, index );
152
- }
153
- }
154
- }
155
- }
156
- }
157
- }
158
- }
159
-
160
130
  sectionList.push( {
161
131
  section: section?.name || 'Section 1',
162
132
  createdBy: req.user._id,
@@ -294,9 +264,9 @@ export async function taskDetails( req, res ) {
294
264
  }
295
265
  if ( storechecklistdetails ) {
296
266
  result.checkListDetails = { ...storechecklistdetails._doc };
297
- let removedUsers = await taskAssignService.find( { checkFlag: false, checkListId: storechecklistdetails._id }, { _id: 1 } );
298
- removedUsers = removedUsers?.map( ( item ) => item._id );
299
- result.checkListDetails = { ...result.checkListDetails, ...{ sections: sectionList }, ...{ removedUsers: removedUsers } };
267
+ let assignedusers = await taskAssignService.find( { checkListId: storechecklistdetails._id }, { _id: 1 } );
268
+ assignedusers = assignedusers?.map( ( item ) => item._id );
269
+ result.checkListDetails = { ...result.checkListDetails, ...{ sections: sectionList }, ...{ assignedusers: assignedusers } };
300
270
  }
301
271
  return res.sendSuccess( result );
302
272
  } catch ( e ) {
@@ -595,11 +565,8 @@ export async function userDetails( req, res ) {
595
565
  if ( !req.query.taskId ) {
596
566
  return res.sendError( { message: 'Task Id is required' }, 400 );
597
567
  }
598
- let limit = parseInt( req.query.limit ) || 5;
599
- let page = parseInt( req.query.offset ) || 0;
600
- let skip = limit * page;
601
568
 
602
- let query = [ { $match: { checkListId: new ObjectId( req.query.taskId ) } } ];
569
+ let query = [ { $match: { checkListId: new ObjectId( req.query.taskId ), ...( req.query.coverage ) ? { coverage: req.query.coverage } : {}, isdeleted: false } } ];
603
570
  if ( req.query?.search?.trim() && req.query?.search?.trim() != '' ) {
604
571
  let searchValue = req.query.search;
605
572
  searchValue = searchValue.split( ',' ).map( ( item ) => item.trim().toLowerCase() );
@@ -611,17 +578,6 @@ export async function userDetails( req, res ) {
611
578
  }
612
579
  }
613
580
 
614
- query.push( {
615
- $facet: {
616
- data: [
617
- { $skip: skip }, { $limit: limit },
618
- ],
619
- count: [
620
- { $count: 'total' },
621
- ],
622
- },
623
- } );
624
-
625
581
  let taskDetails = await taskAssignService.aggregate( query );
626
582
 
627
583
  if ( !taskDetails[0].data.length ) {
@@ -629,19 +585,70 @@ export async function userDetails( req, res ) {
629
585
  }
630
586
 
631
587
  let userDetails = [];
632
- taskDetails[0].data.forEach( ( item ) => {
633
- userDetails.push( {
634
- id: item._id,
635
- userName: item.userName,
636
- userEmail: item.userEmail,
637
- store_id: item.store_id,
638
- storeName: item.storeName,
639
- userPhone: item.userPhone,
640
- city: item.city,
641
- checkFlag: item.checkFlag,
642
- } );
643
- } );
644
- return res.sendSuccess( { users: userDetails, count: taskDetails[0].count[0].total } );
588
+ let storeList = [];
589
+ let userList = [];
590
+ await Promise.all( taskDetails.map( async ( ele ) => {
591
+ if ( ele?.store_id || ele?.clusterName ) {
592
+ storeList.push( ele.assignId );
593
+ } else {
594
+ userList.push( ele.assignId );
595
+ }
596
+
597
+ if ( ele?.clusterName ) {
598
+ let clusterDetails = await clusterServices.findOneCluster( { _id: ele.assignId }, { stores: 1 } );
599
+ if ( clusterDetails ) {
600
+ let storeDetails = await storeService.find( { _id: { $in: clusterDetails.stores.map( ( item ) => item.store ) } } );
601
+ storeDetails.forEach( ( item ) => {
602
+ userDetails.push( {
603
+ id: ele._id,
604
+ userName: item.spocDetails?.[0]?.name,
605
+ userEmail: item.spocDetails?.[0]?.email,
606
+ store_id: item?.storeId,
607
+ storeName: item?.storeName,
608
+ userPhone: item.spocDetails?.[0]?.phone,
609
+ city: ele?.city,
610
+ checkFlag: ele.checkFlag,
611
+ clusterName: ele?.clusterName || '',
612
+ teamName: ele?.teamName || '',
613
+ } );
614
+ } );
615
+ }
616
+ } else if ( ele?.teamName ) {
617
+ let teamDetails = await teamsServices.findOneTeams( { _id: ele.assignId }, { users: 1, teamName: 1 } );
618
+ if ( teamDetails ) {
619
+ let teamUserDetails = await userService.find( { _id: { $in: teamDetails.users.map( ( item ) => item.userId ) } } );
620
+ teamUserDetails.forEach( ( item ) => {
621
+ userDetails.push( {
622
+ id: ele._id,
623
+ userName: item.userName,
624
+ userEmail: item.email,
625
+ store_id: ele?.storeId,
626
+ storeName: ele?.storeName,
627
+ userPhone: item?.mobileNumber,
628
+ city: ele?.city,
629
+ checkFlag: ele.checkFlag,
630
+ clusterName: ele?.clusterName || '',
631
+ teamName: ele?.teamName || '',
632
+ } );
633
+ } );
634
+ }
635
+ } else {
636
+ userDetails.push( {
637
+ id: ele._id,
638
+ userName: ele.userName,
639
+ userEmail: ele.userEmail,
640
+ store_id: ele?.store_id,
641
+ storeName: ele?.storeName,
642
+ userPhone: ele.userPhone,
643
+ city: ele.city,
644
+ checkFlag: ele.checkFlag,
645
+ clusterName: ele?.clusterName || '',
646
+ teamName: ele?.teamName || '',
647
+ } );
648
+ }
649
+ } ) );
650
+
651
+ return res.sendSuccess( { count: userDetails.length, storeList, userList, users: limitedData } );
645
652
  } catch ( e ) {
646
653
  logger.error( { functionName: 'userDetails', error: e, message: req.body } );
647
654
  return res.sendError( e, 500 );
@@ -651,8 +658,6 @@ export async function userDetails( req, res ) {
651
658
  export async function taskConfig( req, res ) {
652
659
  try {
653
660
  let inputBody = req.body;
654
- let storeCount = 0;
655
- let locationCount = 0;
656
661
  let checklistDetails;
657
662
  inputBody.client_id = inputBody.clientId;
658
663
  if ( !inputBody._id ) {
@@ -682,7 +687,7 @@ export async function taskConfig( req, res ) {
682
687
  };
683
688
  await checklistLogs.create( logInsertData );
684
689
 
685
- checklistDetails = await taskService.findOne( { _id: inputBody._id, isdeleted: false } );
690
+ checklistDetails = await taskService.findOne( { _id: inputBody._id, isdeleted: false, client_id: inputBody.clientId } );
686
691
 
687
692
  if ( !checklistDetails ) {
688
693
  return res.sendError( 'No data found', 204 );
@@ -720,39 +725,20 @@ export async function taskConfig( req, res ) {
720
725
  await traxApprover.insertMany( data );
721
726
  }
722
727
  }
723
- await taskAssignService.updateMany( { checkListId: inputBody._id }, { checkFlag: true } );
724
- if ( inputBody.removedUsers.length ) {
725
- await taskAssignService.updateMany( { _id: { $in: inputBody.removedUsers } }, { checkFlag: false } );
728
+ if ( inputBody.assignedUsers.length ) {
729
+ await taskAssignService.deleteMany( { checkListId: inputBody._id } );
730
+ await Promise.all( inputBody.assignedUsers.map( async ( user ) => {
731
+ let data = {
732
+ ...user,
733
+ clientId: req.body.clientId,
734
+ checkListName: checklistDetails.checkListName,
735
+ checklistId: checklistDetails._id,
736
+ coverage: inputBody.coverage,
737
+ };
738
+ await assignUsers( data );
739
+ } ) );
726
740
  }
727
741
  let storeConfigDetails = await taskAssignService.find( { checkListId: inputBody._id, checkFlag: true }, { _id: 0, store_id: 1, userEmail: 1, storeName: 1 } );
728
- let storeList = storeConfigDetails.map( ( store ) => store.store_id );
729
-
730
- storeCount = storeList.length;
731
- if ( storeList.length ) {
732
- let query = [
733
- {
734
- $match: {
735
- client_id: inputBody.client_id,
736
- id: { $in: storeList },
737
- city: { $exists: true },
738
- },
739
- },
740
- {
741
- $group: {
742
- _id: '$city',
743
- },
744
- },
745
- {
746
- $project: {
747
- _id: 0,
748
- city: '$_id',
749
- },
750
- },
751
- ];
752
- let storeDetails = await storeService.aggregate( query );
753
- locationCount = storeDetails.length;
754
- }
755
- await taskService.updateOne( { _id: inputBody._id }, { storeCount, locationCount } );
756
742
  if ( inputBody.submitType == 'publish' ) {
757
743
  let taskName = checklistDetails.checkListName;
758
744
  let dueDate = dayjs( configDetails.scheduleEndTimeISO ).format( 'YYYY-MM-DD hh:mm A' );
@@ -910,7 +896,6 @@ export async function insertSingleProcessData( checklistId ) {
910
896
  await taskProcessedConfigService.updateOne( { _id: checklistDetails._id }, insertDataPerDay );
911
897
  updatedchecklist = checklistDetails;
912
898
  }
913
-
914
899
  if ( updatedchecklist ) {
915
900
  await insertPCBulkV3( getCLconfig, checklistId, updatedchecklist, dateVal, startTimeIso, endTimeIso, insertdata );
916
901
  }
@@ -938,7 +923,80 @@ async function insertPCBulkV3( getCLconfig, checklistId, updatedchecklist, date,
938
923
 
939
924
  if ( allQuestion ) {
940
925
  let userIdList = [];
941
- for ( let element4 of allQuestion ) {
926
+ let assignList = [];
927
+ let assignUserList = [];
928
+ if ( getCLconfig.coverage == 'store' ) {
929
+ let clusterList = allQuestion.filter( ( ele ) => ele?.clusterName ).map( ( item ) => item.assignId );
930
+ if ( clusterList.length ) {
931
+ let clusterDetails = await clusterServices.findcluster( { _id: { $in: clusterList } } );
932
+ if ( clusterDetails.length ) {
933
+ let idList = clusterDetails.flatMap( ( item ) => item.stores.map( ( ele ) => ele.store ) );
934
+ let getStoreDetails = await storeService.find( { _id: { $in: idList } } );
935
+ if ( getStoreDetails.length ) {
936
+ assignList = await Promise.all( getStoreDetails.map( async ( store ) => {
937
+ let userDetails = await userService.findOne( { email: store?.spocDetails?.[0]?.email, clientId: store.clientId } );
938
+ if ( !userDetails ) {
939
+ let data = {
940
+ clientId: store.clientId,
941
+ userName: store.spocDetails?.[0]?.name,
942
+ mobileNumber: store.spocDetails?.[0]?.phone || '',
943
+ email: store.spocDetails[0].email,
944
+ };
945
+ userDetails = await createUser( data );
946
+ }
947
+ let data = {
948
+ store_id: store?.storeId,
949
+ storeName: store?.storeName,
950
+ userId: userDetails._id,
951
+ userName: userDetails.userName,
952
+ userEmail: userDetails.email,
953
+ userPhone: userDetails?.mobileNumber,
954
+ city: store?.storeProfile?.city,
955
+ country: store?.storeProfile?.country,
956
+ checkFlag: true,
957
+ checkListId: getCLconfig._id,
958
+ checkListName: getCLconfig.checkListName,
959
+ client_id: getCLconfig.client_id,
960
+ };
961
+ return data;
962
+ } ) );
963
+ }
964
+ }
965
+ }
966
+ allQuestion = allQuestion.filter( ( ele ) => !clusterList.includes( ele.assignId ) );
967
+ }
968
+ if ( getCLconfig.coverage == 'user' ) {
969
+ let teamsList = allQuestion.filter( ( ele ) => ele?.teamName ).map( ( item ) => item.assignId );
970
+ if ( teamsList.length ) {
971
+ let teamDetails = await teamsServices.findteams( { _id: { $in: teamsList } } );
972
+ if ( teamDetails.length ) {
973
+ let idList = teamDetails.flatMap( ( item ) => item.users.map( ( ele ) => ele.userId ) );
974
+ let getUserDetails = await userService.find( { _id: { $in: idList } } );
975
+ if ( getUserDetails.length ) {
976
+ assignList = getUserDetails.map( ( user ) => {
977
+ let data = {
978
+ store_id: '',
979
+ storeName: '',
980
+ userId: user._id,
981
+ userName: user.userName,
982
+ userEmail: user.email,
983
+ userPhone: user?.mobileNumber,
984
+ city: '',
985
+ country: '',
986
+ checkFlag: true,
987
+ checkListId: getCLconfig._id,
988
+ checkListName: getCLconfig.checkListName,
989
+ client_id: getCLconfig.client_id,
990
+ };
991
+ return data;
992
+ } );
993
+ }
994
+ }
995
+ }
996
+ allQuestion = allQuestion.filter( ( ele ) => !teamsList.includes( ele.assignId ) );
997
+ }
998
+ allQuestion = [ ...allQuestion, ...assignList ];
999
+ await Promise.all( allQuestion.map( async ( element4 ) => {
942
1000
  let query;
943
1001
  query = {
944
1002
  date_string: dayjs( date ).format( 'YYYY-MM-DD' ),
@@ -954,38 +1012,39 @@ async function insertPCBulkV3( getCLconfig, checklistId, updatedchecklist, date,
954
1012
  let getsubmitDetails = await taskProcessedService.find( query );
955
1013
  if ( getsubmitDetails.length ) {
956
1014
  userIdList.push( element4._id );
957
- continue;
958
- }
959
- delete element4._id;
960
- delete element4.checkFlag;
961
- delete element4.isdeleted;
962
- delete element4.createdAt;
963
- delete element4.updatedAt;
964
- element4.checkListId = updatedchecklist._id;
965
- element4.checkListName = getCLconfig.checkListName;
966
- element4.checkListDescription = getCLconfig.checkListDescription;
967
- element4.date_iso = new Date( dayjs( date, 'YYYY-MM-DD' ).format( 'YYYY-MM-DD' ) );
968
- element4.date_string = dayjs( date ).format( 'YYYY-MM-DD' );
969
- element4.allowedOverTime = false;
970
- element4.allowedStoreLocation = getCLconfig.allowedStoreLocation;
971
- element4.scheduleStartTime = '12:00 AM';
972
- element4.scheduleStartTime_iso = startTimeIso.format();
973
- element4.scheduleEndTime = getCLconfig.scheduleEndTime;
974
- element4.scheduleEndTime_iso = endTimeIso.format();
975
- element4.createdBy = new ObjectId( getCLconfig.createdBy );
976
- element4.createdByName = getCLconfig.createdByName;
977
- element4.sourceCheckList_id = getCLconfig._id;
978
- element4.checkListType = getCLconfig.checkListType;
979
- element4.storeCount = getCLconfig.storeCount;
980
- element4.questionCount = getCLconfig.questionCount;
981
- element4.publishDate = getCLconfig.publishDate;
982
- element4.locationCount = getCLconfig.locationCount;
983
- element4.scheduleRepeatedType = 'daily';
984
- element4.approvalEnable = getCLconfig.approver.length ? true : false;
985
- element4.priorityType = getCLconfig.priorityType;
986
- element4.remainder = getCLconfig?.remainder || [];
987
- element4.restrictAttendance = getCLconfig?.restrictAttendance;
988
- }
1015
+ } else {
1016
+ delete element4._id;
1017
+ delete element4.checkFlag;
1018
+ delete element4.isdeleted;
1019
+ delete element4.createdAt;
1020
+ delete element4.updatedAt;
1021
+ element4.checkListId = updatedchecklist._id;
1022
+ element4.checkListName = getCLconfig.checkListName;
1023
+ element4.checkListDescription = getCLconfig.checkListDescription;
1024
+ element4.date_iso = new Date( dayjs( date, 'YYYY-MM-DD' ).format( 'YYYY-MM-DD' ) );
1025
+ element4.date_string = dayjs( date ).format( 'YYYY-MM-DD' );
1026
+ element4.allowedOverTime = false;
1027
+ element4.allowedStoreLocation = getCLconfig.allowedStoreLocation;
1028
+ element4.scheduleStartTime = '12:00 AM';
1029
+ element4.scheduleStartTime_iso = startTimeIso.format();
1030
+ element4.scheduleEndTime = getCLconfig.scheduleEndTime;
1031
+ element4.scheduleEndTime_iso = endTimeIso.format();
1032
+ element4.createdBy = new ObjectId( getCLconfig.createdBy );
1033
+ element4.createdByName = getCLconfig.createdByName;
1034
+ element4.sourceCheckList_id = getCLconfig._id;
1035
+ element4.checkListType = getCLconfig.checkListType;
1036
+ element4.storeCount = getCLconfig.storeCount;
1037
+ element4.questionCount = getCLconfig.questionCount;
1038
+ element4.publishDate = getCLconfig.publishDate;
1039
+ element4.locationCount = getCLconfig.locationCount;
1040
+ element4.scheduleRepeatedType = 'daily';
1041
+ element4.approvalEnable = getCLconfig.approver.length ? true : false;
1042
+ element4.priorityType = getCLconfig.priorityType;
1043
+ element4.remainder = getCLconfig?.remainder || [];
1044
+ element4.restrictAttendance = getCLconfig?.restrictAttendance;
1045
+ assignUserList.push( element4 );
1046
+ }
1047
+ } ) );
989
1048
  if ( userIdList.length ) {
990
1049
  allQuestion = allQuestion.filter( ( item ) => typeof item._id == 'undefined' );
991
1050
  }
@@ -1390,8 +1449,9 @@ export async function createChecklistTask( req, res ) {
1390
1449
  if ( sectionIndex == -1 ) {
1391
1450
  return res.sendError( 'section is not found', 400 );
1392
1451
  }
1393
- let data = { ...question[sectionIndex].questions[req.body.qno - 1], taskId: response?._id, task: true };
1452
+ let findQuestion = question[sectionIndex].questions.findIndex( ( ele ) => ele?.parentQuestion ? ele.qno == req.body.qno && ele.parentQuestion == req.body.parentQuestion : ele.qno == req.body.qno );
1394
1453
 
1454
+ let data = { ...question[sectionIndex].questions[findQuestion], taskId: response?._id, task: true };
1395
1455
  question[sectionIndex].questions[req.body.qno - 1] = data;
1396
1456
  taskDetails.questionAnswers = question;
1397
1457
  let updateData = {
@@ -2168,13 +2228,13 @@ export async function clusterMigrations( req, res ) {
2168
2228
  if ( userexits ) {
2169
2229
  let findStore = await storeService.findOne( { storeName: user.FacilityCode } );
2170
2230
  if ( findStore ) {
2171
- let clusterExists = await clusterModel.findOneCluster( { clusterName: user.AomName } );
2231
+ let clusterExists = await clusterServices.findOneCluster( { clusterName: user.AomName } );
2172
2232
  if ( clusterExists ) {
2173
2233
  let data = {
2174
2234
  storeId: findStore.storeId,
2175
2235
  store: findStore._id,
2176
2236
  };
2177
- let updatecluster = await clusterModel.updateCluster( { clusterName: user.AomName }, { $push: { stores: data } } );
2237
+ let updatecluster = await clusterServices.updateCluster( { clusterName: user.AomName }, { $push: { stores: data } } );
2178
2238
  console.log( updatecluster );
2179
2239
  } else {
2180
2240
  let payload = {
@@ -2196,7 +2256,7 @@ export async function clusterMigrations( req, res ) {
2196
2256
  ],
2197
2257
  'users': [],
2198
2258
  };
2199
- let createcluster = await clusterModel.createclusterModel( payload );
2259
+ let createcluster = await clusterServices.createclusterModel( payload );
2200
2260
  console.log( createcluster );
2201
2261
  }
2202
2262
  }
@@ -3268,6 +3328,340 @@ export async function uploadmultiImage( images ) {
3268
3328
  }
3269
3329
  };
3270
3330
 
3331
+ export async function taskAssign( req, res ) {
3332
+ try {
3333
+ if ( !req.body.taskId ) {
3334
+ return res.sendError( 'Checklist id is required', 400 );
3335
+ }
3336
+ if ( !req.body.coverage ) {
3337
+ return res.sendError( 'Coverage is required', 400 );
3338
+ }
3339
+ if ( !req.body.idList ) {
3340
+ return res.sendError( 'Id is required', 400 );
3341
+ }
3342
+
3343
+ let taskDetails = await taskService.findOne( { _id: req.body.taskId } );
3344
+ if ( !taskDetails ) {
3345
+ return res.sendError( 'No data found', 204 );
3346
+ }
3347
+ let idList;
3348
+ let assignList;
3349
+ if ( req.body.coverage == 'store' ) {
3350
+ let clusterDetails = await clusterServices.findcluster( { _id: { $in: req.body.idList } }, { stores: 1, clusterName: 1 } );
3351
+ if ( clusterDetails.length ) {
3352
+ let clusterList = clusterDetails.map( ( item ) => item._id.toString() );
3353
+ idList = [
3354
+ ...clusterDetails.flatMap( ( item ) => item.stores.map( ( ele ) => ele.store ) ),
3355
+ ...req.body.idList.filter( ( ele ) => !clusterList.includes( ele ) ),
3356
+ ];
3357
+ } else {
3358
+ idList = req.body.idList;
3359
+ }
3360
+ let getStoreDetails = await storeService.find( { _id: { $in: idList } } );
3361
+ if ( !getStoreDetails.length ) {
3362
+ return res.sendError( 'No data found', 204 );
3363
+ }
3364
+ assignList = await Promise.all( getStoreDetails.map( async ( store ) => {
3365
+ let userDetails = await userService.findOne( { email: store?.spocDetails?.[0]?.email, clientId: store.clientId } );
3366
+ if ( !userDetails ) {
3367
+ let data = {
3368
+ clientId: store.clientId,
3369
+ userName: store.spocDetails?.[0]?.name,
3370
+ mobileNumber: store.spocDetails?.[0]?.phone || '',
3371
+ email: store.spocDetails[0].email,
3372
+ };
3373
+ userDetails = await createUser( data );
3374
+ }
3375
+ let data = {
3376
+ store_id: store.storeId,
3377
+ storeName: store.storeName,
3378
+ userId: userDetails._id,
3379
+ userName: userDetails.userName,
3380
+ userEmail: userDetails.email,
3381
+ userPhone: userDetails?.mobileNumber,
3382
+ clusterName: clusterDetails?.clusterName,
3383
+ };
3384
+ return data;
3385
+ } ) );
3386
+ } else {
3387
+ let teamDetails = await teamsServices.findteams( { _id: { $in: req.body.idList } }, { users: 1, teamName: 1 } );
3388
+ if ( teamDetails.length ) {
3389
+ let teamList = teamDetails.map( ( ele ) => ele._id.toString() );
3390
+ idList = [
3391
+ ...teamDetails.flatMap( ( item ) => item.users.map( ( ele ) => ele.userId ) ),
3392
+ ...req.body.idList.filter( ( ele ) => !teamList.includes( ele._id ) ),
3393
+ ];
3394
+ } else {
3395
+ idList = req.body.idList;
3396
+ }
3397
+ let userDetails = await userService.find( { _id: { $in: idList } } );
3398
+ if ( !userDetails.length ) {
3399
+ return res.sendError( 'No data found', 204 );
3400
+ }
3401
+ assignList = [];
3402
+ userDetails.forEach( ( user ) => {
3403
+ assignList.push( {
3404
+ userId: user._id,
3405
+ userName: user.userName,
3406
+ userEmail: user.email,
3407
+ userPhone: user?.mobileNumber,
3408
+ teamName: teamDetails?.teamName,
3409
+ } );
3410
+ } );
3411
+ }
3412
+ return res.sendSuccess( { count: assignList.length, assignList } );
3413
+ } catch ( e ) {
3414
+ logger.error( { functionName: 'checklistAssign', error: e } );
3415
+ return res.sendError( e, 500 );
3416
+ }
3417
+ }
3418
+
3419
+ async function assignUsers( data ) {
3420
+ let assignedData;
3421
+ if ( data.coverage == 'store' ) {
3422
+ if ( data?.type == 'cluster' ) {
3423
+ let query = [
3424
+ {
3425
+ $addFields: {
3426
+ cluster: { $toLower: '$clusterName' },
3427
+ },
3428
+ },
3429
+ {
3430
+ $match: {
3431
+ clientId: data.clientId,
3432
+ ...( !data?.upload ) ? { _id: new mongoose.Types.ObjectId( data.id ) } : { cluster: data.clusterName.toLowerCase() },
3433
+ },
3434
+ },
3435
+ {
3436
+ $project: {
3437
+ stores: 1,
3438
+ clusterName: 1,
3439
+ },
3440
+ },
3441
+ ];
3442
+ let clusterDetails = await clusterServices.aggregateCluster( query );
3443
+ console.log( clusterDetails );
3444
+ if ( clusterDetails.length ) {
3445
+ if ( !data.upload ) {
3446
+ assignedData = {
3447
+ checkFlag: true,
3448
+ checkListId: data.checklistId,
3449
+ checkListName: data.checkListName,
3450
+ client_id: data?.clientId,
3451
+ clusterName: clusterDetails?.[0]?.clusterName,
3452
+ assignId: data?.id || clusterDetails?.[0]?._id,
3453
+ coverage: 'store',
3454
+ };
3455
+ } else {
3456
+ let clusterList = clusterDetails[0].stores.map( ( ele ) => ele.store );
3457
+ let storeDetails = await storeService.find( { _id: clusterList } );
3458
+ assignedData = await Promise.all( storeDetails.map( async ( store ) => {
3459
+ let userDetails = await userService.findOne( { email: store.spocDetails?.[0]?.email, clientId: data.clientId } );
3460
+ if ( !userDetails ) {
3461
+ let userData = {
3462
+ userName: store.spocDetails?.[0]?.name,
3463
+ email: store.spocDetails[0].email,
3464
+ mobileNumber: store.spocDetails?.[0]?.phone,
3465
+ clientId: data.clientId,
3466
+ };
3467
+ userDetails = await createUser( userData );
3468
+ }
3469
+ let userData = {
3470
+ userId: userDetails?._id,
3471
+ userName: userDetails?.userName,
3472
+ userEmail: userDetails?.email,
3473
+ userPhone: userDetails?.mobileNumber,
3474
+ checkFlag: true,
3475
+ checkListId: data.checklistId,
3476
+ checkListName: data.checkListName,
3477
+ client_id: data.clientId,
3478
+ assignId: data?.id || clusterDetails?.[0]?._id,
3479
+ coverage: 'store',
3480
+ teamName: clusterDetails?.[0]?.clusterName,
3481
+ };
3482
+ return userData;
3483
+ } ) );
3484
+ }
3485
+ }
3486
+ } else {
3487
+ let query = [
3488
+ {
3489
+ $addFields: {
3490
+ store: { $toLower: '$storeName' },
3491
+ },
3492
+ },
3493
+ {
3494
+ $match: {
3495
+ clientId: data.clientId,
3496
+ ...( !data.upload ) ? { _id: new mongoose.Types.ObjectId( data.id ) } : { store: data.storeName.toLowerCase() },
3497
+ },
3498
+ },
3499
+ ];
3500
+ let storeDetails = await storeService.aggregate( query );
3501
+ if ( storeDetails.length ) {
3502
+ let email = data?.upload ? data.userEmail : storeDetails?.[0]?.spocDetails?.[0]?.email;
3503
+ let userDetails = await userService.findOne( { email: email, clientId: data.clientId } );
3504
+ if ( !userDetails ) {
3505
+ let userData = {
3506
+ userName: storeDetails?.[0]?.spocDetails?.[0]?.name,
3507
+ email: storeDetails?.[0]?.spocDetails[0].email,
3508
+ mobileNumber: storeDetails?.[0]?.spocDetails?.[0]?.phone,
3509
+ clientId: data.clientId,
3510
+ };
3511
+ userDetails = await createUser( userData );
3512
+ }
3513
+ assignedData = {
3514
+ store_id: storeDetails?.[0]?.storeId,
3515
+ storeName: storeDetails?.[0]?.storeName,
3516
+ userId: userDetails._id,
3517
+ userName: userDetails.userName,
3518
+ userEmail: userDetails.email,
3519
+ userPhone: userDetails?.mobileNumber,
3520
+ city: storeDetails?.[0]?.storeProfile?.city,
3521
+ country: storeDetails?.[0]?.storeProfile?.country,
3522
+ checkFlag: true,
3523
+ checkListId: data.checklistId,
3524
+ checkListName: data.checkListName,
3525
+ client_id: data.clientId,
3526
+ assignId: data?.id || storeDetails?.[0]?._id,
3527
+ coverage: 'store',
3528
+ };
3529
+ }
3530
+ }
3531
+ } else {
3532
+ if ( data.type == 'teams' ) {
3533
+ let query = [
3534
+ {
3535
+ $addFields: {
3536
+ team: { $toLower: '$teamName' },
3537
+ },
3538
+ },
3539
+ {
3540
+ $match: {
3541
+ clientId: data.clientId,
3542
+ ...( !data?.upload ) ? { _id: new mongoose.Types.ObjectId( data.id ) } : { team: data.teamName.toLowerCase() },
3543
+ },
3544
+ },
3545
+ ];
3546
+ let teamDetails = await teamsServices.aggregateTeams( query );
3547
+ if ( teamDetails.length ) {
3548
+ if ( !data.upload ) {
3549
+ assignedData = {
3550
+ checkFlag: true,
3551
+ checkListId: data.checklistId,
3552
+ checkListName: data.checkListName,
3553
+ client_id: data.clientId,
3554
+ teamName: teamDetails?.[0]?.teamName,
3555
+ assignId: data?.id || teamDetails?.[0]?._id,
3556
+ coverage: 'user',
3557
+ };
3558
+ } else {
3559
+ let userIdList = teamDetails[0].users.map( ( ele ) => ele.userId );
3560
+ let userDetails = await userService.find( { _id: userIdList } );
3561
+ assignedData = userDetails.map( ( user ) => {
3562
+ let userData = {
3563
+ userId: user._id,
3564
+ userName: user.userName,
3565
+ userEmail: user.email,
3566
+ userPhone: user.mobileNumber,
3567
+ checkFlag: true,
3568
+ checkListId: data.checklistId,
3569
+ checkListName: data.checkListName,
3570
+ client_id: data.clientId,
3571
+ assignId: data?.id || teamDetails?.[0]?._id,
3572
+ coverage: 'user',
3573
+ teamName: teamDetails?.[0]?.teamName,
3574
+ };
3575
+ return userData;
3576
+ } );
3577
+ }
3578
+ }
3579
+ } else {
3580
+ let query = [
3581
+ {
3582
+ $addFields: {
3583
+ userEmail: { $toLower: '$email' },
3584
+ },
3585
+ },
3586
+ {
3587
+ $match: {
3588
+ clientId: data.clientId,
3589
+ ...( !data?.upload ) ? { _id: new mongoose.Types.ObjectId( data.id ) } : { userEmail: data.userEmail.toLowerCase() },
3590
+ },
3591
+ },
3592
+ ];
3593
+ let userDetails = await userService.aggregate( query );
3594
+ if ( !userDetails.length ) {
3595
+ let userData = {
3596
+ userName: data.userEmail.split( '@' )[0],
3597
+ email: data.userEmail,
3598
+ clientId: data.clientId,
3599
+ };
3600
+ let details = await createUser( userData );
3601
+ userDetails = [ details ];
3602
+ }
3603
+ if ( userDetails.length ) {
3604
+ assignedData = {
3605
+ userId: userDetails?.[0]?._id,
3606
+ userName: userDetails?.[0]?.userName,
3607
+ userEmail: userDetails?.[0]?.email,
3608
+ userPhone: userDetails?.[0]?.mobileNumber,
3609
+ checkFlag: true,
3610
+ checkListId: data.checklistId,
3611
+ checkListName: data.checkListName,
3612
+ client_id: data.clientId,
3613
+ assignId: data?.id || userDetails?.[0]?._id,
3614
+ coverage: 'user',
3615
+ };
3616
+ }
3617
+ }
3618
+ }
3619
+ if ( assignedData && !data.upload ) {
3620
+ await taskAssignService.updateOne( { checkListId: assignedData.checkListId, assignId: assignedData.assignId }, assignedData );
3621
+ } else {
3622
+ return assignedData;
3623
+ }
3624
+ }
3625
+
3626
+ export async function assignTaskUser( req, res ) {
3627
+ try {
3628
+ let inputBody = req.body;
3629
+ let assignDetails = inputBody.assignUsers;
3630
+ let newUsers = inputBody.newUsers;
3631
+ let taskDetails = await taskService.findOne( { _id: inputBody.taskId } );
3632
+ if ( !taskDetails ) {
3633
+ return res.sendError( 'No data found', 204 );
3634
+ }
3635
+ await Promise.all( newUsers.map( async ( user ) => {
3636
+ let getUser = inputBody.assignUsers.find( ( ele ) => ele.userEmail.toLowerCase() == user.toLowerCase() );
3637
+ let userData = {
3638
+ userName: getUser.userName,
3639
+ email: user,
3640
+ mobileNumber: getUser?.phone,
3641
+ clientId: inputBody.clientId,
3642
+ };
3643
+ await createUser( userData );
3644
+ } ) );
3645
+ let assignData = [];
3646
+ await Promise.all( assignDetails.map( async ( assign ) => {
3647
+ assign.checklistId = inputBody.taskId;
3648
+ assign.checkListName = taskDetails.checkListName;
3649
+ assign.coverage = inputBody.coverage;
3650
+ assign.clientId = inputBody.clientId;
3651
+ assign.upload = inputBody.type;
3652
+ let uploadData = await assignUsers( assign );
3653
+ if ( Array.isArray( uploadData ) ) {
3654
+ assignData.push( ...uploadData );
3655
+ } else {
3656
+ assignData.push( uploadData );
3657
+ }
3658
+ } ) );
3659
+ return res.sendSuccess( { idList: [ ...new Set( assignData.map( ( item ) => item.assignId ) ) ], assignData } );
3660
+ } catch ( e ) {
3661
+ logger.error( { functionName: 'assignTaskUser', error: e } );
3662
+ return res.sendError( e, 500 );
3663
+ }
3664
+ }
3271
3665
 
3272
3666
  export async function customertrial( params ) {
3273
3667
  try {
@@ -28,6 +28,9 @@ taskRouter
28
28
  .get( '/teamMigrations', taskController.teamMigrations )
29
29
  .get( '/clusterMigrations', taskController.clusterMigrations )
30
30
  .post( '/createaiChecklist', isAllowedInternalAPIHandler, taskController.createAiChecklist )
31
+ .post( '/createaiTask', isAllowedInternalAPIHandler, taskController.createAiTask )
32
+ .post( '/assign', isAllowedSessionHandler, taskController.taskAssign )
33
+ .post( '/assignUpload', isAllowedSessionHandler, taskController.assignTaskUser )
31
34
  .post( '/createaiTask', isAllowedInternalAPIHandler, validate( aitaskvalidation ), taskController.createAiTask )
32
35
  .post( '/StoreHygienetask', isAllowedInternalAPIHandler, validate( StoreHygienetaskvalidation ), taskController.StoreHygienetask )
33
36
  .post( '/eyeTesttask', isAllowedInternalAPIHandler, validate( eyeTesttaskvalidation ), taskController.eyeTesttask )
@@ -26,6 +26,7 @@ export async function createclusterModel( data ) {
26
26
  export async function findcluster( query, project ) {
27
27
  return await clusterModel.find( query, project );
28
28
  };
29
+
29
30
  export function countDocumentsClusters( query ) {
30
31
  return clusterModel.countDocuments( query );
31
32
  }
@@ -13,7 +13,7 @@ export const updateMany = async ( query = {}, record={} ) => {
13
13
  };
14
14
 
15
15
  export const updateOne = async ( query = {}, record={} ) => {
16
- return model.taskAssignModel.updateOne( query, { $set: record } );
16
+ return model.taskAssignModel.updateOne( query, { $set: record }, { upsert: true } );
17
17
  };
18
18
 
19
19
  export const insertMany = async ( data = [] ) => {
@@ -0,0 +1,30 @@
1
+ import teamsModel from 'tango-api-schema/schema/teams.model.js';
2
+
3
+
4
+ export async function createTeamsModel( data ) {
5
+ return await teamsModel.create( data );
6
+ };
7
+ export async function updateOneTeams( query, record ) {
8
+ return await teamsModel.updateOne( query, { $set: record }, { upsert: true } );
9
+ };
10
+
11
+ export async function aggregateTeams( query ) {
12
+ return await teamsModel.aggregate( query );
13
+ };
14
+ export async function deleteTeams( query ={} ) {
15
+ return await teamsModel.deleteOne( query );
16
+ };
17
+
18
+ export async function findOneTeams( query ={}, field={} ) {
19
+ return await teamsModel.findOne( query, field );
20
+ };
21
+ export async function findteams( query ={}, field={} ) {
22
+ return await teamsModel.find( query, field );
23
+ };
24
+
25
+ export async function updateOneTeamModel( query, record ) {
26
+ return await teamsModel.updateOne( query, record );
27
+ }
28
+ export function countDocumentsTeams( query ) {
29
+ return teamsModel.countDocuments( query );
30
+ }