tango-app-api-task 3.3.1-beta-1 → 3.3.1-beta-3
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 +3 -3
- package/src/controllers/task.controller.js +1065 -190
- package/src/controllers/taskActionCenter.controllers.js +17 -12
- package/src/controllers/taskDashboard.controllers.js +30 -12
- package/src/dtos/task.dto.js +28 -0
- package/src/routes/task.routes.js +7 -2
- package/src/service/taskAssign.service.js +1 -1
- package/src/service/teams.service copy.js +30 -0
- package/src/service/user.service.js +4 -0
|
@@ -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
|
|
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,29 @@ export async function taskDetails( req, res ) {
|
|
|
294
264
|
}
|
|
295
265
|
if ( storechecklistdetails ) {
|
|
296
266
|
result.checkListDetails = { ...storechecklistdetails._doc };
|
|
297
|
-
let
|
|
298
|
-
|
|
299
|
-
|
|
267
|
+
let query = {
|
|
268
|
+
checkListId: storechecklistdetails._id,
|
|
269
|
+
...( storechecklistdetails.coverage == 'store' ) ? { $or: [ { store_id: { $exists: true } }, { clusterName: { $exists: true } } ] } : { $or: [ { userEmail: { $exists: true } }, { teamName: { $exists: true } } ] },
|
|
270
|
+
};
|
|
271
|
+
let assignList = await taskAssignService.find( query, { _id: 1, store_id: 1, clusterName: 1, userEmail: 1, teamName: 1, assignId: 1 } );
|
|
272
|
+
let singleAssign;
|
|
273
|
+
let groupAssign;
|
|
274
|
+
if ( storechecklistdetails.coverage == 'store' ) {
|
|
275
|
+
singleAssign = assignList.filter( ( item ) => typeof item?.store_id != 'undefined' && item?.store_id != '' ).map( ( item ) => {
|
|
276
|
+
return { id: item.assignId, type: 'store' };
|
|
277
|
+
} );
|
|
278
|
+
groupAssign = assignList.filter( ( item ) => typeof item?.clusterName != 'undefined' && item?.clusterName != '' ).map( ( item ) => {
|
|
279
|
+
return { id: item.assignId, type: 'cluster' };
|
|
280
|
+
} );
|
|
281
|
+
} else {
|
|
282
|
+
singleAssign = assignList.filter( ( item ) => typeof item?.userEmail != 'undefined' && item?.userEmail !='' ).map( ( item ) => {
|
|
283
|
+
return { id: item.assignId, type: 'user' };
|
|
284
|
+
} );
|
|
285
|
+
groupAssign = assignList.filter( ( item ) => typeof item?.teamName != 'undefined' && item?.teamName != '' ).map( ( item ) => {
|
|
286
|
+
return { id: item.assignId, type: 'teams' };
|
|
287
|
+
} );
|
|
288
|
+
}
|
|
289
|
+
result.checkListDetails = { ...result.checkListDetails, ...{ sections: sectionList }, singleAssign, groupAssign };
|
|
300
290
|
}
|
|
301
291
|
return res.sendSuccess( result );
|
|
302
292
|
} catch ( e ) {
|
|
@@ -307,7 +297,7 @@ export async function taskDetails( req, res ) {
|
|
|
307
297
|
|
|
308
298
|
export async function validateUser( req, res ) {
|
|
309
299
|
try {
|
|
310
|
-
if ( !req.body.assignedUsers
|
|
300
|
+
if ( !req.body.assignedUsers?.length ) {
|
|
311
301
|
return res.sendError( 'Please Enter User Details', 400 );
|
|
312
302
|
}
|
|
313
303
|
|
|
@@ -465,6 +455,186 @@ export async function validateUser( req, res ) {
|
|
|
465
455
|
}
|
|
466
456
|
};
|
|
467
457
|
|
|
458
|
+
export const validateUserv1 = async ( req, res ) => {
|
|
459
|
+
try {
|
|
460
|
+
if ( !req.body.assignedUsers?.length ) {
|
|
461
|
+
return res.sendError( 'Please Enter user Details', 400 );
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
let assignDetails = req.body.assignedUsers;
|
|
465
|
+
|
|
466
|
+
if ( req.body.coverage == 'store' ) {
|
|
467
|
+
const duplicateStore = assignDetails.reduce( ( acc, obj ) => {
|
|
468
|
+
if ( !acc[obj.storeName.toLowerCase()] ) {
|
|
469
|
+
acc[obj.storeName.toLowerCase()] = {
|
|
470
|
+
email: [ obj.userEmail.toLowerCase() ],
|
|
471
|
+
count: 1,
|
|
472
|
+
};
|
|
473
|
+
} else {
|
|
474
|
+
if ( acc[obj.storeName.toLowerCase()] ) {
|
|
475
|
+
if ( acc[obj.storeName.toLowerCase()].email.includes( obj.userEmail.toLowerCase() ) ) {
|
|
476
|
+
acc[obj.storeName.toLowerCase()].count++;
|
|
477
|
+
} else {
|
|
478
|
+
acc[obj.storeName.toLowerCase()].email.push( obj.userEmail.toLowerCase() );
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
return acc;
|
|
483
|
+
}, {} );
|
|
484
|
+
|
|
485
|
+
const duplicateStores = Object.keys( duplicateStore ).filter( ( storeName ) => duplicateStore[storeName].count > 1 );
|
|
486
|
+
if ( duplicateStores.length ) {
|
|
487
|
+
return res.sendError( { validate: false, duplicates: duplicateStores, message: 'Store and email is duplicated' }, 400 );
|
|
488
|
+
}
|
|
489
|
+
} else {
|
|
490
|
+
const duplicateUser = assignDetails.reduce( ( acc, obj ) => {
|
|
491
|
+
if ( !acc[obj.userEmail.toLowerCase()] ) {
|
|
492
|
+
acc[obj.userEmail.toLowerCase()] = {
|
|
493
|
+
name: [ obj.userName.toLowerCase() ],
|
|
494
|
+
count: 1,
|
|
495
|
+
};
|
|
496
|
+
} else {
|
|
497
|
+
if ( acc[obj.userEmail.toLowerCase()] ) {
|
|
498
|
+
acc[obj.userEmail.toLowerCase()].name.push( obj.userName.toLowerCase() );
|
|
499
|
+
acc[obj.userEmail.toLowerCase()].count++;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
return acc;
|
|
503
|
+
}, {} );
|
|
504
|
+
|
|
505
|
+
const duplicateUsers = Object.keys( duplicateUser ).filter( ( storeName ) => duplicateUser[storeName].count > 1 );
|
|
506
|
+
if ( duplicateUsers.length ) {
|
|
507
|
+
return res.sendError( { validate: false, duplicates: duplicateUsers, message: 'Email is Duplicated' }, 400 );
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
if ( req.body.hasOwnProperty( 'addSingle' ) ) {
|
|
512
|
+
let checkExists = await taskAssignService.findOne( { userEmail: assignDetails[0]?.userEmail, storeName: assignDetails[0]?.storeName, checkListId: req.body.id } );
|
|
513
|
+
if ( checkExists ) {
|
|
514
|
+
return res.sendError( 'User already Exists', 400 );
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
let userEmailList = assignDetails.map( ( item ) => item.userEmail?.trim()?.toLowerCase() );
|
|
519
|
+
let storeList = assignDetails.map( ( item ) => item?.storeName?.trim()?.toLowerCase() );
|
|
520
|
+
|
|
521
|
+
let userQuery = [
|
|
522
|
+
{
|
|
523
|
+
$project: {
|
|
524
|
+
newEmail: { $toLower: '$email' },
|
|
525
|
+
isActive: 1,
|
|
526
|
+
clientId: 1,
|
|
527
|
+
userName: 1,
|
|
528
|
+
email: 1,
|
|
529
|
+
},
|
|
530
|
+
},
|
|
531
|
+
{
|
|
532
|
+
$match: {
|
|
533
|
+
newEmail: { $in: userEmailList },
|
|
534
|
+
isActive: true,
|
|
535
|
+
clientId: req.body.clientId,
|
|
536
|
+
},
|
|
537
|
+
},
|
|
538
|
+
];
|
|
539
|
+
|
|
540
|
+
let emailCheckQuery = [
|
|
541
|
+
{
|
|
542
|
+
$project: {
|
|
543
|
+
newEmail: { $toLower: '$email' },
|
|
544
|
+
isActive: 1,
|
|
545
|
+
clientId: 1,
|
|
546
|
+
},
|
|
547
|
+
},
|
|
548
|
+
{
|
|
549
|
+
$match: {
|
|
550
|
+
newEmail: { $in: userEmailList },
|
|
551
|
+
isActive: true,
|
|
552
|
+
clientId: { $ne: req.body.clientId },
|
|
553
|
+
},
|
|
554
|
+
},
|
|
555
|
+
];
|
|
556
|
+
|
|
557
|
+
let storeQuery = [
|
|
558
|
+
{
|
|
559
|
+
$project: {
|
|
560
|
+
'store': { $toLower: '$storeName' },
|
|
561
|
+
'clientId': 1,
|
|
562
|
+
'storeProfile.city': 1,
|
|
563
|
+
'status': 1,
|
|
564
|
+
'storeId': 1,
|
|
565
|
+
'spocDetails': 1,
|
|
566
|
+
'storeName': 1,
|
|
567
|
+
},
|
|
568
|
+
},
|
|
569
|
+
{
|
|
570
|
+
$match: {
|
|
571
|
+
clientId: req.body.clientId,
|
|
572
|
+
store: { $in: storeList },
|
|
573
|
+
},
|
|
574
|
+
},
|
|
575
|
+
];
|
|
576
|
+
|
|
577
|
+
let [ userDetails, storeDetails, existEmail ] = await Promise.all( [
|
|
578
|
+
await userService.aggregate( userQuery ),
|
|
579
|
+
await storeService.aggregate( storeQuery ),
|
|
580
|
+
await userService.aggregate( emailCheckQuery ),
|
|
581
|
+
] );
|
|
582
|
+
|
|
583
|
+
existEmail = existEmail.map( ( ele ) => ele.newEmail );
|
|
584
|
+
let existUserEmail = userDetails.map( ( ele ) => ele.newEmail );
|
|
585
|
+
let newUserList = userEmailList.filter( ( ele ) => !existUserEmail.includes( ele ) && !existEmail.includes( ele ) );
|
|
586
|
+
let inActiveStores = storeDetails.filter( ( ele ) => ele.status == 'deactive' ).map( ( ele ) => ele.storeName );
|
|
587
|
+
|
|
588
|
+
let existsStore = storeDetails.map( ( ele ) => ele.storeName.toLowerCase() );
|
|
589
|
+
let newStoreList = storeList.filter( ( ele ) => ele != null && !existsStore.includes( ele.toLowerCase() ) );
|
|
590
|
+
if ( req.body.coverage == 'store' ) {
|
|
591
|
+
assignDetails.forEach( ( item ) => {
|
|
592
|
+
let getStoreDetails = storeDetails.find( ( store ) => store.storeName.toLowerCase() == item.storeName.toLowerCase() );
|
|
593
|
+
if ( getStoreDetails ) {
|
|
594
|
+
let storeUserDetails = userDetails.find( ( ele ) => ele.newEmail.toLowerCase() == item.userEmail.toLowerCase() );
|
|
595
|
+
item._id = getStoreDetails._id;
|
|
596
|
+
item.storeId = getStoreDetails.storeId;
|
|
597
|
+
item.userName = getStoreDetails?.spocDetails?.[0]?.email.toLowerCase() == item.userEmail.toLowerCase() ? getStoreDetails?.spocDetails?.[0]?.name : storeUserDetails?.email == item.userEmail.toLowerCase() ? storeUserDetails?.userName : item.userName;
|
|
598
|
+
item.storeName = getStoreDetails.storeName;
|
|
599
|
+
item.userEmail = storeUserDetails?.email || item.userEmail;
|
|
600
|
+
}
|
|
601
|
+
} );
|
|
602
|
+
} else {
|
|
603
|
+
assignDetails.forEach( ( item ) => {
|
|
604
|
+
let getUserDetails = userDetails.find( ( user ) => user.newEmail.toLowerCase() == item.userEmail.toLowerCase() );
|
|
605
|
+
if ( getUserDetails ) {
|
|
606
|
+
item._id = getUserDetails._id;
|
|
607
|
+
item.userName = getUserDetails.userName;
|
|
608
|
+
item.userEmail = getUserDetails.email;
|
|
609
|
+
}
|
|
610
|
+
} );
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
if ( ( newUserList.length || newStoreList.length || existEmail.length || inActiveStores.length ) && !req.body?.addUser ) {
|
|
614
|
+
return res.sendError( { validate: false, user: newUserList, store: newStoreList, existEmail, inActiveStores, data: assignDetails }, 400 );
|
|
615
|
+
}
|
|
616
|
+
await Promise.all( newUserList.map( async ( ele ) => {
|
|
617
|
+
let findUserIndex = assignDetails.findIndex( ( item ) => item?.userEmail?.toLowerCase() == ele?.toLowerCase() );
|
|
618
|
+
if ( findUserIndex != -1 ) {
|
|
619
|
+
let data = {
|
|
620
|
+
userName: assignDetails[findUserIndex]?.userName,
|
|
621
|
+
email: ele,
|
|
622
|
+
mobileNumber: assignDetails[findUserIndex]?.mobileNumber,
|
|
623
|
+
clientId: req.body.clientId,
|
|
624
|
+
};
|
|
625
|
+
let response = await createUser( data );
|
|
626
|
+
if ( req.body.coverage == 'user' ) {
|
|
627
|
+
assignDetails[findUserIndex]._id = response._id;
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
} ) );
|
|
631
|
+
return res.sendSuccess( { validate: true, data: assignDetails } );
|
|
632
|
+
} catch ( e ) {
|
|
633
|
+
logger.error( 'validateUser 2=>', e );
|
|
634
|
+
return res.sendError( e, 500 );
|
|
635
|
+
}
|
|
636
|
+
};
|
|
637
|
+
|
|
468
638
|
async function uploadUser( req, res ) {
|
|
469
639
|
try {
|
|
470
640
|
let inputBody = req.body;
|
|
@@ -595,55 +765,83 @@ export async function userDetails( req, res ) {
|
|
|
595
765
|
if ( !req.query.taskId ) {
|
|
596
766
|
return res.sendError( { message: 'Task Id is required' }, 400 );
|
|
597
767
|
}
|
|
598
|
-
let limit = parseInt( req.query.limit ) || 5;
|
|
599
|
-
let page = parseInt( req.query.offset ) || 0;
|
|
600
|
-
let skip = limit * page;
|
|
601
|
-
|
|
602
|
-
let query = [ { $match: { checkListId: new ObjectId( req.query.taskId ) } } ];
|
|
603
|
-
if ( req.query?.search?.trim() && req.query?.search?.trim() != '' ) {
|
|
604
|
-
let searchValue = req.query.search;
|
|
605
|
-
searchValue = searchValue.split( ',' ).map( ( item ) => item.trim().toLowerCase() );
|
|
606
|
-
if ( searchValue.length > 1 ) {
|
|
607
|
-
query.push( { $addFields: { storeLower: { $toLower: '$storeName' } } } );
|
|
608
|
-
query.push( { $match: { storeLower: { $in: searchValue } } } );
|
|
609
|
-
} else {
|
|
610
|
-
query.push( { $match: { storeName: { $regex: req.query.search.trim(), $options: 'i' } } } );
|
|
611
|
-
}
|
|
612
|
-
}
|
|
768
|
+
// let limit = parseInt( req.query.limit ) || 5;
|
|
769
|
+
// let page = parseInt( req.query.offset ) || 0;
|
|
770
|
+
// let skip = limit * page;
|
|
613
771
|
|
|
614
|
-
query
|
|
615
|
-
$facet: {
|
|
616
|
-
data: [
|
|
617
|
-
{ $skip: skip }, { $limit: limit },
|
|
618
|
-
],
|
|
619
|
-
count: [
|
|
620
|
-
{ $count: 'total' },
|
|
621
|
-
],
|
|
622
|
-
},
|
|
623
|
-
} );
|
|
772
|
+
let query = [ { $match: { checkListId: new ObjectId( req.query.taskId ), isdeleted: false } }, { $sort: { _id: -1 } } ];
|
|
624
773
|
|
|
625
774
|
let taskDetails = await taskAssignService.aggregate( query );
|
|
626
|
-
|
|
627
|
-
if ( !taskDetails[0].data.length ) {
|
|
775
|
+
if ( !taskDetails.length ) {
|
|
628
776
|
return res.sendError( 'No data found', 204 );
|
|
629
777
|
}
|
|
630
778
|
|
|
631
779
|
let userDetails = [];
|
|
632
|
-
taskDetails
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
780
|
+
await Promise.all( taskDetails.map( async ( ele ) => {
|
|
781
|
+
if ( ele?.clusterName ) {
|
|
782
|
+
let clusterDetails = await clusterServices.findOneCluster( { _id: ele.assignId }, { stores: 1 } );
|
|
783
|
+
if ( clusterDetails ) {
|
|
784
|
+
let storeDetails = await storeService.find( { _id: { $in: clusterDetails.stores.map( ( item ) => item.store ) }, status: 'active' } );
|
|
785
|
+
storeDetails.forEach( ( item ) => {
|
|
786
|
+
userDetails.push( {
|
|
787
|
+
id: ele.assignId,
|
|
788
|
+
userName: item.spocDetails?.[0]?.name,
|
|
789
|
+
userEmail: item.spocDetails?.[0]?.email,
|
|
790
|
+
storeId: item?.storeId,
|
|
791
|
+
storeName: item?.storeName,
|
|
792
|
+
userPhone: item.spocDetails?.[0]?.phone,
|
|
793
|
+
city: ele?.city,
|
|
794
|
+
checkFlag: ele.checkFlag,
|
|
795
|
+
clusterName: ele?.clusterName || '',
|
|
796
|
+
teamName: ele?.teamName || '',
|
|
797
|
+
} );
|
|
798
|
+
} );
|
|
799
|
+
}
|
|
800
|
+
} else if ( ele?.teamName ) {
|
|
801
|
+
let teamDetails = await teamsServices.findOneTeams( { _id: ele.assignId }, { users: 1, teamName: 1, Teamlead: 1 } );
|
|
802
|
+
if ( teamDetails ) {
|
|
803
|
+
let idList = [ ...teamDetails.users.map( ( ele ) => ele.userId ), ...teamDetails.Teamlead.map( ( ele ) => ele.userId ) ];
|
|
804
|
+
let teamUserDetails = await userService.find( { _id: { $in: idList } } );
|
|
805
|
+
teamUserDetails.forEach( ( item ) => {
|
|
806
|
+
userDetails.push( {
|
|
807
|
+
id: ele.assignId,
|
|
808
|
+
userName: item.userName,
|
|
809
|
+
userEmail: item.email,
|
|
810
|
+
storeId: ele?.storeId,
|
|
811
|
+
storeName: ele?.storeName,
|
|
812
|
+
userPhone: item?.mobileNumber,
|
|
813
|
+
city: ele?.city,
|
|
814
|
+
checkFlag: ele.checkFlag,
|
|
815
|
+
clusterName: ele?.clusterName || '',
|
|
816
|
+
teamName: ele?.teamName || '',
|
|
817
|
+
} );
|
|
818
|
+
} );
|
|
819
|
+
}
|
|
820
|
+
} else {
|
|
821
|
+
let checkActiveUsers;
|
|
822
|
+
if ( ele?.coverage == 'user' ) {
|
|
823
|
+
checkActiveUsers = await userService.findOne( { _id: ele.userId, isActive: true } );
|
|
824
|
+
}
|
|
825
|
+
if ( checkActiveUsers || ele?.coverage == 'store' ) {
|
|
826
|
+
userDetails.push( {
|
|
827
|
+
_id: ele.assignId,
|
|
828
|
+
userName: ele.userName,
|
|
829
|
+
userEmail: ele.userEmail,
|
|
830
|
+
storeId: ele?.store_id,
|
|
831
|
+
storeName: ele?.storeName,
|
|
832
|
+
userPhone: ele.userPhone,
|
|
833
|
+
city: ele.city,
|
|
834
|
+
checkFlag: ele.checkFlag,
|
|
835
|
+
clusterName: ele?.clusterName || '',
|
|
836
|
+
teamName: ele?.teamName || '',
|
|
837
|
+
} );
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
} ) );
|
|
841
|
+
|
|
842
|
+
return res.sendSuccess( { count: userDetails.length, userDetails } );
|
|
645
843
|
} catch ( e ) {
|
|
646
|
-
logger.error(
|
|
844
|
+
logger.error( 'userDetails =>', e );
|
|
647
845
|
return res.sendError( e, 500 );
|
|
648
846
|
}
|
|
649
847
|
};
|
|
@@ -651,20 +849,22 @@ export async function userDetails( req, res ) {
|
|
|
651
849
|
export async function taskConfig( req, res ) {
|
|
652
850
|
try {
|
|
653
851
|
let inputBody = req.body;
|
|
654
|
-
let storeCount = 0;
|
|
655
|
-
let locationCount = 0;
|
|
656
852
|
let checklistDetails;
|
|
657
853
|
inputBody.client_id = inputBody.clientId;
|
|
658
854
|
if ( !inputBody._id ) {
|
|
659
855
|
return res.sendError( { message: 'Task Id is Required' }, 400 );
|
|
660
856
|
}
|
|
661
857
|
|
|
662
|
-
if ( inputBody.submitType == 'publish' ) {
|
|
663
|
-
|
|
664
|
-
if ( !checkUserAssigned ) {
|
|
665
|
-
return res.sendError( { message: 'Please assign a user' }, 400 );
|
|
666
|
-
}
|
|
858
|
+
if ( !inputBody.assignedUsers.length && inputBody.submitType == 'publish' ) {
|
|
859
|
+
return res.sendError( 'Please Assigned a user', 400 );
|
|
667
860
|
}
|
|
861
|
+
|
|
862
|
+
// if ( inputBody.submitType == 'publish' ) {
|
|
863
|
+
// let checkUserAssigned = await taskAssignService.findOne( { checkListId: inputBody._id, checkFlag: true } );
|
|
864
|
+
// if ( !checkUserAssigned ) {
|
|
865
|
+
// return res.sendError( { message: 'Please assign a user' }, 400 );
|
|
866
|
+
// }
|
|
867
|
+
// }
|
|
668
868
|
if ( !inputBody?.approver.length && inputBody.submitType == 'publish' ) {
|
|
669
869
|
return res.sendError( { message: 'Please assign approver' }, 400 );
|
|
670
870
|
}
|
|
@@ -682,7 +882,7 @@ export async function taskConfig( req, res ) {
|
|
|
682
882
|
};
|
|
683
883
|
await checklistLogs.create( logInsertData );
|
|
684
884
|
|
|
685
|
-
checklistDetails = await taskService.findOne( { _id: inputBody._id, isdeleted: false } );
|
|
885
|
+
checklistDetails = await taskService.findOne( { _id: inputBody._id, isdeleted: false, client_id: inputBody.clientId } );
|
|
686
886
|
|
|
687
887
|
if ( !checklistDetails ) {
|
|
688
888
|
return res.sendError( 'No data found', 204 );
|
|
@@ -720,39 +920,7 @@ export async function taskConfig( req, res ) {
|
|
|
720
920
|
await traxApprover.insertMany( data );
|
|
721
921
|
}
|
|
722
922
|
}
|
|
723
|
-
await taskAssignService.updateMany( { checkListId: inputBody._id }, { checkFlag: true } );
|
|
724
|
-
if ( inputBody.removedUsers.length ) {
|
|
725
|
-
await taskAssignService.updateMany( { _id: { $in: inputBody.removedUsers } }, { checkFlag: false } );
|
|
726
|
-
}
|
|
727
923
|
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
924
|
if ( inputBody.submitType == 'publish' ) {
|
|
757
925
|
let taskName = checklistDetails.checkListName;
|
|
758
926
|
let dueDate = dayjs( configDetails.scheduleEndTimeISO ).format( 'YYYY-MM-DD hh:mm A' );
|
|
@@ -845,6 +1013,7 @@ export async function insertSingleProcessData( checklistId ) {
|
|
|
845
1013
|
insertdata.remainder = getCLconfig?.remainder || [];
|
|
846
1014
|
insertdata.approver = getCLconfig?.approver || [];
|
|
847
1015
|
insertdata.restrictAttendance = getCLconfig?.restrictAttendance;
|
|
1016
|
+
insertdata.coverage = getCLconfig?.coverage;
|
|
848
1017
|
let collectSections = [];
|
|
849
1018
|
let sectionQuery = [];
|
|
850
1019
|
sectionQuery.push( {
|
|
@@ -910,7 +1079,6 @@ export async function insertSingleProcessData( checklistId ) {
|
|
|
910
1079
|
await taskProcessedConfigService.updateOne( { _id: checklistDetails._id }, insertDataPerDay );
|
|
911
1080
|
updatedchecklist = checklistDetails;
|
|
912
1081
|
}
|
|
913
|
-
|
|
914
1082
|
if ( updatedchecklist ) {
|
|
915
1083
|
await insertPCBulkV3( getCLconfig, checklistId, updatedchecklist, dateVal, startTimeIso, endTimeIso, insertdata );
|
|
916
1084
|
}
|
|
@@ -926,10 +1094,15 @@ export async function insertSingleProcessData( checklistId ) {
|
|
|
926
1094
|
|
|
927
1095
|
async function insertPCBulkV3( getCLconfig, checklistId, updatedchecklist, date, startTimeIso, endTimeIso, insertdata ) {
|
|
928
1096
|
let getquestionQuery = [];
|
|
1097
|
+
|
|
1098
|
+
if ( dayjs( getCLconfig.publishDate ).format( 'YYYY-MM-DD' ) != dayjs( date ).format( 'YYYY-MM-DD' ) ) {
|
|
1099
|
+
let todayDate = `${dayjs( date ).format( 'YYYY-MM-DD' )} 00:00 AM`;
|
|
1100
|
+
startTimeIso = dayjs.utc( todayDate, 'YYYY-MM-DD HH:mm A' );
|
|
1101
|
+
}
|
|
1102
|
+
|
|
929
1103
|
getquestionQuery.push( {
|
|
930
1104
|
$match: {
|
|
931
1105
|
checkListId: new ObjectId( checklistId ),
|
|
932
|
-
checkFlag: true,
|
|
933
1106
|
isdeleted: false,
|
|
934
1107
|
},
|
|
935
1108
|
} );
|
|
@@ -938,7 +1111,109 @@ async function insertPCBulkV3( getCLconfig, checklistId, updatedchecklist, date,
|
|
|
938
1111
|
|
|
939
1112
|
if ( allQuestion ) {
|
|
940
1113
|
let userIdList = [];
|
|
941
|
-
|
|
1114
|
+
let assignList = [];
|
|
1115
|
+
let assignUserList = [];
|
|
1116
|
+
if ( getCLconfig.coverage == 'store' ) {
|
|
1117
|
+
let clusterList = allQuestion.filter( ( ele ) => ele?.clusterName ).map( ( item ) => item.assignId );
|
|
1118
|
+
if ( clusterList.length ) {
|
|
1119
|
+
let clusterDetails = await clusterServices.findcluster( { _id: { $in: clusterList } } );
|
|
1120
|
+
if ( clusterDetails.length ) {
|
|
1121
|
+
let idList = clusterDetails.flatMap( ( item ) => item.stores.map( ( ele ) => ele.store ) );
|
|
1122
|
+
let getStoreDetails = await storeService.find( { _id: { $in: idList }, status: 'active' } );
|
|
1123
|
+
if ( getStoreDetails.length ) {
|
|
1124
|
+
getStoreDetails = getStoreDetails.filter( ( store ) => {
|
|
1125
|
+
let findStore = allQuestion.find( ( storeList ) => storeList.assignId.toString() == store._id.toString() );
|
|
1126
|
+
if ( findStore ) {
|
|
1127
|
+
return store?.spocDetails?.[0]?.email.toLowerCase() != findStore.userEmail.toLowerCase();
|
|
1128
|
+
} else {
|
|
1129
|
+
return true;
|
|
1130
|
+
}
|
|
1131
|
+
} );
|
|
1132
|
+
assignList = await Promise.all( getStoreDetails.map( async ( store ) => {
|
|
1133
|
+
let userQuery = [
|
|
1134
|
+
{
|
|
1135
|
+
$project: {
|
|
1136
|
+
newEmail: { $toLower: '$email' },
|
|
1137
|
+
isActive: 1,
|
|
1138
|
+
clientId: 1,
|
|
1139
|
+
userName: 1,
|
|
1140
|
+
email: 1,
|
|
1141
|
+
},
|
|
1142
|
+
},
|
|
1143
|
+
{
|
|
1144
|
+
$match: {
|
|
1145
|
+
newEmail: store?.spocDetails?.[0]?.email.toLowerCase(),
|
|
1146
|
+
isActive: true,
|
|
1147
|
+
clientId: store.clientId,
|
|
1148
|
+
},
|
|
1149
|
+
},
|
|
1150
|
+
];
|
|
1151
|
+
let userDetails = await userService.aggregate( userQuery );
|
|
1152
|
+
if ( !userDetails.length ) {
|
|
1153
|
+
let data = {
|
|
1154
|
+
clientId: store.clientId,
|
|
1155
|
+
userName: store.spocDetails?.[0]?.name,
|
|
1156
|
+
mobileNumber: store.spocDetails?.[0]?.phone || '',
|
|
1157
|
+
email: store.spocDetails[0].email,
|
|
1158
|
+
};
|
|
1159
|
+
userDetails = await createUser( data );
|
|
1160
|
+
userDetails = [ userDetails ];
|
|
1161
|
+
}
|
|
1162
|
+
let data = {
|
|
1163
|
+
store_id: store?.storeId,
|
|
1164
|
+
storeName: store?.storeName,
|
|
1165
|
+
userId: userDetails?.[0]?._id,
|
|
1166
|
+
userName: userDetails?.[0]?.userName,
|
|
1167
|
+
userEmail: userDetails?.[0]?.email,
|
|
1168
|
+
userPhone: userDetails?.[0]?.mobileNumber,
|
|
1169
|
+
city: store?.storeProfile?.city,
|
|
1170
|
+
country: store?.storeProfile?.country,
|
|
1171
|
+
checkFlag: true,
|
|
1172
|
+
checkListId: getCLconfig._id,
|
|
1173
|
+
checkListName: getCLconfig.checkListName,
|
|
1174
|
+
client_id: getCLconfig.client_id,
|
|
1175
|
+
};
|
|
1176
|
+
return data;
|
|
1177
|
+
} ) );
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
allQuestion = allQuestion.filter( ( ele ) => !clusterList.includes( ele.assignId ) );
|
|
1182
|
+
}
|
|
1183
|
+
if ( getCLconfig.coverage == 'user' ) {
|
|
1184
|
+
let teamsList = allQuestion.filter( ( ele ) => ele?.teamName ).map( ( item ) => item.assignId );
|
|
1185
|
+
if ( teamsList.length ) {
|
|
1186
|
+
let teamDetails = await teamsServices.findteams( { _id: { $in: teamsList } } );
|
|
1187
|
+
if ( teamDetails.length ) {
|
|
1188
|
+
let idList = teamDetails.flatMap( ( item ) => item.users.map( ( ele ) => ele.userId ) );
|
|
1189
|
+
let userIdList = allQuestion.filter( ( qn ) => !qn?.teamName ).map( ( qn ) => qn.assignId.toString() );
|
|
1190
|
+
idList = idList.filter( ( id ) => !userIdList.includes( id.toString() ) );
|
|
1191
|
+
let getUserDetails = await userService.find( { _id: { $in: idList } } );
|
|
1192
|
+
if ( getUserDetails.length ) {
|
|
1193
|
+
assignList = getUserDetails.map( ( user ) => {
|
|
1194
|
+
let data = {
|
|
1195
|
+
store_id: '',
|
|
1196
|
+
storeName: '',
|
|
1197
|
+
userId: user._id,
|
|
1198
|
+
userName: user.userName,
|
|
1199
|
+
userEmail: user.email,
|
|
1200
|
+
userPhone: user?.mobileNumber,
|
|
1201
|
+
city: '',
|
|
1202
|
+
country: '',
|
|
1203
|
+
checkFlag: true,
|
|
1204
|
+
checkListId: getCLconfig._id,
|
|
1205
|
+
checkListName: getCLconfig.checkListName,
|
|
1206
|
+
client_id: getCLconfig.client_id,
|
|
1207
|
+
};
|
|
1208
|
+
return data;
|
|
1209
|
+
} );
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
allQuestion = allQuestion.filter( ( ele ) => !teamsList.includes( ele.assignId ) );
|
|
1214
|
+
}
|
|
1215
|
+
allQuestion = [ ...allQuestion, ...assignList ];
|
|
1216
|
+
await Promise.all( allQuestion.map( async ( element4 ) => {
|
|
942
1217
|
let query;
|
|
943
1218
|
query = {
|
|
944
1219
|
date_string: dayjs( date ).format( 'YYYY-MM-DD' ),
|
|
@@ -954,43 +1229,45 @@ async function insertPCBulkV3( getCLconfig, checklistId, updatedchecklist, date,
|
|
|
954
1229
|
let getsubmitDetails = await taskProcessedService.find( query );
|
|
955
1230
|
if ( getsubmitDetails.length ) {
|
|
956
1231
|
userIdList.push( element4._id );
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
1232
|
+
} else {
|
|
1233
|
+
delete element4._id;
|
|
1234
|
+
delete element4.checkFlag;
|
|
1235
|
+
delete element4.isdeleted;
|
|
1236
|
+
delete element4.createdAt;
|
|
1237
|
+
delete element4.updatedAt;
|
|
1238
|
+
element4.checkListId = updatedchecklist._id;
|
|
1239
|
+
element4.checkListName = getCLconfig.checkListName;
|
|
1240
|
+
element4.checkListDescription = getCLconfig.checkListDescription;
|
|
1241
|
+
element4.date_iso = new Date( dayjs( date, 'YYYY-MM-DD' ).format( 'YYYY-MM-DD' ) );
|
|
1242
|
+
element4.date_string = dayjs( date ).format( 'YYYY-MM-DD' );
|
|
1243
|
+
element4.allowedOverTime = false;
|
|
1244
|
+
element4.allowedStoreLocation = getCLconfig.allowedStoreLocation;
|
|
1245
|
+
element4.scheduleStartTime = '12:00 AM';
|
|
1246
|
+
element4.scheduleStartTime_iso = startTimeIso.format();
|
|
1247
|
+
element4.scheduleEndTime = getCLconfig.scheduleEndTime;
|
|
1248
|
+
element4.scheduleEndTime_iso = endTimeIso.format();
|
|
1249
|
+
element4.createdBy = new ObjectId( getCLconfig.createdBy );
|
|
1250
|
+
element4.createdByName = getCLconfig.createdByName;
|
|
1251
|
+
element4.sourceCheckList_id = getCLconfig._id;
|
|
1252
|
+
element4.checkListType = getCLconfig.checkListType;
|
|
1253
|
+
element4.storeCount = getCLconfig.storeCount;
|
|
1254
|
+
element4.questionCount = getCLconfig.questionCount;
|
|
1255
|
+
element4.publishDate = getCLconfig.publishDate;
|
|
1256
|
+
element4.locationCount = getCLconfig.locationCount;
|
|
1257
|
+
element4.scheduleRepeatedType = 'daily';
|
|
1258
|
+
element4.approvalEnable = getCLconfig.approver.length ? true : false;
|
|
1259
|
+
element4.priorityType = getCLconfig.priorityType;
|
|
1260
|
+
element4.remainder = getCLconfig?.remainder || [];
|
|
1261
|
+
element4.restrictAttendance = getCLconfig?.restrictAttendance;
|
|
1262
|
+
element4.coverage = getCLconfig?.coverage;
|
|
1263
|
+
assignUserList.push( element4 );
|
|
1264
|
+
}
|
|
1265
|
+
} ) );
|
|
989
1266
|
if ( userIdList.length ) {
|
|
990
|
-
|
|
1267
|
+
assignUserList = assignUserList.filter( ( item ) => typeof item._id == 'undefined' );
|
|
991
1268
|
}
|
|
992
1269
|
|
|
993
|
-
if (
|
|
1270
|
+
if ( assignUserList ) {
|
|
994
1271
|
let assigndeletequery = {
|
|
995
1272
|
date_iso: date,
|
|
996
1273
|
client_id: insertdata.client_id,
|
|
@@ -1013,7 +1290,7 @@ async function insertPCBulkV3( getCLconfig, checklistId, updatedchecklist, date,
|
|
|
1013
1290
|
|
|
1014
1291
|
const taskToRetain = await taskProcessedService.find( retainTaskQuery );
|
|
1015
1292
|
|
|
1016
|
-
const insertList =
|
|
1293
|
+
const insertList = assignUserList.filter( ( item2 ) =>
|
|
1017
1294
|
!taskToRetain.some( ( item1 ) =>
|
|
1018
1295
|
item1.store_id === item2.store_id && item1.userEmail === item2.userEmail,
|
|
1019
1296
|
),
|
|
@@ -1079,6 +1356,7 @@ export async function uploadImage( req, res ) {
|
|
|
1079
1356
|
|
|
1080
1357
|
async function createUser( data ) {
|
|
1081
1358
|
try {
|
|
1359
|
+
console.log( 'createUser ====>', data );
|
|
1082
1360
|
let params = {
|
|
1083
1361
|
userName: data.userName,
|
|
1084
1362
|
email: data.email,
|
|
@@ -1161,7 +1439,8 @@ async function createUser( data ) {
|
|
|
1161
1439
|
],
|
|
1162
1440
|
};
|
|
1163
1441
|
let response = await userService.create( params );
|
|
1164
|
-
|
|
1442
|
+
// console.log( 'createUser ====>', response );
|
|
1443
|
+
return response;
|
|
1165
1444
|
} catch ( e ) {
|
|
1166
1445
|
logger.error( 'createUser =>', e );
|
|
1167
1446
|
return false;
|
|
@@ -1361,21 +1640,24 @@ export async function createChecklistTask( req, res ) {
|
|
|
1361
1640
|
client_id: inputBody.clientId,
|
|
1362
1641
|
};
|
|
1363
1642
|
await taskQuestionService.create( question );
|
|
1364
|
-
let storeDetails
|
|
1365
|
-
if (
|
|
1366
|
-
|
|
1643
|
+
let storeDetails;
|
|
1644
|
+
if ( inputBody?.storeName ) {
|
|
1645
|
+
storeDetails = await storeService.findOne( { storeName: inputBody.storeName, status: 'active' }, { storeId: 1, storeProfile: 1 } );
|
|
1367
1646
|
}
|
|
1647
|
+
|
|
1368
1648
|
let userDetails = {
|
|
1369
1649
|
userName: inputBody.userName,
|
|
1370
1650
|
userEmail: inputBody.userEmail,
|
|
1371
|
-
store_id: storeDetails.storeId,
|
|
1372
|
-
storeName: inputBody
|
|
1373
|
-
city: storeDetails?.storeProfile?.city,
|
|
1651
|
+
store_id: storeDetails?.storeId?storeDetails.storeId:'',
|
|
1652
|
+
storeName: inputBody?.storeName?inputBody?.storeName:'',
|
|
1653
|
+
city: storeDetails?.storeProfile?.city?storeDetails?.storeProfile?.city:'',
|
|
1374
1654
|
checkFlag: true,
|
|
1375
1655
|
checkListId: response?._id,
|
|
1376
1656
|
checkListName: data.checkListName,
|
|
1377
1657
|
client_id: inputBody.clientId,
|
|
1378
1658
|
userId: userId,
|
|
1659
|
+
assignId: storeDetails?._id,
|
|
1660
|
+
coverage: response?.coverage || 'store',
|
|
1379
1661
|
};
|
|
1380
1662
|
await taskAssignService.create( userDetails );
|
|
1381
1663
|
|
|
@@ -1389,8 +1671,9 @@ export async function createChecklistTask( req, res ) {
|
|
|
1389
1671
|
if ( sectionIndex == -1 ) {
|
|
1390
1672
|
return res.sendError( 'section is not found', 400 );
|
|
1391
1673
|
}
|
|
1392
|
-
let
|
|
1674
|
+
let findQuestion = question[sectionIndex].questions.findIndex( ( ele ) => ele.qno == req.body.qno );
|
|
1393
1675
|
|
|
1676
|
+
let data = { ...question[sectionIndex].questions[findQuestion], taskId: response?._id, task: true };
|
|
1394
1677
|
question[sectionIndex].questions[req.body.qno - 1] = data;
|
|
1395
1678
|
taskDetails.questionAnswers = question;
|
|
1396
1679
|
let updateData = {
|
|
@@ -1403,6 +1686,7 @@ export async function createChecklistTask( req, res ) {
|
|
|
1403
1686
|
_id: inputBody.processedChecklistId,
|
|
1404
1687
|
section_id: inputBody.sectionId,
|
|
1405
1688
|
qno: inputBody.qno,
|
|
1689
|
+
parentQuestion: inputBody.parentQuestion,
|
|
1406
1690
|
},
|
|
1407
1691
|
'upsert': {
|
|
1408
1692
|
taskId: String( response?._id ),
|
|
@@ -1462,6 +1746,7 @@ export async function approveTask( req, res ) {
|
|
|
1462
1746
|
sourceCheckList_id: req.body.sourceCheckList_id,
|
|
1463
1747
|
fromDate: req.body.fromDate,
|
|
1464
1748
|
toDate: req.body.toDate,
|
|
1749
|
+
filtertype: req.body.filtertype,
|
|
1465
1750
|
store_id: inputstores,
|
|
1466
1751
|
},
|
|
1467
1752
|
'upsert': {
|
|
@@ -1539,6 +1824,7 @@ export async function redoTask( req, res ) {
|
|
|
1539
1824
|
};
|
|
1540
1825
|
|
|
1541
1826
|
let response = await taskProcessedService.updateOne( { _id: req.body.payload._id }, updateData );
|
|
1827
|
+
console.log( response );
|
|
1542
1828
|
if ( response.modifiedCount || response.matchedCount ) {
|
|
1543
1829
|
let storeTimeZone = await storeService.findOne( { storeName: taskDetails.storeName }, { 'storeProfile.timeZone': 1 } );
|
|
1544
1830
|
let currentDateTime;
|
|
@@ -1555,8 +1841,8 @@ export async function redoTask( req, res ) {
|
|
|
1555
1841
|
sectionName: question[sectionIndex].sectionName,
|
|
1556
1842
|
questionName: question[sectionIndex].questions[req.body.payload.qno - 1].qname,
|
|
1557
1843
|
action: 'redo',
|
|
1558
|
-
store_id: taskDetails
|
|
1559
|
-
storeName: taskDetails.storeName,
|
|
1844
|
+
store_id: taskDetails?.store_id?taskDetails?.store_id:'',
|
|
1845
|
+
storeName: taskDetails.storeName?taskDetails.storeName:'',
|
|
1560
1846
|
client_id: taskDetails.client_id,
|
|
1561
1847
|
processedChecklistId: taskDetails._id,
|
|
1562
1848
|
type: taskDetails.checkListType,
|
|
@@ -1578,6 +1864,7 @@ export async function redoTask( req, res ) {
|
|
|
1578
1864
|
};
|
|
1579
1865
|
let url = JSON.parse( process.env.LAMBDAURL );
|
|
1580
1866
|
let searchResponse = await fetch( url.redoTask, requestOptions );
|
|
1867
|
+
console.log( searchResponse.ok );
|
|
1581
1868
|
if ( searchResponse.ok ) {
|
|
1582
1869
|
return res.sendSuccess( 'Question redo successfully' );
|
|
1583
1870
|
} else {
|
|
@@ -1650,15 +1937,19 @@ export async function getAnswerCount( req, res ) {
|
|
|
1650
1937
|
export const taskDropdown = async ( req, res ) => {
|
|
1651
1938
|
try {
|
|
1652
1939
|
let requestData = req.body;
|
|
1653
|
-
|
|
1940
|
+
let fromDate = new Date( requestData.fromDate );
|
|
1941
|
+
let toDate = new Date( requestData.toDate );
|
|
1942
|
+
let userTimezoneOffset = toDate.getTimezoneOffset() * 60000;
|
|
1943
|
+
toDate = new Date( toDate.getTime() - userTimezoneOffset );
|
|
1944
|
+
toDate.setUTCHours( 23, 59, 59, 59 );
|
|
1654
1945
|
let result = {};
|
|
1655
1946
|
|
|
1656
1947
|
let findQuery = [];
|
|
1657
1948
|
let findAndQuery = [];
|
|
1658
1949
|
findAndQuery.push(
|
|
1659
1950
|
{ client_id: requestData.clientId },
|
|
1660
|
-
|
|
1661
|
-
|
|
1951
|
+
{ date_iso: { $gte: fromDate } },
|
|
1952
|
+
{ date_iso: { $lte: toDate } },
|
|
1662
1953
|
{ checkListType: { $in: [ 'task', 'checklistTask', 'cctv' ] } },
|
|
1663
1954
|
);
|
|
1664
1955
|
|
|
@@ -1674,6 +1965,7 @@ export const taskDropdown = async ( req, res ) => {
|
|
|
1674
1965
|
scheduleRepeatedType: 1,
|
|
1675
1966
|
scheduleStartTime: 1,
|
|
1676
1967
|
scheduleEndTime: 1,
|
|
1968
|
+
coverage: 1,
|
|
1677
1969
|
},
|
|
1678
1970
|
} );
|
|
1679
1971
|
|
|
@@ -1688,6 +1980,7 @@ export const taskDropdown = async ( req, res ) => {
|
|
|
1688
1980
|
scheduleRepeatedType: { $last: '$scheduleRepeatedType' },
|
|
1689
1981
|
scheduleStartTime: { $last: '$scheduleStartTime' },
|
|
1690
1982
|
scheduleEndTime: { $last: '$scheduleEndTime' },
|
|
1983
|
+
coverage: { $last: '$coverage' },
|
|
1691
1984
|
},
|
|
1692
1985
|
} );
|
|
1693
1986
|
|
|
@@ -1726,6 +2019,7 @@ export const taskDropdown = async ( req, res ) => {
|
|
|
1726
2019
|
scheduleRepeatedType: 1,
|
|
1727
2020
|
scheduleStartTime: 1,
|
|
1728
2021
|
scheduleEndTime: 1,
|
|
2022
|
+
coverage: 1,
|
|
1729
2023
|
checkListDescription: '$checklistconfigs.checkListDescription',
|
|
1730
2024
|
publish: '$checklistconfigs.publish',
|
|
1731
2025
|
},
|
|
@@ -2161,13 +2455,13 @@ export async function clusterMigrations( req, res ) {
|
|
|
2161
2455
|
if ( userexits ) {
|
|
2162
2456
|
let findStore = await storeService.findOne( { storeName: user.FacilityCode } );
|
|
2163
2457
|
if ( findStore ) {
|
|
2164
|
-
let clusterExists = await
|
|
2458
|
+
let clusterExists = await clusterServices.findOneCluster( { clusterName: user.AomName } );
|
|
2165
2459
|
if ( clusterExists ) {
|
|
2166
2460
|
let data = {
|
|
2167
2461
|
storeId: findStore.storeId,
|
|
2168
2462
|
store: findStore._id,
|
|
2169
2463
|
};
|
|
2170
|
-
let updatecluster = await
|
|
2464
|
+
let updatecluster = await clusterServices.updateCluster( { clusterName: user.AomName }, { $push: { stores: data } } );
|
|
2171
2465
|
console.log( updatecluster );
|
|
2172
2466
|
} else {
|
|
2173
2467
|
let payload = {
|
|
@@ -2189,7 +2483,7 @@ export async function clusterMigrations( req, res ) {
|
|
|
2189
2483
|
],
|
|
2190
2484
|
'users': [],
|
|
2191
2485
|
};
|
|
2192
|
-
let createcluster = await
|
|
2486
|
+
let createcluster = await clusterServices.createclusterModel( payload );
|
|
2193
2487
|
console.log( createcluster );
|
|
2194
2488
|
}
|
|
2195
2489
|
}
|
|
@@ -2389,14 +2683,15 @@ export async function StoreHygienetask( req, res ) {
|
|
|
2389
2683
|
inputBody.approver = inputBody.approver.replace( /,$/, '' );
|
|
2390
2684
|
|
|
2391
2685
|
|
|
2392
|
-
|
|
2686
|
+
let title = `New Task Alert ${inputBody.taskName}-${storeDetails.storeName}-${dayjs().format( 'YYYY-MM-DD' )}`;
|
|
2393
2687
|
let time = inputBody?.scheduleEndTime || '11:59 PM';
|
|
2394
2688
|
let date = inputBody?.scheduleDate || dayjs().format( 'YYYY-MM-DD' );
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
|
|
2399
|
-
|
|
2689
|
+
let description = `A new task has been assigned to ${storeDetails.storeName}. Please complete it before the due date of ${date}.`;
|
|
2690
|
+
let userDetails = await userService.findOne( { _id: new mongoose.Types.ObjectId( finduser.userId ) } );
|
|
2691
|
+
if ( userDetails&&userDetails.fcmToken ) {
|
|
2692
|
+
const fcmToken = userDetails.fcmToken;
|
|
2693
|
+
await sendPushNotification( title, description, fcmToken );
|
|
2694
|
+
}
|
|
2400
2695
|
const inputDateTime = dayjs.utc( `${date} ${time}`, 'YYYY-MM-DD hh:mm A' );
|
|
2401
2696
|
const currentTime = dayjs.utc();
|
|
2402
2697
|
if ( inputDateTime.isBefore( currentTime ) ) {
|
|
@@ -2607,14 +2902,15 @@ export async function eyeTesttask( req, res ) {
|
|
|
2607
2902
|
inputBody.approver = inputBody.approver.replace( /,$/, '' );
|
|
2608
2903
|
|
|
2609
2904
|
|
|
2610
|
-
|
|
2905
|
+
let title = `New Task Alert ${inputBody.taskName}-${storeDetails.storeName}-${dayjs().format( 'YYYY-MM-DD' )}`;
|
|
2611
2906
|
let time = inputBody?.scheduleEndTime || '11:59 PM';
|
|
2612
2907
|
let date = inputBody?.scheduleDate || dayjs().format( 'YYYY-MM-DD' );
|
|
2613
|
-
|
|
2614
|
-
|
|
2615
|
-
|
|
2616
|
-
|
|
2617
|
-
|
|
2908
|
+
let description = `A new task has been assigned to ${storeDetails.storeName}. Please complete it before the due date of ${date}.`;
|
|
2909
|
+
let userDetails = await userService.findOne( { _id: new mongoose.Types.ObjectId( finduser.userId ) } );
|
|
2910
|
+
if ( userDetails&&userDetails.fcmToken ) {
|
|
2911
|
+
const fcmToken = userDetails.fcmToken;
|
|
2912
|
+
await sendPushNotification( title, description, fcmToken );
|
|
2913
|
+
}
|
|
2618
2914
|
const inputDateTime = dayjs.utc( `${date} ${time}`, 'YYYY-MM-DD hh:mm A' );
|
|
2619
2915
|
const currentTime = dayjs.utc();
|
|
2620
2916
|
if ( inputDateTime.isBefore( currentTime ) ) {
|
|
@@ -2812,14 +3108,14 @@ export async function createAiTask( req, res ) {
|
|
|
2812
3108
|
inputBody.userEmail = finduser.userEmail;
|
|
2813
3109
|
|
|
2814
3110
|
|
|
2815
|
-
|
|
3111
|
+
let title = `New Task Alert ${inputBody.taskName}-${storeDetails.storeName}-${dayjs().format( 'YYYY-MM-DD' )}`;
|
|
2816
3112
|
let time = inputBody?.scheduleEndTime || '11:59 PM';
|
|
2817
3113
|
let date = inputBody?.scheduleDate || dayjs().format( 'YYYY-MM-DD' );
|
|
2818
|
-
|
|
2819
|
-
|
|
2820
|
-
|
|
2821
|
-
|
|
2822
|
-
|
|
3114
|
+
let description = `A new task has been assigned to ${storeDetails.storeName}. Please complete it before the due date of ${date}.`;
|
|
3115
|
+
if ( userDetails&&userDetails.fcmToken ) {
|
|
3116
|
+
const fcmToken = userDetails.fcmToken;
|
|
3117
|
+
await sendPushNotification( title, description, fcmToken );
|
|
3118
|
+
}
|
|
2823
3119
|
const inputDateTime = dayjs.utc( `${date} ${time}`, 'YYYY-MM-DD hh:mm A' );
|
|
2824
3120
|
const currentTime = dayjs.utc();
|
|
2825
3121
|
if ( inputDateTime.isBefore( currentTime ) ) {
|
|
@@ -3257,6 +3553,43 @@ export async function uploadmultiImage( images ) {
|
|
|
3257
3553
|
}
|
|
3258
3554
|
};
|
|
3259
3555
|
|
|
3556
|
+
async function assignUsers( data ) {
|
|
3557
|
+
let assignedData;
|
|
3558
|
+
if ( data?.type == 'cluster' ) {
|
|
3559
|
+
let clusterDetails = await clusterServices.findcluster( { clientId: data.clientId, _id: new mongoose.Types.ObjectId( data.id ) } );
|
|
3560
|
+
if ( clusterDetails.length ) {
|
|
3561
|
+
let clusterList = clusterDetails[0].stores.map( ( ele ) => ele.store );
|
|
3562
|
+
let storeDetails = await storeService.find( { _id: { $in: clusterList }, status: 'active' } );
|
|
3563
|
+
assignedData = await Promise.all( storeDetails.map( async ( store ) => {
|
|
3564
|
+
let userData = {
|
|
3565
|
+
storeId: store.storeId,
|
|
3566
|
+
storeName: store.storeName,
|
|
3567
|
+
userName: store.spocDetails?.[0]?.name,
|
|
3568
|
+
userEmail: store.spocDetails?.[0]?.email,
|
|
3569
|
+
clusterName: clusterDetails?.[0]?.clusterName,
|
|
3570
|
+
id: clusterDetails?.[0]?._id,
|
|
3571
|
+
};
|
|
3572
|
+
return userData;
|
|
3573
|
+
} ) );
|
|
3574
|
+
}
|
|
3575
|
+
} else {
|
|
3576
|
+
let teamDetails = await teamsServices.findteams( { clientId: data.clientId, _id: new mongoose.Types.ObjectId( data.id ) } );
|
|
3577
|
+
if ( teamDetails.length ) {
|
|
3578
|
+
let userIdList = [ ...teamDetails[0].users.map( ( ele ) => ele.userId ), ...teamDetails[0].Teamlead.map( ( ele ) => ele.userId ) ];
|
|
3579
|
+
let userDetails = await userService.find( { _id: userIdList } );
|
|
3580
|
+
assignedData = userDetails.map( ( user ) => {
|
|
3581
|
+
let userData = {
|
|
3582
|
+
userName: user.userName,
|
|
3583
|
+
userEmail: user.email,
|
|
3584
|
+
teamName: teamDetails?.[0]?.teamName,
|
|
3585
|
+
id: teamDetails?.[0]?._id,
|
|
3586
|
+
};
|
|
3587
|
+
return userData;
|
|
3588
|
+
} );
|
|
3589
|
+
}
|
|
3590
|
+
}
|
|
3591
|
+
return assignedData;
|
|
3592
|
+
}
|
|
3260
3593
|
|
|
3261
3594
|
export async function customertrial( params ) {
|
|
3262
3595
|
try {
|
|
@@ -3282,3 +3615,545 @@ export async function customertrial( params ) {
|
|
|
3282
3615
|
}
|
|
3283
3616
|
};
|
|
3284
3617
|
|
|
3618
|
+
export async function taskAssign( req, res ) {
|
|
3619
|
+
try {
|
|
3620
|
+
if ( !req.body.taskId ) {
|
|
3621
|
+
return res.sendError( 'task id is required', 400 );
|
|
3622
|
+
}
|
|
3623
|
+
if ( !req.body.coverage ) {
|
|
3624
|
+
return res.sendError( 'Coverage is required', 400 );
|
|
3625
|
+
}
|
|
3626
|
+
if ( !req.body.idList ) {
|
|
3627
|
+
return res.sendError( 'Id is required', 400 );
|
|
3628
|
+
}
|
|
3629
|
+
|
|
3630
|
+
let taskDetails = await taskService.findOne( { _id: req.body.taskId } );
|
|
3631
|
+
if ( !taskDetails ) {
|
|
3632
|
+
return res.sendError( 'No data found', 204 );
|
|
3633
|
+
}
|
|
3634
|
+
|
|
3635
|
+
let uniqueArr = [];
|
|
3636
|
+
|
|
3637
|
+
await Promise.all( req.body.idList.map( async ( data ) => {
|
|
3638
|
+
let assignedData = {
|
|
3639
|
+
...data,
|
|
3640
|
+
clientId: req.body.clientId,
|
|
3641
|
+
};
|
|
3642
|
+
let uploadData = await assignUsers( assignedData );
|
|
3643
|
+
if ( uploadData?.length ) {
|
|
3644
|
+
uniqueArr.push( ...uploadData );
|
|
3645
|
+
}
|
|
3646
|
+
} ) );
|
|
3647
|
+
|
|
3648
|
+
if ( uniqueArr.length ) {
|
|
3649
|
+
if ( req.body.coverage == 'store' ) {
|
|
3650
|
+
uniqueArr = uniqueArr.reduce( ( acc, obj ) => {
|
|
3651
|
+
if ( acc[obj.storeName] ) {
|
|
3652
|
+
acc[obj.storeName].clusterName += ',' + obj.clusterName;
|
|
3653
|
+
} else {
|
|
3654
|
+
acc[obj.storeName] = { ...obj };
|
|
3655
|
+
}
|
|
3656
|
+
return acc;
|
|
3657
|
+
}, {} );
|
|
3658
|
+
|
|
3659
|
+
uniqueArr= Object.values( uniqueArr );
|
|
3660
|
+
} else {
|
|
3661
|
+
uniqueArr = uniqueArr.reduce( ( acc, obj ) => {
|
|
3662
|
+
if ( acc[obj.userEmail] ) {
|
|
3663
|
+
acc[obj.userEmail].teamName += ','+ obj.teamName;
|
|
3664
|
+
} else {
|
|
3665
|
+
acc[obj.userEmail] = { ...obj };
|
|
3666
|
+
}
|
|
3667
|
+
return acc;
|
|
3668
|
+
}, {} );
|
|
3669
|
+
uniqueArr = Object.values( uniqueArr );
|
|
3670
|
+
}
|
|
3671
|
+
}
|
|
3672
|
+
|
|
3673
|
+
return res.sendSuccess( { count: uniqueArr.length, uniqueArr } );
|
|
3674
|
+
} catch ( e ) {
|
|
3675
|
+
logger.error( { functionName: 'taskAssign', error: e } );
|
|
3676
|
+
return res.sendError( e, 500 );
|
|
3677
|
+
}
|
|
3678
|
+
}
|
|
3679
|
+
|
|
3680
|
+
export async function updateAssign( req, res ) {
|
|
3681
|
+
try {
|
|
3682
|
+
// if ( !req.body.assignedList.length && !req.body.assignedGroup.length ) {
|
|
3683
|
+
// return res.sendError( 'No data found', 204 );
|
|
3684
|
+
// }
|
|
3685
|
+
let taskDetails = await taskService.findOne( { _id: req.body.taskId, client_id: req.body.clientId } );
|
|
3686
|
+
if ( !taskDetails ) {
|
|
3687
|
+
return res.sendError( 'No data found', 204 );
|
|
3688
|
+
}
|
|
3689
|
+
req.body.assignedGroup = [ ...new Set( req.body.assignedGroup.map( ( item ) => item.id ) ) ];
|
|
3690
|
+
// req.body.assignedGroup = req.body.assignedGroup.map( ( ele ) => {
|
|
3691
|
+
// return { id: ele };
|
|
3692
|
+
// } );
|
|
3693
|
+
await taskAssignService.deleteMany( { checkListId: req.body.taskId } );
|
|
3694
|
+
let assignedUserList = [];
|
|
3695
|
+
let userEmailList = req.body.assignedList.map( ( ele ) => ele.userEmail.toLowerCase() );
|
|
3696
|
+
let query = [
|
|
3697
|
+
{
|
|
3698
|
+
$addFields: {
|
|
3699
|
+
emailToLower: { $toLower: '$email' },
|
|
3700
|
+
},
|
|
3701
|
+
},
|
|
3702
|
+
{
|
|
3703
|
+
$match: {
|
|
3704
|
+
clientId: req.body.clientId,
|
|
3705
|
+
emailToLower: { $in: userEmailList },
|
|
3706
|
+
},
|
|
3707
|
+
},
|
|
3708
|
+
];
|
|
3709
|
+
let assignUserDetails = await userService.aggregate( query );
|
|
3710
|
+
await Promise.all( req.body.assignedList.map( async ( assign ) => {
|
|
3711
|
+
let userDetails = assignUserDetails.find( ( ele ) => ele.email.toLowerCase() == assign.userEmail.toLowerCase() );
|
|
3712
|
+
if ( !userDetails ) {
|
|
3713
|
+
let userData = {
|
|
3714
|
+
userName: assign.userName,
|
|
3715
|
+
email: assign.userEmail,
|
|
3716
|
+
mobileNumber: assign?.phone || '',
|
|
3717
|
+
clientId: req.body.clientId,
|
|
3718
|
+
};
|
|
3719
|
+
userDetails = await createUser( userData );
|
|
3720
|
+
}
|
|
3721
|
+
let data = {
|
|
3722
|
+
...assign,
|
|
3723
|
+
store_id: assign?.storeId,
|
|
3724
|
+
client_id: req.body.clientId,
|
|
3725
|
+
checkListId: req.body.taskId,
|
|
3726
|
+
coverage: req.body.coverage,
|
|
3727
|
+
assignId: assign._id,
|
|
3728
|
+
userId: userDetails?._id,
|
|
3729
|
+
checkListName: taskDetails.checkListName,
|
|
3730
|
+
};
|
|
3731
|
+
delete data._id;
|
|
3732
|
+
assignedUserList.push( data );
|
|
3733
|
+
} ) );
|
|
3734
|
+
let assignGroupDetails = [];
|
|
3735
|
+
if ( req.body.coverage == 'store' ) {
|
|
3736
|
+
assignGroupDetails = await clusterServices.findcluster( { _id: { $in: req.body.assignedGroup } } );
|
|
3737
|
+
} else {
|
|
3738
|
+
assignGroupDetails = await teamsServices.findteams( { _id: { $in: req.body.assignedGroup } } );
|
|
3739
|
+
}
|
|
3740
|
+
await Promise.all( req.body.assignedGroup.map( async ( assign ) => {
|
|
3741
|
+
let groupDetails = assignGroupDetails.find( ( ele ) => ele._id.toString() == assign );
|
|
3742
|
+
if ( groupDetails ) {
|
|
3743
|
+
let groupData = {
|
|
3744
|
+
client_id: req.body.clientId,
|
|
3745
|
+
checkListId: req.body.taskId,
|
|
3746
|
+
coverage: req.body.coverage,
|
|
3747
|
+
assignId: assign,
|
|
3748
|
+
checkListName: taskDetails.checkListName,
|
|
3749
|
+
...( req.body.coverage == 'store' ) ? { clusterName: groupDetails?.clusterName } : { teamName: groupDetails?.teamName },
|
|
3750
|
+
};
|
|
3751
|
+
assignedUserList.push( groupData );
|
|
3752
|
+
}
|
|
3753
|
+
} ) );
|
|
3754
|
+
await taskAssignService.insertMany( assignedUserList );
|
|
3755
|
+
return res.sendSuccess( 'Assign details updated successfully' );
|
|
3756
|
+
} catch ( e ) {
|
|
3757
|
+
logger.error( { functionName: 'updateAssign', error: e } );
|
|
3758
|
+
}
|
|
3759
|
+
};
|
|
3760
|
+
|
|
3761
|
+
export async function commonAiTask( req, res ) {
|
|
3762
|
+
try {
|
|
3763
|
+
let inputBody = req.body;
|
|
3764
|
+
|
|
3765
|
+
inputBody.clientId = 11;
|
|
3766
|
+
inputBody.taskDescription = '';
|
|
3767
|
+
let userId;
|
|
3768
|
+
let storeDetails = await storeService.findOne( { storeName: inputBody.storeName, clientId: inputBody.clientId, status: 'active' }, { storeId: 1, storeName: 1, storeProfile: 1 } );
|
|
3769
|
+
if ( !storeDetails ) {
|
|
3770
|
+
return res.sendError( 'Store Not Found', 500 );
|
|
3771
|
+
}
|
|
3772
|
+
|
|
3773
|
+
let url = JSON.parse( process.env.LAMBDAURL );
|
|
3774
|
+
let checklistId = url.dailystoreChecklistId;
|
|
3775
|
+
let finduser = await checklistassignconfigModel.findOne( { checkListId: new mongoose.Types.ObjectId( checklistId ), store_id: storeDetails.storeId } );
|
|
3776
|
+
|
|
3777
|
+
if ( !finduser ) {
|
|
3778
|
+
return res.sendError( 'No user Found For this store', 500 );
|
|
3779
|
+
}
|
|
3780
|
+
|
|
3781
|
+
userId = finduser.userId;
|
|
3782
|
+
inputBody.userName = finduser.userName;
|
|
3783
|
+
inputBody.userEmail = finduser.userEmail;
|
|
3784
|
+
|
|
3785
|
+
|
|
3786
|
+
let teamList = await findteams( { users: { $elemMatch: { email: finduser.userEmail } } } );
|
|
3787
|
+
|
|
3788
|
+
inputBody.approver = '';
|
|
3789
|
+
for ( let team of teamList ) {
|
|
3790
|
+
for ( let user of team.Teamlead ) {
|
|
3791
|
+
inputBody.approver = user.email + ',' + inputBody.approver;
|
|
3792
|
+
}
|
|
3793
|
+
}
|
|
3794
|
+
inputBody.approver = inputBody.approver.replace( /,$/, '' );
|
|
3795
|
+
|
|
3796
|
+
|
|
3797
|
+
let title = `New Task Alert ${inputBody.taskName}-${storeDetails.storeName}-${dayjs().format( 'YYYY-MM-DD' )}`;
|
|
3798
|
+
let time = inputBody?.scheduleEndTime || '11:59 PM';
|
|
3799
|
+
let date = inputBody?.scheduleDate || dayjs().format( 'YYYY-MM-DD' );
|
|
3800
|
+
let description = `A new task has been assigned to ${storeDetails.storeName}. Please complete it before the due date of ${date}.`;
|
|
3801
|
+
if ( userDetails&&userDetails.fcmToken ) {
|
|
3802
|
+
const fcmToken = userDetails.fcmToken;
|
|
3803
|
+
await sendPushNotification( title, description, fcmToken );
|
|
3804
|
+
}
|
|
3805
|
+
const inputDateTime = dayjs.utc( `${date} ${time}`, 'YYYY-MM-DD hh:mm A' );
|
|
3806
|
+
const currentTime = dayjs.utc();
|
|
3807
|
+
if ( inputDateTime.isBefore( currentTime ) ) {
|
|
3808
|
+
return res.sendError( 'The input date-time is before the current time.', 500 );
|
|
3809
|
+
}
|
|
3810
|
+
|
|
3811
|
+
let approverList = inputBody?.approver.split( ',' );
|
|
3812
|
+
|
|
3813
|
+
|
|
3814
|
+
let userAdmin = await userService.find( { clientId: inputBody.clientId, email: { $in: approverList }, userType: 'client', isActive: true }, { name: '$userName', email: 1 } );
|
|
3815
|
+
|
|
3816
|
+
if ( userAdmin && userAdmin.length === 0 ) {
|
|
3817
|
+
userAdmin = await userService.find( { clientId: inputBody.clientId, email: 'rohit.chawla@lenskart.com', userType: 'client', isActive: true }, { name: '$userName', email: 1 } );
|
|
3818
|
+
}
|
|
3819
|
+
|
|
3820
|
+
let creator = await userService.find( { clientId: inputBody.clientId, email: 'rohit.chawla@lenskart.com', userType: 'client', isActive: true } );
|
|
3821
|
+
if ( creator && creator.length === 0 ) {
|
|
3822
|
+
return res.sendError( 'Invalid Creator Details', 500 );
|
|
3823
|
+
}
|
|
3824
|
+
|
|
3825
|
+
if ( req.body && req.body.referenceImage && req.body.referenceImage.length > 3 ) {
|
|
3826
|
+
return res.sendError( 'Maximum 3 referenceImage only allowed', 500 );
|
|
3827
|
+
}
|
|
3828
|
+
let data = {
|
|
3829
|
+
checkListName: `${inputBody.taskName}(${storeDetails.storeName}-${dayjs().format( 'YYYY-MM-DD' )})`,
|
|
3830
|
+
checkListDescription: inputBody.taskDescription,
|
|
3831
|
+
createdBy: creator[0]._id,
|
|
3832
|
+
createdByName: creator[0].userName,
|
|
3833
|
+
publish: true,
|
|
3834
|
+
questionCount: 1,
|
|
3835
|
+
storeCount: 1,
|
|
3836
|
+
scheduleDate: date,
|
|
3837
|
+
scheduleEndTime: time,
|
|
3838
|
+
scheduleEndTimeISO: dayjs.utc( `${date} ${time}`, 'YYYY-MM-DD hh:mm A' ).format(),
|
|
3839
|
+
priorityType: 'high',
|
|
3840
|
+
client_id: inputBody.clientId,
|
|
3841
|
+
checkListType: 'task',
|
|
3842
|
+
publishDate: new Date(),
|
|
3843
|
+
locationCount: 1,
|
|
3844
|
+
...( inputBody?.checkListId ) ? { referenceCheckListId: inputBody?.checkListId } : {},
|
|
3845
|
+
};
|
|
3846
|
+
data['approver'] = userAdmin;
|
|
3847
|
+
let answer = await findAnswer( inputBody?.answerType );
|
|
3848
|
+
if ( answer.length == 0 ) {
|
|
3849
|
+
return res.sendError( 'please enter Valid AnswerType', 500 );
|
|
3850
|
+
}
|
|
3851
|
+
if ( inputBody?.answerType === 'multiplechoicesingle' || inputBody?.answerType === 'multiplechoicemultiple' ) {
|
|
3852
|
+
if ( inputBody?.options && inputBody?.options.length > 0 ) {
|
|
3853
|
+
let optionsResult = [];
|
|
3854
|
+
let optionList = inputBody?.options.split( ',' );
|
|
3855
|
+
for ( let option of optionList ) {
|
|
3856
|
+
let optiondata = {
|
|
3857
|
+
'answer': '',
|
|
3858
|
+
'sopFlag': false,
|
|
3859
|
+
'validation': false,
|
|
3860
|
+
'validationType': '',
|
|
3861
|
+
'referenceImage': [],
|
|
3862
|
+
'runAI': false,
|
|
3863
|
+
'allowUploadfromGallery': false,
|
|
3864
|
+
'descriptivetype': '',
|
|
3865
|
+
'showLinked': false,
|
|
3866
|
+
'linkedQuestion': 0,
|
|
3867
|
+
'nestedQuestion': [],
|
|
3868
|
+
};
|
|
3869
|
+
optiondata.answer = option;
|
|
3870
|
+
optionsResult.push( optiondata );
|
|
3871
|
+
}
|
|
3872
|
+
answer = optionsResult;
|
|
3873
|
+
} else {
|
|
3874
|
+
return res.sendError( 'please enter Valid Options', 500 );
|
|
3875
|
+
}
|
|
3876
|
+
}
|
|
3877
|
+
let response = await taskService.create( data );
|
|
3878
|
+
if ( response?.approver.length ) {
|
|
3879
|
+
let inputData = [];
|
|
3880
|
+
response?.approver.forEach( ( ele ) => {
|
|
3881
|
+
inputData.push( {
|
|
3882
|
+
userEmail: ele.email,
|
|
3883
|
+
checkListId: response._id,
|
|
3884
|
+
type: 'task',
|
|
3885
|
+
client_id: inputBody.clientId,
|
|
3886
|
+
checkListName: data?.checkListName || '',
|
|
3887
|
+
} );
|
|
3888
|
+
} );
|
|
3889
|
+
await traxApprover.insertMany( inputData );
|
|
3890
|
+
}
|
|
3891
|
+
if ( response?._id ) {
|
|
3892
|
+
let question = [
|
|
3893
|
+
{
|
|
3894
|
+
'qno': 1,
|
|
3895
|
+
'qname': inputBody.question,
|
|
3896
|
+
'answerType': inputBody?.answerType || 'yes/no',
|
|
3897
|
+
'runAI': false,
|
|
3898
|
+
'runAIDescription': '',
|
|
3899
|
+
'allowUploadfromGallery': false,
|
|
3900
|
+
'linkType': false,
|
|
3901
|
+
'questionReferenceImage': [],
|
|
3902
|
+
'answers': answer,
|
|
3903
|
+
'descriptivetype': 'text',
|
|
3904
|
+
},
|
|
3905
|
+
];
|
|
3906
|
+
let images = [];
|
|
3907
|
+
for ( let imgpath of req.body.referenceImage ) {
|
|
3908
|
+
let configURL = JSON.parse( process.env.BUCKET );
|
|
3909
|
+
let inputData = {
|
|
3910
|
+
Bucket: configURL.commonAiTaskBucket,
|
|
3911
|
+
Key: imgpath,
|
|
3912
|
+
};
|
|
3913
|
+
let output = await getObject( inputData );
|
|
3914
|
+
let image = {
|
|
3915
|
+
data: output.Body,
|
|
3916
|
+
name: imgpath,
|
|
3917
|
+
mimetype: output.ContentType,
|
|
3918
|
+
};
|
|
3919
|
+
let uplaodedImage = await uploadmultiImage( image );
|
|
3920
|
+
|
|
3921
|
+
let imgUrl = decodeURIComponent( uplaodedImage.imgUrl.split( '?' )[0] );
|
|
3922
|
+
let url = imgUrl.split( '/' );
|
|
3923
|
+
if ( url.includes( 'https:' ) || url.includes( 'http:' ) ) {
|
|
3924
|
+
url.splice( 0, 3 );
|
|
3925
|
+
}
|
|
3926
|
+
images.push( url.join( '/' ) );
|
|
3927
|
+
}
|
|
3928
|
+
question[0].questionReferenceImage = images;
|
|
3929
|
+
|
|
3930
|
+
if ( inputBody?.answerType === 'image' || inputBody?.answerType === 'descriptiveImage' || inputBody?.answerType === 'multipleImage' ) {
|
|
3931
|
+
answer[0].referenceImage = question[0].questionReferenceImage;
|
|
3932
|
+
}
|
|
3933
|
+
|
|
3934
|
+
|
|
3935
|
+
question = {
|
|
3936
|
+
checkListId: response?._id,
|
|
3937
|
+
question: question,
|
|
3938
|
+
section: 'Section 1',
|
|
3939
|
+
checkList: data.checkListName,
|
|
3940
|
+
client_id: inputBody.clientId,
|
|
3941
|
+
};
|
|
3942
|
+
await taskQuestionService.create( question );
|
|
3943
|
+
|
|
3944
|
+
let userDetails = {
|
|
3945
|
+
userName: inputBody.userName,
|
|
3946
|
+
userEmail: inputBody.userEmail,
|
|
3947
|
+
store_id: storeDetails.storeId,
|
|
3948
|
+
storeName: storeDetails.storeName,
|
|
3949
|
+
city: storeDetails?.storeProfile?.city,
|
|
3950
|
+
checkFlag: true,
|
|
3951
|
+
checkListId: response?._id,
|
|
3952
|
+
checkListName: data.checkListName,
|
|
3953
|
+
client_id: inputBody.clientId,
|
|
3954
|
+
userId: userId,
|
|
3955
|
+
};
|
|
3956
|
+
await taskAssignService.create( userDetails );
|
|
3957
|
+
await insertSingleProcessData( response?._id );
|
|
3958
|
+
return res.sendSuccess( 'Task created successfully' );
|
|
3959
|
+
}
|
|
3960
|
+
} catch ( e ) {
|
|
3961
|
+
logger.error( { function: 'commonAiTask', error: e } );
|
|
3962
|
+
return res.sendError( e, 500 );
|
|
3963
|
+
}
|
|
3964
|
+
}
|
|
3965
|
+
|
|
3966
|
+
|
|
3967
|
+
export async function taskcreation( req, res ) {
|
|
3968
|
+
try {
|
|
3969
|
+
let inputBody = req.body;
|
|
3970
|
+
|
|
3971
|
+
inputBody.clientId = 458;
|
|
3972
|
+
inputBody.taskDescription = '';
|
|
3973
|
+
let userId;
|
|
3974
|
+
let storeDetails = await storeService.findOne( { storeName: inputBody.storeName, clientId: inputBody.clientId, status: 'active' }, { storeId: 1, storeName: 1, storeProfile: 1 } );
|
|
3975
|
+
if ( !storeDetails ) {
|
|
3976
|
+
return res.sendError( 'Store Not Found', 500 );
|
|
3977
|
+
}
|
|
3978
|
+
|
|
3979
|
+
|
|
3980
|
+
let finduser = await userService.findOne( { clientId: inputBody.clientId, email: req.body.user, userType: 'client', isActive: true }, { userName: '$userName', email: 1, userId: '$_id' } );
|
|
3981
|
+
console.log( finduser );
|
|
3982
|
+
|
|
3983
|
+
if ( !finduser ) {
|
|
3984
|
+
return res.sendError( 'No user Found', 500 );
|
|
3985
|
+
}
|
|
3986
|
+
|
|
3987
|
+
userId = finduser._id;
|
|
3988
|
+
inputBody.userName = finduser.userName;
|
|
3989
|
+
inputBody.userEmail = finduser.email;
|
|
3990
|
+
|
|
3991
|
+
|
|
3992
|
+
let title = `New Task Alert ${inputBody.taskName}-${storeDetails.storeName}-${dayjs().format( 'YYYY-MM-DD' )}`;
|
|
3993
|
+
let time = inputBody?.scheduleEndTime || '11:59 PM';
|
|
3994
|
+
let date = inputBody?.scheduleDate || dayjs().format( 'YYYY-MM-DD' );
|
|
3995
|
+
let description = `A new task has been assigned to ${storeDetails.storeName}. Please complete it before the due date of ${date}.`;
|
|
3996
|
+
if ( userDetails&&userDetails.fcmToken ) {
|
|
3997
|
+
const fcmToken = userDetails.fcmToken;
|
|
3998
|
+
await sendPushNotification( title, description, fcmToken );
|
|
3999
|
+
}
|
|
4000
|
+
const inputDateTime = dayjs.utc( `${date} ${time}`, 'YYYY-MM-DD hh:mm A' );
|
|
4001
|
+
const currentTime = dayjs.utc();
|
|
4002
|
+
if ( inputDateTime.isBefore( currentTime ) ) {
|
|
4003
|
+
return res.sendError( 'The input date-time is before the current time.', 500 );
|
|
4004
|
+
}
|
|
4005
|
+
|
|
4006
|
+
|
|
4007
|
+
let userAdmin = await userService.find( { clientId: inputBody.clientId, email: inputBody?.approver, userType: 'client', isActive: true }, { name: '$userName', email: 1 } );
|
|
4008
|
+
|
|
4009
|
+
|
|
4010
|
+
if ( userAdmin && userAdmin.length === 0 ) {
|
|
4011
|
+
return res.sendError( 'Invalid Creator Details', 500 );
|
|
4012
|
+
}
|
|
4013
|
+
|
|
4014
|
+
let creator = await userService.find( { clientId: inputBody.clientId, email: inputBody.creator, userType: 'client', isActive: true } );
|
|
4015
|
+
if ( creator && creator.length === 0 ) {
|
|
4016
|
+
return res.sendError( 'Invalid Creator Details', 500 );
|
|
4017
|
+
}
|
|
4018
|
+
|
|
4019
|
+
if ( req.body && req.body.referenceImage && req.body.referenceImage.length > 3 ) {
|
|
4020
|
+
return res.sendError( 'Maximum 3 referenceImage only allowed', 500 );
|
|
4021
|
+
}
|
|
4022
|
+
let data = {
|
|
4023
|
+
checkListName: `${inputBody.taskName}(${storeDetails.storeName}-${dayjs().format( 'YYYY-MM-DD' )})`,
|
|
4024
|
+
checkListDescription: inputBody.taskDescription,
|
|
4025
|
+
createdBy: creator[0]._id,
|
|
4026
|
+
createdByName: creator[0].userName,
|
|
4027
|
+
publish: true,
|
|
4028
|
+
questionCount: 1,
|
|
4029
|
+
storeCount: 1,
|
|
4030
|
+
scheduleDate: date,
|
|
4031
|
+
scheduleEndTime: time,
|
|
4032
|
+
scheduleEndTimeISO: dayjs.utc( `${date} ${time}`, 'YYYY-MM-DD hh:mm A' ).format(),
|
|
4033
|
+
priorityType: 'high',
|
|
4034
|
+
client_id: inputBody.clientId,
|
|
4035
|
+
checkListType: 'task',
|
|
4036
|
+
publishDate: new Date(),
|
|
4037
|
+
locationCount: 1,
|
|
4038
|
+
...( inputBody?.checkListId ) ? { referenceCheckListId: inputBody?.checkListId } : {},
|
|
4039
|
+
};
|
|
4040
|
+
data['approver'] = userAdmin;
|
|
4041
|
+
let answer = await findAnswer( inputBody?.answerType );
|
|
4042
|
+
if ( answer.length == 0 ) {
|
|
4043
|
+
return res.sendError( 'please enter Valid AnswerType', 500 );
|
|
4044
|
+
}
|
|
4045
|
+
if ( inputBody?.answerType === 'multiplechoicesingle' || inputBody?.answerType === 'multiplechoicemultiple' ) {
|
|
4046
|
+
if ( inputBody?.options && inputBody?.options.length > 1 ) {
|
|
4047
|
+
let optionsResult = [];
|
|
4048
|
+
for ( let option of inputBody?.options ) {
|
|
4049
|
+
let optiondata = {
|
|
4050
|
+
'answer': '',
|
|
4051
|
+
'sopFlag': false,
|
|
4052
|
+
'validation': false,
|
|
4053
|
+
'validationType': '',
|
|
4054
|
+
'referenceImage': [],
|
|
4055
|
+
'runAI': false,
|
|
4056
|
+
'allowUploadfromGallery': false,
|
|
4057
|
+
'descriptivetype': '',
|
|
4058
|
+
'showLinked': false,
|
|
4059
|
+
'linkedQuestion': 0,
|
|
4060
|
+
'nestedQuestion': [],
|
|
4061
|
+
};
|
|
4062
|
+
optiondata.answer = option;
|
|
4063
|
+
optionsResult.push( optiondata );
|
|
4064
|
+
}
|
|
4065
|
+
answer = optionsResult;
|
|
4066
|
+
} else {
|
|
4067
|
+
return res.sendError( 'please enter Valid Options', 500 );
|
|
4068
|
+
}
|
|
4069
|
+
}
|
|
4070
|
+
let response = await taskService.create( data );
|
|
4071
|
+
if ( response?.approver.length ) {
|
|
4072
|
+
let inputData = [];
|
|
4073
|
+
response?.approver.forEach( ( ele ) => {
|
|
4074
|
+
inputData.push( {
|
|
4075
|
+
userEmail: ele.email,
|
|
4076
|
+
checkListId: response._id,
|
|
4077
|
+
type: 'task',
|
|
4078
|
+
client_id: inputBody.clientId,
|
|
4079
|
+
checkListName: data?.checkListName || '',
|
|
4080
|
+
} );
|
|
4081
|
+
} );
|
|
4082
|
+
await traxApprover.insertMany( inputData );
|
|
4083
|
+
}
|
|
4084
|
+
if ( response?._id ) {
|
|
4085
|
+
let question = [
|
|
4086
|
+
{
|
|
4087
|
+
'qno': 1,
|
|
4088
|
+
'qname': inputBody.question,
|
|
4089
|
+
'answerType': inputBody?.answerType || 'yes/no',
|
|
4090
|
+
'runAI': false,
|
|
4091
|
+
'runAIDescription': '',
|
|
4092
|
+
'allowUploadfromGallery': false,
|
|
4093
|
+
'linkType': false,
|
|
4094
|
+
'questionReferenceImage': [],
|
|
4095
|
+
'answers': answer,
|
|
4096
|
+
'descriptivetype': 'text',
|
|
4097
|
+
},
|
|
4098
|
+
];
|
|
4099
|
+
let images = [];
|
|
4100
|
+
|
|
4101
|
+
// for ( let imgpath of req.body.referenceImage ) {
|
|
4102
|
+
// let configURL = JSON.parse( process.env.BUCKET );
|
|
4103
|
+
// let inputData = {
|
|
4104
|
+
// Bucket: configURL.commonAiTaskBucket,
|
|
4105
|
+
// Key: imgpath,
|
|
4106
|
+
// };
|
|
4107
|
+
// let output = await getObject( inputData );
|
|
4108
|
+
// console.log( output );
|
|
4109
|
+
|
|
4110
|
+
// let image = {
|
|
4111
|
+
// data: output.Body,
|
|
4112
|
+
// name: imgpath,
|
|
4113
|
+
// mimetype: output.ContentType,
|
|
4114
|
+
// };
|
|
4115
|
+
// let uplaodedImage = await uploadmultiImage( image );
|
|
4116
|
+
|
|
4117
|
+
// let imgUrl = decodeURIComponent( uplaodedImage.imgUrl.split( '?' )[0] );
|
|
4118
|
+
// let url = imgUrl.split( '/' );
|
|
4119
|
+
// if ( url.includes( 'https:' ) || url.includes( 'http:' ) ) {
|
|
4120
|
+
// url.splice( 0, 3 );
|
|
4121
|
+
// }
|
|
4122
|
+
// images.push( url.join( '/' ) );
|
|
4123
|
+
// }
|
|
4124
|
+
question[0].questionReferenceImage = images;
|
|
4125
|
+
|
|
4126
|
+
if ( inputBody?.answerType === 'image' || inputBody?.answerType === 'descriptiveImage' || inputBody?.answerType === 'multipleImage' ) {
|
|
4127
|
+
answer[0].referenceImage = question[0].questionReferenceImage;
|
|
4128
|
+
}
|
|
4129
|
+
|
|
4130
|
+
|
|
4131
|
+
question = {
|
|
4132
|
+
checkListId: response?._id,
|
|
4133
|
+
question: question,
|
|
4134
|
+
section: 'Section 1',
|
|
4135
|
+
checkList: data.checkListName,
|
|
4136
|
+
client_id: inputBody.clientId,
|
|
4137
|
+
};
|
|
4138
|
+
await taskQuestionService.create( question );
|
|
4139
|
+
let userDetails = {
|
|
4140
|
+
userName: inputBody.userName,
|
|
4141
|
+
userEmail: inputBody.userEmail,
|
|
4142
|
+
store_id: storeDetails.storeId,
|
|
4143
|
+
storeName: storeDetails.storeName,
|
|
4144
|
+
city: storeDetails?.storeProfile?.city,
|
|
4145
|
+
checkFlag: true,
|
|
4146
|
+
checkListId: response?._id,
|
|
4147
|
+
checkListName: data.checkListName,
|
|
4148
|
+
client_id: inputBody.clientId,
|
|
4149
|
+
userId: userId,
|
|
4150
|
+
};
|
|
4151
|
+
await taskAssignService.create( userDetails );
|
|
4152
|
+
await insertSingleProcessData( response?._id );
|
|
4153
|
+
return res.sendSuccess( 'Task created successfully' );
|
|
4154
|
+
}
|
|
4155
|
+
} catch ( e ) {
|
|
4156
|
+
logger.error( { function: 'taskcreation ventota', error: e } );
|
|
4157
|
+
return res.sendError( e, 500 );
|
|
4158
|
+
}
|
|
4159
|
+
}
|