tango-app-api-trax 3.4.1-alpha-0 → 3.4.1-approvecheck-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,32 @@ 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
+ insertOpenSearchData( openSearch.traxActivityLog, inserttraxlogs );
265
286
  return res.sendSuccess( getchecklist );
266
287
  } else {
267
288
  return res.sendError( 'something went wrong please try again', 500 );
268
289
  }
269
290
  } catch ( e ) {
270
- console.log( 'e =>', e );
291
+ // console.log( 'e =>', e );
271
292
  logger.error( { function: 'startChecklist', error: e, body: req.body } );
272
293
  return res.sendError( e, 500 );
273
294
  }
@@ -433,6 +454,27 @@ export async function startTask( req, res ) {
433
454
  };
434
455
  await checklistLogs.create( logData );
435
456
 
457
+ let openSearch = JSON.parse( process.env.OPENSEARCH );
458
+ // console.log( 'openSearch', openSearch );
459
+ let inserttraxlogs = {
460
+ client_id: req.user.clientId,
461
+ createAt: new Date(),
462
+ sourceCheckList_id: task.sourceCheckList_id || '',
463
+ checkListName: task.checkListName || '',
464
+ fromCheckListName: '',
465
+ type: 'task',
466
+ action: 'start',
467
+ storeName: task.storeName || '',
468
+ store_id: task.store_id || '',
469
+ createdByEmail: task.userEmail || '',
470
+ createdBy: task.userName || '',
471
+ coverage: task.coverage || '',
472
+ logDetails: {},
473
+ userType: req.user.userType,
474
+ };
475
+ // let insertOS = await insertOpenSearchData( 'test-traxlogs', inserttraxlogs );
476
+ // console.log( 'inserttraxlogs', inserttraxlogs );
477
+ insertOpenSearchData( openSearch.traxActivityLog, inserttraxlogs );
436
478
  return res.sendSuccess( getUpdatedTask );
