tango-app-api-audit 3.4.11-alpha.2 → 3.4.11-alpha.20

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.
@@ -63,7 +63,7 @@ import {
63
63
  import { aggregateStoreEmpDetection } from '../service/storeEmpDetection.service.js';
64
64
  import { aggregateBinaryAudit } from '../service/binaryAudit.service.js';
65
65
  import { aggregateTraxAuditData, findOneTraxAuditData } from '../service/traxAuditData.service.js';
66
- import { createAuditUserWallet } from '../service/auditUserWallet.service.js';
66
+ import { createAuditUserWallet, findOneAuditUserWallet } from '../service/auditUserWallet.service.js';
67
67
  import { aggregateAuditUsers } from '../service/auditUsers.service.js';
68
68
 
69
69
  /* < -- *** Configuration *** --> */
@@ -324,6 +324,7 @@ export async function getAuditFile( req, res ) {
324
324
  userId: log.userId,
325
325
  moduleType: userDetails[0].moduleType,
326
326
  zoneName: userDetails[0].zoneName,
327
+ timeSpent: log.timeSpent,
327
328
  };
328
329
  const result = {
329
330
  junk: log.junk,
@@ -378,6 +379,7 @@ export async function getAuditFile( req, res ) {
378
379
  storeName: storeDetails?.storeName,
379
380
  address: storeDetails?.address || '',
380
381
  count: log.totalCount,
382
+ timeSpent: log.timeSpent,
381
383
  file: file,
382
384
  isDraft: userDetails[0].isDraft,
383
385
  } );
@@ -527,8 +529,8 @@ export async function getAuditFile( req, res ) {
527
529
  img_name: indexes[1],
528
530
  img_id: image[0],
529
531
  selected: false,
530
- demographic: '',
531
532
  dropped: false,
533
+ demographic: '',
532
534
  count: 1,
533
535
  mappedid: [ mapimg ],
534
536
  } );
