tango-app-api-trax 3.7.52 → 3.7.53

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tango-app-api-trax",
3
- "version": "3.7.52",
3
+ "version": "3.7.53",
4
4
  "description": "Trax",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -23,11 +23,12 @@
23
23
  "firebase-admin": "^13.0.0",
24
24
  "fs": "^0.0.1-security",
25
25
  "handlebars": "^4.7.8",
26
+ "html-pdf": "^3.0.1",
26
27
  "lodash": "^4.17.21",
27
28
  "mongodb": "^6.8.0",
28
29
  "nodemon": "^3.1.4",
29
30
  "path": "^0.12.7",
30
- "tango-api-schema": "^2.5.59",
31
+ "tango-api-schema": "^2.5.61",
31
32
  "tango-app-api-middleware": "^3.5.2",
32
33
  "url": "^0.11.4",
33
34
  "winston": "^3.13.1",
@@ -0,0 +1,11 @@
1
+ import Handlebars from 'handlebars';
2
+
3
+ Handlebars.registerHelper( 'eq', function( arg1, arg2, options ) {
4
+ return ( arg1 == arg2 ) ? options.fn( this ) : options.inverse( this );
5
+ } );
6
+
7
+ Handlebars.registerHelper( 'neq', function( a, b, options ) {
8
+ return a !== b ? options.fn( this ) : options.inverse( this );
9
+ } );
10
+
11
+ export default Handlebars;
@@ -3203,9 +3203,37 @@ export async function checklistTaskSubmissionDetails( req, res ) {
3203
3203
  }
3204
3204
  let details = [];
3205
3205
  if ( req.body.type == 'checklist' ) {
3206
- details = await processedchecklist.find( { sourceCheckList_id: req.body.checklistId, date_iso: new Date( dayjs( req.body.date ).format( 'YYYY-MM-DD' ) ) }, { submitTime: '$submitTime_string', status: '$checklistStatus', approvalStatus: '$approvalStatus', approvalTime: '$approvalTime_string', _id: 0 } );
3206
+ details = await processedchecklist.find( { sourceCheckList_id: req.body.checklistId, checklistStatus: 'submit', date_iso: new Date( dayjs( req.body.date ).format( 'YYYY-MM-DD' ) ) }, {
3207
+ date_string: 1,
3208
+ checkListName: 1,
3209
+ createdByName: 1,
3210
+ store_id: 1,
3211
+ storeName: 1,
3212
+ submmitedBy: '$userName',
3213
+ submmitedByEmail: '$userEmail',
3214
+ checklistStatus: 1,
3215
+ submitTime: 1,
3216
+ approvalTime: 1,
3217
+ approvalTime_string: 1,
3218
+ approvalByName: 1,
3219
+ approvalByEmail: 1,
3220
+ } );
3207
3221
  } else {
3208
- details = await processedTaskService.find( { sourceCheckList_id: req.body.checklistId, date_iso: new Date( dayjs( req.body.date ).format( 'YYYY-MM-DD' ) ) }, { submitTime: '$submitTime_string', status: '$checklistStatus', approvalStatus: '$approvalStatus', approvalTime: '$approvalTime_string', _id: 0 } );
3222
+ details = await processedTaskService.find( { sourceCheckList_id: req.body.checklistId, checklistStatus: 'submit', date_iso: new Date( dayjs( req.body.date ).format( 'YYYY-MM-DD' ) ) }, {
3223
+ date_string: 1,
3224
+ checkListName: 1,
3225
+ createdByName: 1,
3226
+ store_id: 1,
3227
+ storeName: 1,
3228
+ submmitedBy: '$userName',
3229
+ submmitedByEmail: '$userEmail',
3230
+ checklistStatus: 1,
3231
+ submitTime: 1,
3232
+ approvalTime: 1,
3233
+ approvalTime_string: 1,
3234
+ approvalByName: 1,
3235
+ approvalByEmail: 1,
3236
+ } );
3209
3237
  }
3210
3238
  return res.sendSuccess( details );
