// contactController.js module.exports = (pool, transporter) => { const submitForm = async (req, res) => { const { firstName, lastName, organization, email, phone, contactMethod, message } = req.body; try { const result = await pool.query( `INSERT INTO submissions(first_name, last_name, organization, email, phone, contact_method, message) VALUES($1, $2, $3, $4, $5, $6, $7) RETURNING *`, [firstName, lastName, organization, email, phone, contactMethod, message] ); console.log('Successfully saved submission to the database:', result.rows[0]); const mailOptions = { from: `"Contact Form" `, to: process.env.EMAIL_RCPT, subject: 'New Contact Form Submission', html: `

New Submission

Message:

${message}

`, }; const emailInfo = await transporter.sendMail(mailOptions); console.log('Message sent: %s', emailInfo.messageId); res.status(200).json({ message: 'Form submitted successfully and saved to the database!', dataReceived: req.body }); } catch (err) { console.error('An error occurred during form submission:', err.stack); res.status(500).json({ message: 'An error occurred. Please try again.', error: err.message }); } }; return { submitForm, }; };