tango-app-api-analysis-traffic 3.1.0-alpha.6 → 3.1.0-alpha.7

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-analysis-traffic",
3
- "version": "3.1.0-alpha.6",
3
+ "version": "3.1.0-alpha.7",
4
4
  "description": "Traffic Analysis",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -18,6 +18,8 @@
18
18
  "dotenv": "^16.4.5",
19
19
  "express": "^4.19.2",
20
20
  "handlebars": "^4.7.8",
21
+ "html-pdf": "^3.0.1",
22
+ "html-pdf-node": "^1.0.8",
21
23
  "joi-to-swagger": "^6.2.0",
22
24
  "lodash": "^4.17.21",
23
25
  "mongodb": "^6.8.0",
@@ -3,15 +3,20 @@ import * as clientService from '../services/clients.services.js';
3
3
  import * as userService from '../services/user.service.js';
4
4
  import * as storeService from '../services/stores.service.js';
5
5
  import { findCamera } from '../services/camera.service.js';
6
- import handlebars, { registerHelpers } from './handlebars.js';
7
6
  const __filename = fileURLToPath( import.meta.url );
8
7
  const __dirname = dirname( __filename );
9
8
  import { fileURLToPath } from 'url';
10
9
  import { dirname } from 'path';
11
10
  import puppeteer from 'puppeteer';
12
- import { readFileSync } from 'fs';
13
- import { join } from 'path';
11
+ // import { readFileSync } from 'fs';
12
+ // import { join } from 'path';
14
13
  import dayjs from 'dayjs';
14
+ import fs from 'fs';
15
+ import path from 'path';
16
+ import htmlpdf from 'html-pdf-node';
17
+ // import pdf from 'html-pdf';
18
+ import Handlebars, { registerHelpers } from './handlebars.js';
19
+
15
20
  registerHelpers();
16
21
 
17
22
  // Lamda Service Call //
