Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use-strict'
- // Core libraries required to run firebase
- const functions = require('firebase-functions');
- const admin = require('firebase-admin');
- admin.initializeApp(functions.config().firebase);
- // To compute SHA value for transaction
- // TODO : Send Confirmation mail when transactions is successful.
- const sha512 = require('js-sha512');
- // Libraries required to send mail.
- const nodemailer = require("nodemailer");
- const gmailEmail = functions.config().gmail.email; // Google environment variable.
- const gmailPassword = functions.config().gmail.password; // Google environment variable.
- const mailTransport = nodemailer.createTransport({
- service: 'gmail',
- auth: {
- user: gmailEmail,
- pass: gmailPassword,
- },
- });
- const chartjsNode = require('chartjs-node');
- // Chartjs-node is used to generate Charts for the pdf
- //const chartjsNode = require('chartjs-node');
- // Library to create monthly report PDF file.
- const pdfkit = require('pdfkit');
- const genrateHashMod = require('./generateHash');
- const db = admin.firestore();
- // EventListener listens when a transaction starts
- exports.provideHash = functions.firestore.document('TRANSACTIONS/{TRAN_ID}')
- .onCreate((snapshot, context) => {
- return genrateHashMod.handler(snapshot, context, sha512, db);
- })
- /*
- function generateMail() {
- // Mail options
- const mailOptions = {
- from: '"Avas Abacus Co." <noreply@firebase.com>',
- to: 'rahulyupz@gmail.com',
- subject: 'Monthly Report From Abacus Team.',
- 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.'
- attachments: [
- {
- filename: 'Report,pdf',
- content:
- }
- ]
- };
- return mailTransport.sendMail(mailOptions)
- .then(() => console.log("Mail successfully transfered."))
- .catch((error) => console.error('There has been some error', error));
- }*/
- function generatePdf() {
- const reportPdf = admin.storage().bucket().file('/test/example.pdf');
- const doc = new pdfkit;
- doc.pipe(reportPdf.createWriteStream());
- doc.fontSize(20).text('Avas Abacus Co.', {
- align: 'center',
- underline: 'true'
- });
- doc.fontSize(16).text('Monthly Report (March\'19)', {
- align: 'center'
- });
- doc.moveDown();
- doc.moveDown();
- for (var i = 0; i<10; i++) {
- doc.fontSize(10).text(list[i], {
- underline: true
- });
- doc.image(admin.storage().bucket().file('/test/example.png'), {
- fit: [450, 500],
- align: 'center',
- valign: 'center'
- });
- doc.moveDown();
- doc.fontSize(10)
- .text('Total Test Taken : 30', 50, 690);
- doc.fontSize(10)
- .text('Average Score : 87.45', 50, 700);
- if( i!==9) {
- doc.addPage();
- }
- }
- doc.end();
- }
- var config = {
- type: 'line',
- data: {
- 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'],
- datasets: [{
- label: 'Scores',
- 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',]
- }]
- },
- options: {
- title: {
- display: true,
- text: 'Report'
- },
- scales: {
- xAxes: [{
- display: true,
- scaleLabel: {
- display: true,
- labelString: 'Day'
- }
- }],
- yAxes: [{
- display: true,
- scaleLabel: {
- display: true,
- labelString: 'Score'
- }
- }]
- }
- }
- }
- function generateChart() {
- console.log("generateChart is called.");
- var chartnode = new chartjsNode(600, 600);
- console.log("new object created.");
- ` return chartnode.drawChart(config)
- .then(() => {
- console.log("return buffer.");
- return chartnode.getImageBuffer('image/png');
- })
- .then(buffer => {
- console.log("return stream.");
- Array.isArray(buffer) // => true
- return chartnode.getImageStream('image/png');
- })
- .then(streamResult => {
- console.log("return image.");
- streamResult.stream // => Stream object
- streamResult.length // => Integer length of stream
- return chartnode.writeImageToFile('image/png', admin.storage().bucket().file('/test/example.png'));
- })
- .then(() => {
- return generatePdf();
- });
- }
- // Https hook to be triggered monhtly.
- exports.generateReport = functions.https.onRequest((req, res) => {
- console.log("generateFunction called.");
- generateChart();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement