tango-app-api-audit 3.4.11-alpha.15 → 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.15",
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"
@@ -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
  } );
@@ -3888,367 +3890,426 @@ export async function auditImages( req, res ) {
3888
3890
  }
3889
3891
  }
3890
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
+
3891
3912
  export async function getUserAuditCount( req, res ) {
3892
3913
  try {
3893
- const userWallet = JSON.parse( process.env.USER_WALLET );
3894
- const previousDate = dayjs( new Date() ).subtract( 1, 'days' );
3895
- const fileDate = dayjs( previousDate ).format( 'DD-MM-YYYY' );
3896
- const configTimeSpentSec = userWallet.minTimeSpentSec; // 18000;
3897
- // const beforeCountX = userWallet.beforeCountCreditPerc; // 0.0335;
3898
- // const afterCountY = userWallet.afterCountCreditPerc; // 0.0035;
3899
- const reduce = userWallet.reductionPerc; // 0.05;
3900
- const auditUserQuery=[
3901
- {
3902
- $match: {
3903
- $and: [
3904
- { type: { $eq: 'consultant' } },
3905
- { isActive: { $eq: true } },
3906
- ],
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
+ },
3907
3939
  },
3908
- },
3909
- {
3910
- $group: {
3911
- _id: null,
3912
- userList: { $push: '$userId' },
3940
+ {
3941
+ $group: {
3942
+ _id: null,
3943
+ userList: { $push: '$userId' },
3944
+ },
3913
3945
  },
3914
- },
3915
- {
3916
- $project: {
3917
- _id: 0,
3918
- userList: 1,
3946
+ {
3947
+ $project: {
3948
+ _id: 0,
3949
+ userList: 1,
3950
+ },
3919
3951
  },
3920
- },
3921
- ];
3922
- const auditUserList = await aggregateAuditUsers( auditUserQuery );
3923
- if ( auditUserList.length==0 ) {
3924
- return res.sendError( 'No users available', 204 );
3925
- }
3926
- const userCountQuery= [
3927
- {
3928
- $match: {
3929
- $and: [
3930
- { fileDate: { $eq: fileDate } },
3931
- { auditStatus: { $eq: 'completed' } },
3932
- { userId: { $in: auditUserList[0].userList } },
3933
- ],
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
+ },
3934
3966
  },
3935
- },
3936
- // {
3937
- // $project: {
3938
-
3939
- // trafficUserCount: { $cond: [
3940
- // { $eq: [ '$moduleType', 'traffic' ] }, '$userId', '$$REMOVE',
3941
- // ] },
3942
- // zoneUserCount: { $cond: [
3943
- // { $eq: [ '$moduleType', 'zone' ] }, '$userId', '$$REMOVE',
3944
- // ] },
3945
-
3946
- // trafficFilesCount: { $cond: [
3947
- // { $eq: [ '$moduleType', 'traffic' ] }, '$beforeCount', '$$REMOVE',
3948
- // ] },
3949
- // zoneFilesCount: { $cond: [
3950
- // { $eq: [ '$moduleType', 'zone' ] }, '$beforeCount', '$$REMOVE',
3951
- // ] },
3952
-
3953
- // },
3954
- // },
3955
- {
3956
- $group: {
3957
- _id: null,
3958
- // trafficUserCount: { $addToSet: '$trafficUserCount' },
3959
- // zoneUserCount: { $addToSet: '$zoneUserCount' },
3960
- // trafficFilesCount: { $sum: '$trafficFilesCount' },
3961
- // zoneFilesCount: { $sum: '$zoneFilesCount' },
3962
- userCount: { $addToSet: '$userId' },
3963
- 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
+ },
3964
3996
  },
3965
- },
3966
- ];
3997
+ ];
3967
3998
 