@@ -716,6 +718,7 @@ export async function getAuditImage( msg ) {
716
718
  img_name: indexes[1],
717
719
  img_id: image[0],
718
720
  selected: false,
721
+ // demographic: '',
719
722
  dropped: false,
720
723
  count: 1,
721
724
  mappedid: [ mapimg ],
@@ -1167,14 +1170,14 @@ export async function saveDraft( req, res ) {
1167
1170
  };
1168
1171
 
1169
1172
  if ( getUserAuditData?.startTime ) {
1170
- const isoDate = getUserAuditData.startTime;
1171
- const currentTime = dayjs();
1172
- const isoDateTime = dayjs( isoDate );
1173
- const timeDifferenceInMinutes = currentTime.diff( isoDateTime, 'seconds' );
1173
+ // const isoDate = getUserAuditData.startTime;
1174
+ // const currentTime = dayjs();
1175
+ // const isoDateTime = dayjs( isoDate );
1176
+ // const timeDifferenceInMinutes = currentTime.diff( isoDateTime, 'seconds' );
1174
1177
 
1175
- userRecord.timeSpent = timeDifferenceInMinutes;
1178
+ userRecord.timeSpent = inputData.timeSpent;
1176
1179
 
1177
- storeRecord.timeSpent = timeDifferenceInMinutes;
1180
+ storeRecord.timeSpent =inputData.timeSpent;
1178
1181
  }
1179
1182
 
1180
1183
  if ( inputData.userCommands ) {
@@ -1191,6 +1194,7 @@ export async function saveDraft( req, res ) {
1191
1194
  clientName: inputData.queueName,
1192
1195
  beforeCount: inputData.totalCount,
1193
1196
  afterCount: inputData.customerCount,
1197
+ timeSpent: inputData.timeSpent,
1194
1198
  auditId: inputData.auditId,
1195
1199
  moduleType: inputData.moduleType,
1196
1200
  zoneName: inputData.zoneName,
@@ -1321,10 +1325,10 @@ export async function save( req, res ) {
1321
1325
  // // moduleType: inputData.moduleType,
1322
1326
  // // zoneName: inputData.zoneName,
1323
1327
  // } );
1324
- const isoDate = req.userAudit?.startTime;
1325
- const currentTime = dayjs();
1326
- const isoDateTime = dayjs( isoDate );
1327
- const timeDifferenceInMinutes = currentTime.diff( isoDateTime, 'seconds' );
1328
+ // const isoDate = req.userAudit?.startTime;
1329
+ // const currentTime = dayjs();
1330
+ // const isoDateTime = dayjs( isoDate );
1331
+ // const timeDifferenceInMinutes = currentTime.diff( isoDateTime, 'seconds' );
1328
1332
 
1329
1333
  await updateOneUserAudit(
1330
1334
  { _id: new mongoose.Types.ObjectId( inputData.auditId ) },
@@ -1333,7 +1337,7 @@ export async function save( req, res ) {
1333
1337
  endTime: Date.now(),
1334
1338
  isDraft: false,
1335
1339
  afterCount: inputData.customerCount,
1336
- timeSpent: timeDifferenceInMinutes,
1340
+ timeSpent: inputData.timeSpent,
1337
1341
  },
1338
1342
  );
1339
1343
 
@@ -1354,7 +1358,7 @@ export async function save( req, res ) {
1354
1358
  afterCount: inputData.customerCount,
1355
1359
  startTime: req.userAudit?.startTime,
1356
1360
  endTime: Date.now(),
1357
- timeSpent: timeDifferenceInMinutes,
1361
+ timeSpent: inputData.timeSpent,
1358
1362
  auditId: new mongoose.Types.ObjectId( inputData.auditId ),
1359
1363
  },
1360
1364
  createdAt: Date.now(),
@@ -1421,7 +1425,7 @@ export async function save( req, res ) {
1421
1425
  status: 'not_assign',
1422
1426
  afterCount: inputData.customerCount,
1423
1427
  auditType: 'ReAudit',
1424
- timeSpent: timeDifferenceInMinutes,
1428
+ timeSpent: inputData.timeSpent,
1425
1429
  },
1426
1430
  );
1427
1431
 
@@ -1442,7 +1446,7 @@ export async function save( req, res ) {
1442
1446
  afterCount: inputData.customerCount,
1443
1447
  startTime: req.userAudit?.startTime,
1444
1448
  endTime: Date.now(),
1445
- timeSpent: timeDifferenceInMinutes,
1449
+ timeSpent: inputData.timeSpent,
1446
1450
  auditId: new mongoose.Types.ObjectId( inputData.auditId ),
1447
1451
  },
1448
1452
  createdAt: Date.now(),
@@ -1464,7 +1468,7 @@ export async function save( req, res ) {
1464
1468
  {
1465
1469
  status: 'completed',
1466
1470
  afterCount: inputData.customerCount,
1467
- timeSpent: timeDifferenceInMinutes,
1471
+ timeSpent: inputData.timeSpent,
1468
1472
  },
1469
1473
  );
1470
1474
  const sqsProduceQueue = {
@@ -3886,367 +3890,426 @@ export async function auditImages( req, res ) {
3886
3890
  }
3887
3891
  }
3888
3892
 
3893
+ export async function getUserCredit( req, res ) {
3894
+ try {
3895
+ const userId=req.user.email;
3896
+ const getLastDate = await findOneAuditUserWallet( {}, {}, { fileData: -1 } );
3897
+ if ( !getLastDate ) {
3898
+ return res.sendError( 'No records found', 204 );
3899
+ }
3900
+ const getWallet = await findOneAuditUserWallet( { fileDate: getLastDate.fileDate, userEmail: userId }, { totalCredit: 1, fileDate: 1 } );
3901
+ if ( !getWallet ) {
3902
+ return res.sendSuccess( { result: 0 } );
3903
+ }
3904
+ return res.sendSuccess( { result: getWallet, isAuditUser: true } );
3905
+ } catch ( error ) {
3906
+ const err = error.message || 'Internal Server Error';
3907
+ logger.info( { error: error, function: 'getUserCredit' } );
3908
+ return res.sendError( err, 500 );
3909
+ }
3910
+ }
3911
+
3889
3912
  export async function getUserAuditCount( req, res ) {
3890
3913
  try {
3891
- const userWallet = JSON.parse( process.env.USER_WALLET );
3892
- const previousDate = dayjs( new Date() ).subtract( 1, 'days' );
3893
- const fileDate = dayjs( previousDate ).format( 'DD-MM-YYYY' );
3894
- const configTimeSpentSec = userWallet.minTimeSpentSec; // 18000;
3895
- // const beforeCountX = userWallet.beforeCountCreditPerc; // 0.0335;
3896
- // const afterCountY = userWallet.afterCountCreditPerc; // 0.0035;
3897
- const reduce = userWallet.reductionPerc; // 0.05;
3898
- const auditUserQuery=[
3899
- {
3900
- $match: {
3901
- $and: [
3902
- { type: { $eq: 'consultant' } },
3903
- { isActive: { $eq: true } },
3904
- ],
3914
+ for ( let i =4; i<15; i++ ) {
3915
+ const userWallet = JSON.parse( process.env.USER_WALLET );
3916
+ const previousDate = dayjs( new Date() ).subtract( i, 'days' );
3917
+ const fileDate = dayjs( previousDate ).format( 'DD-MM-YYYY' );
3918
+ const configTimeSpentSec = userWallet.minTimeSpentSec; // 18000;
3919
+ // const beforeCountX = userWallet.beforeCountCreditPerc; // 0.0335;
3920
+ // const afterCountY = userWallet.afterCountCreditPerc; // 0.0035;
3921
+ const reduce = userWallet.reductionPerc; // 0.05;
3922
+ const today = new Date();
3923
+
3924
+ // Subtract one day to get the previous day
3925
+ const previousDay = new Date( today );
3926
+ previousDay.setDate( today.getDate() - i );
3927
+ const lateLogin = previousDay;
3928
+ logger.info( { lateLogin1: lateLogin } );
3929
+ lateLogin.setHours( 21, 30, 0, 0 );
3930
+ logger.info( { lateLogin: lateLogin } );
3931
+ const auditUserQuery=[
3932
+ {
3933
+ $match: {
3934
+ $and: [
3935
+ { type: { $eq: 'consultant' } },
3936
+ { isActive: { $eq: true } },
3937
+ ],
3938
+ },
3905
3939
  },
3906
- },
3907
- {
3908
- $group: {
3909
- _id: null,
3910
- userList: { $push: '$userId' },
3940
+ {
3941
+ $group: {
3942
+ _id: null,
3943
+ userList: { $push: '$userId' },
3944
+ },
3911
3945
  },
3912
- },
3913
- {
3914
- $project: {
3915
- _id: 0,
3916
- userList: 1,
3946
+ {
3947
+ $project: {
3948
+ _id: 0,
3949
+ userList: 1,
3950
+ },
3917
3951
  },
3918
- },
3919
- ];
3920
- const auditUserList = await aggregateAuditUsers( auditUserQuery );
3921
- if ( auditUserList.length==0 ) {
3922
- return res.sendError( 'No users available', 204 );
3923
- }
3924
- const userCountQuery= [
3925
- {
3926
- $match: {
3927
- $and: [
3928
- { fileDate: { $eq: fileDate } },
3929
- { auditStatus: { $eq: 'completed' } },
3930
- { userId: { $in: auditUserList[0].userList } },
3931
- ],
3952
+ ];
3953
+ const auditUserList = await aggregateAuditUsers( auditUserQuery );
3954
+ if ( auditUserList.length==0 ) {
3955
+ return res.sendError( 'No users available', 204 );
3956
+ }
3957
+ const userCountQuery= [
3958
+ {
3959
+ $match: {
3960
+ $and: [
3961
+ { fileDate: { $eq: fileDate } },
3962
+ { auditStatus: { $eq: 'completed' } },
3963
+ { userId: { $in: auditUserList[0].userList } },
3964
+ ],
3965
+ },
3932
3966
  },
3933
- },
3934
- // {
3935
- // $project: {
3936
-
3937
- // trafficUserCount: { $cond: [
3938
- // { $eq: [ '$moduleType', 'traffic' ] }, '$userId', '$$REMOVE',
3939
- // ] },
3940
- // zoneUserCount: { $cond: [
3941
- // { $eq: [ '$moduleType', 'zone' ] }, '$userId', '$$REMOVE',
3942
- // ] },
3943
-
3944
- // trafficFilesCount: { $cond: [
3945
- // { $eq: [ '$moduleType', 'traffic' ] }, '$beforeCount', '$$REMOVE',
3946
- // ] },
3947
- // zoneFilesCount: { $cond: [
3948
- // { $eq: [ '$moduleType', 'zone' ] }, '$beforeCount', '$$REMOVE',
3949
- // ] },
3950
-
3951
- // },
3952
- // },
3953
- {
3954
- $group: {
3955
- _id: null,
3956
- // trafficUserCount: { $addToSet: '$trafficUserCount' },
3957
- // zoneUserCount: { $addToSet: '$zoneUserCount' },
3958
- // trafficFilesCount: { $sum: '$trafficFilesCount' },
3959
- // zoneFilesCount: { $sum: '$zoneFilesCount' },
3960
- userCount: { $addToSet: '$userId' },
3961
- filesCount: { $sum: '$beforeCount' },
3967
+ // {
3968
+ // $project: {
3969
+
3970
+ // trafficUserCount: { $cond: [
3971
+ // { $eq: [ '$moduleType', 'traffic' ] }, '$userId', '$$REMOVE',
3972
+ // ] },
3973
+ // zoneUserCount: { $cond: [
3974
+ // { $eq: [ '$moduleType', 'zone' ] }, '$userId', '$$REMOVE',
3975
+ // ] },
3976
+
3977
+ // trafficFilesCount: { $cond: [
3978
+ // { $eq: [ '$moduleType', 'traffic' ] }, '$beforeCount', '$$REMOVE',
3979
+ // ] },
3980
+ // zoneFilesCount: { $cond: [
3981
+ // { $eq: [ '$moduleType', 'zone' ] }, '$beforeCount', '$$REMOVE',
3982
+ // ] },
3983
+
3984
+ // },
3985
+ // },
3986
+ {
3987
+ $group: {
3988
+ _id: null,
3989
+ // trafficUserCount: { $addToSet: '$trafficUserCount' },
3990
+ // zoneUserCount: { $addToSet: '$zoneUserCount' },
3991
+ // trafficFilesCount: { $sum: '$trafficFilesCount' },
3992
+ // zoneFilesCount: { $sum: '$zoneFilesCount' },
3993
+ userCount: { $addToSet: '$userId' },
3994
+ filesCount: { $sum: '$beforeCount' },
3995
+ },
3962
3996
  },
3963
- },
3964
- ];
3997
+ ];
3965
3998
 
3966
- // const storeDataQuery= [
3967
- // {
3968
- // $match: {
3969
- // fileDate: { $eq: fileDate },
3970
- // },
3971
- // },
3972
- // {
3973
- // $project: {
3999
+ // const storeDataQuery= [
4000
+ // {
4001
+ // $match: {
4002
+ // fileDate: { $eq: fileDate },
4003
+ // },
4004
+ // },
4005
+ // {
4006
+ // $project: {
3974
4007
 
3975
- // trafficFilesCount: { $cond: [
3976
- // { $eq: [ '$moduleType', 'traffic' ] }, '$totalFilesCount', '$$REMOVE',
3977
- // ] },
3978
- // zoneFilesCount: { $cond: [
3979
- // { $eq: [ '$moduleType', 'zone' ] }, '$totalFilesCount', '$$REMOVE',
3980
- // ] },
4008
+ // trafficFilesCount: { $cond: [
4009
+ // { $eq: [ '$moduleType', 'traffic' ] }, '$totalFilesCount', '$$REMOVE',
4010
+ // ] },
4011
+ // zoneFilesCount: { $cond: [
4012
+ // { $eq: [ '$moduleType', 'zone' ] }, '$totalFilesCount', '$$REMOVE',
4013
+ // ] },
3981
4014
 
3982
- // },
3983
- // },
3984
- // {
3985
- // $group: {
3986
- // _id: '$moduleType',
3987
- // trafficFilesCount: { $sum: '$trafficFilesCount' },
3988
- // zoneFilesCount: { $sum: '$zoneFilesCount' },
3989
- // },
3990
- // },
3991
- // {
3992
- // $project: {
3993
- // _id: 0,
3994
- // trafficFilesCount: { $ifNull: [ '$trafficFilesCount', 0 ] },
3995
- // zoneFilesCount: { $ifNull: [ '$zoneFilesCount', 0 ] },
3996
- // },
3997
- // },
3998
- // ];
3999
- const userCount = await aggregateUserAuditCount( userCountQuery );
4015
+ // },
4016
+ // },
4017
+ // {
4018
+ // $group: {
4019
+ // _id: '$moduleType',
4020
+ // trafficFilesCount: { $sum: '$trafficFilesCount' },
4021
+ // zoneFilesCount: { $sum: '$zoneFilesCount' },
4022
+ // },
4023
+ // },
4024
+ // {
4025
+ // $project: {
4026
+ // _id: 0,
4027
+ // trafficFilesCount: { $ifNull: [ '$trafficFilesCount', 0 ] },
4028
+ // zoneFilesCount: { $ifNull: [ '$zoneFilesCount', 0 ] },
4029
+ // },
4030
+ // },
4031
+ // ];
4032
+ const userCount = await aggregateUserAuditCount( userCountQuery );
4000
4033
 
4001
- // const totalStoreData = await aggregateAuditStoreData( storeDataQuery );
4002
- if ( userCount?.length == 0 ) {
4003
- return res.sendError( 'No Data Found', 204 );
4004
- }
4005
- const minimumTarget =userCount[0]?.userCount?.length !== 0 && userCount[0]?.filesCount !==0 ? Math.floor( userCount[0].filesCount/userCount[0].userCount.length ) : 0;
4006
- // const trafficMinimumTarget =userCount[0]?.trafficUserCount?.length !== 0 && userCount[0]?.trafficFilesCount !==0 ? Math.floor( userCount[0].trafficFilesCount/userCount[0].trafficUserCount.length ) : 0;
4007
- const beforeCreditCal = 544/minimumTarget;
4008
- // const beforeCountZone = 544/zoneMinimumTarget; // 0.0035;
4009
- const afterCountCredit = 136;
4034
+ // const totalStoreData = await aggregateAuditStoreData( storeDataQuery );
4035
+ if ( userCount?.length == 0 ) {
4036
+ return res.sendError( 'No Data Found', 204 );
4037
+ }
4038
+ const minimumTarget =userCount[0]?.userCount?.length !== 0 && userCount[0]?.filesCount !==0 ? Math.floor( userCount[0].filesCount/userCount[0].userCount.length ) : 0;
4039
+ // const trafficMinimumTarget =userCount[0]?.trafficUserCount?.length !== 0 && userCount[0]?.trafficFilesCount !==0 ? Math.floor( userCount[0].trafficFilesCount/userCount[0].trafficUserCount.length ) : 0;
4040
+ const beforeCreditCal = 544/minimumTarget;
4041
+ // const beforeCountZone = 544/zoneMinimumTarget; // 0.0035;
4042
+ const afterCountCredit = 136;
4010
4043
 
4011
- const query = [
4012
- {
4013
- $match: {
4014
- $and: [
4015
- { fileDate: { $eq: fileDate } },
4016
- {
4017
- auditStatus: { $eq: 'completed' },
4018
- },
4019
- { userId: { $in: auditUserList[0].userList } },
4020
- ],
4044
+ const query = [
4045
+ {
4046
+ $match: {
4047
+ $and: [
4048
+ { fileDate: { $eq: fileDate } },
4049
+ {
4050
+ auditStatus: { $eq: 'completed' },
4051
+ },
4052
+ { userId: { $in: auditUserList[0].userList } },
4053
+ ],
4054
+ },
4021
4055
  },
4022
- },
4023
- {
4024
- $set: { minimumTarget: minimumTarget, afterCountCredit: afterCountCredit, beforeCreditCal: beforeCreditCal },
4025
- },
4026
- {
4027
- $project: {
4028
- userId: 1,
4029
- moduleType: 1,
4030
- beforeCount: 1,
4031
- afterCount: 1,
4032
- timeSpent: 1,
4033
- fileDate: 1,
4034
- fileDateISO: 1,
4035
- minimumTarget: 1,
4036
- afterCountCredit: 1,
4037
- totalEarn: { $ifNull: [ 0, 0 ] },
4038
- singleImageAmount: { $ifNull: [ '$beforeCreditCal', 0 ] },
4039
- totalReducedAmount: { $ifNull: [ 0, 0 ] },
4040
- totalCredit: { $ifNull: [ 0, 0 ] },
4041
- deductionBytimeSpent: { $ifNull: [ 0, 0 ] },
4042
- deductionByMinimumTarget: { $ifNull: [ 0, 0 ] },
4056
+ {
4057
+ $set: { minimumTarget: minimumTarget, afterCountCredit: afterCountCredit, beforeCreditCal: beforeCreditCal },
4043
4058
  },
4044
- },
4045
- {
4046
- $group: {
4047
- _id: { user: '$userId' },
4048
- totalCount: { $sum: 1 },
4049
- moduleType: { $first: '$moduleType' },
4050
- totalBeforeCount: { $sum: '$beforeCount' },
4051
- totalAfterCount: { $sum: '$afterCount' },
4052
- totalTimeSpent: { $sum: '$timeSpent' },
4053
- fileDate: { $first: '$fileDate' },
4054
- fileDateISO: { $first: '$fileDateISO' },
4055
- userId: { $first: '$userId' },
4056
- minimumTarget: { $first: '$minimumTarget' },
4057
- afterCountCredit: { $first: '$afterCountCredit' },
4058
- totalEarn: { $first: '$totalEarn' },
4059
- totalReducedAmount: { $first: '$totalReducedAmount' },
4060
- totalCredit: { $first: '$totalCredit' },
4061
- deductionByMinimumTarget: { $first: '$deductionByMinimumTarget' },
4062
- deductionBytimeSpent: { $first: '$deductionBytimeSpent' },
4063
- singleImageAmount: { $first: '$singleImageAmount' },
4059
+ {
4060
+ $project: {
4061
+ userId: 1,
4062
+ moduleType: 1,
4063
+ beforeCount: 1,
4064
+ afterCount: 1,
4065
+ timeSpent: 1,
4066
+ fileDate: 1,
4067
+ fileDateISO: 1,
4068
+ minimumTarget: 1,
4069
+ afterCountCredit: 1,
4070
+ totalEarn: { $ifNull: [ 0, 0 ] },
4071
+ singleImageAmount: { $ifNull: [ '$beforeCreditCal', 0 ] },
4072
+ totalReducedAmount: { $ifNull: [ 0, 0 ] },
4073
+ totalCredit: { $ifNull: [ 0, 0 ] },
4074
+ deductionBytimeSpent: { $ifNull: [ 0, 0 ] },
4075
+ deductionByMinimumTarget: { $ifNull: [ 0, 0 ] },
4076
+ deductionByLateLogin: { $ifNull: [ 0, 0 ] },
4077
+ startTime: 1,
4078
+ },
4064
4079
  },
4065
- },
4066
- {
4067
- $project: {
4068
- totalCount: 1,
4069
- userId: 1,
4070
- moduleType: 1,
4071
- totalBeforeCount: 1,
4072
- totalAfterCount: 1,
4073
- totalTimeSpent: 1,
4074
- fileDate: 1,
4075
- fileDateISO: 1,
4076
- minimumTarget: 1,
4077
- singleImageAmount: 1,
4078
- beforeCountCredit: { $multiply: [ '$totalBeforeCount', '$singleImageAmount' ] },
4079
- // afterCountCredit: { $cond: [ { $and: [
4080
- // { $gte: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.4 ] }, { $lte: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] },
4081
- // ] }, 136,
4082
- // {
4083
- // $cond: [ { $gt: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] },
4084
- // { $subtract: [ 136, { $multiply: [ { $subtract: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] }, 136 ] } ] },
4085
- // { $subtract: [ 136, { $multiply: [ { $subtract: [ 0.4, { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] } ] }, 136 ] } ] },
4086
- // ],
4087
- // },
4088
- // ] },
4089
- afterCountCredit: afterCountCredit,
4090
- totalEarn: { $add: [ { $multiply: [ '$totalBeforeCount', '$singleImageAmount' ] }, 136 ] },
4091
- deductionBytimeSpent: 1,
4092
- deductionByMinimumTarget: 1,
4093
- mappingperc: { $multiply: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 100 ] },
4094
- deductionByMapping: { $cond: [ { $and: [
4095
- { $gte: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.4 ] }, { $lte: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] },
4096
- ] }, 0,
4097
- {
4098
- $cond: [ { $gt: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] },
4099
- { $multiply: [ { $subtract: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] }, 136 ] },
4100
- { $multiply: [ { $subtract: [ 0.4, { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] } ] }, 136 ] },
4101
- ],
4080
+ {
4081
+ $sort: { startTime: 1 },
4082
+ },
4083
+ {
4084
+ $group: {
4085
+ _id: { user: '$userId' },
4086
+ totalCount: { $sum: 1 },
4087
+ moduleType: { $first: '$moduleType' },
4088
+ totalBeforeCount: { $sum: '$beforeCount' },
4089
+ totalAfterCount: { $sum: '$afterCount' },
4090
+ totalTimeSpent: { $sum: '$timeSpent' },
4091
+ fileDate: { $first: '$fileDate' },
4092
+ fileDateISO: { $first: '$fileDateISO' },
4093
+ userId: { $first: '$userId' },
4094
+ minimumTarget: { $first: '$minimumTarget' },
4095
+ afterCountCredit: { $first: '$afterCountCredit' },
4096
+ totalEarn: { $first: '$totalEarn' },
4097
+ totalReducedAmount: { $first: '$totalReducedAmount' },
4098
+ totalCredit: { $first: '$totalCredit' },
4099
+ deductionByMinimumTarget: { $first: '$deductionByMinimumTarget' },
4100
+ deductionBytimeSpent: { $first: '$deductionBytimeSpent' },
4101
+ singleImageAmount: { $first: '$singleImageAmount' },
4102
+ startTime: { $first: '$startTime' },
4103
+ deductionByLateLogin: { $first: '$deductionByLateLogin' },
4102
4104
  },
4103
- ] },
4104
- totalReducedAmount: 1,
4105
- totalCredit: 1,
4106
-
4107
4105
  },
4108
- },
4109
- {
4110
- $project: {
4111
- totalCount: 1,
4112
- userId: 1,
4113
- moduleType: 1,
4114
- totalBeforeCount: 1,
4115
- totalAfterCount: 1,
4116
- totalTimeSpent: 1,
4117
- hours: { $floor: { $divide: [ '$totalTimeSpent', 3600 ] } },
4118
- minutes: {
4119
- $floor: {
4120
- $divide: [
4121
- { $mod: [ '$totalTimeSpent', 3600 ] }, // Get remaining minutes after hours
4122
- 60,
4123
- ] },
4106
+ {
4107
+ $project: {
4108
+ totalCount: 1,
4109
+ userId: 1,
4110
+ moduleType: 1,
4111
+ totalBeforeCount: 1,
4112
+ totalAfterCount: 1,
4113
+ totalTimeSpent: 1,
4114
+ fileDate: 1,
4115
+ fileDateISO: 1,
4116
+ minimumTarget: 1,
4117
+ singleImageAmount: 1,
4118
+ beforeCountCredit: { $multiply: [ '$totalBeforeCount', '$singleImageAmount' ] },
4119
+ // afterCountCredit: { $cond: [ { $and: [
4120
+ // { $gte: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.4 ] }, { $lte: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] },
4121
+ // ] }, 136,
4122
+ // {
4123
+ // $cond: [ { $gt: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] },
4124
+ // { $subtract: [ 136, { $multiply: [ { $subtract: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] }, 136 ] } ] },
4125
+ // { $subtract: [ 136, { $multiply: [ { $subtract: [ 0.4, { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] } ] }, 136 ] } ] },
4126
+ // ],
4127
+ // },
4128
+ // ] },
4129
+ afterCountCredit: afterCountCredit,
4130
+ totalEarn: { $add: [ { $multiply: [ '$totalBeforeCount', '$singleImageAmount' ] }, 136 ] },
4131
+ deductionBytimeSpent: 1,
4132
+ deductionByMinimumTarget: 1,
4133
+ mappingperc: { $multiply: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 100 ] },
4134
+ deductionByMapping: { $cond: [ { $and: [
4135
+ { $gte: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.4 ] }, { $lte: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] },
4136
+ ] }, 0,
4137
+ {
4138
+ $cond: [ { $gt: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] },
4139
+ { $multiply: [ { $subtract: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] }, 136 ] },
4140
+ { $multiply: [ { $subtract: [ 0.4, { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] } ] }, 136 ] },
4141
+ ],
4142
+ },
4143
+ ] },
4144
+ deductionByLateLogin: 1,
4145
+ totalReducedAmount: 1,
4146
+ totalCredit: 1,
4147
+ startTime: 1,
4148
+
4124
4149
  },
4125
- fileDate: 1,
4126
- fileDateISO: 1,
4127
- minimumTarget: 1,
4128
- singleImageAmount: 1,
4129
- beforeCountCredit: 1,
4130
- afterCountCredit: 1,
4131
- totalEarn: 1,
4132
- deductionBytimeSpent: { $cond: [
4133
- { $lt: [ '$totalTimeSpent', configTimeSpentSec ] },
4134
- { $multiply: [ reduce, '$totalEarn' ] }, '$deductionBytimeSpent',
4135
- ] },
4150
+ },
4151
+ {
4152
+ $project: {
4153
+ totalCount: 1,
4154
+ userId: 1,
4155
+ moduleType: 1,
4156
+ totalBeforeCount: 1,
4157
+ totalAfterCount: 1,
4158
+ totalTimeSpent: 1,
4159
+ hours: { $floor: { $divide: [ '$totalTimeSpent', 3600 ] } },
4160
+ minutes: {
4161
+ $floor: {
4162
+ $divide: [
4163
+ { $mod: [ '$totalTimeSpent', 3600 ] }, // Get remaining minutes after hours
4164
+ 60,
4165
+ ] },
4166
+ },
4167
+ fileDate: 1,
4168
+ fileDateISO: 1,
4169
+ minimumTarget: 1,
4170
+ singleImageAmount: 1,
4171
+ beforeCountCredit: 1,
4172
+ afterCountCredit: 1,
4173
+ totalEarn: 1,
4174
+ deductionBytimeSpent: { $cond: [
4175
+ { $lt: [ '$totalTimeSpent', configTimeSpentSec ] },
4176
+ { $multiply: [ reduce, '$totalEarn' ] }, '$deductionBytimeSpent',
4177
+ ] },
4136
4178
 
4137
- deductionByMinimumTarget:
4179
+ deductionByMinimumTarget:
4138
4180
  { $cond: [
4139
4181
  { $lt: [ '$totalBeforeCount', '$minimumTarget' ] },
4140
4182
  { $multiply: [ reduce, '$totalEarn' ] }, '$deductionByMinimumTarget',
4141
4183
  ] },
4142
- mappingperc: 1,
4143
- deductionByMapping: 1,
4144
- totalCredit: { $subtract: [ '$totalEarn', '$totalReducedAmount' ] },
4184
+ deductionByLateLogin: {
4185
+ $cond: [
4186
+ { $gt: [ '$startTime', lateLogin ] },
4187
+ { $multiply: [ reduce, '$totalEarn' ] }, '$deductionByLateLogin',
4188
+ ],
4189
+ },
4190
+ mappingperc: 1,
4191
+ deductionByMapping: 1,
4192
+ totalCredit: { $subtract: [ '$totalEarn', '$totalReducedAmount' ] },
4193
+ startTime: 1,
4145
4194
 
4146
4195
 
4196
+ },
4147
4197
  },
4148
- },
4149
4198
 
4150
- {
4151
- $project: {
4152
- totalCount: 1,
4153
- userId: 1,
4154
- moduleType: 1,
4155
- totalBeforeCount: 1,
4156
- totalAfterCount: 1,
4157
- totalTimeSpent: {
4158
- $cond: [ { $eq: [ '$hours', 0 ] },
4159
- { $concat: [
4160
- '00:', { $toString: '$minutes' }, ':00',
4161
- ] },
4162
- { $cond: [ { $eq: [ '$minutes', 0 ] },
4199
+ {
4200
+ $project: {
4201
+ totalCount: 1,
4202
+ userId: 1,
4203
+ moduleType: 1,
4204
+ totalBeforeCount: 1,
4205
+ totalAfterCount: 1,
4206
+ totalTimeSpent: {
4207
+ $cond: [ { $eq: [ '$hours', 0 ] },
4163
4208
  { $concat: [
4164
- { $toString: '$hours' }, ':00:00',
4209
+ '00:', { $toString: '$minutes' }, ':00',
4165
4210
  ] },
4166
- {
4167
- $concat: [
4168
- { $toString: '$hours' }, ':',
4169
- { $toString: '$minutes' }, ':00',
4170
- ],
4171
- },
4172
- ] },
4173
- ],
4211
+ { $cond: [ { $eq: [ '$minutes', 0 ] },
4212
+ { $concat: [
4213
+ { $toString: '$hours' }, ':00:00',
4214
+ ] },
4215
+ {
4216
+ $concat: [
4217
+ { $toString: '$hours' }, ':',
4218
+ { $toString: '$minutes' }, ':00',
4219
+ ],
4220
+ },
4221
+ ] },
4222
+ ],
4174
4223
 
4175
- },
4176
- fileDate: 1,
4177
- fileDateISO: 1,
4178
- minimumTarget: 1,
4179
- beforeCountCredit: 1,
4180
- afterCountCredit: 1,
4181
- totalEarn: 1,
4182
- deductionByMinimumTarget: 1,
4183
- singleImageAmount: 1,
4184
- deductionBytimeSpent: 1,
4185
- mappingperc: 1,
4186
- deductionByMapping: 1,
4187
- totalCredit: { $subtract: [ '$totalEarn', '$totalReducedAmount' ] },
4188
- totalReducedAmount: { $round: [ { $add: [ '$deductionBytimeSpent', '$deductionByMinimumTarget', '$deductionByMapping' ] }, 2 ] },
4224
+ },
4225
+ fileDate: 1,
4226
+ fileDateISO: 1,
4227
+ minimumTarget: 1,
4228
+ beforeCountCredit: 1,
4229
+ afterCountCredit: 1,
4230
+ totalEarn: 1,
4231
+ deductionByMinimumTarget: 1,
4232
+ singleImageAmount: 1,
4233
+ deductionBytimeSpent: 1,
4234
+ mappingperc: 1,
4235
+ deductionByMapping: 1,
4236
+ totalCredit: { $subtract: [ '$totalEarn', '$totalReducedAmount' ] },
4237
+ totalReducedAmount: { $round: [ { $add: [ '$deductionBytimeSpent', '$deductionByMinimumTarget', '$deductionByMapping', '$deductionByLateLogin' ] }, 2 ] },
4238
+ deductionByLateLogin: 1,
4239
+ startTime: {
4240
+ $dateToString: {
4241
+ format: '%H:%M:%S',
4242
+ date: '$startTime',
4243
+ timezone: 'Asia/Kolkata',
4244
+ },
4189
4245
 
4246
+ },
4247
+
4248
+ },
4190
4249
  },
4191
- },
4192
- {
4193
- $lookup: {
4194
- from: 'users',
4195
- let: { userId: '$userId' }, // Assuming userId is the linking field
4196
- pipeline: [
4197
- {
4198
- $match: {
4199
- $expr: { $eq: [ '$_id', '$$userId' ] },
4250
+ {
4251
+ $lookup: {
4252
+ from: 'users',
4253
+ let: { userId: '$userId' }, // Assuming userId is the linking field
4254
+ pipeline: [
4255
+ {
4256
+ $match: {
4257
+ $expr: { $eq: [ '$_id', '$$userId' ] },
4258
+ },
4200
4259
  },
4201
- },
4202
- {
4203
- $project: {
4204
- _id: 0, // Exclude _id if not needed
4205
- email: 1,
4206
- userName: 1,
4260
+ {
4261
+ $project: {
4262
+ _id: 0, // Exclude _id if not needed
4263
+ email: 1,
4264
+ userName: 1,
4265
+ },
4207
4266
  },
4208
- },
4209
- ],
4210
- as: 'users',
4267
+ ],
4268
+ as: 'users',
4269
+ },
4211
4270
  },
4212
- },
4213
- { $unwind: { path: '$users', preserveNullAndEmptyArrays: true } },
4214
- {
4215
- $project: {
4216
- _id: 0,
4217
- totalFilesCount: { $round: [ '$totalCount', 2 ] },
4218
- moduleType: 1,
4219
- totalAfterCount: { $round: [ '$totalAfterCount', 2 ] },
4220
- totalBeforeCount: { $round: [ '$totalBeforeCount', 2 ] },
4221
- timeSpent: '$totalTimeSpent',
4222
- fileDate: 1,
4223
- fileDateISO: 1,
4224
- beforeCountCredit: 1,
4225
- afterCountCredit: 1,
4226
- totalEarn: { $round: [ '$totalEarn', 2 ] },
4227
- minimumTarget: 1,
4228
- singleImageAmount: 1,
4229
- deductionByMinimumTarget: { $round: [ '$deductionByMinimumTarget', 2 ] },
4230
- deductionBytimeSpent: { $round: [ '$deductionBytimeSpent', 2 ] },
4231
- mappingperc: 1,
4232
- deductionByMapping: 1,
4233
- totalReducedAmount: 1,
4234
- totalCredit: { $round: [ { $subtract: [ '$totalEarn', '$totalReducedAmount' ] }, 2 ] },
4235
- userEmail: '$users.email', // Access first element from array
4236
- userName: '$users.userName', // Access first element from array,
4271
+ { $unwind: { path: '$users', preserveNullAndEmptyArrays: true } },
4272
+ {
4273
+ $project: {
4274
+ _id: 0,
4275
+ totalFilesCount: { $round: [ '$totalCount', 2 ] },
4276
+ moduleType: 1,
4277
+ totalAfterCount: { $round: [ '$totalAfterCount', 2 ] },
4278
+ totalBeforeCount: { $round: [ '$totalBeforeCount', 2 ] },
4279
+ timeSpent: '$totalTimeSpent',
4280
+ fileDate: 1,
4281
+ fileDateISO: 1,
4282
+ beforeCountCredit: 1,
4283
+ afterCountCredit: 1,
4284
+ totalEarn: { $round: [ '$totalEarn', 2 ] },
4285
+ minimumTarget: 1,
4286
+ singleImageAmount: 1,
4287
+ deductionByMinimumTarget: { $round: [ '$deductionByMinimumTarget', 2 ] },
4288
+ deductionBytimeSpent: { $round: [ '$deductionBytimeSpent', 2 ] },
4289
+ mappingperc: 1,
4290
+ deductionByMapping: 1,
4291
+ totalReducedAmount: 1,
4292
+ totalCredit: { $round: [ { $subtract: [ '$totalEarn', '$totalReducedAmount' ] }, 2 ] },
4293
+ userEmail: '$users.email', // Access first element from array
4294
+ userName: '$users.userName', // Access first element from array,
4295
+ startTime: 1,
4296
+ deductionByLateLogin: 1,
4237
4297
 
4298
+ },
4238
4299
  },
4239
- },
4240
- ];
4241
- const consolidatedData = await aggregateUserAuditCount( query );
4242
- if ( consolidatedData.length == 0 ) {
4243
- return res.sendError( 'No Data found', 204 );
4244
- }
4245
- const result = await createAuditUserWallet( consolidatedData );
4246
- if ( result.length == 0 ) {
4247
- return res.sendError( 'No Record Inserted', 500 );
4300
+ ];
4301
+ const consolidatedData = await aggregateUserAuditCount( query );
4302
+ if ( consolidatedData.length == 0 ) {
4303
+ return res.sendError( 'No Data found', 204 );
4304
+ }
4305
+ const result = await createAuditUserWallet( consolidatedData );
4306
+ if ( result.length == 0 ) {
4307
+ return res.sendError( 'No Record Inserted', 500 );
4308
+ }
4309
+ if ( i == 14 ) {
4310
+ return res.sendSuccess( { message: 'Inserted successfully!' } );
4311
+ }
4248
4312
  }
4249
- return res.sendSuccess( { message: 'Inserted successfully!' } );
4250
4313
  } catch ( error ) {
4251
4314
  const err = error.message || 'Internal Server Error';
4252
4315
  logger.info( { error: error, message: req.body, function: 'getUserAuditCount' } );
@@ -4671,8 +4734,7 @@ export async function auditViewLogs( req, res ) {
4671
4734
  },
4672
4735
  };
4673
4736
  break;
4674
- case 'uniform-detection':
4675
- case 'mobile-detection': // Handle both in the same block
4737
+ case 'mobile-detection':
4676
4738
  logsQuery = {
4677
4739
  'size': 100,
4678
4740
  'query': {