tango-app-api-store-builder 1.0.0-beta-123 → 1.0.0-beta-124

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-store-builder",
3
- "version": "1.0.0-beta-123",
3
+ "version": "1.0.0-beta-124",
4
4
  "description": "storeBuilder",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -32,7 +32,7 @@
32
32
  "path": "^0.12.7",
33
33
  "selenium-webdriver": "^4.31.0",
34
34
  "sharp": "^0.34.1",
35
- "tango-api-schema": "2.2.155",
35
+ "tango-api-schema": "2.2.160",
36
36
  "tango-app-api-middleware": "3.1.48",
37
37
  "url": "^0.11.4",
38
38
  "winston": "^3.17.0",
@@ -474,9 +474,9 @@ export async function updateFixtureStatus( req, res ) {
474
474
  console.log( updateResponse.answers[0] );
475
475
  let findissuse= updateResponse.answers[0].issues.filter( ( data ) => data._id==req.body.issueId );
476
476
  console.log( findissuse );
477
- let findDetails = findissuse[0].Details.filter( ( det ) => det.status==='pending' );
477
+ let findDetails = findissuse[0].Details.filter( ( det ) => det.status==='agree' );
478
478
  console.log( '======', findDetails.length );
479
- if ( findDetails.length==0 ) {
479
+ if ( findissuse[0].Details.length=== findDetails.length ) {
480
480
  await planoTaskService.updateOnefilters(
481
481
  { _id: new mongoose.Types.ObjectId( req.body._id ) },
482
482
  {
@@ -8619,3 +8619,38 @@ export async function migrateCrestv1( req, res ) {
8619
8619
  // exportFixtureJsonToExcel( fixtures, 'output.xlsx' );
8620
8620
 
8621
8621
 
8622
+ // import { readFile, writeFile } from 'fs/promises';
8623
+
8624
+
8625
+ // try {
8626
+ // const raw = await readFile('response.json', 'utf-8');
8627
+ // const json = JSON.parse(raw);
8628
+
8629
+ // const resultArray = json;
8630
+ // const uniqueTypes = new Set();
8631
+
8632
+ // for (const obj of resultArray) {
8633
+ // for (const item of obj.data?.result) {
8634
+ // if (Array.isArray(item.fixtures)) {
8635
+ // for (const fixture of item.fixtures) {
8636
+ // console.log(fixture)
8637
+ // const { fixtureName } = fixture;
8638
+ // if (fixtureName) {
8639
+ // const parts = fixtureName.split(' - ');
8640
+ // if (parts.length >= 3) {
8641
+ // const type = parts.slice(2).join(' - ');
8642
+ // uniqueTypes.add(type);
8643
+ // }
8644
+ // }
8645
+ // }
8646
+ // }
8647
+ // }
8648
+
8649
+
8650
+ // }
8651
+
8652
+ // await writeFile('unique.json', JSON.stringify([...uniqueTypes], null, 2), 'utf-8');
8653
+
8654
+ // } catch (err) {
8655
+ // console.error('Error processing file:', err);
8656
+ // }
@@ -2983,6 +2983,7 @@ export async function storeFixturesTaskv2( req, res ) {
2983
2983
  let productCapacity = 0;
2984
2984
  let fixtureCount = 0;
2985
2985
  let totalVmCount = 0;
2986
+ let redoCount = 0;
2986
2987
  const layoutPolygonWithFixtures = await Promise.all(
2987
2988
  floor.layoutPolygon.map( async ( element ) => {
2988
2989
  const fixtures = await storeFixtureService.findAndSort( {
@@ -3045,6 +3046,7 @@ export async function storeFixturesTaskv2( req, res ) {
3045
3046
  if ( compliance?.status && compliance.status == 'incomplete' ) {
3046
3047
  let issueDetails = compliance?.answers?.[0]?.issues.find( ( ele ) => ele.status == 'disagree' );
3047
3048
  if ( issueDetails ) {
3049
+ redoCount ++;
3048
3050
  disabled = false;
3049
3051
  }
3050
3052
  }
@@ -3124,7 +3126,6 @@ export async function storeFixturesTaskv2( req, res ) {
3124
3126
  const vmDetails = await Promise.all( fixture.toObject()?.vmConfig?.map( async ( vm ) => {
3125
3127
  totalVmCount += 1;
3126
3128
  const vmInfo = await planoVmService.findOne( { _id: vm.vmId } );
3127
- console.log( vmInfo?.toObject() );
3128
3129
  return {
3129
3130
  ...vm,
3130
3131
  ...vmInfo?.toObject(),
@@ -3135,6 +3136,7 @@ export async function storeFixturesTaskv2( req, res ) {
3135
3136
  if ( compliance?.status && compliance.status == 'incomplete' ) {
3136
3137
  let issueDetails = compliance?.answers?.[0]?.issues.find( ( ele ) => ele.status == 'disagree' );
3137
3138
  if ( issueDetails ) {
3139
+ redoCount++;
3138
3140
  disabled = false;
3139
3141
  }
3140
3142
  }
@@ -3164,6 +3166,7 @@ export async function storeFixturesTaskv2( req, res ) {
3164
3166
  return {
3165
3167
  ...floor.toObject(),
3166
3168
  fixtureCount: fixtureCount,
3169
+ redoCount: redoCount,
3167
3170
  vmCount: totalVmCount,
3168
3171
  layoutPolygon: layoutPolygonWithFixtures,
3169
3172
  centerFixture: centerFixturesWithStatus,
@@ -3268,28 +3271,43 @@ export async function planoList( req, res ) {
3268
3271
  _id: '$planoType',
3269
3272
  dateString: { $last: '$date_string' },
3270
3273
  checklistStatus: { $last: '$checklistStatus' },
3274
+ taskId: { $last: '$_id' },
3271
3275
  },
3272
3276
  },
3273
3277
  ],
3274
3278
  as: 'planoTask',
3275
3279
  },
3276
3280
  },
3281
+ {
3282
+ $addFields: {
3283
+ taskIds: {
3284
+ $map: {
3285
+ input: '$planoTask',
3286
+ as: 'task',
3287
+ in: '$$task.taskId',
3288
+ },
3289
+ },
3290
+ },
3291
+ },
3277
3292
  {
3278
3293
  $lookup: {
3279
3294
  from: 'planotaskcompliances',
3280
- let: { plano: '$_id' },
3295
+ let: { plano: '$_id', taskId: '$taskIds' },
3281
3296
  pipeline: [
3282
3297
  {
3283
3298
  $match: {
3284
3299
  $expr: {
3285
- $eq: [ '$planoId', '$$plano' ],
3300
+ $and: [
3301
+ { $eq: [ '$planoId', '$$plano' ] },
3302
+ { $in: [ '$taskId', '$$taskId' ] },
3303
+ ],
3286
3304
  },
3287
3305
  },
3288
3306
  },
3289
3307
  { $sort: { _id: -1 } },
3290
3308
  {
3291
3309
  $group: {
3292
- _id: { planoId: '$planoId', type: '$type' },
3310
+ _id: { type: '$type', planoId: '$planoId' },
3293
3311
  layoutCount: {
3294
3312
  $sum: {
3295
3313
  $cond: {
@@ -3573,6 +3591,7 @@ export async function planoList( req, res ) {
3573
3591
  },
3574
3592
  {
3575
3593
  $project: {
3594
+ storeId: 1,
3576
3595
  storeName: 1,
3577
3596
  layoutName: 1,
3578
3597
  layoutDetails: '$layout.layoutDetails',
@@ -3640,14 +3659,6 @@ export async function planoList( req, res ) {
3640
3659
 
3641
3660
  query.push( {
3642
3661
  $facet: {
3643
- planoList: [
3644
- {
3645
- $group: {
3646
- _id: '',
3647
- planoIds: { $addToSet: '$_id' },
3648
- },
3649
- },
3650
- ],
3651
3662
  data: [
3652
3663
  { $skip: skip },
3653
3664
  { $limit: limit },
@@ -3669,243 +3680,225 @@ export async function planoList( req, res ) {
3669
3680
  let taskQuery = [
3670
3681
  {
3671
3682
  $match: {
3672
- client_id: req.body.clientId,
3673
3683
  planoId: { $in: idList },
3684
+ isPlano: true,
3685
+ date_iso: { $lte: new Date( dayjs().format( 'YYYY-MM-DD' ) ) },
3674
3686
  },
3675
3687
  },
3676
3688
  {
3677
3689
  $group: {
3678
- _id: '$planoType',
3679
- checklistStatus: { $last: '$checklistStatus' },
3690
+ _id: { store: '$storeName', type: '$planoType' },
3680
3691
  planoId: { $last: '$planoId' },
3692
+ checklistStatus: { $last: '$checklistStatus' },
3693
+ taskId: { $last: '$_id' },
3694
+ },
3695
+ },
3696
+ {
3697
+ $match: {
3698
+ checklistStatus: 'submit',
3681
3699
  },
3682
3700
  },
3683
3701
  {
3684
3702
  $project: {
3685
3703
  _id: 0,
3686
- checklistStatus: 1,
3704
+ type: '$_id.type',
3687
3705
  planoId: 1,
3706
+ checklistStatus: 1,
3707
+ taskId: 1,
3688
3708
  },
3689
3709
  },
3690
3710
  ];
3691
- let planoTaskDetails = await planotaskService.aggregate( taskQuery );
3692
- console.log( planoTaskDetails );
3693
- idList = planoTaskDetails.map( ( ele ) => new mongoose.Types.ObjectId( ele.planoId ) );
3694
- console.log( idList, 'list' );
3695
- taskQuery = [
3711
+
3712
+ let pendingDetails = await planotaskService.aggregate( taskQuery );
3713
+ query = [
3696
3714
  {
3697
3715
  $match: {
3698
- planoId: { $in: idList },
3699
- checklistStatus: 'submit',
3716
+ taskId: { $in: pendingDetails.map( ( ele ) => ele.taskId ) },
3700
3717
  },
3701
3718
  },
3702
3719
  {
3703
- $lookup: {
3704
- from: 'planotaskcompliances',
3705
- let: { plano: '$planoId', type: '$planoType' },
3706
- pipeline: [
3707
- {
3708
- $match: {
3709
- $expr: {
3720
+ $group: {
3721
+ _id: { type: '$type', planoId: '$planoId' },
3722
+ layoutPending: {
3723
+ $sum: {
3724
+ $cond: {
3725
+ if: {
3710
3726
  $and: [
3711
- { $eq: [ '$planoId', '$$plano' ] },
3712
- { $eq: [ '$type', '$$type' ] },
3713
- ],
3714
- },
3715
- },
3716
- },
3717
- {
3718
- $group: {
3719
- _id: { planoId: '$planoId', type: '$type' },
3720
- layoutPending: {
3721
- $sum: {
3722
- $cond: {
3723
- if: {
3724
- $and: [
3725
- {
3726
- $eq: [ '$type', 'layout' ],
3727
- },
3728
- {
3729
- $anyElementTrue: {
3730
- $map: {
3731
- input: {
3732
- $reduce: {
3733
- input: '$answers',
3734
- initialValue: [],
3735
- in: {
3736
- $concatArrays: [
3737
- '$$value',
3738
- {
3739
- $ifNull: [
3740
- '$$this.issues',
3741
- [],
3742
- ],
3743
- },
3744
- ],
3745
- },
3727
+ {
3728
+ $eq: [ '$type', 'layout' ],
3729
+ },
3730
+ {
3731
+ $anyElementTrue: {
3732
+ $map: {
3733
+ input: {
3734
+ $reduce: {
3735
+ input: '$answers',
3736
+ initialValue: [],
3737
+ in: {
3738
+ $concatArrays: [
3739
+ '$$value',
3740
+ {
3741
+ $ifNull: [
3742
+ '$$this.issues',
3743
+ [],
3744
+ ],
3746
3745
  },
3747
- },
3748
- as: 'issue',
3749
- in: {
3750
- $eq: [
3751
- '$$issue.status',
3752
- 'pending',
3753
- ],
3754
- },
3746
+ ],
3755
3747
  },
3756
3748
  },
3757
3749
  },
3758
- ],
3750
+ as: 'issue',
3751
+ in: {
3752
+ $eq: [
3753
+ '$$issue.status',
3754
+ 'pending',
3755
+ ],
3756
+ },
3757
+ },
3759
3758
  },
3760
- then: 1,
3761
- else: 0,
3762
3759
  },
3763
- },
3760
+ ],
3764
3761
  },
3765
- fixturePending: {
3766
- $sum: {
3767
- $cond: {
3768
- if: {
3769
- $and: [
3770
- {
3771
- $eq: [ '$type', 'fixture' ],
3772
- },
3773
- {
3774
- $anyElementTrue: {
3775
- $map: {
3776
- input: {
3777
- $reduce: {
3778
- input: '$answers',
3779
- initialValue: [],
3780
- in: {
3781
- $concatArrays: [
3782
- '$$value',
3783
- {
3784
- $ifNull: [
3785
- '$$this.issues',
3786
- [],
3787
- ],
3788
- },
3789
- ],
3790
- },
3762
+ then: 1,
3763
+ else: 0,
3764
+ },
3765
+ },
3766
+ },
3767
+ fixturePending: {
3768
+ $sum: {
3769
+ $cond: {
3770
+ if: {
3771
+ $and: [
3772
+ {
3773
+ $eq: [ '$type', 'fixture' ],
3774
+ },
3775
+ {
3776
+ $anyElementTrue: {
3777
+ $map: {
3778
+ input: {
3779
+ $reduce: {
3780
+ input: '$answers',
3781
+ initialValue: [],
3782
+ in: {
3783
+ $concatArrays: [
3784
+ '$$value',
3785
+ {
3786
+ $ifNull: [
3787
+ '$$this.issues',
3788
+ [],
3789
+ ],
3791
3790
  },
3792
- },
3793
- as: 'issue',
3794
- in: {
3795
- $eq: [
3796
- '$$issue.status',
3797
- 'pending',
3798
- ],
3799
- },
3791
+ ],
3800
3792
  },
3801
3793
  },
3802
3794
  },
3803
- ],
3795
+ as: 'issue',
3796
+ in: {
3797
+ $eq: [
3798
+ '$$issue.status',
3799
+ 'pending',
3800
+ ],
3801
+ },
3802
+ },
3804
3803
  },
3805
- then: 1,
3806
- else: 0,
3807
3804
  },
3808
- },
3805
+ ],
3809
3806
  },
3810
- vmPending: {
3811
- $sum: {
3812
- $cond: {
3813
- if: {
3814
- $and: [
3815
- {
3816
- $eq: [ '$type', 'vm' ],
3817
- },
3818
- {
3819
- $anyElementTrue: {
3820
- $map: {
3821
- input: {
3822
- $reduce: {
3823
- input: '$answers',
3824
- initialValue: [],
3825
- in: {
3826
- $concatArrays: [
3827
- '$$value',
3828
- {
3829
- $ifNull: [
3830
- '$$this.issues',
3831
- [],
3832
- ],
3833
- },
3834
- ],
3835
- },
3807
+ then: 1,
3808
+ else: 0,
3809
+ },
3810
+ },
3811
+ },
3812
+ vmPending: {
3813
+ $sum: {
3814
+ $cond: {
3815
+ if: {
3816
+ $and: [
3817
+ {
3818
+ $eq: [ '$type', 'vm' ],
3819
+ },
3820
+ {
3821
+ $anyElementTrue: {
3822
+ $map: {
3823
+ input: {
3824
+ $reduce: {
3825
+ input: '$answers',
3826
+ initialValue: [],
3827
+ in: {
3828
+ $concatArrays: [
3829
+ '$$value',
3830
+ {
3831
+ $ifNull: [
3832
+ '$$this.issues',
3833
+ [],
3834
+ ],
3836
3835
  },
3837
- },
3838
- as: 'issue',
3839
- in: {
3840
- $eq: [
3841
- '$$issue.status',
3842
- 'pending',
3843
- ],
3844
- },
3836
+ ],
3845
3837
  },
3846
3838
  },
3847
3839
  },
3848
- ],
3840
+ as: 'issue',
3841
+ in: {
3842
+ $eq: [
3843
+ '$$issue.status',
3844
+ 'pending',
3845
+ ],
3846
+ },
3847
+ },
3849
3848
  },
3850
- then: 1,
3851
- else: 0,
3852
3849
  },
3853
- },
3854
- },
3855
- },
3856
- },
3857
- {
3858
- $group: {
3859
- _id: 0,
3860
- layoutPending: {
3861
- $sum: { $cond: {
3862
- if: {
3863
- $gt: [ '$layoutPending', 0 ],
3864
- },
3865
- then: 1,
3866
- else: 0,
3867
- } },
3868
- },
3869
- fixturePending: {
3870
- $sum: { $cond: {
3871
- if: {
3872
- $gt: [ '$fixturePending', 0 ],
3873
- },
3874
- then: 1,
3875
- else: 0,
3876
- } },
3877
- },
3878
- vmPending: {
3879
- $sum: { $cond: {
3880
- if: {
3881
- $gt: [ '$vmPending', 0 ],
3882
- },
3883
- then: 1,
3884
- else: 0,
3885
- } },
3850
+ ],
3886
3851
  },
3852
+ then: 1,
3853
+ else: 0,
3887
3854
  },
3888
3855
  },
3889
- ],
3890
- as: 'taskFeedback',
3856
+ },
3891
3857
  },
3892
3858
  },
3893
3859
  {
3894
- $project: {
3860
+ $group: {
3895
3861
  _id: 0,
3896
- taskFeedback: { $ifNull: [ { $arrayElemAt: [ '$taskFeedback', 0 ] }, [] ] },
3862
+ layoutPending: {
3863
+ $sum: { $cond: {
3864
+ if: {
3865
+ $gt: [ '$layoutPending', 0 ],
3866
+ },
3867
+ then: 1,
3868
+ else: 0,
3869
+ } },
3870
+ },
3871
+ fixturePending: {
3872
+ $sum: { $cond: {
3873
+ if: {
3874
+ $gt: [ '$fixturePending', 0 ],
3875
+ },
3876
+ then: 1,
3877
+ else: 0,
3878
+ } },
3879
+ },
3880
+ vmPending: {
3881
+ $sum: { $cond: {
3882
+ if: {
3883
+ $gt: [ '$vmPending', 0 ],
3884
+ },
3885
+ then: 1,
3886
+ else: 0,
3887
+ } },
3888
+ },
3897
3889
  },
3898
3890
  },
3899
3891
  {
3900
3892
  $project: {
3901
- layoutPending: '$taskFeedback.layoutPending',
3902
- fixturePending: '$taskFeedback.fixturePending',
3903
- vmPending: '$taskFeedback.vmPending',
3893
+ _id: 0,
3894
+ layoutPending: '$layoutPending',
3895
+ fixturePending: '$fixturePending',
3896
+ vmPending: '$vmPending',
3904
3897
  },
3905
3898
  },
3906
3899
  ];
3907
3900
 
3908
- let pendingDetails = await planotaskService.aggregate( taskQuery );
3901
+ pendingDetails = await planoTaskComplianceService.aggregate( query );
3909
3902
  let result = {
3910
3903
  data: planoDetails[0].data,
3911
3904
  count: planoDetails?.[0]?.count?.[0]?.total || 0,
@@ -14,6 +14,8 @@ import mongoose from 'mongoose';
14
14
  // const ObjectId = mongoose.Types.ObjectId;
15
15
  import * as floorService from '../service/storeBuilder.service.js';
16
16
  import * as planoStaticService from '../service/planoStaticData.service.js';
17
+ import * as assignService from '../service/assignService.service.js';
18
+
17
19
 
18
20
  dayjs.extend( timeZone );
19
21
 
@@ -110,157 +112,196 @@ async function createUser( data ) {
110
112
 
111
113
  export async function createTask( req, res ) {
112
114
  try {
113
- let taskDetails = await taskService.find( { isPlano: true, client_id: req.body.clientId, ...( req.body.checkListName )? { checkListName: req.body.checkListName } : {} } );
114
- let storeList = req.body.stores.map( ( ele ) => ele.store.toLowerCase() );
115
- let userDetails;
116
- if ( !taskDetails.length ) {
117
- return res.sendError( 'No data found', 204 );
118
- }
119
- let endDate;
120
115
  let scheduleEndTime = '11:59 PM';
121
- let taskConfig = await planoStaticService.findOne( { clientId: req.body.clientId, type: 'task' } );
122
- if ( taskConfig && !req.body?.endTime ) {
123
- scheduleEndTime = taskConfig?.dueTime || '11:59 PM';
124
- req.body.days = taskConfig?.dueDay || 1;
125
- req.body.geoFencing = taskConfig?.allowedStoreLocation || false;
126
- }
127
- if ( req.body?.endTime ) {
128
- scheduleEndTime = req.body.endTime;
129
- }
130
- endDate = dayjs().add( req.body.days, 'day' ).format( 'YYYY-MM-DD' );
131
- let userEmailList = [ ...new Set( req.body.stores.map( ( ele ) => ele.email ) ) ];
132
- for ( let mail of userEmailList ) {
133
- let query = [
134
- {
135
- $addFields: {
136
- emailLower: { $toLower: '$email' },
116
+ if ( req.body?.redo ) {
117
+ if ( !req.body.taskId ) {
118
+ return res.sendError( 'Task id is required', 400 );
119
+ }
120
+ let taskInfo = await processedService.findOne( { _id: req.body.taskId } );
121
+ if ( !taskInfo ) {
122
+ return res.sendError( 'No data found', 204 );
123
+ }
124
+ await processedService.updateOne( { _id: req.body.taskId }, { checklistStatus: 'open', redoStatus: true, date_iso: new Date( dayjs().format( 'YYYY-MM-DD' ) ), scheduleStartTime_iso: dayjs.utc( '12:00 AM', 'hh:mm A' ).format(), scheduleEndTime_iso: dayjs.utc( scheduleEndTime, 'hh:mm A' ).format(), date_string: dayjs().format( 'YYYY-MM-DD' ) } );
125
+ return res.sendSuccess( 'Task redo triggered successfully' );
126
+ } else {
127
+ let taskDetails = await taskService.find( { isPlano: true, client_id: req.body.clientId, ...( req.body.checkListName )? { checkListName: req.body.checkListName } : {} } );
128
+ if ( !taskDetails.length ) {
129
+ return res.sendError( 'No data found', 204 );
130
+ }
131
+ let userDetails;
132
+ let storeList;
133
+ let endDate;
134
+ let taskConfig = await planoStaticService.findOne( { clientId: req.body.clientId, type: 'task' } );
135
+ req.body.days = req.body?.days || 7;
136
+ if ( taskConfig && !req.body?.endTime ) {
137
+ scheduleEndTime = taskConfig?.dueTime || '11:59 PM';
138
+ req.body.days = taskConfig?.dueDay || 1;
139
+ req.body.geoFencing = taskConfig?.allowedStoreLocation || false;
140
+ }
141
+ if ( req.body?.endTime ) {
142
+ scheduleEndTime = req.body.endTime;
143
+ }
144
+ endDate = dayjs().add( req.body.days, 'day' ).format( 'YYYY-MM-DD' );
145
+ endDate = `${endDate} ${scheduleEndTime}`;
146
+ if ( !req.body?.stores?.length ) {
147
+ let assignQuery = [
148
+ {
149
+ $addFields: {
150
+ store: { $toLower: '$storeName' },
151
+ },
137
152
  },
138
- },
139
- {
140
- $match: {
141
- clientId: req.body.clientId,
142
- emailLower: mail.toLowerCase(),
153
+ {
154
+ $match: {
155
+ client_id: req.body.clientId,
156
+ store: req.body.store.toLowerCase(),
157
+ },
143
158
  },
144
- },
145
- ];
146
- userDetails = await userService.aggregate( query );
147
- if ( !userDetails.length ) {
148
- let userData = {
149
- clientId: req.body.clientId,
150
- mobileNumber: '',
151
- email: mail,
152
- userName: mail.split( '@' )[0],
153
- };
154
- await createUser( userData );
159
+ ];
160
+ let getUserDetails = await assignService.aggregate( assignQuery );
161
+ if ( !getUserDetails.length ) {
162
+ return res.sendError( 'Email is required' );
163
+ }
164
+ req.body.stores = [
165
+ {
166
+ store: getUserDetails[0].storeName,
167
+ email: req.body?.email ? req.body.email : getUserDetails[0].userEmail,
168
+ },
169
+ ];
155
170
  }
156
- }
157
- endDate = `${endDate} ${scheduleEndTime}`;
158
- await Promise.all( taskDetails.map( async ( task ) => {
159
- let splitName = task?.checkListName.split( ' ' );
160
- splitName.pop();
161
- let data = {
162
- client_id: req.body.clientId,
163
- date_iso: new Date( dayjs().format( 'YYYY-MM-DD' ) ),
164
- date_string: dayjs().format( 'YYYY-MM-DD' ),
165
- sourceCheckList_id: task._id,
166
- checkListName: task.checkListName,
167
- checkListId: task._id,
168
- scheduleStartTime: '12:00 AM',
169
- scheduleEndTime: scheduleEndTime,
170
- scheduleStartTime_iso: dayjs.utc( '12:00 AM', 'hh:mm A' ).format(),
171
- scheduleEndTime_iso: dayjs.utc( endDate, 'YYYY-MM-DD hh:mm A' ).format(),
172
- allowedOverTime: false,
173
- allowedStoreLocation: req.body?.geoFencing || false,
174
- createdBy: task.createdBy,
175
- createdByName: task.createdByName,
176
- questionAnswers: [],
177
- isdeleted: false,
178
- questionCount: 0,
179
- storeCount: 0,
180
- locationCount: 0,
181
- checkListType: 'task',
182
- country: '',
183
- store_id: '',
184
- storeName: '',
185
- userId: '',
186
- userName: '',
187
- userEmail: '',
188
- checklistStatus: 'open',
189
- timeFlagStatus: true,
190
- timeFlag: 0,
191
- questionFlag: 0,
192
- mobileDetectionFlag: 0,
193
- storeOpenCloseFlag: 0,
194
- reinitiateStatus: false,
195
- markasread: false,
196
- uniformDetectionFlag: 0,
197
- scheduleRepeatedType: 'daily',
198
- approvalStatus: false,
199
- approvalEnable: false,
200
- redoStatus: false,
201
- isPlano: true,
202
- planoType: splitName.length == 1 ? splitName[0].toLowerCase() : splitName[0].toLowerCase() + splitName[2],
203
- };
204
- let query = [
205
- {
206
- $addFields: {
207
- store: { $toLower: '$storeName' },
171
+ storeList = req.body.stores.map( ( ele ) => ele.store.toLowerCase() );
172
+ let userEmailList = [ ...new Set( req.body.stores.map( ( ele ) => ele.email ) ) ];
173
+ for ( let mail of userEmailList ) {
174
+ let query = [
175
+ {
176
+ $addFields: {
177
+ emailLower: { $toLower: '$email' },
178
+ },
208
179
  },
209
- },
210
- {
211
- $match: {
180
+ {
181
+ $match: {
182
+ clientId: req.body.clientId,
183
+ emailLower: mail.toLowerCase(),
184
+ },
185
+ },
186
+ ];
187
+ userDetails = await userService.aggregate( query );
188
+ if ( !userDetails.length ) {
189
+ let userData = {
212
190
  clientId: req.body.clientId,
213
- store: { $in: storeList },
191
+ mobileNumber: '',
192
+ email: mail,
193
+ userName: mail.split( '@' )[0],
194
+ };
195
+ await createUser( userData );
196
+ }
197
+ }
198
+ await Promise.all( taskDetails.map( async ( task ) => {
199
+ let splitName = task?.checkListName.split( ' ' );
200
+ splitName.pop();
201
+ let data = {
202
+ client_id: req.body.clientId,
203
+ date_iso: new Date( dayjs().format( 'YYYY-MM-DD' ) ),
204
+ date_string: dayjs().format( 'YYYY-MM-DD' ),
205
+ sourceCheckList_id: task._id,
206
+ checkListName: task.checkListName,
207
+ checkListId: task._id,
208
+ scheduleStartTime: '12:00 AM',
209
+ scheduleEndTime: scheduleEndTime,
210
+ scheduleStartTime_iso: dayjs.utc( '12:00 AM', 'hh:mm A' ).format(),
211
+ scheduleEndTime_iso: dayjs.utc( endDate, 'YYYY-MM-DD hh:mm A' ).format(),
212
+ allowedOverTime: false,
213
+ allowedStoreLocation: req.body?.geoFencing || false,
214
+ createdBy: task.createdBy,
215
+ createdByName: task.createdByName,
216
+ questionAnswers: [],
217
+ isdeleted: false,
218
+ questionCount: 0,
219
+ storeCount: 0,
220
+ locationCount: 0,
221
+ checkListType: 'task',
222
+ country: '',
223
+ store_id: '',
224
+ storeName: '',
225
+ userId: '',
226
+ userName: '',
227
+ userEmail: '',
228
+ checklistStatus: 'open',
229
+ timeFlagStatus: true,
230
+ timeFlag: 0,
231
+ questionFlag: 0,
232
+ mobileDetectionFlag: 0,
233
+ storeOpenCloseFlag: 0,
234
+ reinitiateStatus: false,
235
+ markasread: false,
236
+ uniformDetectionFlag: 0,
237
+ scheduleRepeatedType: 'daily',
238
+ approvalStatus: false,
239
+ approvalEnable: false,
240
+ redoStatus: false,
241
+ isPlano: true,
242
+ planoType: splitName.length == 1 ? splitName[0].toLowerCase() : splitName[0].toLowerCase() + splitName[2],
243
+ };
244
+ let query = [
245
+ {
246
+ $addFields: {
247
+ store: { $toLower: '$storeName' },
248
+ },
214
249
  },
215
- },
216
- ];
217
-
218
- let storeDetails = await storeService.aggregate( query );
219
- await Promise.all( storeDetails.map( async ( store ) => {
220
- let getUserEmail = req.body.stores.find( ( ele ) => ele.store.toLowerCase() == store.storeName.toLowerCase() );
221
- let planoDetails = await planoService.findOne( { storeName: store.storeName } );
222
- if ( planoDetails ) {
223
- let floorDetails = await floorService.find( { planoId: planoDetails._id }, { _id: 1, floorName: 1 } );
224
- for ( let i=0; i<floorDetails.length; i++ ) {
225
- if ( getUserEmail ) {
226
- let query = [
227
- {
228
- $addFields: {
229
- emailLower: { $toLower: '$email' },
250
+ {
251
+ $match: {
252
+ clientId: req.body.clientId,
253
+ store: { $in: storeList },
254
+ },
255
+ },
256
+ ];
257
+
258
+ let storeDetails = await storeService.aggregate( query );
259
+ await Promise.all( storeDetails.map( async ( store ) => {
260
+ let getUserEmail = req.body.stores.find( ( ele ) => ele.store.toLowerCase() == store.storeName.toLowerCase() );
261
+ let planoDetails = await planoService.findOne( { storeName: store.storeName } );
262
+ if ( planoDetails ) {
263
+ let floorDetails = await floorService.find( { planoId: planoDetails._id }, { _id: 1, floorName: 1 } );
264
+ for ( let i=0; i<floorDetails.length; i++ ) {
265
+ if ( getUserEmail ) {
266
+ let query = [
267
+ {
268
+ $addFields: {
269
+ emailLower: { $toLower: '$email' },
270
+ },
230
271
  },
231
- },
232
- {
233
- $match: {
234
- clientId: req.body.clientId,
235
- emailLower: getUserEmail.email.toLowerCase(),
272
+ {
273
+ $match: {
274
+ clientId: req.body.clientId,
275
+ emailLower: getUserEmail.email.toLowerCase(),
276
+ },
236
277
  },
237
- },
238
- ];
239
- userDetails = await userService.aggregate( query );
240
- userDetails = userDetails[0];
241
- }
242
- let taskData = { ...data };
243
- if ( floorDetails.length > 1 ) {
244
- taskData.checkListName = taskData.checkListName +' - '+ floorDetails[i].floorName;
245
- }
246
- taskData.floorId = floorDetails[i]._id;
247
- taskData.store_id = store.storeId;
248
- taskData.storeName = store.storeName;
249
- taskData.userId = userDetails._id;
250
- taskData.userName = userDetails.userName;
251
- taskData.userEmail = userDetails.email;
252
- taskData.planoId = planoDetails?._id;
253
- for ( let i=0; i<req.body.days; i++ ) {
254
- let currDate = dayjs().add( i, 'day' );
255
- let insertData = { ...taskData, date_string: currDate.format( 'YYYY-MM-DD' ), date_iso: new Date( currDate.format( 'YYYY-MM-DD' ) ), scheduleStartTime_iso: dayjs.utc( `${currDate.format( 'YYYY-MM-DD' )} 12:00 AM`, 'YYYY-MM-DD hh:mm A' ).format() };
256
- await processedService.updateOne( { date_string: currDate.format( 'YYYY-MM-DD' ), store_id: insertData.store_id, userEmail: insertData.userEmail, planoId: insertData.planoId, sourceCheckList_id: task._id, ...( taskData?.floorId ) ? { floorId: taskData.floorId }:{} }, insertData );
278
+ ];
279
+ userDetails = await userService.aggregate( query );
280
+ userDetails = userDetails[0];
281
+ }
282
+ let taskData = { ...data };
283
+ if ( floorDetails.length > 1 ) {
284
+ taskData.checkListName = taskData.checkListName +' - '+ floorDetails[i].floorName;
285
+ }
286
+ taskData.floorId = floorDetails[i]._id;
287
+ taskData.store_id = store.storeId;
288
+ taskData.storeName = store.storeName;
289
+ taskData.userId = userDetails._id;
290
+ taskData.userName = userDetails.userName;
291
+ taskData.userEmail = userDetails.email;
292
+ taskData.planoId = planoDetails?._id;
293
+ for ( let i=0; i<req.body.days; i++ ) {
294
+ let currDate = dayjs().add( i, 'day' );
295
+ let insertData = { ...taskData, date_string: currDate.format( 'YYYY-MM-DD' ), date_iso: new Date( currDate.format( 'YYYY-MM-DD' ) ), scheduleStartTime_iso: dayjs.utc( `${currDate.format( 'YYYY-MM-DD' )} 12:00 AM`, 'YYYY-MM-DD hh:mm A' ).format() };
296
+ let response = await processedService.updateOne( { date_string: currDate.format( 'YYYY-MM-DD' ), store_id: insertData.store_id, userEmail: insertData.userEmail, planoId: insertData.planoId, sourceCheckList_id: task._id, ...( taskData?.floorId ) ? { floorId: taskData.floorId }:{} }, insertData );
297
+ console.log( response );
298
+ }
257
299
  }
258
300
  }
259
- }
301
+ } ) );
260
302
  } ) );
261
- } ) );
262
-
263
- return res.sendSuccess( 'Task created successfully' );
303
+ return res.sendSuccess( 'Task created successfully' );
304
+ }
264
305
  } catch ( e ) {
265
306
  console.log( e );
266
307
  logger.error( { functionName: 'createTask', error: e } );
@@ -0,0 +1,7 @@
1
+ import model from 'tango-api-schema';
2
+
3
+ export async function aggregate( query = [] ) {
4
+ return await model.checklistassignconfigModel.aggregate( query );
5
+ }
6
+
7
+