tango-app-api-audit 3.4.32 → 3.4.33

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.32",
3
+ "version": "3.4.33",
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.2.12",
27
+ "tango-api-schema": "^2.2.19",
28
28
  "tango-app-api-middleware": "^3.1.48",
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, findOneAuditUserWallet } from '../service/auditUserWallet.service.js';
66
+ import { bulkWriteAuditUserWallet, createAuditUserWallet, findOneAuditUserWallet } from '../service/auditUserWallet.service.js';
67
67
  import { updateOneAuditUsers } from '../service/auditUsers.service.js';
68
68
  // import { aggregateAuditUsers } from '../service/auditUsers.service.js';
69
69
 
@@ -3942,6 +3942,12 @@ export async function getUserAuditCount( req, res ) {
3942
3942
  const userWallet = JSON.parse( process.env.USER_WALLET );
3943
3943
  const today = new Date();
3944
3944
 
3945
+ // Subtract one day to get the previous day
3946
+ const previousDay = new Date( today );
3947
+ previousDay.setDate( today.getDate() - 1 );
3948
+ const lateLogin = previousDay;
3949
+ lateLogin.setHours( 21, 30, 0, 0 );
3950
+
3945
3951
  // Get yesterday's date
3946
3952
  const yesterday = new Date( today.getFullYear(), today.getMonth(), today.getDate() - 1 );
3947
3953
 
@@ -3950,45 +3956,144 @@ export async function getUserAuditCount( req, res ) {
3950
3956
 
3951
3957
  // Calculate the difference in days
3952
3958
  const differenceInDays = ( yesterday.getDate() - firstDateOfMonth.getDate() );
3959
+ logger.info( { differenceInDays: differenceInDays } );
3953
3960
 
3954
- for ( let i =1; i<=differenceInDays; i++ ) {
3955
- const previousDate = dayjs( new Date() ).subtract( i, 'days' );
3956
- const fileDate = dayjs( previousDate ).format( 'DD-MM-YYYY' );
3957
- const reduce = userWallet.reductionPerc; // 0.05;
3961
+ const auditUserQuery=[
3962
+ {
3963
+ $match: {
3964
+ $and: [
3965
+ { tangoUserType: { $eq: 'audit' } },
3966
+ { isActive: { $eq: true } },
3967
+ ],
3968
+ },
3969
+ },
3970
+ {
3971
+ $group: {
3972
+ _id: null,
3973
+ userList: { $push: '$_id' },
3974
+ },
3975
+ },
3976
+ {
3977
+ $project: {
3978
+ _id: 0,
3979
+ userList: 1,
3980
+ },
3981
+ },
3982
+ ];
3983
+ const auditUserList = await aggregateUser( auditUserQuery );
3984
+ if ( auditUserList.length==0 ) {
3985
+ return res.sendError( 'No users available', 204 );
3986
+ }
3958
3987
 
3959
- // Subtract one day to get the previous day
3960
- const previousDay = new Date( today );
3961
- previousDay.setDate( today.getDate() - i );
3962
- const lateLogin = previousDay;
3963
- logger.info( { lateLogin1: lateLogin } );
3964
- lateLogin.setHours( 21, 30, 0, 0 );
3965
- logger.info( { lateLogin: lateLogin } );
3966
- const auditUserQuery=[
3967
- {
3968
- $match: {
3969
- $and: [
3970
- { tangoUserType: { $eq: 'audit' } },
3971
- { isActive: { $eq: true } },
3988
+ const lateLoginQuery =[
3989
+ {
3990
+ $match: {
3991
+ $and: [
3992
+ { fileDate: { $eq: dayjs( yesterday ).format( 'DD-MM-YYYY' ) } },
3993
+ {
3994
+ auditStatus: { $eq: 'completed' },
3995
+ },
3996
+ { userId: { $in: auditUserList[0].userList } },
3997
+ ],
3998
+ },
3999
+ },
4000
+ {
4001
+ $sort: {
4002
+ startTime: 1,
4003
+ },
4004
+ },
4005
+ {
4006
+ $group: {
4007
+ _id: '$userId',
4008
+ userId: { $first: '$userId' },
4009
+ startTime: { $first: '$startTime' },
4010
+ fileDate: { $first: '$fileDate' },
4011
+ },
4012
+ },
4013
+ {
4014
+ $lookup: {
4015
+ from: 'users',
4016
+ let: { userId: '$userId' }, // Assuming userId is the linking field
4017
+ pipeline: [
4018
+ {
4019
+ $match: {
4020
+ $expr: { $eq: [ '$_id', '$$userId' ] },
4021
+ },
4022
+ },
4023
+ {
4024
+ $project: {
4025
+ _id: 0, // Exclude _id if not needed
4026
+ email: 1,
4027
+ userName: 1,
4028
+ },
4029
+ },
4030
+ ],
4031
+ as: 'users',
4032
+ },
4033
+ },
4034
+ { $unwind: { path: '$users', preserveNullAndEmptyArrays: true } },
4035
+
4036
+ {
4037
+ $project: {
4038
+ startTime: 1,
4039
+ userId: 1,
4040
+ userEmail: '$users.email',
4041
+ fileDate: 1,
4042
+ parsedDate: {
4043
+ $dateFromString: {
4044
+ dateString: '$fileDate',
4045
+ format: '%d-%m-%Y',
4046
+ },
4047
+ },
4048
+ deductionByLateLogin: {
4049
+ $cond: [
4050
+ { $gt: [ '$startTime', lateLogin ] }, true, false,
3972
4051
  ],
3973
4052
  },
3974
4053
  },
3975
- {
3976
- $group: {
3977
- _id: null,
3978
- userList: { $push: '$userId' },
4054
+ }, {
4055
+ $group: {
4056
+ _id: null,
4057
+ updatedData: {
4058
+ $push: {
4059
+ $cond: [
4060
+ { $eq: [ '$deductionByLateLogin', true ] }, { userId: '$userId', userEmail: '$userEmail', fileDate: '$fileDate', week: { $week: '$parsedDate' },
4061
+ month: { $month: '$parsedDate' } }, '$$REMOVE',
4062
+ ],
4063
+ },
3979
4064
  },
3980
4065
  },
3981
- {
3982
- $project: {
3983
- _id: 0,
3984
- userList: 1,
3985
- },
4066
+ },
4067
+ {
4068
+ $project: {
4069
+ _id: 0,
4070
+ updatedData: 1,
3986
4071
  },
3987
- ];
3988
- const auditUserList = await aggregateUser( auditUserQuery );
3989
- if ( auditUserList.length==0 ) {
3990
- return res.sendError( 'No users available', 204 );
3991
- }
4072
+ },
4073
+ ];
4074
+ const getLateLogin = await aggregateUserAuditCount( lateLoginQuery );
4075
+ for ( let i=0; getLateLogin[0]?.updatedData?.length>0 && i<getLateLogin[0]?.updatedData?.length; i++ ) {
4076
+ const query = {
4077
+ email: getLateLogin[0]?.updatedData[i]?.userEmail,
4078
+ week: getLateLogin[0]?.updatedData[i]?.week,
4079
+ };
4080
+ const record ={
4081
+ email: getLateLogin[0]?.updatedData[i]?.userEmail,
4082
+ userId: getLateLogin[0]?.updatedData[i]?.userId,
4083
+ month: getLateLogin[0]?.updatedData[i]?.month,
4084
+ week: getLateLogin[0]?.updatedData[i]?.week,
4085
+ fileDate: [ getLateLogin[0]?.updatedData[i]?.fileDate ],
4086
+ lateLogin: 1,
4087
+
4088
+ };
4089
+ await updateOneAuditUsers( query, record );
4090
+ }
4091
+
4092
+ for ( let i =1; i<=differenceInDays+1; i++ ) {
4093
+ const previousDate = dayjs( new Date() ).subtract( i, 'days' );
4094
+ const fileDate = dayjs( previousDate ).format( 'DD-MM-YYYY' );
4095
+ const reduce = userWallet.reductionPerc; // 0.05;
4096
+
3992
4097
  const userCountQuery= [
3993
4098
  {
3994
4099
  $match: {
@@ -4009,11 +4114,11 @@ export async function getUserAuditCount( req, res ) {
4009
4114
  ];
4010
4115
 
4011
4116
  const userCount = await aggregateUserAuditCount( userCountQuery );
4012
-
4013
4117
  if ( userCount?.length == 0 ) {
4014
4118
  return res.sendError( 'No Data Found', 204 );
4015
4119
  }
4016
4120
  const minimumTarget =userCount[0]?.userCount?.length !== 0 && userCount[0]?.filesCount !==0 ? Math.floor( userCount[0].filesCount/userCount[0].userCount.length ) : 0;
4121
+ logger.info( { bc: userWallet?.beforeCountCredit, ac: userWallet?.afterCountCredit } );
4017
4122
  const beforeCreditCal = userWallet?.beforeCountCredit/minimumTarget;
4018
4123
  const afterCountCredit = userWallet?.afterCountCredit;
4019
4124
  const filter = [
@@ -4024,90 +4129,6 @@ export async function getUserAuditCount( req, res ) {
4024
4129
  { userId: { $in: auditUserList[0].userList } },
4025
4130
  ];
4026
4131
 
4027
- const lateLoginQuery =[
4028
- {
4029
- $match: {
4030
- $and: filter,
4031
- },
4032
- },
4033
- {
4034
- $lookup: {
4035
- from: 'users',
4036
- let: { userId: '$userId' }, // Assuming userId is the linking field
4037
- pipeline: [
4038
- {
4039
- $match: {
4040
- $expr: { $eq: [ '$_id', '$$userId' ] },
4041
- },
4042
- },
4043
- {
4044
- $project: {
4045
- _id: 0, // Exclude _id if not needed
4046
- email: 1,
4047
- userName: 1,
4048
- },
4049
- },
4050
- ],
4051
- as: 'users',
4052
- },
4053
- },
4054
- { $unwind: { path: '$users', preserveNullAndEmptyArrays: true } },
4055
- {
4056
- $project: {
4057
- startTime: 1,
4058
- userId: 1,
4059
- userEmail: '$user.email',
4060
- fileDate: 1,
4061
- parsedDate: {
4062
- $dateFromString: {
4063
- dateString: '$fileDate',
4064
- format: '%d-%m-%Y',
4065
- },
4066
- },
4067
- deductionByLateLogin: {
4068
- $cond: [
4069
- { $gt: [ '$startTime', lateLogin ] }, true, false,
4070
- ],
4071
- },
4072
- },
4073
- }, {
4074
- $group: {
4075
- _id: null,
4076
- updatedData: {
4077
- $push: {
4078
- $cond: [
4079
- { $eq: [ '$deductionByLateLogin', true ] }, { userId: '$userId', userEmail: '$userEmail', fileDate: 'fileDate', week: { $week: '$parsedDate' },
4080
- month: { $month: '$parsedDate' } }, '$$REMOVE',
4081
- ],
4082
- },
4083
- },
4084
- },
4085
- },
4086
- {
4087
- $project: {
4088
- _id: 0,
4089
- updatedData: 1,
4090
- },
4091
- },
4092
- ];
4093
- const getLateLogin = await aggregateUserAuditCount( lateLoginQuery );
4094
-
4095
- for ( let i=0; getLateLogin[0]?.updatedData?.length>0 && 1<getLateLogin[0]?.updatedData?.length; i++ ) {
4096
- const query = {
4097
- email: getLateLogin[0]?.updatedData[i]?.userEmail,
4098
- week: getLateLogin[0]?.updatedData[i]?.week,
4099
- };
4100
- const record ={
4101
- email: getLateLogin[0]?.updatedData[i]?.userEmail,
4102
- userId: getLateLogin[0]?.updatedData[i]?.userId,
4103
- month: getLateLogin[0]?.updatedData[i]?.month,
4104
- week: getLateLogin[0]?.updatedData[i]?.week,
4105
- fileDate: getLateLogin[0]?.updatedData[i]?.fileDate,
4106
- lateLogin: 1,
4107
-
4108
- };
4109
- await updateOneAuditUsers( query, record );
4110
- }
4111
4132
  const query = [
4112
4133
  {
4113
4134
  $match: {
@@ -4135,6 +4156,12 @@ export async function getUserAuditCount( req, res ) {
4135
4156
  deductionByMinimumTarget: { $ifNull: [ 0, 0 ] },
4136
4157
  deductionByLateLogin: { $ifNull: [ 0, 0 ] },
4137
4158
  startTime: 1,
4159
+ parsedDate: {
4160
+ $dateFromString: {
4161
+ dateString: '$fileDate',
4162
+ format: '%d-%m-%Y',
4163
+ },
4164
+ },
4138
4165
  },
4139
4166
  },
4140
4167
  {
@@ -4150,6 +4177,7 @@ export async function getUserAuditCount( req, res ) {
4150
4177
  totalTimeSpent: { $sum: '$timeSpent' },
4151
4178
  fileDate: { $first: '$fileDate' },
4152
4179
  fileDateISO: { $first: '$fileDateISO' },
4180
+ week: { $first: '$parsedDate' },
4153
4181
  userId: { $first: '$userId' },
4154
4182
  minimumTarget: { $first: '$minimumTarget' },
4155
4183
  afterCountCredit: { $first: '$afterCountCredit' },
@@ -4162,6 +4190,33 @@ export async function getUserAuditCount( req, res ) {
4162
4190
  deductionByLateLogin: { $first: '$deductionByLateLogin' },
4163
4191
  },
4164
4192
  },
4193
+ {
4194
+ $lookup: {
4195
+ from: 'auditUsers',
4196
+ let: { userId: '$userId', week: { $week: '$parsedDate' } }, // Assuming userId is the linking field
4197
+ pipeline: [
4198
+ {
4199
+ $match: {
4200
+ $expr: {
4201
+ $and: [
4202
+ { $eq: [ '$userId', '$$userId' ] },
4203
+ { $eq: [ '$week', '$$week' ] },
4204
+ ],
4205
+ },
4206
+ },
4207
+ },
4208
+ {
4209
+ $project: {
4210
+ _id: 0, // Exclude _id if not needed
4211
+ fileDate: 1,
4212
+ lateLogin: { $size: '$fileDate' },
4213
+ },
4214
+ },
4215
+ ],
4216
+ as: 'auditUsers',
4217
+ },
4218
+ },
4219
+ { $unwind: { path: '$auditUsers', preserveNullAndEmptyArrays: true } },
4165
4220
  {
4166
4221
  $project: {
4167
4222
  totalCount: 1,
@@ -4226,8 +4281,11 @@ export async function getUserAuditCount( req, res ) {
4226
4281
  ] },
4227
4282
  deductionByLateLogin: {
4228
4283
  $cond: [
4229
- { $gt: [ '$startTime', lateLogin ] },
4230
- { $multiply: [ reduce, '$totalEarn' ] }, '$deductionByLateLogin',
4284
+ { $and: [
4285
+ { $gt: [ '$auditUsers.lateLogin', 2 ] },
4286
+ { $eq: [ '$fileDate', dayjs( yesterday ).format( 'DD-MM-YYYY' ) ] },
4287
+ ] },
4288
+ { $multiply: [ reduce, '$totalEarn' ] }, 0,
4231
4289
  ],
4232
4290
  },
4233
4291
  mappingperc: 1,
@@ -4339,15 +4397,27 @@ export async function getUserAuditCount( req, res ) {
4339
4397
  },
4340
4398
  },
4341
4399
  ];
4400
+
4342
4401
  const consolidatedData = await aggregateUserAuditCount( query );
4343
4402
  if ( consolidatedData.length == 0 ) {
4344
4403
  return res.sendError( 'No Data found', 204 );
4345
4404
  }
4346
- const result = await createAuditUserWallet( consolidatedData );
4405
+
4406
+ const operations = consolidatedData.map( ( record ) => ( {
4407
+ updateOne: {
4408
+ filter: { userEmail: record.userEmail, fileDate: record.fileDate }, // Match criteria
4409
+ update: {
4410
+ $set: record,
4411
+ },
4412
+ upsert: true, // Perform upsert
4413
+ },
4414
+ } ) );
4415
+
4416
+ const result = await bulkWriteAuditUserWallet( operations );
4347
4417
  if ( result.length == 0 ) {
4348
4418
  return res.sendError( 'No Record Inserted', 500 );
4349
4419
  }
4350
- if ( i == differenceInDays ) {
4420
+ if ( i == differenceInDays+1 ) {
4351
4421
  return res.sendSuccess( { message: 'Inserted successfully!' } );
4352
4422
  }
4353
4423
  }
@@ -4957,5 +5027,3 @@ export async function convertTimestampToDateTime( timestamp ) {
4957
5027
  const seconds = ( '0' + adjustedDate.getSeconds() ).slice( -2 );
4958
5028
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
4959
5029
  }
4960
-
4961
-
@@ -5,6 +5,10 @@ export function createAuditUserWallet( record ) {
5
5
  return auditUserWalletModel.create( record );
6
6
  };
7
7
 
8
+ export function bulkWriteAuditUserWallet( record ) {
9
+ return auditUserWalletModel.bulkWrite( record );
10
+ };
11
+
8
12
  export function findOneAuditUserWallet( record, fields, sort = { createdAt: -1 } ) {
9
13
  return auditUserWalletModel.findOne( record, fields ).sort( sort );
10
14
  };
@@ -10,7 +10,7 @@ export function findOneAuditUsers( query, fields ) {
10
10
 
11
11
  export function updateOneAuditUsers( query, record ) {
12
12
  return auditUsersModel.updateOne( query,
13
- { $inc: lateLogin,
13
+ { $addToSet: record.fileDate,
14
14
  $setOnInsert: record }, { upsert: true } );
15
15
  };
16
16