tango-app-api-audit 1.0.3 → 1.0.5

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/index.js CHANGED
@@ -3,8 +3,9 @@
3
3
  import { auditRouter } from './src/routes/audit.routes.js';
4
4
  import { auditMetricsRouter } from './src/routes/auditMetrics.routes.js';
5
5
  import { auditMetricsDocs } from './src/docs/auditMetrics.docs.js';
6
+ import { auditDocs } from './src/docs/audit.docs.js';
6
7
 
7
8
 
8
- export { auditRouter, auditMetricsRouter, auditMetricsDocs };
9
+ export { auditRouter, auditMetricsRouter, auditMetricsDocs, auditDocs };
9
10
 
10
11
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tango-app-api-audit",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "audit & audit metrics apis",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -1,12 +1,12 @@
1
1
  import { chunkArray, getJsonFileData, getUTC, listFileByPath, listFileWithoutLimit, signedUrl } from 'tango-app-api-middleware';
2
- import { aggregateUserAudit, createUserAudit } from '../service/userAudit.service.js';
2
+ import { aggregateUserAudit, createUserAudit, findOneUserAudit, updateOneUserAudit } from '../service/userAudit.service.js';
3
3
  import { aggregateAssignAudit, updateOneAssignAudit } from '../service/assignAudit.service.js';
4
4
  import { findOneUser } from '../service/user.service.js';
5
5
  import dayjs from 'dayjs';
6
6
  import { aggregateClient } from '../service/client.service.js';
7
7
  import { logger } from 'tango-app-api-middleware';
8
8
  import { sqsReceive } from 'tango-app-api-middleware/src/utils/aws/sqs.js';
9
- import { createStoreAudit } from '../service/storeAudit.service.js';
9
+ import { createStoreAudit, updateOneStoreAudit } from '../service/storeAudit.service.js';
10
10
 
11
11
  export async function getAuditFile( req, res ) {
12
12
  try {
@@ -742,3 +742,70 @@ export async function getQueueDetailList( req, res ) {
742
742
  return res.sendError( error );
743
743
  }
744
744
  }
745
+
746
+ export async function saveDraft( req, res ) {
747
+ try {
748
+ const inputData = req.body;
749
+ const getUserAuditData = await findOneUserAudit( { _id: inputData.auditId } );
750
+ if ( !getUserAuditData ) {
751
+ return res.sendError( 'No Data Found', 204 );
752
+ }
753
+ if ( getUserAuditData.auditStatus == 'skipped' ) {
754
+ return res.sendError( 'File Assigned to Someone else', 203 );
755
+ }
756
+
757
+ const userQuery = {
758
+ _id: inputData.auditId,
759
+ };
760
+ let userRecord ={
761
+ isDraft: true,
762
+ auditStatus: 'drafted',
763
+ };
764
+ const storeQuery ={
765
+ storeId: inputData.storeId,
766
+ fileDate: inputData.fileDate,
767
+ };
768
+
769
+ let storeRecord ={
770
+ status: 'drafted',
771
+ };
772
+
773
+ if ( getUserAuditData?.startTime ) {
774
+ const isoDate = getUserAuditData.startTime;
775
+ const currentTime = dayjs();
776
+ const isoDateTime = dayjs( isoDate );
777
+ const timeDifferenceInMinutes = currentTime.diff( isoDateTime, 'seconds' );
778
+
779
+ userRecord.timeSpent=timeDifferenceInMinutes;
780
+
781
+ storeRecord.timeSpent = timeDifferenceInMinutes;
782
+ }
783
+
784
+ if ( inputData.userCommands ) {
785
+ userRecord.userCommands = inputData.userCommands;
786
+ const logData = {
787
+ userId: req.user._id,
788
+ userName: req.user.userName,
789
+ logType: 'audit',
790
+ logSubType: 'auditDraft',
791
+ logData: {
792
+ auditType: inputData.auditType,
793
+ storeId: inputData.storeId,
794
+ clientName: inputData.queueName,
795
+ beforeCount: inputData.totalCount,
796
+ afterCount: inputData.customerCount,
797
+ auditId: inputData.auditId,
798
+ },
799
+ };
800
+ logger.info( { logData: logData } ); // need to insert to os
801
+ }
802
+ logger.info( { userRecord: userRecord, storeRecord: storeRecord } );
803
+ await updateOneUserAudit( userQuery, userRecord );
804
+ await updateOneStoreAudit( storeQuery, storeRecord );
805
+ return res.sendSuccess( inputData );
806
+ } catch ( error ) {
807
+ const err = error.message || 'Internal Server Error';
808
+ logger.error( { error: error, message: req.body, function: 'saveDraft' } );
809
+ return res.sendError( err, 500 );
810
+ }
811
+ }
@@ -245,8 +245,13 @@ export async function clientMetrics( req, res ) {
245
245
  let filter = [
246
246
  { fileDateISO: { $gte: dateRange.start } },
247
247
  { fileDateISO: { $lte: dateRange.end } },
248
- { clientId: { $in: inputData.filterByClient } },
248
+
249
249
  ];
250
+ if ( inputData?.filterByClient?.length> 0 ) {
251
+ filter.push(
252
+ { clientId: { $in: inputData.filterByClient } },
253
+ );
254
+ }
250
255
  const query = [
251
256
  {
252
257
  $match: {
@@ -377,7 +382,7 @@ export async function clientMetrics( req, res ) {
377
382
  'File Date': '$fileDate',
378
383
  'Client Name': '$clientName',
379
384
  'Client Id': '$clientId',
380
- 'Completed Percentage': '$completionPercentage' + ' %',
385
+ 'Completed Percentage': '$completionPercentage',
381
386
  'Installed Stores': '$installedStore',
382
387
  'Audit Stores': '$totalFilesCount',
383
388
  'Inprogress Stores': '$inprogressStoresCount',
@@ -386,9 +391,6 @@ export async function clientMetrics( req, res ) {
386
391
  'Status': '$clientStatus',
387
392
  },
388
393
  },
389
- {
390
-
391
- },
392
394
  );
393
395
  }
394
396
 
@@ -1,4 +1,4 @@
1
- import { getFileSchema } from '../dtos/audit.dtos.js';
1
+ import { getFileSchema, saveDraftSchema } from '../dtos/audit.dtos.js';
2
2
  import j2s from 'joi-to-swagger';
3
3
 
4
4
  export const auditDocs = {
@@ -36,4 +36,26 @@ export const auditDocs = {
36
36
  },
37
37
  },
38
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
+ },
39
61
  };
@@ -13,6 +13,29 @@ export const getFileValid = {
13
13
  query: getFileSchema,
14
14
  };
15
15
 
16
+ export const saveDraftSchema = joi.object(
17
+ {
18
+ userCommands: joi.string().optional(),
19
+ storeId: joi.string().required(),
20
+ auditId: joi.string().required(),
21
+ auditType: joi.string().required(),
22
+ fileDate: joi.string().required(),
23
+ queueName: joi.string().required(),
24
+ totalCount: joi.number().required(),
25
+ junkCount: joi.number().optional(),
26
+ junk: joi.array().optional(),
27
+ employeeCount: joi.number().optional(),
28
+ employee: joi.array().optional(),
29
+ customerCount: joi.number().optional(),
30
+ customer: joi.array().optional(),
31
+ retagCount: joi.number().optional(),
32
+ },
33
+ );
34
+
35
+ export const saveDraftValid = {
36
+ body: saveDraftSchema,
37
+ };
38
+
16
39
  export const getQueueDetailListSchema = joi.object(
17
40
  {
18
41
  searchValue: joi.string().optional(),
@@ -47,7 +47,7 @@ export const clientMetricsSchema = joi.object(
47
47
  toDate: joi.string().required(),
48
48
  searchValue: joi.string().optional(),
49
49
  filterByClient: joi.array().required(),
50
- status: joi.array().optional(),
50
+ filterBystatus: joi.array().optional(),
51
51
  sortColumnName: joi.string().optional(),
52
52
  sortBy: joi.number().optional(),
53
53
  limit: joi.number().optional(),
@@ -1,12 +1,13 @@
1
1
  import express from 'express';
2
2
  import { isExistsQueue } from '../validation/audit.validation.js';
3
3
  import { isAllowedSessionHandler, validate } from 'tango-app-api-middleware';
4
- import { getAuditFile, getQueueDetailList } from '../controllers/audit.controllers.js';
5
- import { getFileValid, getQueueDetailListValid } from '../dtos/audit.dtos.js';
4
+ import { getAuditFile, getQueueDetailList, saveDraft } from '../controllers/audit.controllers.js';
5
+ import { getFileValid, getQueueDetailListValid, saveDraftValid } from '../dtos/audit.dtos.js';
6
6
 
7
7
  export const auditRouter = express.Router();
8
8
 
9
9
  auditRouter.get( '/get-file', validate( getFileValid ), isAllowedSessionHandler, isExistsQueue, getAuditFile );
10
+ auditRouter.post( '/save-draft', validate( saveDraftValid ), isAllowedSessionHandler, saveDraft );
10
11
  auditRouter.get( '/get-queue-detail-list', validate( getQueueDetailListValid ), isAllowedSessionHandler, getQueueDetailList );
11
12
 
12
13
  export default auditRouter;
@@ -4,5 +4,5 @@ export function aggregateAssignAudit( query ) {
4
4
  return assignAuditModel.aggregate( query, { collation: { locale: 'en', strength: 2 } } );
5
5
  }
6
6
  export function updateOneAssignAudit( query, record ) {
7
- return assignAuditModel.updateOne( query, record );
7
+ return assignAuditModel.updateOne( query, { $set: record } );
8
8
  }
@@ -1,7 +1,7 @@
1
1
  import storeAuditModel from 'tango-api-schema/schema/storeAudit.model.js';
2
2
 
3
3
  export function updateOneStoreAudit( query, record ) {
4
- return storeAuditModel.updateOne( query, record );
4
+ return storeAuditModel.updateOne( query, { $set: record } );
5
5
  }
6
6
 
7
7
  export function createStoreAudit( record ) {
@@ -6,9 +6,13 @@ export function aggregateUserAudit( query ) {
6
6
  }
7
7
 
8
8
  export function updateOneUserAudit( query, record ) {
9
- return userAuditModel.updateOne( query, record );
9
+ return userAuditModel.updateOne( query, { $set: record } );
10
10
  }
11
11
 
12
12
  export function createUserAudit( record ) {
13
13
  return userAuditModel.create( record );
14
14
  }
15
+
16
+ export function findOneUserAudit( query, fields ) {
17
+ return userAuditModel.findOne( query, fields );
18
+ }