3968
- // const storeDataQuery= [
3969
- // {
3970
- // $match: {
3971
- // fileDate: { $eq: fileDate },
3972
- // },
3973
- // },
3974
- // {
3975
- // $project: {
3999
+ // const storeDataQuery= [
4000
+ // {
4001
+ // $match: {
4002
+ // fileDate: { $eq: fileDate },
4003
+ // },
4004
+ // },
4005
+ // {
4006
+ // $project: {
3976
4007
 
3977
- // trafficFilesCount: { $cond: [
3978
- // { $eq: [ '$moduleType', 'traffic' ] }, '$totalFilesCount', '$$REMOVE',
3979
- // ] },
3980
- // zoneFilesCount: { $cond: [
3981
- // { $eq: [ '$moduleType', 'zone' ] }, '$totalFilesCount', '$$REMOVE',
3982
- // ] },
4008
+ // trafficFilesCount: { $cond: [
4009
+ // { $eq: [ '$moduleType', 'traffic' ] }, '$totalFilesCount', '$$REMOVE',
4010
+ // ] },
4011
+ // zoneFilesCount: { $cond: [
4012
+ // { $eq: [ '$moduleType', 'zone' ] }, '$totalFilesCount', '$$REMOVE',
4013
+ // ] },
3983
4014
 
3984
- // },
3985
- // },
3986
- // {
3987
- // $group: {
3988
- // _id: '$moduleType',
3989
- // trafficFilesCount: { $sum: '$trafficFilesCount' },
3990
- // zoneFilesCount: { $sum: '$zoneFilesCount' },
3991
- // },
3992
- // },
3993
- // {
3994
- // $project: {
3995
- // _id: 0,
3996
- // trafficFilesCount: { $ifNull: [ '$trafficFilesCount', 0 ] },
3997
- // zoneFilesCount: { $ifNull: [ '$zoneFilesCount', 0 ] },
3998
- // },
3999
- // },
4000
- // ];
4001
- 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 );
4002
4033
 
4003
- // const totalStoreData = await aggregateAuditStoreData( storeDataQuery );
4004
- if ( userCount?.length == 0 ) {
4005
- return res.sendError( 'No Data Found', 204 );
4006
- }
4007
- const minimumTarget =userCount[0]?.userCount?.length !== 0 && userCount[0]?.filesCount !==0 ? Math.floor( userCount[0].filesCount/userCount[0].userCount.length ) : 0;
4008
- // const trafficMinimumTarget =userCount[0]?.trafficUserCount?.length !== 0 && userCount[0]?.trafficFilesCount !==0 ? Math.floor( userCount[0].trafficFilesCount/userCount[0].trafficUserCount.length ) : 0;
4009
- const beforeCreditCal = 544/minimumTarget;
4010
- // const beforeCountZone = 544/zoneMinimumTarget; // 0.0035;
4011
- 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;
4012
4043
 
