jp-shared 1.1.10 → 1.1.12
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 +1 -1
- package/utils/index.js +1 -0
- package/utils/sendInvoice-utils.js +135 -0
package/package.json
CHANGED
package/utils/index.js
CHANGED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
const nodemailer = require("nodemailer");
|
|
2
|
+
const puppeteer = require("puppeteer-core");
|
|
3
|
+
const chromium = require("chrome-aws-lambda");
|
|
4
|
+
const localPuppeteer = require("puppeteer");
|
|
5
|
+
const { invoiceEmailTemplate } = require("./invoiceTemplate");
|
|
6
|
+
const { ToWords } = require("to-words");
|
|
7
|
+
const { OUR_ORG_INFO } = require("../constants");
|
|
8
|
+
const { invoiceSchema } = require("../../../src/validators");
|
|
9
|
+
const { isProduction, isTest } = require("jp-shared/utils/env-utils");
|
|
10
|
+
const toWords = new ToWords();
|
|
11
|
+
|
|
12
|
+
const generatePDF = async (htmlContent) => {
|
|
13
|
+
try {
|
|
14
|
+
// from below is for local development
|
|
15
|
+
// const browser = await localPuppeteer.launch();
|
|
16
|
+
|
|
17
|
+
//Launch browser with necessary configuration for Firebase or serverless environments
|
|
18
|
+
const browser = await puppeteer.launch({
|
|
19
|
+
executablePath: await chromium.executablePath, // Path to Chromium for Firebase/Serverless
|
|
20
|
+
args: chromium.args, // Required arguments for running headless
|
|
21
|
+
headless: chromium.headless, // Ensure headless mode is enabled
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// const browser = isProduction() || isTest() ? await puppeteer.launch({
|
|
25
|
+
// executablePath: await chromium.executablePath, // Path to Chromium for Firebase/Serverless
|
|
26
|
+
// args: chromium.args, // Required arguments for running headless
|
|
27
|
+
// headless: chromium.headless, // Ensure headless mode is enabled
|
|
28
|
+
// }) : await localPuppeteer.launch();
|
|
29
|
+
|
|
30
|
+
// Create a new page in the browser
|
|
31
|
+
const page = await browser.newPage();
|
|
32
|
+
|
|
33
|
+
// Set the HTML content dynamically passed to this function
|
|
34
|
+
await page.setContent(htmlContent);
|
|
35
|
+
|
|
36
|
+
// Generate the PDF from the page's content
|
|
37
|
+
const pdfBuffer = await page.pdf({
|
|
38
|
+
format: "A4", // Set paper format to A4 (you can customize this if needed)
|
|
39
|
+
printBackground: true, // Include background images or colors in the PDF
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// Close the browser once done
|
|
43
|
+
await browser.close();
|
|
44
|
+
|
|
45
|
+
// Return the PDF as a buffer
|
|
46
|
+
return pdfBuffer;
|
|
47
|
+
} catch (error) {
|
|
48
|
+
console.error("Error generating PDF:", error);
|
|
49
|
+
throw error; // Re-throw the error for further handling
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
const sendInvoice = async (invoiceInput) => {
|
|
53
|
+
|
|
54
|
+
// Validate input
|
|
55
|
+
const { error, value } = invoiceSchema.validate(invoiceInput);
|
|
56
|
+
if (error) throw new Error(`Invalid invoice input: ${error.message}`);
|
|
57
|
+
|
|
58
|
+
// Destructure the validated value
|
|
59
|
+
const {
|
|
60
|
+
recruiterEmail,
|
|
61
|
+
gstAmount,
|
|
62
|
+
totalAmount,
|
|
63
|
+
amount,
|
|
64
|
+
gstPercentage,
|
|
65
|
+
billNumber,
|
|
66
|
+
jobId,
|
|
67
|
+
planName,
|
|
68
|
+
} = value;
|
|
69
|
+
|
|
70
|
+
// Check if the required environment variables are set
|
|
71
|
+
if (!process.env.EMAIL || !process.env.EMAIL_PASSWORD) {
|
|
72
|
+
const errorMsg = "Email credentials are not set in environment variables.";
|
|
73
|
+
console.error(errorMsg);
|
|
74
|
+
throw new Error(errorMsg); // Throw error to propagate it back to the caller
|
|
75
|
+
}
|
|
76
|
+
// console.log(totalAmount);
|
|
77
|
+
// Email content
|
|
78
|
+
const htmlBody = `
|
|
79
|
+
<p>Dear Customer,</p>
|
|
80
|
+
<p>Please find attached your invoice. If you have any questions, feel free to reach out to us.</p>
|
|
81
|
+
<p>Best regards,<br>Ocean academy</p>
|
|
82
|
+
`;
|
|
83
|
+
const invoiceData = {
|
|
84
|
+
date: new Date().toLocaleDateString(),
|
|
85
|
+
billNo: billNumber,
|
|
86
|
+
jobId,
|
|
87
|
+
items: [{ description: `${planName}`, amount: `${amount}` }],
|
|
88
|
+
totalAmount,
|
|
89
|
+
totalAmountWords: toWords.convert(totalAmount, { currency: true }),
|
|
90
|
+
gstPercentage,
|
|
91
|
+
gstAmount,
|
|
92
|
+
...OUR_ORG_INFO,
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
// Generate the invoice HTML content
|
|
96
|
+
const invoiceHTML = invoiceEmailTemplate(invoiceData);
|
|
97
|
+
const pdfBuffer = await generatePDF(invoiceHTML);
|
|
98
|
+
|
|
99
|
+
// Create a transporter using your email credentials
|
|
100
|
+
const transporter = nodemailer.createTransport({
|
|
101
|
+
service: "gmail",
|
|
102
|
+
auth: {
|
|
103
|
+
user: process.env.EMAIL,
|
|
104
|
+
pass: process.env.EMAIL_PASSWORD, // SMTP Password
|
|
105
|
+
},
|
|
106
|
+
// === add this === //
|
|
107
|
+
tls: { rejectUnauthorized: false },
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// Email options
|
|
111
|
+
const mailOptions = {
|
|
112
|
+
from: process.env.EMAIL,
|
|
113
|
+
to: recruiterEmail,
|
|
114
|
+
subject: "Welcome to our OA Job Portal!",
|
|
115
|
+
html: htmlBody, // HTML content from the template
|
|
116
|
+
attachments: [
|
|
117
|
+
{
|
|
118
|
+
filename: "invoice.pdf",
|
|
119
|
+
content: pdfBuffer,
|
|
120
|
+
encoding: "base64",
|
|
121
|
+
},
|
|
122
|
+
],
|
|
123
|
+
};
|
|
124
|
+
// Send the email with the PDF attachment
|
|
125
|
+
try {
|
|
126
|
+
const info = await transporter.sendMail(mailOptions);
|
|
127
|
+
// console.log("Email sent successfully!", info);
|
|
128
|
+
return { status: 200, message: "Email sent successfully!", info };
|
|
129
|
+
} catch (error) {
|
|
130
|
+
console.error("Error sending email:", error);
|
|
131
|
+
throw new Error("Error sending email", error);
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
module.exports = { sendInvoice, generatePDF };
|