tango-app-api-audit 1.0.31 → 1.0.32

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/.eslintrc.cjs CHANGED
@@ -36,7 +36,7 @@ module.exports = {
36
36
  'no-unused-vars': 'error',
37
37
  'new-cap': [ 'error', { 'newIsCap': true, 'capIsNew': false } ],
38
38
  'prefer-const': 'off',
39
- 'no-console': 'error',
39
+ // 'no-console': 'error',
40
40
  },
41
41
  };
42
42
 
package/index.js CHANGED
@@ -10,7 +10,7 @@ import { storeRouter } from './src/routes/store.routes.js';
10
10
  import { storeDocs } from './src/docs/store.docs.js';
11
11
  import { userDocs } from './src/docs/user.docs.js';
12
12
  import { userRouter } from './src/routes/user.routes.js';
13
-
14
- export { auditRouter, auditMetricsRouter, auditMetricsDocs, auditDocs, clientRouter, clientDocs, storeRouter, storeDocs, userRouter, userDocs };
13
+ import { zoneAuditRouter } from './src/routes/zone.routes.js';
14
+ export { auditRouter, auditMetricsRouter, auditMetricsDocs, auditDocs, clientRouter, clientDocs, storeRouter, storeDocs, userRouter, userDocs, zoneAuditRouter };
15
15
 
16
16
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tango-app-api-audit",
3
- "version": "1.0.31",
3
+ "version": "1.0.32",
4
4
  "description": "audit & audit metrics apis",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -24,8 +24,8 @@
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.0.138",
28
- "tango-app-api-middleware": "^3.1.27",
27
+ "tango-api-schema": "^2.0.143",
28
+ "tango-app-api-middleware": "^3.1.28",
29
29
  "winston": "^3.13.0",
30
30
  "winston-daily-rotate-file": "^5.0.0"
31
31
  },
@@ -395,7 +395,7 @@ export async function getAuditFile( req, res ) {
395
395
  };
396
396
 
397
397
  insertData = await createUserAudit( record );
398
- await createStoreAudit( storeRecord );
398
+ await createAssignZoneAudit( storeRecord );
399
399
  } else {
400
400
  insertData = userDetails[0];
401
401
  }
