Advertisement
Guest User

Untitled

a guest
Mar 28th, 2019
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. 'use-strict'
  2. // Core libraries required to run firebase
  3. const functions = require('firebase-functions');
  4. const admin = require('firebase-admin');
  5. admin.initializeApp(functions.config().firebase);
  6.  
  7. // To compute SHA value for transaction
  8. // TODO : Send Confirmation mail when transactions is successful.
  9. const sha512 = require('js-sha512');
  10.  
  11. // Libraries required to send mail.
  12. const nodemailer = require("nodemailer");
  13. const gmailEmail = functions.config().gmail.email; // Google environment variable.
  14. const gmailPassword = functions.config().gmail.password; // Google environment variable.
  15. const mailTransport = nodemailer.createTransport({
  16. service: 'gmail',
  17. auth: {
  18. user: gmailEmail,
  19. pass: gmailPassword,
  20. },
  21. });
  22.  
  23. const chartjsNode = require('chartjs-node');
  24.  
  25. // Chartjs-node is used to generate Charts for the pdf
  26. //const chartjsNode = require('chartjs-node');
  27. // Library to create monthly report PDF file.
  28. const pdfkit = require('pdfkit');
  29. const genrateHashMod = require('./generateHash');
  30. const db = admin.firestore();
  31.  
  32. // EventListener listens when a transaction starts
  33. exports.provideHash = functions.firestore.document('TRANSACTIONS/{TRAN_ID}')
  34. .onCreate((snapshot, context) => {
  35. return genrateHashMod.handler(snapshot, context, sha512, db);
  36. })
  37.  
  38. /*
  39. function generateMail() {
  40. // Mail options
  41. const mailOptions = {
  42. from: '"Avas Abacus Co." <noreply@firebase.com>',
  43. to: 'rahulyupz@gmail.com',
  44. subject: 'Monthly Report From Abacus Team.',
  45. text: 'As promised we are providing you with monthly report of your child. On the basis of this report you can make informed decisions. A warmth welcome to became a part of avas team.'
  46. attachments: [
  47. {
  48. filename: 'Report,pdf',
  49. content:
  50. }
  51. ]
  52. };
  53.  
  54. return mailTransport.sendMail(mailOptions)
  55. .then(() => console.log("Mail successfully transfered."))
  56. .catch((error) => console.error('There has been some error', error));
  57. }*/
  58.  
  59. function generatePdf() {
  60. const reportPdf = admin.storage().bucket().file('/test/example.pdf');
  61. const doc = new pdfkit;
  62. doc.pipe(reportPdf.createWriteStream());
  63.  
  64. doc.fontSize(20).text('Avas Abacus Co.', {
  65. align: 'center',
  66. underline: 'true'
  67. });
  68. doc.fontSize(16).text('Monthly Report (March\'19)', {
  69. align: 'center'
  70. });
  71.  
  72. doc.moveDown();
  73. doc.moveDown();
  74.  
  75. for (var i = 0; i<10; i++) {
  76. doc.fontSize(10).text(list[i], {
  77. underline: true
  78. });
  79.  
  80. doc.image(admin.storage().bucket().file('/test/example.png'), {
  81. fit: [450, 500],
  82. align: 'center',
  83. valign: 'center'
  84. });
  85.  
  86. doc.moveDown();
  87.  
  88. doc.fontSize(10)
  89. .text('Total Test Taken : 30', 50, 690);
  90. doc.fontSize(10)
  91. .text('Average Score : 87.45', 50, 700);
  92.  
  93. if( i!==9) {
  94. doc.addPage();
  95. }
  96. }
  97. doc.end();
  98. }
  99.  
  100. var config = {
  101. type: 'line',
  102. data: {
  103. label: ['01', '02', '03', '04','05','06','07','08','09','10', '11', '12', '13', '14','15','16','17','18','19','20 ','21', '22', '23', '24','25','26','27','28','29', '30'],
  104. datasets: [{
  105. label: 'Scores',
  106. data: ['91', '82', '93', '74','85', '66', '77', '88', '79', '80', '91', '82', '93', '74', '85', '66', '77', '88', '79', '80', '91', '82', '93', '74', '85', '66', '77', '88', '79', '80',]
  107. }]
  108. },
  109. options: {
  110. title: {
  111. display: true,
  112. text: 'Report'
  113. },
  114. scales: {
  115. xAxes: [{
  116. display: true,
  117. scaleLabel: {
  118. display: true,
  119. labelString: 'Day'
  120. }
  121. }],
  122. yAxes: [{
  123. display: true,
  124. scaleLabel: {
  125. display: true,
  126. labelString: 'Score'
  127. }
  128. }]
  129. }
  130. }
  131. }
  132.  
  133. function generateChart() {
  134. console.log("generateChart is called.");
  135.  
  136. var chartnode = new chartjsNode(600, 600);
  137. console.log("new object created.");
  138. ` return chartnode.drawChart(config)
  139. .then(() => {
  140. console.log("return buffer.");
  141.  
  142. return chartnode.getImageBuffer('image/png');
  143. })
  144. .then(buffer => {
  145. console.log("return stream.");
  146. Array.isArray(buffer) // => true
  147. return chartnode.getImageStream('image/png');
  148. })
  149. .then(streamResult => {
  150. console.log("return image.");
  151. streamResult.stream // => Stream object
  152. streamResult.length // => Integer length of stream
  153. return chartnode.writeImageToFile('image/png', admin.storage().bucket().file('/test/example.png'));
  154. })
  155. .then(() => {
  156. return generatePdf();
  157. });
  158. }
  159.  
  160. // Https hook to be triggered monhtly.
  161. exports.generateReport = functions.https.onRequest((req, res) => {
  162. console.log("generateFunction called.");
  163. generateChart();
  164. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement