i-darrshan's picture
initial push of the update
8b105ad
// controllers/subscriptionController.js
const {oAuth2Client} = require('../config/googleOAuth'); // Import the OAuth2 client
const { addSubscriber } = require('../utils/addSubscriber'); // Assuming this exists to add the email to the database
const { checkAndRefreshAccessToken } = require('../utils/refreshToken'); // Import the helper function
const {google} = require('googleapis');
const handleSubscriptionEmail = async (req, res) => {
const email = req.body.email;
console.log('Received request to send email to:', email);
if (!email) {
console.log("Email is required");
return res.status(400).send({ error: 'Email is required' });
}
const result = await addSubscriber(email);
if (result.error) {
// Return error if email already exists or any database error occurs
return res.status(result.status).send({ error: result.error });
}
await checkAndRefreshAccessToken(); // Refresh the token if expired
// Set OAuth2 credentials
// oAuth2Client.setCredentials(userTokens);
// Check if OAuth2 credentials are missing
if (!oAuth2Client.credentials || !oAuth2Client.credentials.access_token) {
return res.status(500).send({
error: 'OAuth credentials missing. Please authorize the app first.'
});
}
// Prepare the email content
const subject = "Subscription Confirmation";
const message = `<div style="font-family: Arial, sans-serif; color: #333;">
<img src="https://www.genomatics.in/wp-content/uploads/2020/01/cropped-LOGO-1-1.png" alt="Genomatics Logo">
<h2 style="color: #0056b3;">Hello,</h2>
<p>Thank you for subscribing to <strong>Genomatics</strong>! We’re excited to have you join our community of genomics enthusiasts, researchers, and professionals. As a valued subscriber, you’ll be among the first to receive updates on breakthrough discoveries, exclusive insights, and expert tips on leveraging genomics for impactful decision-making.</p>
<h3 style="color: #0056b3;">What You’ll Get:</h3>
<ul>
<li><strong>Exclusive Insights:</strong> Stay ahead with our latest findings on how genomics is shaping the future of medicine, biotechnology, and more.</li>
<li><strong>Updates on Our Innovations:</strong> Be the first to know about new products and tools designed to empower you in your genomics journey.</li>
<li><strong>Invitations to Webinars and Events:</strong> Gain access to events and discussions led by leaders in the field to deepen your understanding and application of genomics.</li>
</ul>
<p>If you have any questions or ideas you’d like us to cover, feel free to reach out. We’re here to support you every step of the way.</p>
<p>Once again, welcome aboard! We look forward to sharing this journey of discovery with you.</p>
<p style="margin-top: 20px;">Warm regards,</p>
<p><strong>The Genomatics Team</strong></p>
<hr style="border: 0; height: 1px; background: #ddd; margin: 20px 0;">
<p style="font-size: 12px; color: #666;">
This email was sent in response to your subscription to the Genomatics platform.
</p>
</div>`;
const emailContent = [
`To: ${email}`,
'Content-Type: text/html; charset="UTF-8"',
'MIME-Version: 1.0',
`Subject: ${subject}`,
'',
message,
].join('\n');
// Encode the email content
const encodedEmail = Buffer.from(emailContent)
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_');
// Send email via Gmail API
try {
const gmail = google.gmail({ version: 'v1', auth: oAuth2Client });
const response = await gmail.users.messages.send({
userId: 'me',
requestBody: {
raw: encodedEmail,
},
});
res.status(200).send({ message: 'Subscription email sent!', response });
} catch (error) {
console.log('Error sending email:', error);
res.status(500).send({ error: 'Failed to send subscription confirmation email, however you are subscribed and will receive newsletters' });
}
};
module.exports = { handleSubscriptionEmail };