tango-app-api-trax 3.4.1-beta-2 → 3.5.0-alpha-0

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.
@@ -1,4 +1,4 @@
1
- import { logger, signedUrl, fileUpload, getOtp, sendEmailWithSES, getUuid } from 'tango-app-api-middleware';
1
+ import { logger, signedUrl, fileUpload, getOtp, sendEmailWithSES, getUuid, insertOpenSearchData } from 'tango-app-api-middleware';
2
2
  import * as processedchecklist from '../services/processedchecklist.services.js';
3
3
  import * as processedtask from '../services/processedTaskList.service.js';
4
4
  import * as PCLconfig from '../services/processedchecklistconfig.services.js';
@@ -25,6 +25,7 @@ import handlebars from 'handlebars';
25
25
  dayjs.extend( customParseFormat );
26
26
  dayjs.extend( timeZone );
27
27
  import isSameOrBefore from 'dayjs/plugin/isSameOrBefore.js';
28
+ import * as cameraService from '../services/camera.service.js';
28
29
  dayjs.extend( isSameOrBefore );
29
30
 
30
31
  export async function storeList( req, res ) {
@@ -262,12 +263,33 @@ export async function startChecklist( req, res ) {
262
263
  getchecklist[index].questionAnswers[secIndex].questions = questions;
263
264
  }
264
265
  }
266
+ let openSearch = JSON.parse( process.env.OPENSEARCH );
267
+ // console.log( 'openSearch', openSearch );
268
+ let inserttraxlogs = {
269
+ client_id: req.user.clientId,
270
+ createAt: new Date(),
271
+ sourceCheckList_id: getBeforeChecklist[0]?.sourceCheckList_id || '',
272
+ checkListName: getBeforeChecklist[0]?.checkListName || '',
273
+ fromCheckListName: '',
274
+ type: 'checkList',
275
+ action: 'start',
276
+ storeName: getBeforeChecklist[0]?.storeName || '',
277
+ store_id: getBeforeChecklist[0]?.store_id || '',
278
+ createdByEmail: getBeforeChecklist[0]?.userEmail || '',
279
+ createdBy: getBeforeChecklist[0]?.userName || '',
280
+ coverage: getBeforeChecklist[0]?.coverage || '',
281
+ logDetails: {},
282
+ userType: req.user.userType,
283
+ };
284
+ // let insertOS = await insertOpenSearchData( 'test-traxlogs', inserttraxlogs );
285
+ console.log( 'inserttraxlogs =>', inserttraxlogs );
286
+ insertOpenSearchData( openSearch.traxActivityLog, inserttraxlogs );
265
287
  return res.sendSuccess( getchecklist );
266
288
  } else {
267
289
  return res.sendError( 'something went wrong please try again', 500 );
268
290
  }
269
291
  } catch ( e ) {
270
- console.log( 'e =>', e );
292
+ // console.log( 'e =>', e );
271
293
  logger.error( { function: 'startChecklist', error: e, body: req.body } );
272
294
  return res.sendError( e, 500 );
273
295
  }
@@ -433,6 +455,27 @@ export async function startTask( req, res ) {
433
455
  };
434
456
  await checklistLogs.create( logData );
435
457
 
458
+ let openSearch = JSON.parse( process.env.OPENSEARCH );
459
+ // console.log( 'openSearch', openSearch );
460
+ let inserttraxlogs = {
461
+ client_id: req.user.clientId,
462
+ createAt: new Date(),
463
+ sourceCheckList_id: task.sourceCheckList_id || '',
464
+ checkListName: task.checkListName || '',
465
+ fromCheckListName: '',
466
+ type: 'task',
467
+ action: 'start',
468
+ storeName: task.storeName || '',
469
+ store_id: task.store_id || '',
470
+ createdByEmail: task.userEmail || '',
471
+ createdBy: task.userName || '',
472
+ coverage: task.coverage || '',
473
+ logDetails: {},
474
+ userType: req.user.userType,
475
+ };
476
+ // let insertOS = await insertOpenSearchData( 'test-traxlogs', inserttraxlogs );
477
+ console.log( 'inserttraxlogs =>', inserttraxlogs );
478
+ insertOpenSearchData( openSearch.traxActivityLog, inserttraxlogs );
436
479
  return res.sendSuccess( getUpdatedTask );