@@ -0,0 +1,255 @@
1
+ import { getUTC, insertOpenSearchData, listFileByPath, signedUrl, sqsReceive } from 'tango-app-api-middleware';
2
+ import { aggregateZoneUserAudit, createZoneUserAudit } from '../service/zoneUserAudit.service.js';
3
+ import { logger } from 'tango-app-api-middleware';
4
+ import { aggregateAssignZoneAudit } from '../service/assignZoneAudit.service.js';
5
+ import { createStoreZoneAudit } from '../service/storeZoneAudit.service.js';
6
+ import { findOneUser } from '../service/user.service.js';
7
+ import { findOneStore } from '../service/store.service.js';
8
+
9
+ export async function getZoneAuditFile( req, res ) {
10
+ try {
11
+ const bucket = JSON.parse( process.env.BUCKET );
12
+ const inputData = req.query;
13
+ const data = await getUTC( new Date(), new Date() );
14
+ const start = data.start;
15
+ const end = data.end;
16
+ const fetchData = {};
17
+ let msg = {};
18
+
19
+ const userQuery = [
20
+ {
21
+ $match: {
22
+ $and: [
23
+ { userId: req.user._id },
24
+ { queueName: inputData.queueName },
25
+ { auditStatus: { $nin: [ 'completed', 'skipped' ] } },
26
+ { createdAt: { $gte: start } },
27
+ { createdAt: { $lte: end } },
28
+ { moduleType: 'zone' },
29
+ ],
30
+ },
31
+ },
32
+ {
33
+ $sort: { createdAt: -1 },
34
+ },
35
+ {
36
+ $limit: 1,
37
+ },
38
+ ];
39
+ const userDetails = await aggregateZoneUserAudit( userQuery );
40
+ console.log( userDetails, 'zoneAuditStatus==>' );
41
+ const zoneAuditStatus =
42
+ userDetails && userDetails.length > 0 ? userDetails[0].auditStatus : null;
43
+
44
+ switch ( zoneAuditStatus ) {
45
+ case null:
46
+
47
+ const query = [
48
+ {
49
+ $match: {
50
+ $and: [
51
+ { userId: { $eq: req.user._id } },
52
+ { isCompleted: { $eq: false } },
53
+ { queueName: inputData.queueName },
54
+ ],
55
+ },
56
+ },
57
+ {
58
+ $sort: { createdAt: -1 },
59
+ },
60
+ {
61
+ $limit: 1,
62
+ },
63
+ ];
64
+
65
+ const userAssign = await aggregateAssignZoneAudit( query );
66
+ console.log( userAssign, 'userAssign' );
67
+ if ( userAssign.length > 0 ) {
68
+ logger.info( 'Hit in user assign', { inputData } );
69
+ } else {
70
+ logger.info( 'Hit in new file', { inputData } );
71
+ const consumer = await sqsReceive( inputData.queueName );
72
+ console.log( consumer, 'consumer' );
73
+ if ( !consumer ) {
74
+ logger.error( {
75
+ error: consumer,
76
+ function: 'getAuditFile',
77
+ message: 'SQS Receive queue is empty',
78
+ } );
79
+ return res.sendError( `${inputData.queueName} is Empty`, 204 );
80
+ }
81
+ msg = JSON.parse( consumer );
82
+ }
83
+ break;
84
+
85
+ default:
86
+ msg = {
87
+ store_id: userDetails[0].storeId,
88
+ curr_date: userDetails[0].fileDate,
89
+ audit_type: userDetails[0].auditType,
90
+ total_count: userDetails[0].beforeCount,
91
+ zone_id: userDetails[0].zoneName,
92
+ };
93
+ break;
94
+ }
95
+ /** *
96
+ *
97
+ * Reaudit pending
98
+ */
99
+
100
+
101
+ const storeId = msg.store_id;
102
+ const fileDate = msg.curr_date;
103
+ const files = [];
104
+ let insertData = {};
105
+ fetchData.Bucket = bucket.auditInput; // need to change
106
+ fetchData.file_path = `${fileDate}/${storeId}/${msg.zone_id}/`;
107
+ if ( inputData.limit ) {
108
+ fetchData.MaxKeys = inputData.limit;
109
+ }
110
+ if ( inputData.nextId ) {
111
+ fetchData.ContinuationToken = decodeURIComponent( inputData.nextId );
112
+ }
113
+ const list = await listFileByPath( fetchData );
114
+
115
+ const folderPath = list.data;
116
+ const nextQuery = list.pageToken;
117
+ logger.info( { list: list } );
118
+ if ( folderPath?.length > 0 ) {
119
+ for ( let i = 0; i < folderPath.length; i++ ) {
120
+ const img = folderPath[i].Key.split( '/' );
121
+ const image = img[3].split( '.' );
122
+ const indexes = image[0].split( '_' );
123
+ fetchData.file_path = folderPath[i].Key;
124
+
125
+ const data = await signedUrl( fetchData );
126
+ const mapimg = {
127
+ img_path: data,
128
+ img_name: indexes[1],
129
+ img_id: image[0],
130
+ };
131
+ files.push( {
132
+ img_path: data,
133
+ img_name: indexes[1],
134
+ img_id: image[0],
135
+ selected: false,
136
+ dropped: false,
137
+ count: 1,
138
+ mappedid: [ mapimg ],
139
+ } );
140
+ }
141
+ if ( userDetails.length === 0 ) {
142
+ const [ day, month, year ] = msg.curr_date.split( '-' );
143
+ const temp = `${year}-${month}-${day}`;
144
+ let start = new Date( temp );
145
+ logger.info( { start: start, date: msg.curr_date } );
146
+ const userTimezoneOffset = start.getTimezoneOffset() * 60000;
147
+ start = new Date( start.getTime() - userTimezoneOffset );
148
+ start.setUTCHours( 0, 0, 0, 0 );
149
+
150
+ const record = {
151
+ userId: req.user._id,
152
+ storeId: msg.store_id,
153
+ clientId: msg.store_id.split( '-' )[0],
154
+ auditType: 'Audit',
155
+ fileDate: msg.curr_date,
156
+ queueName: inputData.queueName,
157
+ beforeCount: msg.total_count || files.length,
158
+ zoneAuditStatus: 'inprogress',
159
+ zoneName: msg.zone_id,
160
+ fileDateISO: start,
161
+ timeSpent: 0,
162
+ startTime: new Date(),
163
+ moduleType: 'zone',
164
+ };
165
+
166
+ const storeRecord = {
167
+ userId: req.user._id,
168
+ storeId: msg.store_id,
169
+ clientId: msg.store_id.split( '-' )[0],
170
+ auditType: 'Audit',
171
+ fileDate: msg.curr_date,
172
+ queueName: inputData.queueName,
173
+ beforeCount: msg.total_count || files.length,
174
+ status: 'inprogress',
175
+ fileDateISO: start,
176
+ timeSpent: 0,
177
+ zoneName: msg.zone_id,
178
+
179
+ };
180
+ console.log( record );
181
+ console.log( storeRecord );
182
+ insertData = await createZoneUserAudit( record );
183
+ await createStoreZoneAudit( storeRecord );
184
+ } else {
185
+ insertData = userDetails[0];
186
+ console.log( insertData );
187
+ }
188
+ } else {
189
+ logger.error( {
190
+ error: { folderPath: folderPath, nextQuery: nextQuery },
191
+ function: 'getAuditFile',
192
+ message: 'Bucket image data not availale',
193
+ } );
194
+ return res.sendError( 'Bucket is Empty', 204 );
195
+ }
196
+ const userdata = await findOneUser( { _id: insertData.userId } );
197
+ const storeQuery = {
198
+ storeId: storeId,
199
+ };
200
+ const storeFields = {
201
+ storeName: 1,
202
+ address: '$storeProfile.address',
203
+ };
204
+ const storeInfo = await findOneStore( storeQuery, storeFields );
205
+ if ( inputData.nextId ) {
206
+ const logData = {
207
+ userId: insertData.userId,
208
+ userName: userdata.name,
209
+ logType: 'auditZone',
210
+ logSubType: 'auditZoneStart',
211
+ logData: {
212
+ fileDate: insertData.fileDate,
213
+ auditType: insertData.auditType,
214
+ storeId: storeId,
215
+ clientName: inputData.queueName,
216
+ auditId: insertData._id,
217
+ },
218
+ };
219
+ await insertOpenSearchData( openSearch.activityLog, logData );
220
+ }
221
+ logger.info( { storeInfo: storeInfo } );
222
+
223
+ return res.sendSuccess( {
224
+ result: files,
225
+ count: msg.total_count,
226
+ storeId: storeId,
227
+ storeName: storeInfo?.storeName,
228
+ address: storeInfo?.address,
229
+ zoneName: msg.zone_id,
230
+ file: {
231
+ queueName: inputData.queueName,
232
+ store_id: storeId,
233
+ file_date: fileDate,
234
+ type: 'Audit',
235
+ auditId: insertData._id,
236
+ userId: insertData.userId,
237
+ nextToken: nextQuery ? encodeURIComponent( nextQuery ) : null,
238
+ },
239
+ isDraft: insertData.isDraft,
240
+ } );
241
+ } catch ( error ) {
242
+ // {"store_id": "430-5", "curr_date": '08-08-2024', "total_count": 115, "zone_id": "HR"}
243
+ // {"store_id":"11-2043","audit_type":"ReAudit","curr_date":"07-08-2024","total_count":41,"before_count":88}
244
+ // {store_id: '430-5', curr_date: '08-08-2024', total_count: 115,zone_id:'HR'}
245
+ // {'store_id': "430-5", 'curr_date': "08-08-2024", 'total_count': 115,'zone_id':"HR"}
246
+ // {'store_id': '430-5', 'curr_date': '08-08-2024', 'total_count': 115,'zone_id':"HR"}
247
+ // {"store_id":"430-5","curr_date":"08-08-2024","total_count":115,"zone_id":"HR"}
248
+ logger.error( {
249
+ error: error,
250
+ message: req.query,
251
+ function: 'getZoneAuditFile',
252
+ } );
253
+ return res.sendError( error, 500 );
254
+ }
255
+ }
@@ -0,0 +1,152 @@
1
+ import j2s from 'joi-to-swagger';
2
+ import { getFileSchema } from '../dtos/zoneAudit.dtos.js';
3
+
4
+ export const zoneAuditDocs = {
5
+ '/v3/zone/get-file': {
6
+ get: {
7
+ tags: [ 'Zone' ],
8
+ description: 'Get a file via queue message',
9
+ operationId: 'get-file',
10
+ parameters: [
11
+ {
12
+ in: 'query',
13
+ name: 'queueName',
14
+ scema: j2s( getFileSchema ).swagger,
15
+ require: true,
16
+ },
17
+ {
18
+ in: 'query',
19
+ name: 'nextId',
20
+ scema: j2s( getFileSchema ).swagger,
21
+ require: false,
22
+ },
23
+ {
24
+ in: 'query',
25
+ name: 'limit',
26
+ scema: j2s( getFileSchema ).swagger,
27
+ require: false,
28
+ },
29
+ ],
30
+ responses: {
31
+ 200: { description: 'Successful' },
32
+ 401: { description: 'Unauthorized User' },
33
+ 422: { description: 'Field Error' },
34
+ 500: { description: 'Server Error' },
35
+ 204: { description: 'Not Found' },
36
+ },
37
+ },
38
+ },
39
+ // '/v3/audit/save-draft': {
40
+ // post: {
41
+ // tags: [ 'Audit' ],
42
+ // description: `save audited file. which is save in the mongo db`,
43
+ // operationId: 'save-draft',
44
+ // parameters: {},
45
+ // requestBody: {
46
+ // content: {
47
+ // 'application/json': {
48
+ // schema: j2s( saveDraftSchema ).swagger,
49
+ // },
50
+ // },
51
+ // },
52
+ // responses: {
53
+ // 200: { description: 'Successful' },
54
+ // 401: { description: 'Unauthorized User' },
55
+ // 422: { description: 'Field Error' },
56
+ // 500: { description: 'Server Error' },
57
+ // 204: { description: 'Not Found' },
58
+ // },
59
+ // },
60
+ // },
61
+ // '/v3/audit/get-drafted-data': {
62
+ // get: {
63
+ // tags: [ 'Audit' ],
64
+ // description: 'Get a saved file via DB',
65
+ // operationId: 'get-drafted-data',
66
+ // parameters: [
67
+ // {
68
+ // in: 'query',
69
+ // name: 'fileDate',
70
+ // scema: j2s( getDraftedDataSchema ).swagger,
71
+ // require: true,
72
+ // },
73
+ // {
74
+ // in: 'query',
75
+ // name: 'storeId',
76
+ // scema: j2s( getDraftedDataSchema ).swagger,
77
+ // require: false,
78
+ // },
79
+ // ],
80
+ // responses: {
81
+ // 200: { description: 'Successful' },
82
+ // 401: { description: 'Unauthorized User' },
83
+ // 422: { description: 'Field Error' },
84
+ // 500: { description: 'Server Error' },
85
+ // 204: { description: 'Not Found' },
86
+ // },
87
+ // },
88
+ // },
89
+ // '/v3/audit/save': {
90
+ // post: {
91
+ // tags: [ 'Audit' ],
92
+ // description: `submit the audit file. which is store in the bucket in the json format`,
93
+ // operationId: 'save',
94
+ // parameters: {},
95
+ // requestBody: {
96
+ // content: {
97
+ // 'application/json': {
98
+ // schema: j2s( saveSchema ).swagger,
99
+ // },
100
+ // },
101
+ // },
102
+ // responses: {
103
+ // 200: { description: 'The Audited file has been submited successfully ' },
104
+ // 401: { description: 'Unauthorized User' },
105
+ // 422: { description: 'Field Error' },
106
+ // 500: { description: 'Server Error' },
107
+ // 204: { description: 'Not Found' },
108
+ // },
109
+ // },
110
+ // },
111
+ // '/v3/audit/work-space': {
112
+ // get: {
113
+ // tags: [ 'Audit' ],
114
+ // description: 'queue wise info of audit files ',
115
+ // operationId: 'work-space',
116
+ // parameters: [
117
+ // {
118
+ // in: 'query',
119
+ // name: 'searchValue',
120
+ // scema: j2s( workSpaceSchema ).swagger,
121
+ // require: false,
122
+ // },
123
+ // {
124
+ // in: 'query',
125
+ // name: 'offset',
126
+ // scema: j2s( workSpaceSchema ).swagger,
127
+ // require: false,
128
+ // },
129
+ // {
130
+ // in: 'query',
131
+ // name: 'limit',
132
+ // scema: j2s( workSpaceSchema ).swagger,
133
+ // require: false,
134
+ // },
135
+ // {
136
+ // in: 'query',
137
+ // name: 'isExport',
138
+ // scema: j2s( workSpaceSchema ).swagger,
139
+ // require: false,
140
+ // },
141
+ // ],
142
+ // responses: {
143
+ // 200: { description: 'Successful' },
144
+ // 401: { description: 'Unauthorized User' },
145
+ // 422: { description: 'Field Error' },
146
+ // 500: { description: 'Server Error' },
147
+ // 204: { description: 'Not Found' },
148
+ // },
149
+ // },
150
+ // },
151
+
152
+ };
@@ -0,0 +1,15 @@
1
+ import joi from 'joi';
2
+
3
+
4
+ export const getFileSchema = joi.object(
5
+ {
6
+ queueName: joi.string().required(),
7
+ nextId: joi.string().optional().allow( '' ),
8
+ limit: joi.number().optional(),
9
+ },
10
+ );
11
+
12
+
13
+ export const getFileValid = {
14
+ query: getFileSchema,
15
+ };
@@ -0,0 +1,10 @@
1
+ import { Router } from 'express';
2
+ import { getFileValid } from '../dtos/zoneAudit.dtos.js';
3
+ import { isExistsQueue } from '../validation/audit.validation.js';
4
+ import { isAllowedSessionHandler, validate } from 'tango-app-api-middleware';
5
+ import { getZoneAuditFile } from '../controllers/zoneAudit.controller.js';
6
+
7
+ export const zoneAuditRouter=Router();
8
+
9
+
10
+ zoneAuditRouter.get( '/get-file', isAllowedSessionHandler, validate( getFileValid ), isExistsQueue, getZoneAuditFile );
@@ -0,0 +1,12 @@
1
+ import assignZoneAuditModel from 'tango-api-schema/schema/assignZoneAudit.model.js';
2
+
3
+ export function aggregateAssignZoneAudit( query ) {
4
+ return assignZoneAuditModel.aggregate( query, { collation: { locale: 'en', strength: 2 } } );
5
+ }
6
+ export function updateOneAssignZoneAudit( query, record ) {
7
+ return assignZoneAuditModel.updateOne( query, { $set: record } );
8
+ }
9
+
10
+ export function createAssignZoneAudit( query ) {
11
+ return assignZoneAuditModel.create( query );
12
+ }
@@ -0,0 +1,17 @@
1
+ import storeZoneAuditModel from 'tango-api-schema/schema/storeZoneAudit.model.js';
2
+
3
+ export function updateOneStoreZoneAudit( query, record ) {
4
+ return storeZoneAuditModel.updateOne( query, { $set: record } );
5
+ }
6
+
7
+ export function createStoreZoneAudit( record ) {
8
+ return storeZoneAuditModel.create( record );
9
+ }
10
+
11
+ export function aggregateStoreZoneAudit( query ) {
12
+ return storeZoneAuditModel.aggregate( query, { collation: { locale: 'en', strength: 2 } } );
13
+ }
14
+
15
+ export function findOneStoreZoneAudit( query, fields ) {
16
+ return storeZoneAuditModel.findOne( query, fields );
17
+ }
@@ -0,0 +1,17 @@
1
+ import dataModel from 'tango-api-schema';
2
+
3
+ export function aggregateZoneUserAudit( query ) {
4
+ return dataModel.userAuditModel.aggregate( query, { collation: { locale: 'en', strength: 2 } } );
5
+ }
6
+
7
+ export function updateOneZoneUserAudit( query, record ) {
8
+ return dataModel.userAuditModel.updateOne( query, { $set: record } );
9
+ }
10
+
11
+ export function createZoneUserAudit( record ) {
12
+ return dataModel.userAuditModel.create( record );
13
+ }
14
+
15
+ export function findOneZoneUserAudit( query, fields ) {
16
+ return dataModel.userAuditModel.findOne( query, fields );
17
+ }