tango-app-api-audit 3.4.11-alpha.16 → 3.4.11-alpha.17

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-audit",
3
- "version": "3.4.11-alpha.16",
3
+ "version": "3.4.11-alpha.17",
4
4
  "description": "audit & audit metrics apis",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -24,7 +24,7 @@
24
24
  "mongodb": "^6.7.0",
25
25
  "nodemon": "^3.1.3",
26
26
  "swagger-ui-express": "^5.0.1",
27
- "tango-api-schema": "^2.1.38",
27
+ "tango-api-schema": "^2.1.40",
28
28
  "tango-app-api-middleware": "^3.1.33",
29
29
  "winston": "^3.13.0",
30
30
  "winston-daily-rotate-file": "^5.0.0"
@@ -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
  } );
@@ -3909,365 +3911,405 @@ export async function getUserCredit( req, res ) {
3909
3911
 
3910
3912
  export async function getUserAuditCount( req, res ) {
3911
3913
  try {
3912
- const userWallet = JSON.parse( process.env.USER_WALLET );
3913
- const previousDate = dayjs( new Date() ).subtract( 1, 'days' );
3914
- const fileDate = dayjs( previousDate ).format( 'DD-MM-YYYY' );
3915
- const configTimeSpentSec = userWallet.minTimeSpentSec; // 18000;
3916
- // const beforeCountX = userWallet.beforeCountCreditPerc; // 0.0335;
3917
- // const afterCountY = userWallet.afterCountCreditPerc; // 0.0035;
3918
- const reduce = userWallet.reductionPerc; // 0.05;
3919
- const auditUserQuery=[
3920
- {
3921
- $match: {
3922
- $and: [
3923
- { type: { $eq: 'consultant' } },
3924
- { isActive: { $eq: true } },
3925
- ],
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
+ },
3926
3939
  },
3927
- },
3928
- {
3929
- $group: {
3930
- _id: null,
3931
- userList: { $push: '$userId' },
3940
+ {
3941
+ $group: {
3942
+ _id: null,
3943
+ userList: { $push: '$userId' },
3944
+ },
3932
3945
  },
3933
- },
3934
- {
3935
- $project: {
3936
- _id: 0,
3937
- userList: 1,
3946
+ {
3947
+ $project: {
3948
+ _id: 0,
3949
+ userList: 1,
3950
+ },
3938
3951
  },
3939
- },
3940
- ];
3941
- const auditUserList = await aggregateAuditUsers( auditUserQuery );
3942
- if ( auditUserList.length==0 ) {
3943
- return res.sendError( 'No users available', 204 );
3944
- }
3945
- const userCountQuery= [
3946
- {
3947
- $match: {
3948
- $and: [
3949
- { fileDate: { $eq: fileDate } },
3950
- { auditStatus: { $eq: 'completed' } },
3951
- { userId: { $in: auditUserList[0].userList } },
3952
- ],
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
+ },
3953
3966
  },
3954
- },
3955
- // {
3956
- // $project: {
3957
-
3958
- // trafficUserCount: { $cond: [
3959
- // { $eq: [ '$moduleType', 'traffic' ] }, '$userId', '$$REMOVE',
3960
- // ] },
3961
- // zoneUserCount: { $cond: [
3962
- // { $eq: [ '$moduleType', 'zone' ] }, '$userId', '$$REMOVE',
3963
- // ] },
3964
-
3965
- // trafficFilesCount: { $cond: [
3966
- // { $eq: [ '$moduleType', 'traffic' ] }, '$beforeCount', '$$REMOVE',
3967
- // ] },
3968
- // zoneFilesCount: { $cond: [
3969
- // { $eq: [ '$moduleType', 'zone' ] }, '$beforeCount', '$$REMOVE',
3970
- // ] },
3971
-
3972
- // },
3973
- // },
3974
- {
3975
- $group: {
3976
- _id: null,
3977
- // trafficUserCount: { $addToSet: '$trafficUserCount' },
3978
- // zoneUserCount: { $addToSet: '$zoneUserCount' },
3979
- // trafficFilesCount: { $sum: '$trafficFilesCount' },
3980
- // zoneFilesCount: { $sum: '$zoneFilesCount' },
3981
- userCount: { $addToSet: '$userId' },
3982
- 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
+ },
3983
3996
  },
3984
- },
3985
- ];
3997
+ ];
3986
3998
 
3987
- // const storeDataQuery= [
3988
- // {
3989
- // $match: {
3990
- // fileDate: { $eq: fileDate },
3991
- // },
3992
- // },
3993
- // {
3994
- // $project: {
3999
+ // const storeDataQuery= [
4000
+ // {
4001
+ // $match: {
4002
+ // fileDate: { $eq: fileDate },
4003
+ // },
4004
+ // },
4005
+ // {
4006
+ // $project: {
3995
4007
 
3996
- // trafficFilesCount: { $cond: [
3997
- // { $eq: [ '$moduleType', 'traffic' ] }, '$totalFilesCount', '$$REMOVE',
3998
- // ] },
3999
- // zoneFilesCount: { $cond: [
4000
- // { $eq: [ '$moduleType', 'zone' ] }, '$totalFilesCount', '$$REMOVE',
4001
- // ] },
4008
+ // trafficFilesCount: { $cond: [
4009
+ // { $eq: [ '$moduleType', 'traffic' ] }, '$totalFilesCount', '$$REMOVE',
4010
+ // ] },
4011
+ // zoneFilesCount: { $cond: [
4012
+ // { $eq: [ '$moduleType', 'zone' ] }, '$totalFilesCount', '$$REMOVE',
4013
+ // ] },
4002
4014
 
4003
- // },
4004
- // },
4005
- // {
4006
- // $group: {
4007
- // _id: '$moduleType',
4008
- // trafficFilesCount: { $sum: '$trafficFilesCount' },
4009
- // zoneFilesCount: { $sum: '$zoneFilesCount' },
4010
- // },
4011
- // },
4012
- // {
4013
- // $project: {
4014
- // _id: 0,
4015
- // trafficFilesCount: { $ifNull: [ '$trafficFilesCount', 0 ] },
4016
- // zoneFilesCount: { $ifNull: [ '$zoneFilesCount', 0 ] },
4017
- // },
4018
- // },
4019
- // ];
4020
- 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 );
4021
4033
 
4022
- // const totalStoreData = await aggregateAuditStoreData( storeDataQuery );
4023
- if ( userCount?.length == 0 ) {
4024
- return res.sendError( 'No Data Found', 204 );
4025
- }
4026
- const minimumTarget =userCount[0]?.userCount?.length !== 0 && userCount[0]?.filesCount !==0 ? Math.floor( userCount[0].filesCount/userCount[0].userCount.length ) : 0;
4027
- // const trafficMinimumTarget =userCount[0]?.trafficUserCount?.length !== 0 && userCount[0]?.trafficFilesCount !==0 ? Math.floor( userCount[0].trafficFilesCount/userCount[0].trafficUserCount.length ) : 0;
4028
- const beforeCreditCal = 544/minimumTarget;
4029
- // const beforeCountZone = 544/zoneMinimumTarget; // 0.0035;
4030
- 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;
4031
4043
 
4032
- const query = [
4033
- {
4034
- $match: {
4035
- $and: [
4036
- { fileDate: { $eq: fileDate } },
4037
- {
4038
- auditStatus: { $eq: 'completed' },
4039
- },
4040
- { userId: { $in: auditUserList[0].userList } },
4041
- ],
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
+ },
4042
4055
  },
4043
- },
4044
- {
4045
- $set: { minimumTarget: minimumTarget, afterCountCredit: afterCountCredit, beforeCreditCal: beforeCreditCal },
4046
- },
4047
- {
4048
- $project: {
4049
- userId: 1,
4050
- moduleType: 1,
4051
- beforeCount: 1,
4052
- afterCount: 1,
4053
- timeSpent: 1,
4054
- fileDate: 1,
4055
- fileDateISO: 1,
4056
- minimumTarget: 1,
4057
- afterCountCredit: 1,
4058
- totalEarn: { $ifNull: [ 0, 0 ] },
4059
- singleImageAmount: { $ifNull: [ '$beforeCreditCal', 0 ] },
4060
- totalReducedAmount: { $ifNull: [ 0, 0 ] },
4061
- totalCredit: { $ifNull: [ 0, 0 ] },
4062
- deductionBytimeSpent: { $ifNull: [ 0, 0 ] },
4063
- deductionByMinimumTarget: { $ifNull: [ 0, 0 ] },
4056
+ {
4057
+ $set: { minimumTarget: minimumTarget, afterCountCredit: afterCountCredit, beforeCreditCal: beforeCreditCal },
4064
4058
  },
4065
- },
4066
- {
4067
- $group: {
4068
- _id: { user: '$userId' },
4069
- totalCount: { $sum: 1 },
4070
- moduleType: { $first: '$moduleType' },
4071
- totalBeforeCount: { $sum: '$beforeCount' },
4072
- totalAfterCount: { $sum: '$afterCount' },
4073
- totalTimeSpent: { $sum: '$timeSpent' },
4074
- fileDate: { $first: '$fileDate' },
4075
- fileDateISO: { $first: '$fileDateISO' },
4076
- userId: { $first: '$userId' },
4077
- minimumTarget: { $first: '$minimumTarget' },
4078
- afterCountCredit: { $first: '$afterCountCredit' },
4079
- totalEarn: { $first: '$totalEarn' },
4080
- totalReducedAmount: { $first: '$totalReducedAmount' },
4081
- totalCredit: { $first: '$totalCredit' },
4082
- deductionByMinimumTarget: { $first: '$deductionByMinimumTarget' },
4083
- deductionBytimeSpent: { $first: '$deductionBytimeSpent' },
4084
- 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
+ },
4085
4079
  },
4086
- },
4087
- {
4088
- $project: {
4089
- totalCount: 1,
4090
- userId: 1,
4091
- moduleType: 1,
4092
- totalBeforeCount: 1,
4093
- totalAfterCount: 1,
4094
- totalTimeSpent: 1,
4095
- fileDate: 1,
4096
- fileDateISO: 1,
4097
- minimumTarget: 1,
4098
- singleImageAmount: 1,
4099
- beforeCountCredit: { $multiply: [ '$totalBeforeCount', '$singleImageAmount' ] },
4100
- // afterCountCredit: { $cond: [ { $and: [
4101
- // { $gte: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.4 ] }, { $lte: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] },
4102
- // ] }, 136,
4103
- // {
4104
- // $cond: [ { $gt: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] },
4105
- // { $subtract: [ 136, { $multiply: [ { $subtract: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] }, 136 ] } ] },
4106
- // { $subtract: [ 136, { $multiply: [ { $subtract: [ 0.4, { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] } ] }, 136 ] } ] },
4107
- // ],
4108
- // },
4109
- // ] },
4110
- afterCountCredit: afterCountCredit,
4111
- totalEarn: { $add: [ { $multiply: [ '$totalBeforeCount', '$singleImageAmount' ] }, 136 ] },
4112
- deductionBytimeSpent: 1,
4113
- deductionByMinimumTarget: 1,
4114
- mappingperc: { $multiply: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 100 ] },
4115
- deductionByMapping: { $cond: [ { $and: [
4116
- { $gte: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.4 ] }, { $lte: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] },
4117
- ] }, 0,
4118
- {
4119
- $cond: [ { $gt: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] },
4120
- { $multiply: [ { $subtract: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] }, 136 ] },
4121
- { $multiply: [ { $subtract: [ 0.4, { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] } ] }, 136 ] },
4122
- ],
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' },
4123
4104
  },
