File size: 5,472 Bytes
8b16861
 
e08ecbf
8b16861
e08ecbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b16861
 
e08ecbf
 
 
 
 
8b16861
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e08ecbf
 
 
 
 
8b16861
e08ecbf
 
 
8b16861
e08ecbf
8b16861
 
 
 
e08ecbf
 
8b16861
 
e08ecbf
 
 
 
 
 
8b16861
e08ecbf
8b16861
e08ecbf
8b16861
 
 
 
e08ecbf
 
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
const { checkAndRefreshAccessToken } = require('../utils/refreshToken');
const { checkExistingContactRequest, insertContactRequest } = require('../utils/contactRequestDB');
const fetch = require('node-fetch');
const { sendEmail } = require('../utils/sendEmail');

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 = `
            <div style="font-family: Arial, sans-serif; color: #333;">
                <center><img src="https://drive.google.com/thumbnail?id=17oMmzl_mTNvohvLhSWHbb_XNPfCC8KaO" alt="Genomatics Logo" height="150px"></center>
                <h2 style="color: #0056b3;">Hello ${name},</h2>
                <p>Thank you for reaching out! We have received your message and will get back to you soon.</p>
                <p><strong>Your Submitted Information:</strong></p>
                <ul>
                    <li><strong>Name:</strong> ${name}</li>
                    <li><strong>Email:</strong> ${email}</li>
                    <li><strong>Subject:</strong> ${subject}</li>
                    <li><strong>Message:</strong> ${message}</li>
                </ul>
                <p>If you have any immediate questions, feel free to reply to this email.</p>
                <p style="margin-top: 20px;">Best regards,</p>
                <p><strong>The Genomatics Team</strong></p>
            </div>`;

        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 = `
            <div style="font-family: Arial, sans-serif; color: #333; line-height: 1.6;">
                <center><img src="https://drive.google.com/thumbnail?id=17oMmzl_mTNvohvLhSWHbb_XNPfCC8KaO" alt="Genomatics Logo" height="150px"></center>
                <h2 style="color: #0056b3;">๐Ÿ“ฉ New Contact Request Received</h2>
                <p>Dear Team,</p>
                <p>A new contact form with some general enquiry/request has been submitted. Details are as follows:</p>
                <table style="width: 100%; border-collapse: collapse;">
                    <tr><td style="padding: 8px; font-weight: bold;">Name:</td><td style="padding: 8px;">${name}</td></tr>
                    <tr><td style="padding: 8px; font-weight: bold;">Email:</td><td style="padding: 8px;">${email}</td></tr>
                    <tr><td style="padding: 8px; font-weight: bold;">Phone:</td><td style="padding: 8px;">${phone}</td></tr>
                    <tr><td style="padding: 8px; font-weight: bold;">Subject:</td><td style="padding: 8px;">${subject}</td></tr>
                    <tr><td style="padding: 8px; font-weight: bold;">Message:</td><td style="padding: 8px;">${message}</td></tr>
                </table>
                <p>Please follow up with the requester accordingly.</p>
                <p style="margin-top: 20px;">Best regards,</p>
                <p><strong>Genomatics System</strong></p>
            </div>`;

        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('Failed to send Contact request confirmation email, however request registered successfully!', error);
        res.status(200).send({ error: 'Failed 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
    };

    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 };