@@ -279,7 +284,7 @@ export const sendEmailers = async ( req, res ) => {
279
284
  let storeData = {
280
285
  storeId: getStoreSourceData.storeId,
281
286
  storeName: getStoreSourceData.storeName,
282
- storeLocation: getStoreSourceData.storeProfile.city+' '+getStoreSourceData.storeProfile.pincode,
287
+ storeLocation: getStoreSourceData.storeProfile?.city+' '+getStoreSourceData.storeProfile.pincode,
283
288
  storeCount: emailerSourceData._source.storeIds.length-1,
284
289
  storeBaseImage: cameraBaseImage,
285
290
  };
@@ -632,7 +637,7 @@ async function lamdaAPI3( templateData ) {
632
637
  let newTemplateData = templateData;
633
638
  const overallLamdaURL = 'https://bqk7ozecu7smfrzyb2hbb5lo540avjkl.lambda-url.ap-south-1.on.aws/';
634
639
  let lamdaAPIResultData = await LamdaServiceCall( overallLamdaURL, newTemplateData );
635
- if ( !lamdaAPIResultData || lamdaAPIResultData.status_code !== 200 ) {
640
+ if ( !lamdaAPIResultData || lamdaAPIResultData.status_code !== '200' ) {
636
641
  return '';
637
642
  }
638
643
  return lamdaAPIResultData;
@@ -643,7 +648,6 @@ async function lamdaAPI3( templateData ) {
643
648
  }
644
649
  }
645
650
 
646
-
647
651
  async function lamdaAPI4( templateData ) {
648
652
  try {
649
653
  // let lamdaAPIResultData = {
@@ -755,7 +759,7 @@ async function lamdaAPI4( templateData ) {
755
759
  let newTemplateData = templateData;
756
760
  const overallLamdaURL = 'https://7ozawm36scwmwhh4vzkecqkgv40lpjxt.lambda-url.ap-south-1.on.aws/';
757
761
  let lamdaAPIResultData = await LamdaServiceCall( overallLamdaURL, newTemplateData );
758
- if ( !lamdaAPIResultData || lamdaAPIResultData.status_code !== 200 ) {
762
+ if ( !lamdaAPIResultData || lamdaAPIResultData.status_code !== '200' ) {
759
763
  return '';
760
764
  }
761
765
  return lamdaAPIResultData;
@@ -1127,40 +1131,30 @@ async function getLamdaChartData( templateData ) {
1127
1131
  async function emailerSendEmail( storeData, clientData, lamdaMetrics, lamdaCharts, templateData ) {
1128
1132
  try {
1129
1133
  let hbsFileName = '../hbs/dailyMailerSingle.hbs';
1130
- if ( templateData.allStores.length == 1 && templateData.templateType == 'daily' ) {
1131
- // // Call Lamda API No 1 Single Store Single Date
1134
+ if ( templateData.allStores.length === 1 && templateData.templateType === 'daily' ) {
1132
1135
  hbsFileName = '../hbs/dailyMailerSingle.hbs';
1133
- } else if ( templateData.allStores.length > 1 && templateData.templateType == 'daily' ) {
1134
- // // Call Lamda API No 3 Multiple Store Single Date
1136
+ } else if ( templateData.allStores.length > 1 && templateData.templateType === 'daily' ) {
1135
1137
  hbsFileName = '../hbs/dailyMailerMultiple.hbs';
1136
- } else if ( templateData.allStores.length == 1 && templateData.templateType == 'weekly' ) {
1137
- // // Call Lamda API No 2 Single Store Multiple Date
1138
+ } else if ( templateData.allStores.length === 1 && templateData.templateType === 'weekly' ) {
1138
1139
  hbsFileName = '../hbs/weeklyMailerSingle.hbs';
1139
- } else if ( templateData.allStores.length > 1 && templateData.templateType == 'weekly' ) {
1140
- // // Call Lamda API No 4 Multiple Store Multiple Date
1140
+ } else if ( templateData.allStores.length > 1 && templateData.templateType === 'weekly' ) {
1141
1141
  hbsFileName = '../hbs/weeklyMailerMultiple.hbs';
1142
- } else if ( templateData.allStores.length == 1 && templateData.templateType == 'monthly' ) {
1143
- // // Call Lamda API No 5 Single Store Multiple Date
1142
+ } else if ( templateData.allStores.length === 1 && templateData.templateType === 'monthly' ) {
1144
1143
  hbsFileName = '../hbs/dailyMailerSingle.hbs';
1145
- } else if ( templateData.allStores.length > 1 && templateData.templateType == 'monthly' ) {
1146
- // // Call Lamda API No 6 Multiple Store Multiple Date
1144
+ } else if ( templateData.allStores.length > 1 && templateData.templateType === 'monthly' ) {
1147
1145
  hbsFileName = '../hbs/dailyMailerSingle.hbs';
1148
- } else {
1149
- // Default Value
1150
1146
  }
1151
- // Prepare email with Handlebars template
1152
- const fileContent = readFileSync(
1153
- join( __dirname, hbsFileName ),
1154
- 'utf8',
1155
- );
1156
- const htmlContent = handlebars.compile( fileContent );
1157
1147
 
1158
- const emailVars = {
1159
- // email: inputData.email,
1148
+ // Prepare email with Handlebars template
1149
+ const hbsFilePath = path.join( __dirname, hbsFileName );
1150
+ const templateHtml = fs.readFileSync( hbsFilePath, 'utf8' );
1151
+ const template = Handlebars.compile( templateHtml );
1152
+ console.log( 'JSON.parse( process.env.URL ).apiDomain', JSON.parse( process.env.URL ).apiDomain );
1153
+ const html = template( { data: {
1160
1154
  clientName: clientData.clientName,
1161
- domain: JSON.parse( process.env.URL ).apiDomain,
1162
- overallChartImage: lamdaCharts.dailyData, // Use the S3 URL for the overall chart
1163
- hourlyChartImage: lamdaCharts.hourlyData, // Use the S3 URL for the hourly chart
1155
+ domain: JSON.parse( process.env.URL ).domain,
1156
+ overallChartImage: lamdaCharts.dailyData,
1157
+ hourlyChartImage: lamdaCharts.hourlyData,
1164
1158
  lamdaMetrics: lamdaMetrics,
1165
1159
  storeData: storeData,
1166
1160
  templateData: templateData,
@@ -1173,29 +1167,43 @@ async function emailerSendEmail( storeData, clientData, lamdaMetrics, lamdaChart
1173
1167
  missedOpportunityCalculation: clientData?.featureConfigs?.missedOpportunityCalculation || 'engagers-conversion',
1174
1168
  bufferTime: 30,
1175
1169
  },
1170
+ } } );
1171
+
1172
+
1173
+ let file = { content: html };
1174
+ let options = {
1175
+ format: 'A4',
1176
+ margin: {
1177
+ top: '0.5in',
1178
+ right: '0.5in',
1179
+ bottom: '0.5in',
1180
+ left: '0.5in',
1181
+ },
1182
+ printBackground: true,
1183
+ preferCSSPageSize: true,
1176
1184
  };
1177
1185
 
1178
- const html = htmlContent( { data: emailVars } );
1186
+ let pdfBuffer = await htmlpdf.generatePdf( file, options );
1187
+ // console.log( 'PDF generated successfully' );
1179
1188
 
1180
- // Prepare email subject and send the email
1181
1189
  const subject = `Footfall Trend Report for ${clientData.clientName}`;
1182
1190
  const ses = JSON.parse( process.env.SES );
1183
1191
 
1184
- // templateData.userEmail
1185
1192
  await sendEmailWithSES(
1186
- 'keerthivasan@tangotech.co.in',
1193
+ 'nafila@tangotech.co.in',
1187
1194
  subject,
1188
1195
  html,
1189
- '',
1196
+ {
1197
+ filename: `Attachment_${Date.now()}.pdf`,
1198
+ content: pdfBuffer,
1199
+ contentType: 'application/pdf',
1200
+ },
1190
1201
  ses.adminEmail,
1191
1202
  );
1192
1203
 
1193
- // return res.sendSuccess( { result: 'EMAIL-SENT' } );
1194
-
1195
1204
  return true;
1196
1205
  } catch ( error ) {
1197
- // console.log( 'error emailerSendEmail=>', error );
1198
- logger.error( { error: error, function: 'emailerSendEmail' } );
1206
+ console.error( 'Error in emailerSendEmail:', error );
1199
1207
  return false;
1200
1208
  }
1201
1209
  }
@@ -1718,180 +1726,3 @@ const footfallTrend = async ( data ) => {
1718
1726
 
1719
1727
  return weeklyChartBuffer;
1720
1728
  };
1721
-
1722
-
1723
- // const footfallTrend = async ( data ) => {
1724
- // console.log( 'Generating footfall chart...' );
1725
-
1726
- // const browser = await puppeteer.launch( { headless: true } );
1727
- // const page = await browser.newPage();
1728
-
1729
- // const chartHtmlContent = `
1730
- // <!DOCTYPE html>
1731
- // <html>
1732
- // <head>
1733
- // <style>
1734
- // #Footfalltrend {
1735
- // width: 100%;
1736
- // height: 600px;
1737
- // }
1738
- // </style>
1739
- // <script src="https://cdn.amcharts.com/lib/5/index.js"></script>
1740
- // <script src="https://cdn.amcharts.com/lib/5/xy.js"></script>
1741
- // <script src="https://cdn.amcharts.com/lib/5/themes/Animated.js"></script>
1742
- // </head>
1743
- // <body>
1744
- // <div id="Footfalltrend"></div>
1745
- // <script>
1746
- // am5.ready(function() {
1747
- // const root = am5.Root.new('Footfalltrend');
1748
- // root.setThemes([am5themes_Animated.new(root)]);
1749
-
1750
- // const chart = root.container.children.push(
1751
- // am5xy.XYChart.new(root, {
1752
- // panX: false,
1753
- // panY: false,
1754
- // wheelX: 'panX',
1755
- // wheelY: 'zoomX',
1756
- // layout: root.verticalLayout
1757
- // })
1758
- // );
1759
-
1760
- // chart.set("scrollbarX", am5.Scrollbar.new(root, {
1761
- // orientation: "horizontal"
1762
- // }));
1763
-
1764
- // const xRenderer = am5xy.AxisRendererX.new(root, {
1765
- // minGridDistance: 20,
1766
- // minorGridEnabled: true,
1767
- // });
1768
-
1769
- // xRenderer.labels.template.setAll({
1770
- // rotation: -45,
1771
- // centerY: am5.p50,
1772
- // centerX: am5.p100,
1773
- // paddingRight: 5,
1774
- // });
1775
-
1776
- // const xAxis = chart.xAxes.push(
1777
- // am5xy.CategoryAxis.new(root, {
1778
- // categoryField: "storeName",
1779
- // renderer: xRenderer,
1780
- // tooltip: am5.Tooltip.new(root, { labelText: "{fullStoreName}" })
1781
- // })
1782
- // );
1783
-
1784
- // const formattedData = ${JSON.stringify( data )}.map((item) => ({
1785
- // fullStoreName: item.storeName,
1786
- // storeName: item.storeName,
1787
- // footfall: Number(item.footfall_count),
1788
- // conversion: Number(item.conversion_count),
1789
- // bounced: Number(item.bounced_count),
1790
- // engagers: Number(item.engagers_count),
1791
- // missedopportunities: Number(item.missedOpportunity_count),
1792
- // }));
1793
-
1794
- // formattedData.sort((a, b) => {
1795
- // const dateA = new Date(a.storeName);
1796
- // const dateB = new Date(b.storeName);
1797
- // return dateA - dateB;
1798
- // });
1799
-
1800
- // xAxis.data.setAll(formattedData);
1801
-
1802
- // const yAxis = chart.yAxes.push(
1803
- // am5xy.ValueAxis.new(root, {
1804
- // min: 0,
1805
- // renderer: am5xy.AxisRendererY.new(root, {})
1806
- // })
1807
- // );
1808
-
1809
- // yAxis.children.unshift(am5.Label.new(root, {
1810
- // rotation: -90,
1811
- // text: "Footfall",
1812
- // y: am5.p50,
1813
- // centerX: am5.p50,
1814
- // fontFamily: "Arial",
1815
- // fontSize: "14px",
1816
- // fill: am5.color(0x000000)
1817
- // }));
1818
-
1819
- // const createColumnSeries = (name, field, color) => {
1820
- // const series = chart.series.push(
1821
- // am5xy.ColumnSeries.new(root, {
1822
- // name: name,
1823
- // xAxis: xAxis,
1824
- // yAxis: yAxis,
1825
- // valueYField: field,
1826
- // categoryXField: "storeName",
1827
- // fill: color,
1828
- // stroke: color,
1829
- // stacked: true,
1830
- // })
1831
- // );
1832
-
1833
- // series.data.setAll(formattedData);
1834
-
1835
- // series.bullets.push(() => {
1836
- // return am5.Bullet.new(root, {
1837
- // sprite: am5.Label.new(root, {
1838
- // text: "{valueY}",
1839
- // fill: root.interfaceColors.get("alternativeText"),
1840
- // centerY: am5.p50,
1841
- // centerX: am5.p50,
1842
- // populateText: true,
1843
- // fontSize: 12 // Reduce font size
1844
- // })
1845
- // });
1846
- // });
1847
-
1848
- // series.columns.template.setAll({
1849
- // tooltipText: "{name}: {valueY}",
1850
- // width: am5.percent(90),
1851
- // tooltipY: 0,
1852
- // });
1853
-
1854
- // series.appear(1000);
1855
- // return series;
1856
- // };
1857
-
1858
- // createColumnSeries("Bounced", "bounced", am5.color(0x67b7dc));
1859
- // createColumnSeries("Missed Opportunity", "missedopportunities", am5.color(0x6794dc));
1860
- // createColumnSeries("Conversion", "conversion", am5.color(0x6771dc));
1861
-
1862
- // const legend = chart.children.push(
1863
- // am5.Legend.new(root, {
1864
- // centerX: am5.p50,
1865
- // x: am5.p50
1866
- // })
1867
- // );
1868
-
1869
- // legend.data.setAll(chart.series.values);
1870
-
1871
- // chart.set("cursor", am5xy.XYCursor.new(root, {
1872
- // behavior: "none"
1873
- // }));
1874
-
1875
- // chart.appear(1000, 100);
1876
- // });
1877
- // </script>
1878
- // </body>
1879
- // </html>
1880
- // `;
1881
- // let weeklyChartBuffer;
1882
- // try {
1883
- // await page.setContent( chartHtmlContent, { waitUntil: 'domcontentloaded' } );
1884
- // await page.waitForSelector( '#Footfalltrend', { visible: true } );
1885
- // weeklyChartBuffer = await page.screenshot( { encoding: 'binary' } );
1886
- // // await page.screenshot( { path: chartImagePath } );
1887
- // // console.log( 'Screenshot saved to:', chartImagePath );
1888
- // // console.log( 'Screenshot saved to:', chartImagePath );
1889
- // } catch ( err ) {
1890
- // console.error( 'Error generating chart screenshot:', err );
1891
- // throw new Error( 'Failed to generate chart screenshot' );
1892
- // } finally {
1893
- // await browser.close();
1894
- // }
1895
-
1896
- // return weeklyChartBuffer;
1897
- // };