4013
- const query = [
4014
- {
4015
- $match: {
4016
- $and: [
4017
- { fileDate: { $eq: fileDate } },
4018
- {
4019
- auditStatus: { $eq: 'completed' },
4020
- },
4021
- { userId: { $in: auditUserList[0].userList } },
4022
- ],
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
+ },
4023
4055
  },
4024
- },
4025
- {
4026
- $set: { minimumTarget: minimumTarget, afterCountCredit: afterCountCredit, beforeCreditCal: beforeCreditCal },
4027
- },
4028
- {
4029
- $project: {
4030
- userId: 1,
4031
- moduleType: 1,
4032
- beforeCount: 1,
4033
- afterCount: 1,
4034
- timeSpent: 1,
4035
- fileDate: 1,
4036
- fileDateISO: 1,
4037
- minimumTarget: 1,
4038
- afterCountCredit: 1,
4039
- totalEarn: { $ifNull: [ 0, 0 ] },
4040
- singleImageAmount: { $ifNull: [ '$beforeCreditCal', 0 ] },
4041
- totalReducedAmount: { $ifNull: [ 0, 0 ] },
4042
- totalCredit: { $ifNull: [ 0, 0 ] },
4043
- deductionBytimeSpent: { $ifNull: [ 0, 0 ] },
4044
- deductionByMinimumTarget: { $ifNull: [ 0, 0 ] },
4056
+ {
4057
+ $set: { minimumTarget: minimumTarget, afterCountCredit: afterCountCredit, beforeCreditCal: beforeCreditCal },
4045
4058
  },
4046
- },
4047
- {
4048
- $group: {
4049
- _id: { user: '$userId' },
4050
- totalCount: { $sum: 1 },
4051
- moduleType: { $first: '$moduleType' },
4052
- totalBeforeCount: { $sum: '$beforeCount' },
4053
- totalAfterCount: { $sum: '$afterCount' },
4054
- totalTimeSpent: { $sum: '$timeSpent' },
4055
- fileDate: { $first: '$fileDate' },
4056
- fileDateISO: { $first: '$fileDateISO' },
4057
- userId: { $first: '$userId' },
4058
- minimumTarget: { $first: '$minimumTarget' },
4059
- afterCountCredit: { $first: '$afterCountCredit' },
4060
- totalEarn: { $first: '$totalEarn' },
4061
- totalReducedAmount: { $first: '$totalReducedAmount' },
4062
- totalCredit: { $first: '$totalCredit' },
4063
- deductionByMinimumTarget: { $first: '$deductionByMinimumTarget' },
4064
- deductionBytimeSpent: { $first: '$deductionBytimeSpent' },
4065
- 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
+ },
4066
4079
  },
4067
- },
4068
- {
4069
- $project: {
4070
- totalCount: 1,
4071
- userId: 1,
4072
- moduleType: 1,
4073
- totalBeforeCount: 1,
4074
- totalAfterCount: 1,
4075
- totalTimeSpent: 1,
4076
- fileDate: 1,
4077
- fileDateISO: 1,
4078
- minimumTarget: 1,
4079
- singleImageAmount: 1,
4080
- beforeCountCredit: { $multiply: [ '$totalBeforeCount', '$singleImageAmount' ] },
4081
- // afterCountCredit: { $cond: [ { $and: [
4082
- // { $gte: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.4 ] }, { $lte: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] },
4083
- // ] }, 136,
4084
- // {
4085
- // $cond: [ { $gt: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] },
4086
- // { $subtract: [ 136, { $multiply: [ { $subtract: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] }, 136 ] } ] },
4087
- // { $subtract: [ 136, { $multiply: [ { $subtract: [ 0.4, { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] } ] }, 136 ] } ] },
4088
- // ],
4089
- // },
4090
- // ] },
4091
- afterCountCredit: afterCountCredit,
4092
- totalEarn: { $add: [ { $multiply: [ '$totalBeforeCount', '$singleImageAmount' ] }, 136 ] },
4093
- deductionBytimeSpent: 1,
4094
- deductionByMinimumTarget: 1,
4095
- mappingperc: { $multiply: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 100 ] },
4096
- deductionByMapping: { $cond: [ { $and: [
4097
- { $gte: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.4 ] }, { $lte: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] },
4098
- ] }, 0,
4099
- {
4100
- $cond: [ { $gt: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] },
4101
- { $multiply: [ { $subtract: [ { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] }, 0.7 ] }, 136 ] },
4102
- { $multiply: [ { $subtract: [ 0.4, { $divide: [ '$totalAfterCount', '$totalBeforeCount' ] } ] }, 136 ] },
4103
- ],
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' },
4104
4104
  },
4105
- ] },
4106
- totalReducedAmount: 1,
4107
- totalCredit: 1,
4108
-
4109
4105
  },
4110
- },
4111
- {
4112
- $project: {
4113
- totalCount: 1,
4114
- userId: 1,
4115
- moduleType: 1,
4116
- totalBeforeCount: 1,
4117
- totalAfterCount: 1,
4118
- totalTimeSpent: 1,
4119
- hours: { $floor: { $divide: [ '$totalTimeSpent', 3600 ] } },
4120
- minutes: {
4121
- $floor: {
4122
- $divide: [
4123
- { $mod: [ '$totalTimeSpent', 3600 ] }, // Get remaining minutes after hours
4124
- 60,
4125
- ] },
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
+
4126
4149
  },
4127
- fileDate: 1,
4128
- fileDateISO: 1,
4129
- minimumTarget: 1,
4130
- singleImageAmount: 1,
4131
- beforeCountCredit: 1,
4132
- afterCountCredit: 1,
4133
- totalEarn: 1,
4134
- deductionBytimeSpent: { $cond: [
4135
- { $lt: [ '$totalTimeSpent', configTimeSpentSec ] },
4136
- { $multiply: [ reduce, '$totalEarn' ] }, '$deductionBytimeSpent',
4137
- ] },
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
+ ] },
4138
4178
 
4139
- deductionByMinimumTarget:
4179
+ deductionByMinimumTarget:
4140
4180
  { $cond: [
4141
4181
  { $lt: [ '$totalBeforeCount', '$minimumTarget' ] },
4142
4182
  { $multiply: [ reduce, '$totalEarn' ] }, '$deductionByMinimumTarget',
4143
4183
  ] },
4144
- mappingperc: 1,
4145
- deductionByMapping: 1,
4146
- 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,
4147
4194
 
4148
4195
 
4196
+ },
4149
4197
  },
4150
- },
4151
4198
 
4152
- {
4153
- $project: {
4154
- totalCount: 1,
4155
- userId: 1,
4156
- moduleType: 1,
4157
- totalBeforeCount: 1,
4158
- totalAfterCount: 1,
4159
- totalTimeSpent: {
4160
- $cond: [ { $eq: [ '$hours', 0 ] },
4161
- { $concat: [
4162
- '00:', { $toString: '$minutes' }, ':00',
4163
- ] },
4164
- { $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 ] },
4165
4208
  { $concat: [
4166
- { $toString: '$hours' }, ':00:00',
4209
+ '00:', { $toString: '$minutes' }, ':00',
4167
4210
  ] },
4168
- {
4169
- $concat: [
4170
- { $toString: '$hours' }, ':',
4171
- { $toString: '$minutes' }, ':00',
4172
- ],
4173
- },
4174
- ] },
4175
- ],
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
+ ],
4176
4223
 
4177
- },
4178
- fileDate: 1,
4179
- fileDateISO: 1,
4180
- minimumTarget: 1,
4181
- beforeCountCredit: 1,
4182
- afterCountCredit: 1,
4183
- totalEarn: 1,
4184
- deductionByMinimumTarget: 1,
4185
- singleImageAmount: 1,
4186
- deductionBytimeSpent: 1,
4187
- mappingperc: 1,
4188
- deductionByMapping: 1,
4189
- totalCredit: { $subtract: [ '$totalEarn', '$totalReducedAmount' ] },
4190
- 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
+ },
4191
4245
 
4246
+ },
4247
+
4248
+ },
4192
4249
  },
4193
- },
4194
- {
4195
- $lookup: {
4196
- from: 'users',
4197
- let: { userId: '$userId' }, // Assuming userId is the linking field
4198
- pipeline: [
4199
- {
4200
- $match: {
4201
- $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
+ },
4202
4259
  },
4203
- },
4204
- {
4205
- $project: {
4206
- _id: 0, // Exclude _id if not needed
4207
- email: 1,
4208
- userName: 1,
4260
+ {
4261
+ $project: {
4262
+ _id: 0, // Exclude _id if not needed
4263
+ email: 1,
4264
+ userName: 1,
4265
+ },
4209
4266
  },
4210
- },
4211
- ],
4212
- as: 'users',
4267
+ ],
4268
+ as: 'users',
4269
+ },
4213
4270
  },