437
479
  } catch ( error ) {
438
480
  logger.error( { function: 'startTask', error, body: req.body } );
@@ -1754,6 +1796,7 @@ export async function submitChecklist( req, res ) {
1754
1796
  let flagCount = QuestionFlag( req, res );
1755
1797
  updateData.questionFlag = flagCount;
1756
1798
  updateData.submitTime_string = currentDateTime.format( 'hh:mm A, DD MMM YYYY' );
1799
+ updateData.deviceDetails = requestData?.deviceDetails || {};
1757
1800
  if ( requestData.submittype == 'draft' ) {
1758
1801
  logger.info( `v5 => Checklist Save => store Name: ${getchecklist[0].storeName}, User Email: ${getchecklist[0].userEmail}, Checklist Name: ${getchecklist[0].checkListName}` );
1759
1802
  updateData.questionAnswers = requestData.questionAnswers;
@@ -1805,7 +1848,7 @@ export async function submitChecklist( req, res ) {
1805
1848
  userName: getchecklist[0].userName || '',
1806
1849
  coverage: getchecklist[0].coverage || '',
1807
1850
  };
1808
- console.log( 'logInsertData=>', logInsertData );
1851
+ // console.log( 'logInsertData=>', logInsertData );
1809
1852
  await checklistLogs.create( logInsertData );
1810
1853
  // let time = dayjs().format( 'HH:mm:ss' );
1811
1854
  // let [ hours, minutes ] = time.split( ':' ).map( Number );
@@ -1840,6 +1883,28 @@ export async function submitChecklist( req, res ) {
1840
1883
  // await detectionService.create( detectionData );
1841
1884
  if ( requestData.submittype == 'submit' ) {
1842
1885
  updateOpenSearch( req.user, requestData );
1886
+ let openSearch = JSON.parse( process.env.OPENSEARCH );
1887
+ // console.log( 'openSearch', openSearch );
1888
+ let inserttraxlogs = {
1889
+ client_id: req.user.clientId,
1890
+ createAt: new Date(),
1891
+ sourceCheckList_id: getchecklist[0].sourceCheckList_id,
1892
+ checkListName: getchecklist[0].checkListName,
1893
+ type: 'checkList',
1894
+ action: requestData?.submittype === 'draft' ? 'save' : 'submit',
1895
+ storeName: getchecklist[0].storeName,
1896
+ store_id: getchecklist[0].store_id,
1897
+ userName: getchecklist[0].userName,
1898
+ userEmail: getchecklist[0].userEmail,
1899
+ createdByEmail: getchecklist[0].userEmail,
1900
+ createdBy: getchecklist[0].userName,
1901
+ coverage: getchecklist[0].coverage,
1902
+ checkListType: getchecklist[0].checkListType,
1903
+ logDetails: {},
1904
+ userType: req.user.userType,
1905
+ };
1906
+ // console.log( 'inserttraxlogs', inserttraxlogs );
1907
+ insertOpenSearchData( openSearch.traxActivityLog, inserttraxlogs );
1843
1908
  }
1844
1909
 
1845
1910
  return res.sendSuccess( 'Checklist Updated Successfully' );
@@ -2006,6 +2071,28 @@ export async function submitTask( req, res ) {
2006
2071
 
2007
2072
  if ( submittype == 'submit' ) {
2008
2073
  updateOpenSearchTask( user, requestData );
2074
+ let openSearch = JSON.parse( process.env.OPENSEARCH );
2075
+ // console.log( 'openSearch', openSearch );
2076
+ let inserttraxlogs = {
2077
+ client_id: req.user.clientId,
2078
+ createAt: new Date(),
2079
+ sourceCheckList_id: checklist.sourceCheckList_id,
2080
+ checkListName: checklist.checkListName,
2081
+ type: 'task',
2082
+ action: requestData?.submittype === 'draft' ? 'save' : 'submit',
2083
+ storeName: checklist.storeName,
2084
+ store_id: checklist.store_id,
2085
+ userName: checklist.userName,
2086
+ userEmail: checklist.userEmail,
2087
+ createdByEmail: checklist.userEmail,
2088
+ createdBy: checklist.userName,
2089
+ coverage: checklist.coverage,
2090
+ checkListType: checklist.checkListType,
2091
+ logDetails: {},
2092
+ userType: req.user.userType,
2093
+ };
2094
+ // console.log( 'inserttraxlogs', inserttraxlogs );
2095
+ insertOpenSearchData( openSearch.traxActivityLog, inserttraxlogs );
2009
2096
  }
2010
2097
 
2011
2098
  return res.sendSuccess( 'Task Updated Successfully' );
@@ -2017,6 +2104,189 @@ export async function submitTask( req, res ) {
2017
2104
  return res.sendError( error.message || 'Internal Server Error', 500 );
2018
2105
  }
2019
2106
  }
2107
+ export async function submiteyeTestTask( req, res ) {
2108
+ try {
2109
+ const { body: requestData, user } = req;
2110
+ const { processedcheckListId, date, submittype, currentTime, questionAnswers, streamId, qrCode } = requestData;
2111
+ // console.log( requestData );
2112
+ if ( !streamId ) {
2113
+ return res.sendError( 'streamId is required', 400 );
2114
+ }
2115
+ if ( !qrCode||qrCode=='' ) {
2116
+ return res.sendError( 'qrCode is required', 400 );
2117
+ }
2118
+ let findoldQrExists = await cameraService.findOne( { qrCode: qrCode } );
2119
+ if ( findoldQrExists ) {
2120
+ return res.sendError( 'Qr code already Exists', 400 );
2121
+ }
2122
+ let findQrExists = await cameraService.findOne( { streamName: streamId, qrCode: qrCode } );
2123
+ // console.log( findQrExists );
2124
+ if ( findQrExists ) {
2125
+ return res.sendError( 'Qr code already Exists', 400 );
2126
+ }
2127
+
2128
+ await cameraService.updateOne( { streamName: streamId }, { qrCode: qrCode } );
2129
+ return res.sendSuccess( 'Qr code added successfully' );
2130
+
2131
+ const findQuery = [
2132
+ {
2133
+ $match: {
2134
+ _id: new ObjectId( processedcheckListId ),
2135
+ userId: user._id,
2136
+ date_string: date,
2137
+ },
2138
+ },
2139
+ ];
2140
+
2141
+ const [ checklist ] = await processedTask.aggregate( findQuery );
2142
+
2143
+ if ( !checklist ) {
2144
+ return res.sendError( 'Task edited. Please fill again', 422 );
2145
+ }
2146
+
2147
+ // eslint-disable-next-line camelcase
2148
+ const { checklistStatus, storeName, userEmail, checkListName, store_id } = checklist;
2149
+
2150
+ if ( checklistStatus === 'open' ) {
2151
+ return res.sendError( 'Checklist is in open status. Please start.', 400 );
2152
+ }
2153
+
2154
+ if ( checklistStatus === 'submit' ) {
2155
+ return res.sendError( 'Checklist already submitted', 400 );
2156
+ }
2157
+
2158
+ const storeTimeZone = await storeService.findOne( { storeName }, { 'storeProfile.timeZone': 1 } );
2159
+ const currentDateTime = storeTimeZone?.storeProfile?.timeZone ?
2160
+ dayjs().tz( storeTimeZone.storeProfile.timeZone ) :
2161
+ dayjs();
2162
+
2163
+ const updateQuery = {
2164
+ _id: new ObjectId( processedcheckListId ),
2165
+ userId: user._id,
2166
+ date_string: date,
2167
+ };
2168
+
2169
+ // questionAnswers.forEach( ( section ) => {
2170
+ // section.questions.forEach( ( question ) => {
2171
+ // if ( question.redo === true ) {
2172
+ // question.redo = false;
2173
+ // }
2174
+ // } );
2175
+ // } );
2176
+
2177
+ const updateData = {
2178
+ questionAnswers,
2179
+ updatedAt: dayjs.utc( currentDateTime.format( 'hh:mm:ss A, DD MMM YYYY' ), 'hh:mm:ss A, DD MMM YYYY' ).format(),
2180
+ questionFlag: QuestionFlag( req, res ),
2181
+ };
2182
+
2183
+ if ( submittype === 'draft' ) {
2184
+ logger.info(
2185
+ `v5 => Checklist Save => store Name: ${storeName}, User Email: ${userEmail}, Checklist Name: ${checkListName}`,
2186
+ );
2187
+ updateData.submitTime_string = currentDateTime.format( 'hh:mm A, DD MMM YYYY' );
2188
+ } else if ( submittype === 'submit' ) {
2189
+ logger.info(
2190
+ `v5 => Checklist Submit => store Name: ${storeName}, User Email: ${userEmail}, Checklist Name: ${checkListName}`,
2191
+ );
2192
+ Object.assign( updateData, {
2193
+ submitTime: dayjs.utc( currentDateTime.format( 'hh:mm:ss A, DD MMM YYYY' ), 'hh:mm:ss A, DD MMM YYYY' ).format(),
2194
+ checklistStatus: 'submit',
2195
+ submitMobileTime: currentTime,
2196
+ submitTime_string: currentDateTime.format( 'hh:mm A, DD MMM YYYY' ),
2197
+ } );
2198
+ }
2199
+
2200
+ const updateResult = await processedTask.updateOne( updateQuery, updateData );
2201
+
2202
+ if ( updateResult.modifiedCount > 0 ) {
2203
+ if ( submittype === 'submit' ) {
2204
+ const query1 = [
2205
+ {
2206
+ $match: {
2207
+ userId: user._id,
2208
+ sourceCheckList_id: checklist.sourceCheckList_id,
2209
+ checklistStatus: { $in: [ 'open', 'inprogress' ] },
2210
+ },
2211
+ },
2212
+ {
2213
+ $project: {
2214
+ _id: 1,
2215
+ date_iso: 1,
2216
+ date_string: 1,
2217
+ },
2218
+ },
2219
+ ];
2220
+
2221
+ const processedTasksToDelete = await processedTask.aggregate( query1 );
2222
+
2223
+ if ( processedTasksToDelete?.length ) {
2224
+ const deleteIds = processedTasksToDelete?.filter( ( val ) => val?.date_string !== date )?.map( ( val ) => val?._id );
2225
+
2226
+ if ( deleteIds?.length ) {
2227
+ await processedTask.deleteMany( { _id: { $in: deleteIds } } );
2228
+ }
2229
+ }
2230
+ } else if ( submittype === 'draft' ) {
2231
+ const query2 = [
2232
+ {
2233
+ $match: {
2234
+ _id: checklist.sourceCheckList_id,
2235
+ },
2236
+ },
2237
+ ];
2238
+ const [ taskConfig ] = await taskConfigService.aggregate( query2 );
2239
+
2240
+ if ( taskConfig.scheduleEndTimeISO ) {
2241
+ const endDate = dayjs( taskConfig.scheduleEndTimeISO ).utc();
2242
+ const startDate = dayjs().add( 1, 'day' );
2243
+
2244
+ let currentDate = startDate;
2245
+
2246
+ while ( currentDate.isSameOrBefore( endDate ) ) {
2247
+ const updateDate = currentDate.format( 'YYYY-MM-DD' );
2248
+
2249
+ const updateQuery = {
2250
+ sourceCheckList_id: taskConfig._id,
2251
+ date_string: updateDate,
2252
+ userId: user._id,
2253
+ };
2254
+
2255
+ if ( Object.values( updateQuery ).length === 3 ) {
2256
+ await processedTask.updateOne( updateQuery, { questionAnswers, checklistStatus: 'inprogress' } );
2257
+ }
2258
+
2259
+ currentDate = currentDate.add( 1, 'day' );
2260
+ }
2261
+ }
2262
+ }
2263
+ const logInsertData = {
2264
+ // eslint-disable-next-line camelcase
2265
+ store_id,
2266
+ storeName,
2267
+ action: submittype === 'draft' ? 'saved' : 'submitted',
2268
+ checklistId: checklist.sourceCheckList_id,
2269
+ processedChecklistId: checklist._id,
2270
+ type: checklist.checkListType,
2271
+ checkListName,
2272
+ client_id: user.clientId,
2273
+ redoStatus: requestData?.redoStatus ? true : false,
2274
+ };
2275
+ await checklistLogs.create( logInsertData );
2276
+
2277
+ if ( submittype == 'submit' ) {
2278
+ updateOpenSearchTask( user, requestData );
2279
+ }
2280
+
2281
+ return res.sendSuccess( 'Task Updated Successfully' );
2282
+ } else {
2283
+ return res.sendError( 'Something went wrong. Please try again', 500 );
2284
+ }
2285
+ } catch ( error ) {
2286
+ logger.error( { function: 'submiteyeTestTask', error, body: req.body } );
2287
+ return res.sendError( error.message || 'Internal Server Error', 500 );
2288
+ }
2289
+ }
2020
2290
 
2021
2291
 
2022
2292
  async function updateOpenSearch( user, data ) {
@@ -2288,7 +2558,7 @@ export async function dashboardv1( req, res ) {
2288
2558
 
2289
2559
  return res.sendSuccess( totalResult );
2290
2560
  } catch ( e ) {
2291
- console.log( 'v1 =>', e );
2561
+ // console.log( 'v1 =>', e );
2292
2562
  logger.error( { function: 'dashboardv1', error: e, body: req.body } );
2293
2563
  return res.sendError( e, 500 );
2294
2564
  }
@@ -2463,6 +2733,7 @@ export async function checklistv1( req, res ) {
2463
2733
  client_id: { $ifNull: [ '$client_id', '' ] },
2464
2734
  coverage: { $ifNull: [ '$coverage', '' ] },
2465
2735
  taskType: { $ifNull: [ '$planoType', '' ] },
2736
+ streamId: { $ifNull: [ '$streamId', '' ] },
2466
2737
  },
2467
2738
  },
2468
2739
  ];
@@ -2557,6 +2828,9 @@ export async function questionList( req, res ) {
2557
2828
  storeName: { $ifNull: [ '$storeName', '' ] },
2558
2829
  redoStatus: { $ifNull: [ '$redoStatus', false ] },
2559
2830
  rawImageUpload: { $ifNull: [ '$rawImageUpload', false ] },
2831
+ rawVideoUpload: { $ifNull: [ '$rawVideoUpload', false ] },
2832
+ taskType: { $ifNull: [ '$planoType', '' ] },
2833
+ streamId: { $ifNull: [ '$streamId', '' ] },
2560
2834
  },
2561
2835
  } );
2562
2836
 
@@ -3246,7 +3520,7 @@ export const sendSignInOtpEmail = async ( emailVars ) => {
3246
3520
  const result = await sendEmailWithSES( emailVars.email, subject, html, attachments, ses.adminEmail );
3247
3521
  return result;
3248
3522
  } catch ( error ) {
3249
- logger.error( { error: error, function: 'sendSignInOtpEmail', body: req.body } );
3523
+ logger.error( { error: error, function: 'sendSignInOtpEmail', body: emailVars } );
3250
3524
  return error;
3251
3525
  }
3252
3526
  };