4124
- ] },
4125
- totalReducedAmount: 1,
4126
- totalCredit: 1,
4127
-
4128
4105
  },
4129
- },
4130
- {
4131
- $project: {
4132
- totalCount: 1,
4133
- userId: 1,
4134
- moduleType: 1,
4135
- totalBeforeCount: 1,
4136
- totalAfterCount: 1,
4137
- totalTimeSpent: 1,
4138
- hours: { $floor: { $divide: [ '$totalTimeSpent', 3600 ] } },
4139
- minutes: {
4140
- $floor: {
4141
- $divide: [
4142
- { $mod: [ '$totalTimeSpent', 3600 ] }, // Get remaining minutes after hours
4143
- 60,
4144
- ] },
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
+
4145
4149
  },
4146
- fileDate: 1,
4147
- fileDateISO: 1,
4148
- minimumTarget: 1,
4149
- singleImageAmount: 1,
4150
- beforeCountCredit: 1,
4151
- afterCountCredit: 1,
4152
- totalEarn: 1,
4153
- deductionBytimeSpent: { $cond: [
4154
- { $lt: [ '$totalTimeSpent', configTimeSpentSec ] },
4155
- { $multiply: [ reduce, '$totalEarn' ] }, '$deductionBytimeSpent',
4156
- ] },
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
+ ] },
4157
4178
 
4158
- deductionByMinimumTarget:
4179
+ deductionByMinimumTarget:
4159
4180
  { $cond: [
4160
4181
  { $lt: [ '$totalBeforeCount', '$minimumTarget' ] },
4161
4182
  { $multiply: [ reduce, '$totalEarn' ] }, '$deductionByMinimumTarget',
4162
4183
  ] },
4163
- mappingperc: 1,
4164
- deductionByMapping: 1,
4165
- 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,
4166
4194
 
4167
4195
 