437
480
  } catch ( error ) {
438
481
  logger.error( { function: 'startTask', error, body: req.body } );
@@ -1754,7 +1797,9 @@ export async function submitChecklist( req, res ) {
1754
1797
  let flagCount = QuestionFlag( req, res );
1755
1798
  updateData.questionFlag = flagCount;
1756
1799
  updateData.submitTime_string = currentDateTime.format( 'hh:mm A, DD MMM YYYY' );
1757
- updateData.deviceDetails = requestData?.deviceDetails || {};
1800
+ if ( requestData.deviceDetails && requestData.deviceDetails != '' ) {
1801
+ updateData.deviceDetails =JSON.parse( requestData.deviceDetails );
1802
+ }
1758
1803
  if ( requestData.submittype == 'draft' ) {
1759
1804
  logger.info( `v5 => Checklist Save => store Name: ${getchecklist[0].storeName}, User Email: ${getchecklist[0].userEmail}, Checklist Name: ${getchecklist[0].checkListName}` );
1760
1805
  updateData.questionAnswers = requestData.questionAnswers;
@@ -1806,7 +1851,7 @@ export async function submitChecklist( req, res ) {
1806
1851
  userName: getchecklist[0].userName || '',
1807
1852
  coverage: getchecklist[0].coverage || '',
1808
1853
  };
1809
- console.log( 'logInsertData=>', logInsertData );
1854
+ // console.log( 'logInsertData=>', logInsertData );
1810
1855
  await checklistLogs.create( logInsertData );
1811
1856
  // let time = dayjs().format( 'HH:mm:ss' );
1812
1857
  // let [ hours, minutes ] = time.split( ':' ).map( Number );
@@ -1841,6 +1886,28 @@ export async function submitChecklist( req, res ) {
1841
1886
  // await detectionService.create( detectionData );
1842
1887
  if ( requestData.submittype == 'submit' ) {
1843
1888
  updateOpenSearch( req.user, requestData );
1889
+ let openSearch = JSON.parse( process.env.OPENSEARCH );
1890
+ // console.log( 'openSearch', openSearch );
1891
+ let inserttraxlogs = {
1892
+ client_id: req.user.clientId,
1893
+ createAt: new Date(),
1894
+ sourceCheckList_id: getchecklist[0].sourceCheckList_id,
1895
+ checkListName: getchecklist[0].checkListName,
1896
+ type: 'checkList',
1897
+ action: requestData?.submittype === 'draft' ? 'save' : 'submit',
1898
+ storeName: getchecklist[0].storeName,
1899
+ store_id: getchecklist[0].store_id,
1900
+ userName: getchecklist[0].userName,
1901
+ userEmail: getchecklist[0].userEmail,
1902
+ createdByEmail: getchecklist[0].userEmail,
1903
+ createdBy: getchecklist[0].userName,
1904
+ coverage: getchecklist[0].coverage,
1905
+ checkListType: getchecklist[0].checkListType,
1906
+ logDetails: {},
1907
+ userType: req.user.userType,
1908
+ };
1909
+ console.log( 'inserttraxlogs =>', inserttraxlogs );
1910
+ insertOpenSearchData( openSearch.traxActivityLog, inserttraxlogs );
1844
1911
  }
1845
1912
 
1846
1913
  return res.sendSuccess( 'Checklist Updated Successfully' );
@@ -1911,6 +1978,10 @@ export async function submitTask( req, res ) {
1911
1978
  questionFlag: QuestionFlag( req, res ),
1912
1979
  };
1913
1980
 
1981
+ if ( requestData.deviceDetails && requestData.deviceDetails != '' ) {
1982
+ updateData.deviceDetails =JSON.parse( requestData.deviceDetails );
1983
+ }
1984
+
1914
1985
  if ( submittype === 'draft' ) {
1915
1986
  logger.info(
1916
1987
  `v5 => Checklist Save => store Name: ${storeName}, User Email: ${userEmail}, Checklist Name: ${checkListName}`,
@@ -2007,6 +2078,28 @@ export async function submitTask( req, res ) {
2007
2078
 
2008
2079
  if ( submittype == 'submit' ) {
2009
2080
  updateOpenSearchTask( user, requestData );
2081
+ let openSearch = JSON.parse( process.env.OPENSEARCH );
2082
+ // console.log( 'openSearch', openSearch );
2083
+ let inserttraxlogs = {
2084
+ client_id: req.user.clientId,
2085
+ createAt: new Date(),
2086
+ sourceCheckList_id: checklist.sourceCheckList_id,
2087
+ checkListName: checklist.checkListName,
2088
+ type: 'task',
2089
+ action: requestData?.submittype === 'draft' ? 'save' : 'submit',
2090
+ storeName: checklist.storeName,
2091
+ store_id: checklist.store_id,
2092
+ userName: checklist.userName,
2093
+ userEmail: checklist.userEmail,
2094
+ createdByEmail: checklist.userEmail,
2095
+ createdBy: checklist.userName,
2096
+ coverage: checklist.coverage,
2097
+ checkListType: checklist.checkListType,
2098
+ logDetails: {},
2099
+ userType: req.user.userType,
2100
+ };
2101
+ console.log( 'inserttraxlogs =>', inserttraxlogs );
2102
+ insertOpenSearchData( openSearch.traxActivityLog, inserttraxlogs );
2010
2103
  }
2011
2104
 
2012
2105
  return res.sendSuccess( 'Task Updated Successfully' );
@@ -2018,6 +2111,193 @@ export async function submitTask( req, res ) {
2018
2111
  return res.sendError( error.message || 'Internal Server Error', 500 );
2019
2112
  }
2020
2113
  }
2114
+ export async function submiteyeTestTask( req, res ) {
2115
+ try {
2116
+ const { body: requestData, user } = req;
2117
+ const { processedcheckListId, date, submittype, currentTime, questionAnswers, streamId, qrCode } = requestData;
2118
+ // console.log( requestData );
2119
+ if ( !streamId ) {
2120
+ return res.sendError( 'streamId is required', 400 );
2121
+ }
2122
+ if ( !qrCode||qrCode=='' ) {
2123
+ return res.sendError( 'qrCode is required', 400 );
2124
+ }
2125
+ let findoldQrExists = await cameraService.findOne( { qrCode: qrCode } );
2126
+ if ( findoldQrExists ) {
2127
+ return res.sendError( 'Qr code already Exists', 400 );
2128
+ }
2129
+ let findQrExists = await cameraService.findOne( { streamName: streamId, qrCode: qrCode } );
2130
+ // console.log( findQrExists );
2131
+ if ( findQrExists ) {
2132
+ return res.sendError( 'Qr code already Exists', 400 );
2133
+ }
2134
+
2135
+ await cameraService.updateOne( { streamName: streamId }, { qrCode: qrCode } );
2136
+ return res.sendSuccess( 'Qr code added successfully' );
2137
+
2138
+ const findQuery = [
2139
+ {
2140
+ $match: {
2141
+ _id: new ObjectId( processedcheckListId ),
2142
+ userId: user._id,
2143
+ date_string: date,
2144
+ },
2145
+ },
2146
+ ];
2147
+
2148
+ const [ checklist ] = await processedTask.aggregate( findQuery );
2149
+
2150
+ if ( !checklist ) {
2151
+ return res.sendError( 'Task edited. Please fill again', 422 );
2152
+ }
2153
+
2154
+ // eslint-disable-next-line camelcase
2155
+ const { checklistStatus, storeName, userEmail, checkListName, store_id } = checklist;
2156
+
2157
+ if ( checklistStatus === 'open' ) {
2158
+ return res.sendError( 'Checklist is in open status. Please start.', 400 );
2159
+ }
2160
+
2161
+ if ( checklistStatus === 'submit' ) {
2162
+ return res.sendError( 'Checklist already submitted', 400 );
2163
+ }
2164
+
2165
+ const storeTimeZone = await storeService.findOne( { storeName }, { 'storeProfile.timeZone': 1 } );
2166
+ const currentDateTime = storeTimeZone?.storeProfile?.timeZone ?
2167
+ dayjs().tz( storeTimeZone.storeProfile.timeZone ) :
2168
+ dayjs();
2169
+
2170
+ const updateQuery = {
2171
+ _id: new ObjectId( processedcheckListId ),
2172
+ userId: user._id,
2173
+ date_string: date,
2174
+ };
2175
+
2176
+ // questionAnswers.forEach( ( section ) => {
2177
+ // section.questions.forEach( ( question ) => {
2178
+ // if ( question.redo === true ) {
2179
+ // question.redo = false;
2180
+ // }
2181
+ // } );
2182
+ // } );
2183
+
2184
+ const updateData = {
2185
+ questionAnswers,
2186
+ updatedAt: dayjs.utc( currentDateTime.format( 'hh:mm:ss A, DD MMM YYYY' ), 'hh:mm:ss A, DD MMM YYYY' ).format(),
2187
+ questionFlag: QuestionFlag( req, res ),
2188
+ };
2189
+
2190
+ if ( requestData.deviceDetails && requestData.deviceDetails != '' ) {
2191
+ updateData.deviceDetails =JSON.parse( requestData.deviceDetails );
2192
+ }
2193
+
2194
+ if ( submittype === 'draft' ) {
2195
+ logger.info(
2196
+ `v5 => Checklist Save => store Name: ${storeName}, User Email: ${userEmail}, Checklist Name: ${checkListName}`,
2197
+ );
2198
+ updateData.submitTime_string = currentDateTime.format( 'hh:mm A, DD MMM YYYY' );
2199
+ } else if ( submittype === 'submit' ) {
2200
+ logger.info(
2201
+ `v5 => Checklist Submit => store Name: ${storeName}, User Email: ${userEmail}, Checklist Name: ${checkListName}`,
2202
+ );
2203
+ Object.assign( updateData, {
2204
+ submitTime: dayjs.utc( currentDateTime.format( 'hh:mm:ss A, DD MMM YYYY' ), 'hh:mm:ss A, DD MMM YYYY' ).format(),
2205
+ checklistStatus: 'submit',
2206
+ submitMobileTime: currentTime,
2207
+ submitTime_string: currentDateTime.format( 'hh:mm A, DD MMM YYYY' ),
2208
+ } );
2209
+ }
2210
+
2211
+ const updateResult = await processedTask.updateOne( updateQuery, updateData );
2212
+
2213
+ if ( updateResult.modifiedCount > 0 ) {
2214
+ if ( submittype === 'submit' ) {
2215
+ const query1 = [
2216
+ {
2217
+ $match: {
2218
+ userId: user._id,
2219
+ sourceCheckList_id: checklist.sourceCheckList_id,
2220
+ checklistStatus: { $in: [ 'open', 'inprogress' ] },
2221
+ },
2222
+ },
2223
+ {
2224
+ $project: {
2225
+ _id: 1,
2226
+ date_iso: 1,
2227
+ date_string: 1,
2228
+ },
2229
+ },
2230
+ ];
2231
+
2232
+ const processedTasksToDelete = await processedTask.aggregate( query1 );
2233
+
2234
+ if ( processedTasksToDelete?.length ) {
2235
+ const deleteIds = processedTasksToDelete?.filter( ( val ) => val?.date_string !== date )?.map( ( val ) => val?._id );
2236
+
2237
+ if ( deleteIds?.length ) {
2238
+ await processedTask.deleteMany( { _id: { $in: deleteIds } } );
2239
+ }
2240
+ }
2241
+ } else if ( submittype === 'draft' ) {
2242
+ const query2 = [
2243
+ {
2244
+ $match: {
2245
+ _id: checklist.sourceCheckList_id,
2246
+ },
2247
+ },
2248
+ ];
2249
+ const [ taskConfig ] = await taskConfigService.aggregate( query2 );
2250
+
2251
+ if ( taskConfig.scheduleEndTimeISO ) {
2252
+ const endDate = dayjs( taskConfig.scheduleEndTimeISO ).utc();
2253
+ const startDate = dayjs().add( 1, 'day' );
2254
+
2255
+ let currentDate = startDate;
2256
+
2257
+ while ( currentDate.isSameOrBefore( endDate ) ) {
2258
+ const updateDate = currentDate.format( 'YYYY-MM-DD' );
2259
+
2260
+ const updateQuery = {
2261
+ sourceCheckList_id: taskConfig._id,
2262
+ date_string: updateDate,
2263
+ userId: user._id,
2264
+ };
2265
+
2266
+ if ( Object.values( updateQuery ).length === 3 ) {
2267
+ await processedTask.updateOne( updateQuery, { questionAnswers, checklistStatus: 'inprogress' } );
2268
+ }
2269
+
2270
+ currentDate = currentDate.add( 1, 'day' );
2271
+ }
2272
+ }
2273
+ }
2274
+ const logInsertData = {
2275
+ // eslint-disable-next-line camelcase
2276
+ store_id,
2277
+ storeName,
2278
+ action: submittype === 'draft' ? 'saved' : 'submitted',
2279
+ checklistId: checklist.sourceCheckList_id,
2280
+ processedChecklistId: checklist._id,
2281
+ type: checklist.checkListType,
2282
+ checkListName,
2283
+ client_id: user.clientId,
2284
+ redoStatus: requestData?.redoStatus ? true : false,
2285
+ };
2286
+ await checklistLogs.create( logInsertData );
2287
+
2288
+ if ( submittype == 'submit' ) {
2289
+ updateOpenSearchTask( user, requestData );
2290
+ }
2291
+
2292
+ return res.sendSuccess( 'Task Updated Successfully' );
2293
+ } else {
2294
+ return res.sendError( 'Something went wrong. Please try again', 500 );
2295
+ }
2296
+ } catch ( error ) {
2297
+ logger.error( { function: 'submiteyeTestTask', error, body: req.body } );
2298
+ return res.sendError( error.message || 'Internal Server Error', 500 );
2299
+ }
2300
+ }
2021
2301
 
2022
2302
 
2023
2303
  async function updateOpenSearch( user, data ) {
@@ -2289,7 +2569,7 @@ export async function dashboardv1( req, res ) {
2289
2569
 
2290
2570
  return res.sendSuccess( totalResult );
2291
2571
  } catch ( e ) {
2292
- console.log( 'v1 =>', e );
2572
+ // console.log( 'v1 =>', e );
2293
2573
  logger.error( { function: 'dashboardv1', error: e, body: req.body } );
2294
2574
  return res.sendError( e, 500 );
2295
2575
  }
@@ -2464,6 +2744,7 @@ export async function checklistv1( req, res ) {
2464
2744
  client_id: { $ifNull: [ '$client_id', '' ] },
2465
2745
  coverage: { $ifNull: [ '$coverage', '' ] },
2466
2746
  taskType: { $ifNull: [ '$planoType', '' ] },
2747
+ streamId: { $ifNull: [ '$streamId', '' ] },
2467
2748
  },
2468
2749
  },
2469
2750
  ];
@@ -2559,6 +2840,8 @@ export async function questionList( req, res ) {
2559
2840
  redoStatus: { $ifNull: [ '$redoStatus', false ] },
2560
2841
  rawImageUpload: { $ifNull: [ '$rawImageUpload', false ] },
2561
2842
  rawVideoUpload: { $ifNull: [ '$rawVideoUpload', false ] },
2843
+ taskType: { $ifNull: [ '$planoType', '' ] },
2844
+ streamId: { $ifNull: [ '$streamId', '' ] },
2562
2845
  },
2563
2846
  } );
2564
2847