3211
3239
  } catch ( e ) {
@@ -21,12 +21,19 @@ import * as clientService from '../services/clients.services.js';
21
21
  import { create } from '../services/authentication.service.js';
22
22
  import { readFileSync } from 'fs';
23
23
  import { join } from 'path';
24
- import handlebars from 'handlebars';
24
+ // import handlebars from 'handlebars';
25
25
  dayjs.extend( customParseFormat );
26
26
  dayjs.extend( timeZone );
27
27
  import isSameOrBefore from 'dayjs/plugin/isSameOrBefore.js';
28
28
  import * as cameraService from '../services/camera.service.js';
29
29
  dayjs.extend( isSameOrBefore );
30
+ import * as pdf from 'html-pdf';
31
+ import handlebars from './handlebar-helper.js';
32
+ import fs from 'fs';
33
+ import { fileURLToPath } from 'url';
34
+ import path from 'path';
35
+ const __filename = fileURLToPath( import.meta.url );
36
+ const __dirname = path.dirname( __filename );
30
37
 
31
38
  export async function storeList( req, res ) {
32
39
  try {
@@ -1082,7 +1089,7 @@ export async function sopMobilechecklistMultiSectionFormatterv1( req, res, next
1082
1089
 
1083
1090
  if ( requestData.submittype == 'submit' ) {
1084
1091
  reqAnswers.forEach( ( reqA ) => {
1085
- if ( ![ 'multiplechoicemultiple', 'multipleImage' ].includes( reqA?.answerType ) ) {
1092
+ if ( ![ 'multiplechoicemultiple', 'multipleImage' ].includes( reqA?.answerType ) && ( reqA.answerType == 'dropDown' && !reqA.allowMultiple ) ) {
1086
1093
  if ( ( !reqA.linkType && ( reqA.answer == null || reqA.answer == '' ) ) || ( reqA.linkType && reqA.linkquestionenabled && ( reqA.answer == null || reqA.answer == '' ) ) ) {
1087
1094
  return res.sendError( 'Please Fill All Fields', 400 );
1088
1095
  }
@@ -1220,7 +1227,7 @@ export async function sopMobilechecklistMultiSectionFormatterv1( req, res, next
1220
1227
  structure.redoComment = qaAnswers[j]?.redoComment;
1221
1228
  };
1222
1229
  newArray.push( structure );
1223
- } else if ( qaAnswers[j].answerType == 'multiplechoicesingle' ) {
1230
+ } else if ( qaAnswers[j].answerType == 'multiplechoicesingle' || ( qaAnswers[j].answerType == 'dropdown' && !qaAnswers[j].allowMultiple ) ) {
1224
1231
  let qaans = qaAnswers[j].answers;
1225
1232
  let ms = [];
1226
1233
  for ( let k = 0; k < qaans.length; k++ ) {
@@ -1276,7 +1283,7 @@ export async function sopMobilechecklistMultiSectionFormatterv1( req, res, next
1276
1283
  structure.redoComment = qaAnswers[j]?.redoComment;
1277
1284
  };
1278
1285
  newArray.push( structure );
1279
- } else if ( qaAnswers[j].answerType == 'multiplechoicemultiple' ) {
1286
+ } else if ( qaAnswers[j].answerType == 'multiplechoicemultiple' || ( qaAnswers[j].answerType == 'dropdown' && qaAnswers[j].allowMultiple ) ) {
1280
1287
  let qaans = qaAnswers[j].answers;
1281
1288
  let mcmo = [];
1282
1289
  for ( let k = 0; k < qaans.length; k++ ) {
@@ -4484,3 +4491,133 @@ export async function chunkUpload( req, res ) {
4484
4491
  return res.sendError( e, 500 );
4485
4492
  }
4486
4493
  }
4494
+
4495
+ export async function downloadChecklist( req, res ) {
4496
+ try {
4497
+ let requestData = req.body;
4498
+
4499
+ if ( !requestData.checklistId ) {
4500
+ res.sendBadRequest( 'checklistId is Required' );
4501
+ }
4502
+
4503
+ let checklistDetails;
4504
+
4505
+ if ( requestData.type == 'checklist' ) {
4506
+ checklistDetails = await processedchecklist.findOne( { _id: requestData.checklistId, checklistStatus: 'submit' } );
4507
+ } else {
4508
+ checklistDetails = await processedTask.findOne( { _id: requestData.checklistId, checklistStatus: 'submit' } );
4509
+ }
4510
+
4511
+ let storeDetails = await storeService.findOne( { storeId: checklistDetails.store_id }, { storeProfile: 1 } );
4512
+ if ( !storeDetails ) {
4513
+ return res.sendError( 'No data found', 204 );
4514
+ }
4515
+
4516
+ let clientDetails = await clientService.findOne( { clientId: checklistDetails.client_id } );
4517
+
4518
+ if ( !clientDetails ) {
4519
+ return res.sendError( 'No client found', 204 );
4520
+ }
4521
+
4522
+ checklistDetails = { ...checklistDetails.toObject(), ...storeDetails?.toObject()?.storeProfile };
4523
+
4524
+ console.log( checklistDetails );
4525
+
4526
+ let scheduleDateTime = dayjs( checklistDetails.submitTime );
4527
+ checklistDetails.submitDate = scheduleDateTime.format( 'DD MMM, YYYY' );
4528
+ checklistDetails.submitTime = scheduleDateTime.format( 'hh:mm A' );
4529
+ // if ( checklistDetails?.brandDetails?.brandLogo ) {
4530
+ // let bucketpath = req.user.client_id + '/logo';
4531
+
4532
+ // let params = {
4533
+ // bucketName: JSON.parse( process.env.BUCKET )?.sop,
4534
+ // filePath: `${bucketpath}/${checklistDetails?.brandDetails?.brandLogo}`,
4535
+ // };
4536
+ // let url = await signedUrl( params.filePath, params.bucketName );
4537
+
4538
+ // let brandImage = await convertUrltoBase64( url );
4539
+ // if ( brandImage ) {
4540
+ // checklistDetails.brandLogo = brandImage;
4541
+ // }
4542
+ // }
4543
+ // checklistDetails.brandName = checklistDetails.brandDetails.brandName;
4544
+ for ( let section of checklistDetails.questionAnswers ) {
4545
+ for ( let question of section.questions ) {
4546
+ question.remarks = question.remarks == null || question.remarks == 'null' ? '' : question.remarks;
4547
+ for ( let answer of question.userAnswer ) {
4548
+ if ( answer.referenceImage != '' ) {
4549
+ let inputData = {
4550
+ Bucket: JSON.parse( process.env.BUCKET )?.sop,
4551
+ file_path: answer.referenceImage,
4552
+ };
4553
+ let url = await signedUrl( inputData );
4554
+ const base64Image = await convertUrltoBase64( url );
4555
+ if ( base64Image ) {
4556
+ answer.referenceImage = base64Image;
4557
+ }
4558
+ }
4559
+ if ( answer.validationType == 'Capture Image' && answer.validationAnswer != '' ) {
4560
+ let inputData = {
4561
+ Bucket: JSON.parse( process.env.BUCKET )?.sop,
4562
+ file_path: answer.validationAnswer,
4563
+ };
4564
+ let url = await signedUrl( inputData );
4565
+ const base64Image = await convertUrltoBase64( url );
4566
+ if ( base64Image ) {
4567
+ answer.validationAnswer = base64Image;
4568
+ }
4569
+ }
4570
+ if ( answer.answer != '' && [ 'image', 'descriptiveImage' ].includes( question.answerType ) ) {
4571
+ let inputData = {
4572
+ Bucket: JSON.parse( process.env.BUCKET )?.sop,
4573
+ file_path: answer.answer,
4574
+ };
4575
+ let url = await signedUrl( inputData );
4576
+ const base64Image = await convertUrltoBase64( url );
4577
+ if ( base64Image ) {
4578
+ answer.answer = base64Image;
4579
+ }
4580
+ }
4581
+ }
4582
+ }
4583
+ }
4584
+ const targetFolder = path.join( __dirname, '..', '/hbs/template.hbs' );
4585
+ const templateHtml = fs.readFileSync( targetFolder, 'utf8' );
4586
+ const template = handlebars.compile( templateHtml );
4587
+ const html = template( { data: checklistDetails } );
4588
+ pdf.create( html ).toBuffer( ( err, buffer ) => {
4589
+ if ( err ) {
4590
+ res.status( 500 ).send( 'Error Generating PDF' );
4591
+ } else {
4592
+ res.setHeader( 'Content-Disposition', 'attachment; filename=checkListDetails.pdf' );
4593
+ res.contentType( 'application/pdf' );
4594
+ res.send( buffer );
4595
+ }
4596
+ } );
4597
+ } catch ( e ) {
4598
+ console.log( 'getChecklistQuestionAnswers =>', e );
4599
+ return false;
4600
+ }
4601
+ }
4602
+
4603
+ async function convertUrltoBase64( url ) {
4604
+ try {
4605
+ const response = await fetch( url );
4606
+
4607
+ if ( !response.ok ) {
4608
+ throw new Error( 'Failed to fetch image' );
4609
+ }
4610
+
4611
+ const contentType = response.headers.get( 'content-type' );
4612
+ const arrayBuffer = await response.arrayBuffer();
4613
+
4614
+ const base64Image =
4615
+ `data:${contentType};base64,` +
4616
+ Buffer.from( arrayBuffer ).toString( 'base64' );
4617
+
4618
+ return base64Image;
4619
+ } catch ( error ) {
4620
+ // console.error( 'Error fetching image:', error.message );
4621
+ return false;
4622
+ }
4623
+ }