4214
- },
4215
- { $unwind: { path: '$users', preserveNullAndEmptyArrays: true } },
4216
- {
4217
- $project: {
4218
- _id: 0,
4219
- totalFilesCount: { $round: [ '$totalCount', 2 ] },
4220
- moduleType: 1,
4221
- totalAfterCount: { $round: [ '$totalAfterCount', 2 ] },
4222
- totalBeforeCount: { $round: [ '$totalBeforeCount', 2 ] },
4223
- timeSpent: '$totalTimeSpent',
4224
- fileDate: 1,
4225
- fileDateISO: 1,
4226
- beforeCountCredit: 1,
4227
- afterCountCredit: 1,
4228
- totalEarn: { $round: [ '$totalEarn', 2 ] },
4229
- minimumTarget: 1,
4230
- singleImageAmount: 1,
4231
- deductionByMinimumTarget: { $round: [ '$deductionByMinimumTarget', 2 ] },
4232
- deductionBytimeSpent: { $round: [ '$deductionBytimeSpent', 2 ] },
4233
- mappingperc: 1,
4234
- deductionByMapping: 1,
4235
- totalReducedAmount: 1,
4236
- totalCredit: { $round: [ { $subtract: [ '$totalEarn', '$totalReducedAmount' ] }, 2 ] },
4237
- userEmail: '$users.email', // Access first element from array
4238
- 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,
4239
4297
 
4298
+ },
4240
4299
  },
4241
- },
4242
- ];
4243
- const consolidatedData = await aggregateUserAuditCount( query );
4244
- if ( consolidatedData.length == 0 ) {
4245
- return res.sendError( 'No Data found', 204 );
4246
- }
4247
- const result = await createAuditUserWallet( consolidatedData );
4248
- if ( result.length == 0 ) {
4249
- 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
+ }
4250
4312
  }
4251
- return res.sendSuccess( { message: 'Inserted successfully!' } );
4252
4313
  } catch ( error ) {
4253
4314
  const err = error.message || 'Internal Server Error';
4254
4315
  logger.info( { error: error, message: req.body, function: 'getUserAuditCount' } );
@@ -544,6 +544,20 @@ export const auditDocs = {
544
544
  },
545
545
  },
546
546
  },
547
+ '/v3/audit/get-user-credit': {
548
+ get: {
549
+ tags: [ 'Audit User Wallet' ],
550
+ description: 'Get single past date credit',
551
+ operationId: 'get-user-credit',
552
+ responses: {
553
+ 200: { description: 'Successful' },
554
+ 401: { description: 'Unauthorized User' },
555
+ 422: { description: 'Field Error' },
556
+ 500: { description: 'Server Error' },
557
+ 204: { description: 'Not Found' },
558
+ },
559
+ },
560
+ },
547
561
  '/v3/audit/get-user-audit-count': {
548
562
  get: {
549
563
  tags: [ 'Audit User Wallet' ],
@@ -1,7 +1,7 @@
1
1
  import express from 'express';
2
- import { isAuditDocumentExist, isAuditInputFolderExist, isExistsQueue, validateUserAudit } from '../validation/audit.validation.js';
2
+ import { isAuditDocumentExist, isAuditInputFolderExist, isAuditUser, isExistsQueue, validateUserAudit } from '../validation/audit.validation.js';
3
3
  import { isAllowedSessionHandler, validate } from 'tango-app-api-middleware';
4
- import { auditImages, auditStoreList, auditUserList, clientMetrics, clients, getAuditFile, getDraftedData, overAllAuditSummary, overViewCard, overviewTable, pendingSummaryTable, reTrigger, save, saveDraft, storeMetrics, summarySplit, totalNotAssignedCount, userAuditHistory, userMetrics, workSpace, getUserAuditCount, getUserAuditCountMTD, auditViewLogs } from '../controllers/audit.controllers.js';
4
+ import { auditImages, auditStoreList, auditUserList, clientMetrics, clients, getAuditFile, getDraftedData, overAllAuditSummary, overViewCard, overviewTable, pendingSummaryTable, reTrigger, save, saveDraft, storeMetrics, summarySplit, totalNotAssignedCount, userAuditHistory, userMetrics, workSpace, getUserAuditCount, getUserAuditCountMTD, auditViewLogs, getUserCredit } from '../controllers/audit.controllers.js';
5
5
  import { auditStoreValid, clientMetricsValid, clientValid, getDraftedDataValid, getFileValid, overAllAuditSummaryValid, overViewCardValid, pendingSummaryTableValid, reTriggerValid, saveDraftValid, saveValid, storeMetricsValid, summarySplitValid, userAuditHistoryValid, userMetricsValid, workSpaceValid, auditImageValid, overviewTableValid } from '../dtos/audit.dtos.js';
6
6
 
7
7
  export const auditRouter = express.Router();
@@ -37,6 +37,7 @@ auditRouter.post( '/metrics/overview-table', isAllowedSessionHandler, validate(
37
37
  auditRouter.get( '/total-not-assigned-count', isAllowedSessionHandler, totalNotAssignedCount );
38
38
 
39
39
  // audit user wallet
40
+ auditRouter.get( '/get-user-credit', isAllowedSessionHandler, isAuditUser, getUserCredit );
40
41
  auditRouter.get( '/get-user-audit-count', isAllowedSessionHandler, getUserAuditCount );
41
42
  auditRouter.get( '/get-user-audit-count-mtd', isAllowedSessionHandler, getUserAuditCountMTD );
42
43
 
@@ -5,6 +5,10 @@ export function createAuditUserWallet( record ) {
5
5
  return auditUserWalletModel.create( record );
6
6
  };
7
7
 
8
+ export function findOneAuditUserWallet( record, fields, sort = { createdAt: -1 } ) {
9
+ return auditUserWalletModel.findOne( record, fields ).sort( sort );
10
+ };
11
+
8
12
  export function updateManyAuditUserWallet( query, record ) {
9
13
  return auditUserWalletModel.updateMany( query, record, { upsert: true } );
10
14
  };
@@ -3,3 +3,7 @@ import auditUsersModel from 'tango-api-schema/schema/auditUsers.model.js';
3
3
  export function aggregateAuditUsers( query ) {
4
4
  return auditUsersModel.aggregate( query );
5
5
  };
6
+
7
+ export function findOneAuditUsers( query, fields ) {
8
+ return auditUsersModel.findOne( query, fields );
9
+ };
@@ -2,6 +2,7 @@ import { checkFileExist, chunkArray, getJsonFileData, getObject, getQueueUrl, li
2
2
  import { findOneUserAudit } from '../service/userAudit.service.js';
3
3
  import { findOneStoreAudit } from '../service/storeAudit.service.js';
4
4
  import AdmZip from 'adm-zip';
5
+ import { findOneAuditUsers } from '../service/auditUsers.service.js';
5
6
 
6
7
  export async function isExistsQueue( req, res, next ) {
7
8
  try {
@@ -333,4 +334,18 @@ export async function getAuditFilterData( data ) {
333
334
  }
334
335
  }
335
336
 
337
+ export async function isAuditUser( req, res, next ) {
338
+ try {
339
+ const userEmail = req.user?.email;
340
+ const getAuditUser = await findOneAuditUsers( { email: userEmail, isActive: true } );
341
+ if ( !getAuditUser ) {
342
+ return res.sendSuccess( { result: 0, isAuditUser: false } );
343
+ }
344
+ return next();
345
+ } catch ( error ) {
346
+ const err = error.message || 'Internal Server Error';
347
+ logger.error( { error: error, function: 'isAuditUser' } );
348
+ return res.sendError( err, 500 );
349
+ }
350
+ }
336
351