4196
+ },
4168
4197
  },
4169
- },
4170
4198
 
4171
- {
4172
- $project: {
4173
- totalCount: 1,
4174
- userId: 1,
4175
- moduleType: 1,
4176
- totalBeforeCount: 1,
4177
- totalAfterCount: 1,
4178
- totalTimeSpent: {
4179
- $cond: [ { $eq: [ '$hours', 0 ] },
4180
- { $concat: [
4181
- '00:', { $toString: '$minutes' }, ':00',
4182
- ] },
4183
- { $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 ] },
4184
4208
  { $concat: [
4185
- { $toString: '$hours' }, ':00:00',
4209
+ '00:', { $toString: '$minutes' }, ':00',
4186
4210
  ] },
4187
- {
4188
- $concat: [
4189
- { $toString: '$hours' }, ':',
4190
- { $toString: '$minutes' }, ':00',
4191
- ],
4192
- },
4193
- ] },
4194
- ],
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
+ ],
4195
4223
 
4196
- },
4197
- fileDate: 1,
4198
- fileDateISO: 1,
4199
- minimumTarget: 1,
4200
- beforeCountCredit: 1,
4201
- afterCountCredit: 1,
4202
- totalEarn: 1,
4203
- deductionByMinimumTarget: 1,
4204
- singleImageAmount: 1,
4205
- deductionBytimeSpent: 1,
4206
- mappingperc: 1,
4207
- deductionByMapping: 1,
4208
- totalCredit: { $subtract: [ '$totalEarn', '$totalReducedAmount' ] },
4209
- 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
+ },
4210
4245
 
4246
+ },
4247
+
4248
+ },
4211
4249
  },
4212
- },
4213
- {
4214
- $lookup: {
4215
- from: 'users',
4216
- let: { userId: '$userId' }, // Assuming userId is the linking field
4217
- pipeline: [
4218
- {
4219
- $match: {
4220
- $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
+ },
4221
4259
  },
4222
- },
4223
- {
4224
- $project: {
4225
- _id: 0, // Exclude _id if not needed
4226
- email: 1,
4227
- userName: 1,
4260
+ {
4261
+ $project: {
4262
+ _id: 0, // Exclude _id if not needed
4263
+ email: 1,
4264
+ userName: 1,
4265
+ },
4228
4266
  },
4229
- },
4230
- ],
4231
- as: 'users',
4267
+ ],
4268
+ as: 'users',
4269
+ },
4232
4270
  },
4233
- },
4234
- { $unwind: { path: '$users', preserveNullAndEmptyArrays: true } },
4235
- {
4236
- $project: {
4237
- _id: 0,
4238
- totalFilesCount: { $round: [ '$totalCount', 2 ] },
4239
- moduleType: 1,
4240
- totalAfterCount: { $round: [ '$totalAfterCount', 2 ] },
4241
- totalBeforeCount: { $round: [ '$totalBeforeCount', 2 ] },
4242
- timeSpent: '$totalTimeSpent',
4243
- fileDate: 1,
4244
- fileDateISO: 1,
4245
- beforeCountCredit: 1,
4246
- afterCountCredit: 1,
4247
- totalEarn: { $round: [ '$totalEarn', 2 ] },
4248
- minimumTarget: 1,
4249
- singleImageAmount: 1,
4250
- deductionByMinimumTarget: { $round: [ '$deductionByMinimumTarget', 2 ] },
4251
- deductionBytimeSpent: { $round: [ '$deductionBytimeSpent', 2 ] },
4252
- mappingperc: 1,
4253
- deductionByMapping: 1,
4254
- totalReducedAmount: 1,
4255
- totalCredit: { $round: [ { $subtract: [ '$totalEarn', '$totalReducedAmount' ] }, 2 ] },
4256
- userEmail: '$users.email', // Access first element from array
4257
- 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,
4258
4297
 
4298
+ },
4259
4299
  },
4260
- },
4261
- ];
4262
- const consolidatedData = await aggregateUserAuditCount( query );
4263
- if ( consolidatedData.length == 0 ) {
4264
- return res.sendError( 'No Data found', 204 );
4265
- }
4266
- const result = await createAuditUserWallet( consolidatedData );
4267
- if ( result.length == 0 ) {
4268
- 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
+ }
4269
4312
  }
4270
- return res.sendSuccess( { message: 'Inserted successfully!' } );
4271
4313
  } catch ( error ) {
4272
4314
  const err = error.message || 'Internal Server Error';
4273
4315
  logger.info( { error: error, message: req.body, function: 'getUserAuditCount' } );