tango-app-api-audit 3.5.26 → 3.5.28

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.5.26",
3
+ "version": "3.5.28",
4
4
  "description": "audit & audit metrics apis",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -1131,32 +1131,125 @@ export async function summaryList( req, res ) {
1131
1131
  }
1132
1132
 
1133
1133
 
1134
- if ( inputData.keyType === 'rm' ) {
1135
- return res.sendSuccess( { result: [
1134
+ if ( inputData.keyType === 'rm' || inputData.keyType === 'cluster' ) {
1135
+ const clusterQuery =[
1136
+ { $match: {
1137
+ $and: [
1138
+ { 'Teamlead.email': { $in: inputData.RMList } },
1139
+ { clientId: { $eq: inputData?.clientId } },
1140
+ ],
1141
+ },
1142
+ },
1143
+ { $unwind: { path: '$Teamlead', preserveNullAndEmptyArrays: true } },
1144
+ { $unwind: { path: '$stores', preserveNullAndEmptyArrays: true } },
1136
1145
  {
1137
- RMName: '',
1138
- clusterCount: '',
1139
- storeCount: '',
1140
- totalFiles: 0,
1141
- completedFiles: 0,
1142
- tangoScore: 0,
1143
- trustScore: 0,
1144
- },
1145
-
1146
- ], count: 1 } );
1147
- } else if ( inputData.keyType === 'cluster' ) {
1148
- return res.sendSuccess( { result: [
1146
+ $group: {
1147
+ _id: { clusterName: '$clusterName', RMName: '$Teamlead.email' },
1148
+ clusterName: { $first: '$clusterName' },
1149
+ RMName: { $first: '$Teamlead.email' },
1150
+ store: { $addToSet: '$stores.storeId' },
1151
+
1152
+ },
1153
+ },
1154
+ { $project: {
1155
+ RMName: 1,
1156
+ clusterName: 1,
1157
+ storesCount: { $size: '$store' },
1158
+ store: 1,
1159
+ } },
1160
+ ];
1161
+ const RMQuery =[
1162
+ { $match: {
1163
+ $and: [
1164
+ {
1165
+ 'Teamlead.email': { $in: inputData.RMList },
1166
+ },
1167
+ {
1168
+ 'clientId': { $eq: inputData?.clientId },
1169
+ },
1170
+ ],
1171
+ },
1172
+ },
1173
+ { $unwind: { path: '$Teamlead', preserveNullAndEmptyArrays: true } },
1174
+ { $unwind: { path: '$stores', preserveNullAndEmptyArrays: true } },
1149
1175
  {
1150
- RMName: '',
1151
- clusterName: '',
1152
- storeName: '',
1153
- totalFiles: 0,
1154
- completedFiles: 0,
1155
- tangoScore: 0,
1156
- trustScore: 0,
1176
+ $group: {
1177
+ _id: '$Teamlead.email',
1178
+ clusterName: { $first: '$clusterName' },
1179
+ RMName: { $first: '$Teamlead.email' },
1180
+ store: { $addToSet: '$stores.storeId' },
1181
+
1182
+ },
1157
1183
  },
1184
+ { $project: {
1185
+ RMName: 1,
1186
+ clusterName: 1,
1187
+ storesCount: { $size: '$store' },
1188
+ store: 1,
1189
+ } },
1190
+ ];
1191
+ const getCluster = await aggregateClusters( inputData.keyType === 'rm'?RMQuery: clusterQuery );
1192
+ logger.info( { getCluster: getCluster } );
1193
+ if ( !getCluster || getCluster?.length == 0 ) {
1194
+ return res.sendError( 'No data', 204 );
1195
+ }
1196
+ const temp =[];
1197
+ for ( const [ i, cluster ] of getCluster.entries() ) {
1198
+ logger.info( { cluster: cluster } );
1199
+ temp.push( cluster );
1200
+ const getClusterQuery= {
1201
+ 'size': 0,
1202
+ 'query': {
1203
+ 'bool': {
1204
+ 'must': [
1205
+ { 'terms': { 'storeId.keyword': cluster?.store } },
1206
+ { 'term': { 'type.keyword': inputData?.type } },
1207
+ {
1208
+ 'range': {
1209
+ 'date': {
1210
+ 'gte': inputData?.fromDate,
1211
+ 'lte': inputData?.toDate,
1212
+ },
1213
+ },
1214
+ },
1215
+
1216
+ ],
1217
+ },
1218
+ },
1219
+ 'aggs': {
1220
+ 'total_count': {
1221
+ 'value_count': { 'field': '_id' },
1222
+ },
1223
+ 'avg_complianceScore': {
1224
+ 'avg': { 'field': 'ComplianceScore' },
1225
+ },
1226
+ 'audited_trustScore': {
1227
+ 'filter': { 'terms': { 'auditStatus.keyword': [ 'Audited', 'reAudited' ] } },
1228
+ 'aggs': {
1229
+ 'avg_trustScore': {
1230
+ 'avg': { 'field': 'trustScore' },
1231
+ },
1232
+ 'totalCount': {
1233
+ 'value_count': { 'field': '_id' },
1234
+ },
1235
+ },
1236
+ },
1237
+ },
1238
+ };
1239
+ const getResponse = await getOpenSearchData( openSearch.EyeTestInput, getClusterQuery );
1240
+ logger.info( { getResponse: getResponse } );
1241
+ if ( getResponse?.body?.aggregations ) {
1242
+ delete temp[i].store;
1243
+ delete temp[i]._id;
1244
+ temp[i].totalFiles = getResponse?.body?.aggregations?.total_count?.value || 0,
1245
+ temp[i].completedFiles=getResponse?.body?.aggregations?.audited_trustScore?.totalCount?.value || 0;
1246
+ temp[i].complianceScore = getResponse?.body?.aggregations?.avg_complianceScore?.value || 0;
1247
+ temp[i].trustScore =getResponse?.body?.aggregations?.audited_trustScore?.totalCount?.value || 'NA';
1248
+ logger.info( { getResponse: getResponse, ms: '........1' } );
1249
+ }
1250
+ }
1158
1251
 
1159
- ], count: 1 } );
1252
+ return res.sendSuccess( { result: temp, count: getCluster?.length || 0 } );
1160
1253
  } else {
1161
1254
  let search ={
1162
1255
  'must': filter,
@@ -1326,9 +1419,19 @@ export async function summaryList( req, res ) {
1326
1419
  if ( element?._source?.userId&&element?._source?.userId!='' ) {
1327
1420
  userName = await findOneUser( { _id: element?._source?.userId }, { _id: 0, userName: 1 } );
1328
1421
  }
1422
+
1423
+ let RMName = '';
1424
+ let clusterName='';
1425
+ if ( element?._source?.storeId&&element?._source?.storeId!='' ) {
1426
+ const getCluster = await findOneClusters( { 'stores.storeId': element?._source?.storeId }, { Teamlead: 1, clusterName: 1 } );
1427
+ RMName =( getCluster && getCluster.Teamlead?.length > 0 )? getCluster?.Teamlead?.[0]?.email : '';
1428
+ clusterName = getCluster?.clusterName;
1429
+ }
1329
1430
  if ( element?._source?.type == 'physical' ) {
1330
1431
  exportData.push( {
1331
1432
  'Date': dayjs( element?._source?.date ).format( 'D MMM, YYYY' ),
1433
+ 'RM Name': RMName,
1434
+ 'Cluster Name': clusterName,
1332
1435
  'Store Name': element?._source?.storeName,
1333
1436
  'Store ID': element?._source?.storeId || 'Un Assigned',
1334
1437
  'Store Name': element?._source?.storeName ||'Un Assigned',
@@ -1346,6 +1449,8 @@ export async function summaryList( req, res ) {
1346
1449
  } else {
1347
1450
  exportData.push( {
1348
1451
  'Date': dayjs( element?._source?.date ).format( 'D MMM, YYYY' ),
1452
+ 'Cluster Name': clusterName,
1453
+ 'Store Name': element?._source?.storeName,
1349
1454
  'Store Name': element?._source?.storeName,
1350
1455
  'Store ID': element?._source?.storeId || 'Un Assigned',
1351
1456
  'Store Name': element?._source?.storeName ||'Un Assigned',
@@ -146,14 +146,7 @@ export const summaryListSchema = joi.object( {
146
146
  'any.required': 'Type is required',
147
147
  'string.empty': 'Type cannot be empty',
148
148
  } ),
149
- clientId: joi.string().when( '$userType', {
150
- is: 'tango',
151
- then: joi.required().messages( {
152
- 'any.required': 'clientId is required',
153
- 'string.base': 'clientId must be a string',
154
- } ),
155
- otherwise: joi.optional(),
156
- } ),
149
+ clientId: joi.string().required(),
157
150
  demographics: joi.array().optional(),
158
151
  RMList: joi.array().items(
159
152
  joi.string().required(),
@@ -5,7 +5,7 @@ export function aggregateClusters( query ) {
5
5
  return clusterModel.aggregate( query, { collation: { locale: 'en', strength: 2 } } );
6
6
  }
7
7
 
8
- export function findClusters( query ) {
8
+ export function findClusters( query, field ) {
9
9
  return clusterModel.find( query, field );
10
10
  }
11
11
 
@@ -3,12 +3,14 @@ import { logger } from 'tango-app-api-middleware';
3
3
  export async function roleValidation( req, res, next ) {
4
4
  try {
5
5
  const userRole = req?.user?.role;
6
+ req.body.clientId = req?.user?.userType == 'tango'? req?.body?.clientId : req?.user?.clientId;
6
7
  switch ( userRole ) {
7
8
  case 'user':
8
- delete req?.body?.RMList;
9
+ req.body.RMList = [ req?.user?.email ];
9
10
  break;
10
11
  case 'admin':
11
- delete req?.body?.RMList;
12
+ req.body.RMList = [ req?.user?.email ];
13
+ // delete req?.body?.RMList;
12
14
  break;
13
15
  case 'superadmin':
14
16
  break;