const { checkAndRefreshAccessToken } = require('../utils/refreshToken'); const { checkExistingContactRequest, insertContactRequest } = require('../utils/contactRequestDB'); const fetch = require('node-fetch'); const { sendEmail } = require('../utils/sendEmail'); //Getting Vapi Usage Flag (Whether to use or not) const { useVapi} = require("../config"); const assistant_url = 'https://api.vapi.ai/call'; const submitContactForm = async (req, res) => { const { name, email, phone, subject, message } = req.body; console.log('Received contact form submission:', req.body); if (!name || !email || !phone || !subject || !message) { return res.status(400).send({ error: 'All fields are required.' }); } await checkAndRefreshAccessToken(); const existingRequest = await checkExistingContactRequest(name, email, subject); if (existingRequest) { console.log("Your contact request with the same subject is in queue"); return res.status(400).send({ error: 'Your contact request with the same subject is in queue' }); } await insertContactRequest(name, email, phone, subject, message); console.log('Contact request added successfully'); try{ // 🔹 Email to User const userEmailSubject = `Contact Form Submission: ${subject}`; const userEmailContent = `
Genomatics Logo

Hello ${name},

Thank you for reaching out to us! We have received your message and will get back to you soon.

Your Submitted Information:

Name: ${name}

Email: ${email}

Subject: ${subject}

Message:

${message}

We appreciate your interest and are excited to help you. If you have any immediate questions, please feel free to reply to this email directly.

Best regards,

The Genomatics Team


This email was sent in response to your contact form submission on the Genomatics platform.

Follow us on social media:

Twitter LinkedIn
`; await sendEmail(email, userEmailSubject, userEmailContent); console.log("Confirmation email sent to user"); // 🔹 Email to Team const teamEmail = process.env.TEAM_MAIL_IDS; const teamEmailSubject = `New Contact Form Submission: ${subject}`; const teamEmailContent = `
Genomatics Logo

📩 New Contact Request Received

Dear Team,

A new contact form with some general enquiry/request has been submitted. Details are as follows:

Name:${name}
Email:${email}
Phone:${phone}
Subject:${subject}
Message:${message}

Please follow up with the requester accordingly.

Best regards,

Genomatics System

`; await sendEmail(teamEmail, teamEmailSubject, teamEmailContent); console.log("Notification email sent to team"); res.status(200).send({ message: 'Contact message sent successfully!' }); } catch (error) { console.error('Unable to send Contact request confirmation email, however request registered successfully!', error); res.status(200).send({ error: 'Unable to send Contact request confirmation email, however request registered successfully!' }); } // 🔹 Vapi AI Call const curr_time = new Date().toLocaleDateString('en-GB').replace(/\//g, '-'); const postData = { "name": `${phone}_${curr_time}_CRC`, "assistantId": process.env.CONTACT_ASSISTANT_ID, "assistantOverrides": { "variableValues": { "name": name, "subject": subject, "comments": message } }, "customer": { "number": phone }, "phoneNumberId": process.env.PHONE_NUMBER_ID }; if(useVapi){ fetch(assistant_url, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${process.env.VAPI_KEY}` }, body: JSON.stringify(postData) }) .then(response => response.json()) .then(data => console.log('Vapi AI Call Success:', data)) .catch(error => console.error('Vapi AI Call Error:', error)); } }; module.exports = { submitContactForm };