-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
121 lines (100 loc) · 2.9 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import express from "express";
import puppeteer from "puppeteer";
import cors from "cors";
import nodemailer from "nodemailer";
import { getAuth0AccessToken } from "./auth0.js";
import axios from "axios";
const app = express();
const port = process.env.PORT; // Port number for the server
const corsOptions = {
origin: process.env.PUBLIC_APP_URL,
};
app.use(cors(corsOptions));
app.use(express.json());
const exportDashboard = async (url) => {
const accessToken = await getAuth0AccessToken();
const browser = await puppeteer.launch({
executablePath: "/usr/bin/chromium-browser",
args: [
"--no-sandbox",
"--headless",
"--disable-gpu",
"--disable-dev-shm-usage",
],
});
const page = await browser.newPage();
await page.setExtraHTTPHeaders({
Authorization: `Bearer ${accessToken}`,
});
await page.goto(`${process.env.APP_URL}/${url}`, {
waitUntil: "networkidle0",
});
await page.setViewport({ width: 1080, height: 1920, deviceScaleFactor: 2 });
await page.emulateMediaType("screen");
const pdfBuffer = await page.pdf({
format: "A4",
printBackground: true,
});
await browser.close();
return pdfBuffer;
};
const transporter = nodemailer.createTransport({
host: process.env.SENDGRID_HOST,
port: 587,
auth: {
user: "apikey",
pass: process.env.SENDGRID_API_KEY,
},
from: process.env.SENDGRID_SENDER_EMAIL,
});
app.post("/export", async (req, res) => {
const body = req.body;
const dashboardUrl = body?.dashboardUrl;
if (!dashboardUrl) {
return res.status(400).send("Please provide a valid URL");
}
const pdfBuffer = await exportDashboard(dashboardUrl);
const buffer = Buffer.from(pdfBuffer);
res.set({
"Content-Type": "application/pdf",
"Content-Disposition": 'inline; filename="file.pdf"',
"Content-Length": buffer.length,
});
res.send(buffer);
});
// Receive invoices files, receive customer data, receive organization data
app.post("/send-email", async (req, res) => {
const { body } = req.body;
console.log(body);
if (!body.from || !body.to || !body.subject || !body.text || !body.html) {
return res.status(400).send("Please provide all the required params");
}
const from = `${body.from} <${process.env.SENDGRID_SENDER_EMAIL}>`;
const getPdfFiles = await Promise.all(
body.attachments.map(async ({ filename, contentUrl }) => {
const pdfFile = await axios.get(contentUrl, {
responseType: "arraybuffer",
});
return {
filename,
content: pdfFile.data,
};
})
);
const sentEmail = await transporter.sendMail({
from,
to: body.to,
subject: body.subject,
text: body.text,
html: body.html,
attachments: getPdfFiles,
});
res.send(`Message sent: ${sentEmail}`);
});
app.get("/health", (req, res) => {
res.send("OK");
res.status(200);
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});