tango-app-api-client 3.6.5-vms.2 → 3.6.5-vms.21

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
@@ -2,6 +2,7 @@
2
2
 
3
3
  import { clientRouter } from './src/routes/client.routes.js';
4
4
  import { clientDocs } from './src/docs/client.docs.js';
5
+ import { vmsauditRouter } from './src/routes/vmsAudit.routes.js';
5
6
 
6
- export { clientRouter, clientDocs };
7
+ export { clientRouter, clientDocs, vmsauditRouter };
7
8
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tango-app-api-client",
3
- "version": "3.6.5-vms.2",
3
+ "version": "3.6.5-vms.21",
4
4
  "description": "client",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -31,7 +31,7 @@
31
31
  "npm": "^10.9.1",
32
32
  "sharp": "^0.34.3",
33
33
  "swagger-ui-express": "^5.0.0",
34
- "tango-api-schema": "^2.4.19",
34
+ "tango-api-schema": "^2.5.2",
35
35
  "tango-app-api-middleware": "^3.6.0",
36
36
  "winston": "^3.11.0",
37
37
  "winston-daily-rotate-file": "^5.0.0"
@@ -2946,12 +2946,76 @@ export async function getrevopconfig( req, res ) {
2946
2946
  export async function updateFDConfig( req, res ) {
2947
2947
  try {
2948
2948
  const inputData = req.body;
2949
+ const { taggingLimitation, ...otherData } = inputData;
2949
2950
  const inputQuery = req.query;
2950
2951
  const query ={
2951
2952
  clientId: inputQuery.clientId,
2952
2953
  };
2954
+ if ( taggingLimitation ) {
2955
+ const newValues = taggingLimitation;
2956
+ const today = new Date().toISOString().split( 'T' )[0];
2957
+ const getUpdateData = await clientModel.updateOne(
2958
+ {
2959
+ 'clientId': inputQuery.clientId,
2960
+ 'footfallDirectoryConfigs.taggingLimitation.effectiveFrom': today,
2961
+ },
2962
+ {
2963
+ $set: {
2964
+ 'footfallDirectoryConfigs.taggingLimitation.$.values': newValues,
2965
+ },
2966
+ },
2967
+ );
2968
+
2969
+ if ( getUpdateData?.matchedCount === 0 ) {
2970
+ await clientModel.updateOne(
2971
+ { clientId: inputQuery.clientId },
2972
+ {
2973
+ $push: {
2974
+ 'footfallDirectoryConfigs.taggingLimitation': {
2975
+ effectiveFrom: today,
2976
+ values: newValues,
2977
+ },
2978
+ },
2979
+ },
2980
+ );
2981
+ }
2982
+ }
2983
+
2953
2984
  // update the footfall directory configuration data
2954
- let result = await updateOneClient( query, { footfallDirectoryConfigs: inputData } );
2985
+ let record = {};
2986
+ if ( otherData?.contactEmail ) {
2987
+ record['footfallDirectoryConfigs.contactEmail'] =otherData?.contactEmail;
2988
+ }
2989
+
2990
+ if ( otherData?.revision ) {
2991
+ record['footfallDirectoryConfigs.revision'] =otherData?.revision;
2992
+ }
2993
+
2994
+ if ( otherData?.allowImageView ) {
2995
+ record['footfallDirectoryConfigs.allowImageView'] =otherData?.allowImageView;
2996
+ }
2997
+
2998
+ if ( otherData?.allowTicketCreation ) {
2999
+ record['footfallDirectoryConfigs.allowTicketCreation'] =otherData?.allowTicketCreation;
3000
+ }
3001
+
3002
+ if ( otherData?.isAutoCloseEnable ) {
3003
+ record['footfallDirectoryConfigs.isAutoCloseEnable'] =otherData?.isAutoCloseEnable;
3004
+ }
3005
+
3006
+ if ( otherData?.autoCloseAccuracy ) {
3007
+ record['footfallDirectoryConfigs.autoCloseAccuracy'] =otherData?.autoCloseAccuracy;
3008
+ }
3009
+
3010
+ if ( otherData?.accuracyBreach ) {
3011
+ record['footfallDirectoryConfigs.accuracyBreach'] =otherData?.accuracyBreach;
3012
+ }
3013
+
3014
+ if ( otherData?.tangoReview ) {
3015
+ record['footfallDirectoryConfigs.tangoReview'] =otherData?.tangoReview;
3016
+ }
3017
+
3018
+ let result = await updateOneClient( query, record );
2955
3019
  if ( result?.acknowledged === true ) {
2956
3020
  return res.sendSuccess( result );
2957
3021
  } else {
@@ -2967,10 +3031,89 @@ export async function updateFDConfig( req, res ) {
2967
3031
  export async function getFDConfig( req, res ) {
2968
3032
  try {
2969
3033
  const inputData = req.query;
2970
- let result = await findOneClient( { clientId: inputData.clientId }, { footfallDirectoryConfigs: 1 } );
2971
- if ( result===null ) {
3034
+ // let result = await findOneClient( { clientId: inputData.clientId }, { footfallDirectoryConfigs: 1 } );
3035
+ const configQuery = [
3036
+ {
3037
+ $match: {
3038
+ clientId: inputData?.clientId,
3039
+ },
3040
+ },
3041
+
3042
+ // Convert all effectiveFrom to proper Date
3043
+ {
3044
+ $addFields: {
3045
+ taggingLimitationWithDate: {
3046
+ $map: {
3047
+ input: '$footfallDirectoryConfigs.taggingLimitation',
3048
+ as: 'item',
3049
+ in: {
3050
+ effectiveFrom: { $toDate: '$$item.effectiveFrom' },
3051
+ values: '$$item.values',
3052
+ },
3053
+ },
3054
+ },
3055
+ },
3056
+ },
3057
+
3058
+ // Filter items <= input date
3059
+ {
3060
+ $addFields: {
3061
+ matchedLimitation: {
3062
+ $filter: {
3063
+ input: '$taggingLimitationWithDate',
3064
+ as: 'item',
3065
+ cond: {
3066
+ $lte: [
3067
+ '$$item.effectiveFrom',
3068
+ { $toDate: inputData.dateString },
3069
+ ],
3070
+ },
3071
+ },
3072
+ },
3073
+ },
3074
+ },
3075
+
3076
+ // Sort DESC and pick ONLY top 1 -> latest effective record
3077
+ {
3078
+ $addFields: {
3079
+ effectiveLimitation: {
3080
+ $arrayElemAt: [
3081
+ {
3082
+ $slice: [
3083
+ {
3084
+ $sortArray: {
3085
+ input: '$matchedLimitation',
3086
+ sortBy: { effectiveFrom: -1 },
3087
+ },
3088
+ },
3089
+ 1,
3090
+ ],
3091
+ },
3092
+ 0,
3093
+ ],
3094
+ },
3095
+ },
3096
+ },
3097
+
3098
+ {
3099
+ $project: {
3100
+ 'config': 1,
3101
+ 'effectiveLimitation': 1,
3102
+ 'footfallDirectoryConfigs': 1,
3103
+
3104
+ },
3105
+ },
3106
+ ];
3107
+
3108
+
3109
+ const getData = await aggregateClient( configQuery );
3110
+ let result = getData[0];
3111
+ if ( !result || result===null ) {
2972
3112
  return res.sendError( 'no data found', 204 );
2973
3113
  }
3114
+ result.footfallDirectoryConfigs.taggingLimitation = result?.effectiveLimitation?.values;
3115
+ delete result.effectiveLimitation;
3116
+
2974
3117
  return res.sendSuccess( result );
2975
3118
  } catch ( error ) {
2976
3119
  const err = error.message || 'Internal Server Error';
@@ -2983,38 +3126,38 @@ export async function getFDConfig( req, res ) {
2983
3126
  export async function updateTaggingType( req, res ) {
2984
3127
  try {
2985
3128
  const inputData = req.body;
2986
-
2987
- const inputQuery = req.query; // up
2988
- const types = inputData?.taggingLimitation?.map( ( x ) => x.type );
2989
- // Step 1: remove existing items with same "type"
2990
- await clientModel.updateOne(
2991
- { clientId: inputQuery.clientId },
3129
+ const newValues = inputData.taggingLimitation;
3130
+ const today = new Date().toISOString().split( 'T' )[0];
3131
+ logger.info( { today, newValues } );
3132
+ const result = await clientModel.updateOne(
2992
3133
  {
2993
- $pull: {
2994
- 'footfallDirectoryConfigs.taggingLimitation': {
2995
- type: { $in: types },
2996
- },
2997
- },
3134
+ 'clientId': inputData?.clientId,
3135
+ 'footfallDirectoryConfigs.taggingLimitation.effectiveFrom': today,
2998
3136
  },
2999
- );
3000
-
3001
- // Remove duplicate entries by type
3002
- const uniqueData = uniqueByType( inputData.taggingLimitation );
3003
-
3004
- // Step 2: insert fresh new items
3005
- let result =await clientModel.updateOne(
3006
- { clientId: inputQuery.clientId },
3007
3137
  {
3008
- $addToSet: {
3009
- 'footfallDirectoryConfigs.taggingLimitation': {
3010
- $each: uniqueData,
3011
- },
3138
+ $set: {
3139
+ 'footfallDirectoryConfigs.taggingLimitation.$.values': newValues,
3012
3140
  },
3013
3141
  },
3014
3142
  );
3143
+ logger.info( { result } );
3144
+ if ( result?.matchedCount === 0 ) {
3145
+ await clientModel.updateOne(
3146
+ { clientId: inputData.clientId },
3147
+ {
3148
+ $push: {
3149
+ 'footfallDirectoryConfigs.taggingLimitation': {
3150
+ effectiveFrom: today,
3151
+ values: newValues,
3152
+ },
3153
+ },
3154
+ },
3155
+ );
3156
+ }
3157
+
3015
3158
 
3016
3159
  if ( result?.acknowledged === true ) {
3017
- return res.sendSuccess( result );
3160
+ return res.sendSuccess( 'Tagging limitation has been updated' );
3018
3161
  } else {
3019
3162
  return res.sendError( 'no data', 204 );
3020
3163
  }
@@ -3025,14 +3168,14 @@ export async function updateTaggingType( req, res ) {
3025
3168
  }
3026
3169
  }
3027
3170
 
3028
- function uniqueByType( arr ) {
3029
- const seen = new Set();
3030
- return arr.filter( ( item ) => {
3031
- if ( seen.has( item.type ) ) return false;
3032
- seen.add( item.type );
3033
- return true;
3034
- } );
3035
- }
3171
+ // function uniqueByType( arr ) {
3172
+ // const seen = new Set();
3173
+ // return arr.filter( ( item ) => {
3174
+ // if ( seen.has( item.type ) ) return false;
3175
+ // seen.add( item.type );
3176
+ // return true;
3177
+ // } );
3178
+ // }
3036
3179
 
3037
3180
  export async function createStoreRequest( req, res ) {
3038
3181
  try {
@@ -3050,11 +3193,11 @@ export async function createStoreRequest( req, res ) {
3050
3193
  const upsertRecord = await updateOneUpsertVmsStoreRequest( { storeId: inputData?.storeId, dateString: inputData?.dateString }, record );
3051
3194
  logger.info( { upsertRecord } );
3052
3195
  if ( upsertRecord?.upsertedCount === 1 ) {
3053
- return res.sendSuccess( 'The Request has been sent Successfully' );
3196
+ return res.sendTemp( 'The Request has been sent Successfully' );
3054
3197
  } else if ( upsertRecord?.matchedCount === 1 && upsertRecord?.modifiedCount === 1 ) {
3055
- return res.sendSuccess( 'The Request has been updated Successfully' );
3198
+ return res.sendTemp( 'The Request has been updated Successfully' );
3056
3199
  } else if ( upsertRecord?.matchedCount === 1 && upsertRecord?.modifiedCount === 0 ) {
3057
- return res.sendSuccess( 'The Request already exists and nothing to update' );
3200
+ return res.sendTemp( 'The Request already exists and nothing to update' );
3058
3201
  } else {
3059
3202
  return res.sendError( 'Internal Server Error', 500 );
3060
3203
  }
@@ -3096,6 +3239,7 @@ export async function listStoreRequest( req, res ) {
3096
3239
  {
3097
3240
  $project: {
3098
3241
  _id: 0,
3242
+ id: { $concat: [ '$storeId', '_', '$dateString' ] },
3099
3243
  storeName: 1,
3100
3244
  storeId: 1,
3101
3245
  dateString: 1,
@@ -0,0 +1,488 @@
1
+ import { logger, searchOpenSearchData, getOpenSearchById, updateOpenSearchData } from 'tango-app-api-middleware';
2
+ import { findOneStore } from '../service/store.service.js';
3
+ import { aggregatevmsUserAudit, createvmsUserAudit, findOnevmsUserAudit, updateOnevmsUserAudit, updatemanyvmsUserAudit } from '../service/vmsuserAudit.service.js';
4
+ import { createvmsAuditLog, findOnevmsAuditLog } from '../service/vmsauditLog.service.js';
5
+ import { insertOpenSearchData, clearScroll, scrollResponse } from 'tango-app-api-middleware';
6
+ import { findOneUser } from '../service/user.service.js';
7
+ import dayjs from 'dayjs';
8
+ import utc from 'dayjs/plugin/utc.js';
9
+ import timezone from 'dayjs/plugin/timezone.js';
10
+
11
+ dayjs.extend( utc );
12
+ dayjs.extend( timezone );
13
+ export async function getAuditFile( req, res ) {
14
+ try {
15
+ // const bucket = JSON.parse( process.env.BUCKET );
16
+ const url = JSON.parse( process.env.URL );
17
+ const openSearch = JSON.parse( process.env.OPENSEARCH );
18
+ const inputData = req.query;
19
+ const files = [];
20
+ const storeQuery = {
21
+ storeId: inputData.storeId,
22
+ };
23
+ const storeFields = {
24
+ storeName: 1,
25
+ clientId: 1,
26
+ address: '$storeProfile.address',
27
+ };
28
+ const storeInfo = await findOneStore( storeQuery, storeFields );
29
+
30
+ const userQuery = [
31
+ {
32
+ $match: {
33
+ $and: [
34
+ { userId: req.user._id },
35
+ { auditStatus: { $nin: [ 'completed', 'skipped' ] } },
36
+ { storeId: inputData.storeId },
37
+ { fileDate: inputData.Date },
38
+ ],
39
+ },
40
+ },
41
+ {
42
+ $sort: { createdAt: -1 },
43
+ },
44
+ {
45
+ $limit: 1,
46
+ },
47
+ ];
48
+
49
+ const userDetails = await aggregatevmsUserAudit( userQuery );
50
+ console.log( '🚀 ~ getAuditFile ~ userDetails:', userDetails );
51
+ const auditStatus = userDetails && userDetails.length > 0 ? userDetails[0].auditStatus : null;
52
+ if ( auditStatus === 'drafted' ) {
53
+ const log = await findOnevmsAuditLog(
54
+ {
55
+ userId: userDetails[0].userId,
56
+ fileDate: userDetails[0].fileDate,
57
+ storeId: userDetails[0].storeId,
58
+ totalCount: userDetails[0].beforeCount,
59
+ },
60
+ {},
61
+ { createdAt: -1 },
62
+ );
63
+ console.log( '🚀 ~ getAuditFile ~ log:', log );
64
+ if ( !log ) {
65
+ await updateOnevmsUserAudit(
66
+ { _id: userDetails[0]._id },
67
+ { $set: { isDraft: false, auditStatus: 'skipped' } },
68
+ );
69
+ logger.info( 'audit update in drafted', {
70
+ _id: userDetails[0]._id,
71
+ isDraft: false,
72
+ auditStatus: 'skipped',
73
+ } );
74
+ return res.sendError( 'User saved data has been deleted', 204 );
75
+ }
76
+
77
+ const file = {
78
+ auditId: userDetails[0]._id,
79
+ storeId: userDetails[0].storeId,
80
+ Date: userDetails[0].fileDate,
81
+ userId: log.userId,
82
+ timeSpent: log.timeSpent,
83
+ clientId: userDetails[0].clientId,
84
+ };
85
+ const userdata = await findOneUser( { _id: log.userId } );
86
+ if (
87
+ !inputData.nextId ||
88
+ inputData.nextId === '' ||
89
+ inputData.nextId == null
90
+ ) {
91
+ const logData = {
92
+ userId: log.userId,
93
+ userName: userdata.userName,
94
+ logType: 'vmsaudit',
95
+ logSubType: 'auditStart',
96
+ logData: {
97
+ fileDate: userDetails[0].fileDate,
98
+ storeId: userDetails[0].storeId,
99
+ auditId: userDetails[0]._id,
100
+ beforeCount: userDetails[0].beforeCount,
101
+ },
102
+ createdAt: new Date(),
103
+ };
104
+ await insertOpenSearchData( openSearch.vmsauditLog, logData );
105
+ }
106
+ const storeQuery = {
107
+ storeId: userDetails[0].storeId,
108
+ };
109
+ const storeFields = {
110
+ storeId: 1,
111
+ storeName: 1,
112
+ address: '$storeProfile.address',
113
+ };
114
+ const storeDetails = await findOneStore( storeQuery, storeFields );
115
+ let docId = `${inputData.storeId}_${inputData.Date}_footfall-directory-tagging`;
116
+ if ( inputData.tickettype === 'internal' ) {
117
+ docId = `${inputData.storeId}_${inputData.Date}_internal_footfall-directory-tagging`;
118
+ }
119
+ console.log( '🚀 ~ getAuditFile ~ docId:draft', docId );
120
+ const existinginternalDoc = await getOpenSearchById( openSearch.footfallDirectory, docId );
121
+ let ticketSource = existinginternalDoc?.body?._source;
122
+ const currentTime = new Date();
123
+ const updatePayload = {
124
+ doc: {
125
+
126
+ updatedAt: currentTime,
127
+ createdByRole: req?.user?.role || '',
128
+ createdByEmail: req?.user?.email || '',
129
+ createdByUserName: req?.user?.userName || '',
130
+ createdByUserId: req?.user?._id || '',
131
+ },
132
+ };
133
+ console.log( '🚀 ~ getAuditFile ~ updatePayload:', updatePayload );
134
+ console.log( '🚀 ~ getAuditFile ~ ticketSource:', ticketSource );
135
+ if ( ticketSource ) {
136
+ ticketSource.mappingInfo.map( ( data ) => {
137
+ if ( data.type === 'tangoreview' ) {
138
+ data.status = 'In-Progress';
139
+ }
140
+ } );
141
+ ticketSource.createdByRole = req?.user?.role || '';
142
+ ticketSource.createdByEmail = req?.user?.email || '';
143
+ ticketSource.createdByUserName = req?.user?.userName || '';
144
+ ticketSource.createdByUserId = req?.user?._id || '';
145
+ console.log( '🚀 ~ getAuditFile ~ ticketSource:', ticketSource );
146
+ await updateOpenSearchData(
147
+ openSearch.footfallDirectory,
148
+ docId,
149
+ { doc: ticketSource },
150
+
151
+ );
152
+ } else if ( ticketSource && inputData.tickettype === 'internal' ) {
153
+ await updateOpenSearchData(
154
+ openSearch.footfallDirectory,
155
+ docId,
156
+ updatePayload,
157
+ );
158
+ }
159
+ return res.sendSuccess( {
160
+ result: log.draftedData,
161
+ storeId: storeDetails?.storeId,
162
+ storeName: storeDetails?.storeName,
163
+ address: storeDetails?.address || '',
164
+ count: log.totalCount,
165
+ timeSpent: log.timeSpent,
166
+ file: file,
167
+ isDraft: userDetails[0].isDraft,
168
+ } );
169
+ }
170
+ console.log( inputData );
171
+
172
+ const query = {
173
+ size: inputData?.limit || 2,
174
+ _source: [ 'module', 'status', 'date', 'store_id', 'outputCluster', 'personPath', 'REIDCluster', 'isEmployee', 'isJunk', 'EmployeeStatusFinal' ], // Only fetch necessary fields
175
+
176
+ query: {
177
+ bool: {
178
+ 'must': [
179
+ {
180
+ term: { 'store_id.keyword': inputData.storeId },
181
+ },
182
+ {
183
+ term: { 'date.keyword': dayjs( inputData.Date ).format( 'DD-MM-YYYY' ) },
184
+ },
185
+ {
186
+ term: { 'module.keyword': 'CUSTOMER' },
187
+ },
188
+ {
189
+ term: { 'status.keyword': 'PP_CLUSTER_FORMED' },
190
+ },
191
+ {
192
+ terms: { EmployeeStatusFinal: [ 1, 2 ] },
193
+ },
194
+ ],
195
+ 'should': [
196
+ { 'term': { 'isJunk': false } },
197
+ { 'bool': { 'must_not': { 'exists': { 'field': 'isJunk' } } } },
198
+ ],
199
+ 'minimum_should_match': 1,
200
+ },
201
+ },
202
+
203
+ };
204
+ console.log( openSearch.vmsAudit );
205
+ let list = inputData.nextId ? await scrollResponse( inputData.nextId ) : await searchOpenSearchData( openSearch.vmsAudit, query );
206
+
207
+ let docId = `${inputData.storeId}_${inputData.Date}_footfall-directory-tagging`;
208
+ if ( inputData.tickettype === 'internal' ) {
209
+ docId = `${inputData.storeId}_${inputData.Date}_internal_footfall-directory-tagging`;
210
+ }
211
+ console.log( '🚀 ~ getAuditFile ~ docId:draft', docId );
212
+ const existinginternalDoc = await getOpenSearchById( openSearch.footfallDirectory, docId );
213
+ let ticketSource = existinginternalDoc?.body?._source;
214
+ const currentTime = new Date();
215
+ const updatePayload = {
216
+ doc: {
217
+
218
+ updatedAt: currentTime,
219
+ createdByRole: req?.user?.role || '',
220
+ createdByEmail: req?.user?.email || '',
221
+ createdByUserName: req?.user?.userName || '',
222
+ createdByUserId: req?.user?._id || '',
223
+ },
224
+ };
225
+ console.log( '🚀 ~ getAuditFile ~ updatePayload:', updatePayload );
226
+ console.log( '🚀 ~ getAuditFile ~ ticketSource:', ticketSource );
227
+ if ( ticketSource ) {
228
+ ticketSource.mappingInfo.map( ( data ) => {
229
+ if ( data.type === 'tangoreview' ) {
230
+ data.status = 'In-Progress';
231
+ console.log( '🚀 ~ getAuditFile ~ data.status:', data.status );
232
+ }
233
+ } );
234
+ ticketSource.createdByRole = req?.user?.role || '';
235
+ ticketSource.createdByEmail = req?.user?.email || '';
236
+ ticketSource.createdByUserName = req?.user?.userName || '';
237
+ ticketSource.createdByUserId = req?.user?._id || '';
238
+ console.log( '🚀 ~ getAuditFile ~ ticketSource:', ticketSource );
239
+ await updateOpenSearchData(
240
+ openSearch.footfallDirectory,
241
+ docId,
242
+ { doc: ticketSource },
243
+ );
244
+ } else if ( ticketSource && inputData.tickettype === 'internal' ) {
245
+ await updateOpenSearchData(
246
+ openSearch.footfallDirectory,
247
+ docId,
248
+ updatePayload,
249
+ );
250
+ }
251
+ const folderPath = list?.body?.hits?.hits;
252
+ if ( list?.body?.hits?.hits?.length == 0 ) {
253
+ await clearScroll( list?.body?._scroll_id );
254
+ }
255
+ if ( folderPath?.length > 0 ) {
256
+ for ( let i = 0; i < folderPath.length; i++ ) {
257
+ const img = folderPath[i]?._source?.personPath?.split( '/' );
258
+ const image = img[3]?.split( '.' );
259
+ const indexes = folderPath[i]?._source?.outputCluster === 50000 ? folderPath[i]?._source?.REIDCluster : folderPath[i]?._source?.outputCluster;
260
+ // fetchData.file_path = folderPath[i]?._source?.personPath;
261
+ const data = `${url.trackInput}${folderPath[i]?._source?.personPath}`;
262
+ const mapimg = {
263
+ img_path: data,
264
+ img_name: indexes,
265
+ img_id: image[0],
266
+ };
267
+ addUniqueFile( files, {
268
+ img_path: data,
269
+ img_name: indexes,
270
+ img_id: image[0],
271
+ selected: false,
272
+ dropped: false,
273
+ demographic: '',
274
+ count: 1,
275
+ mappedid: [ mapimg ],
276
+ } );
277
+ }
278
+ } else if ( inputData.nextId !== '' && inputData.nextId !== null && inputData.nextId !== undefined ) {
279
+ if ( !list ) {
280
+ return res.sendError( 'token expired', 404 );
281
+ }
282
+ list.body._scroll_id = '';
283
+ // insertData = userDetails[0];
284
+ } else {
285
+ logger.error( {
286
+ error: { folderPath: folderPath, nextQuery: inputData.nextId },
287
+ function: 'getAuditFile',
288
+ message: 'Bucket image data not availale',
289
+ } );
290
+ return res.sendError( 'Bucket is Empty', 204 );
291
+ }
292
+ const [ year, month, day ] = inputData.Date.split( '-' );
293
+ const temp = `${year}-${month}-${day}`;
294
+ console.log( temp );
295
+ let start = new Date( temp );
296
+ const userTimezoneOffset = start.getTimezoneOffset() * 60000;
297
+ start = new Date( start.getTime() - userTimezoneOffset );
298
+ start.setUTCHours( 0, 0, 0, 0 );
299
+ console.log( start );
300
+ const record = {
301
+ userId: req.user._id,
302
+ storeId: inputData.storeId,
303
+ clientId: storeInfo?.clientId,
304
+ fileDate: inputData.Date,
305
+ beforeCount: inputData.count,
306
+ auditStatus: 'inprogress',
307
+ fileDateISO: start,
308
+ timeSpent: 0,
309
+ startTime: new Date(),
310
+ };
311
+ const insertData = await createvmsUserAudit( record );
312
+ if ( inputData.nextId !== '' && inputData.nextId !== null && inputData.nextId !== undefined && inputData.moduleType === 'track' ) {
313
+ if ( !list ) {
314
+ return res.sendError( 'token expired', 404 );
315
+ }
316
+ list.body._scroll_id = '';
317
+ }
318
+ return res.sendSuccess( {
319
+ result: files,
320
+ count: inputData.count,
321
+ storeId: inputData.storeId,
322
+ storeName: storeInfo?.storeName,
323
+ address: storeInfo?.address,
324
+ file: {
325
+ clientId: storeInfo?.clientId,
326
+ storeId: inputData.storeId,
327
+ Date: inputData.Date,
328
+ auditId: insertData._id,
329
+ userId: insertData.userId,
330
+ nextToken: list?.body?._scroll_id ? list?.body?._scroll_id : null,
331
+
332
+ },
333
+ isDraft: insertData.isDraft,
334
+ } );
335
+ } catch ( error ) {
336
+ const err = error.message || 'Internal Server Error';
337
+ logger.error( {
338
+ error: error,
339
+ message: req.query,
340
+ function: 'getAuditFile',
341
+ } );
342
+ return res.sendError( err, 500 );
343
+ }
344
+ }
345
+ function addUniqueFile( files, newFile ) {
346
+ const exists = files.some( ( file ) => file.img_name === newFile.img_name );
347
+ if ( !exists ) {
348
+ files.push( newFile );
349
+ }
350
+ }
351
+ export async function saveDraft( req, res ) {
352
+ try {
353
+ const openSearch = JSON.parse( process.env.OPENSEARCH );
354
+ const inputData = req.body;
355
+ inputData.userId = req.user._id;
356
+ const getUserAuditData = await findOnevmsUserAudit( { _id: inputData.auditId } );
357
+ if ( !getUserAuditData ) {
358
+ return res.sendError( 'No Data Found', 204 );
359
+ }
360
+ if ( getUserAuditData.auditStatus == 'skipped' ) {
361
+ return res.sendError( 'File Assigned to Someone else', 203 );
362
+ }
363
+
364
+ const userQuery = {
365
+ _id: inputData.auditId,
366
+ };
367
+ let userRecord = {
368
+ isDraft: true,
369
+ auditStatus: 'drafted',
370
+ };
371
+
372
+
373
+ if ( getUserAuditData?.startTime ) {
374
+ userRecord.timeSpent = inputData.timeSpent;
375
+ }
376
+
377
+ if ( inputData.userCommands ) {
378
+ userRecord.userCommands = inputData.userCommands;
379
+ const logData = {
380
+ userId: req.user._id,
381
+ userName: req.user.userName,
382
+ logType: 'vmsaudit',
383
+ logSubType: 'auditDraft',
384
+ logData: {
385
+ fileDate: inputData.fileDate,
386
+ storeId: inputData.storeId,
387
+ beforeCount: inputData.totalCount,
388
+ afterCount: inputData.customerCount,
389
+ timeSpent: inputData.timeSpent,
390
+ auditId: inputData.auditId,
391
+ },
392
+ createdAt: new Date(),
393
+ };
394
+ await insertOpenSearchData( openSearch.vmsauditLog, logData );
395
+ }
396
+ await createvmsAuditLog( inputData );
397
+ await updateOnevmsUserAudit( userQuery, userRecord );
398
+ console.log( '🚀 ~ saveDraft ~ userRecord:', userRecord );
399
+ return res.sendSuccess( {
400
+ result: 'The file has been drafted successfully',
401
+ } );
402
+ } catch ( error ) {
403
+ const err = error.message || 'Internal Server Error';
404
+ logger.error( { error: error, message: req.body, function: 'saveDraft' } );
405
+ return res.sendError( err, 500 );
406
+ }
407
+ }
408
+ export async function getDraftedData( req, res ) {
409
+ try {
410
+ const inputData = req.query;
411
+ const userId = inputData.userId || req.user._id;
412
+ const query = {
413
+ fileDate: inputData.fileDate,
414
+ storeId: inputData.storeId,
415
+ userId: userId,
416
+ auditId: inputData.auditId,
417
+ };
418
+ console.log( query );
419
+ const result = await findOnevmsAuditLog( query, {}, { createdAt: -1 }, 1 );
420
+
421
+ if ( !result ) {
422
+ return res.sendError( 'No Data Found', 204 );
423
+ }
424
+ return res.sendSuccess( { result: result } );
425
+ } catch ( error ) {
426
+ const err = error.message || 'Internal Server Error';
427
+ logger.error( {
428
+ error: error,
429
+ message: req.query,
430
+ function: 'getDraftedData',
431
+ } );
432
+ return res.sendError( err, 500 );
433
+ }
434
+ }
435
+
436
+
437
+ export async function reAssignAudit( req, res ) {
438
+ try {
439
+ const openSearch = JSON.parse( process.env.OPENSEARCH );
440
+ let inputData = req.body;
441
+ let docId = `${inputData.storeId}_${inputData.dateString}_internal_footfall-directory-tagging`;
442
+ if ( inputData.tickettype ==='store' ) {
443
+ docId = `${inputData.storeId}_${inputData.dateString}_footfall-directory-tagging`;
444
+ }
445
+ console.log( '🚀 ~ getAuditFile ~ docId:draft', docId );
446
+ const existingDoc = await getOpenSearchById( openSearch.footfallDirectory, docId );
447
+ const ticketSource = existingDoc?.body?._source;
448
+ const currentTime = new Date();
449
+ const userdata = await findOneUser( { email: inputData.email } );
450
+ const updatePayload = {
451
+ doc: {
452
+ status: 'In-Progress',
453
+ updatedAt: currentTime,
454
+ createdByRole: userdata?.role || '',
455
+ createdByEmail: userdata?.email || '',
456
+ createdByUserName: userdata?.userName || '',
457
+ createdByUserId: userdata?._id || '',
458
+ },
459
+ };
460
+ console.log( '🚀 ~ getAuditFile ~ updatePayload:', updatePayload );
461
+ console.log( '🚀 ~ getAuditFile ~ ticketSource:', ticketSource );
462
+ if ( ticketSource ) {
463
+ await updateOpenSearchData(
464
+ openSearch.footfallDirectory,
465
+ docId,
466
+ updatePayload,
467
+ );
468
+ }
469
+
470
+ console.log( '🚀 ~ reAssignAudit ~ inputData.dateString:', inputData.dateString );
471
+ await updatemanyvmsUserAudit(
472
+ { auditStatus: { $ne: 'completed' }, storeId: inputData.storeId, fileDate: inputData.dateString },
473
+ { status: 'skipped' } );
474
+
475
+
476
+ return res.sendSuccess( 'Ticket Reassigned Successfully' );
477
+ } catch ( error ) {
478
+ const err = error.message || 'Internal Server Error';
479
+ logger.error( {
480
+ error: error,
481
+ message: req.query,
482
+ function: 'reAssignAudit',
483
+ } );
484
+ return res.sendError( err, 500 );
485
+ }
486
+ }
487
+
488
+
@@ -360,13 +360,17 @@ export const updateFDConfigBodySchema = joi.object( {
360
360
  tangoReview: joi.string().optional(),
361
361
  revision: joi.array().items( joi.object( {
362
362
  actionType: joi.string().required(),
363
- isChecked: joi.boolean().required(),
363
+ isChecked: joi.boolean().optional().default( false ),
364
364
  } ) ).optional(),
365
365
  taggingLimitation: joi.array().items( joi.object( {
366
+ iconName: joi.string().optional(),
367
+ name: joi.string().optional(),
366
368
  type: joi.string().required(),
367
369
  value: joi.number().required(),
368
370
  unit: joi.string().required(),
371
+ key: joi.string().required(),
369
372
  } ) ).optional(),
373
+ contactEmail: joi.string().optional().allow( '' ),
370
374
 
371
375
  } );
372
376
 
@@ -374,8 +378,12 @@ export const updateFDConfigQuerySchema = joi.object( {
374
378
  clientId: joi.string().required(),
375
379
  } );
376
380
 
381
+
382
+ const todayDate = () => new Date().toISOString().split( 'T' )[0];
383
+
377
384
  export const getFDConfigSchema = joi.object( {
378
385
  clientId: joi.string().required(),
386
+ dateString: joi.string().optional().default( () => todayDate() ),
379
387
  } );
380
388
 
381
389
  export const updateFDConfigValid = {
@@ -392,6 +400,9 @@ export const updateTaggingTypeSchema = joi.object( {
392
400
  type: joi.string().required(),
393
401
  value: joi.number().required(),
394
402
  unit: joi.string().required(),
403
+ name: joi.string().required(),
404
+ iconName: joi.string().required(),
405
+ key: joi.string().required(),
395
406
  } ) ).required(),
396
407
 
397
408
  } );
@@ -0,0 +1,25 @@
1
+
2
+ import joi from 'joi';
3
+ export const getDraftedDataSchema = joi.object( {
4
+ storeId: joi.string().required(),
5
+ fileDate: joi.string().required(),
6
+ userId: joi.string().optional(),
7
+ auditId: joi.string().required(),
8
+ } );
9
+ export const getFileSchema = joi.object( {
10
+ nextId: joi.string().optional().allow( '' ),
11
+ limit: joi.number().optional(),
12
+ storeId: joi.string().required(),
13
+ Date: joi.string().required(),
14
+ count: joi.string().required(),
15
+ tickettype: joi.string().required(),
16
+ } );
17
+
18
+
19
+ export const getDraftedDataValid = {
20
+ query: getDraftedDataSchema,
21
+ };
22
+
23
+ export const getFileValid = {
24
+ query: getFileSchema,
25
+ };
@@ -0,0 +1,11 @@
1
+ import express from 'express';
2
+ import { accessVerification, isAllowedSessionHandler, validate } from 'tango-app-api-middleware';
3
+ import { getDraftedDataValid, getFileValid } from '../dtos/vmsAudit.dtos.js';
4
+ import { getAuditFile, getDraftedData, saveDraft, reAssignAudit } from '../controllers/vmsAudit.controller.js';
5
+ export const vmsauditRouter = express.Router();
6
+
7
+
8
+ vmsauditRouter.get( '/get-file', isAllowedSessionHandler, accessVerification( { userType: [ 'tango' ] } ), validate( getFileValid ), getAuditFile );
9
+ vmsauditRouter.post( '/save-draft', isAllowedSessionHandler, accessVerification( { userType: [ 'tango' ] } ), saveDraft );
10
+ vmsauditRouter.get( '/get-drafted-data', isAllowedSessionHandler, accessVerification( { userType: [ 'tango' ] } ), validate( getDraftedDataValid ), getDraftedData );
11
+ vmsauditRouter.post( '/reAssign-audit', isAllowedSessionHandler, accessVerification( { userType: [ 'tango' ] } ), reAssignAudit );
@@ -0,0 +1,10 @@
1
+ import vmsauditLogsModel from 'tango-api-schema/schema/vmsauditLogs.model.js';
2
+
3
+
4
+ export function createvmsAuditLog( record ) {
5
+ return vmsauditLogsModel.create( record );
6
+ }
7
+
8
+ export function findOnevmsAuditLog( query, fields={}, sort={ createdAt: -1 }, limit=10 ) {
9
+ return vmsauditLogsModel.findOne( query, fields ).sort( sort ).limit( limit );
10
+ }
@@ -0,0 +1,25 @@
1
+ import vmsuserAuditModel from 'tango-api-schema/schema/vmsuserAudit.model.js';
2
+
3
+
4
+ export function aggregatevmsUserAudit( query ) {
5
+ return vmsuserAuditModel.aggregate( query, { collation: { locale: 'en', strength: 2 } } );
6
+ }
7
+
8
+ export function updateOnevmsUserAudit( query, record ) {
9
+ return vmsuserAuditModel.updateOne( query, { $set: record } );
10
+ }
11
+ export function updatemanyvmsUserAudit( query, record ) {
12
+ return vmsuserAuditModel.updateMany( query, { $set: record } );
13
+ }
14
+
15
+ export function createvmsUserAudit( record ) {
16
+ return vmsuserAuditModel.create( record );
17
+ }
18
+
19
+ export function findOnevmsUserAudit( query, fields ) {
20
+ return vmsuserAuditModel.findOne( query, fields );
21
+ }
22
+
23
+ export function aggregatevmsUserAuditCount( query ) {
24
+ return vmsuserAuditModel.aggregate( query );
25
+ }