186 lines
4.1 KiB
JavaScript
186 lines
4.1 KiB
JavaScript
import prisma from '../prisma/client.js';
|
|
|
|
import { sendEmail } from '../utils/sendEmail.js';
|
|
import { getEmailsByType } from '../utils/getEmailByTypes.js';
|
|
|
|
/* CREATE INQUIRY */
|
|
export const createInquiry = async (req, res) => {
|
|
try {
|
|
const { fullName, number, emailId, subject, message } = req.body;
|
|
|
|
if (!fullName || !number) {
|
|
return res.status(400).json({
|
|
success: false,
|
|
message: 'Full name and number are required',
|
|
});
|
|
}
|
|
|
|
const inquiry = await prisma.inquiry.create({
|
|
data: {
|
|
fullName,
|
|
number,
|
|
emailId,
|
|
subject,
|
|
message,
|
|
},
|
|
});
|
|
try {
|
|
const emailList = await getEmailsByType('INQUIRY');
|
|
|
|
if (emailList && emailList.length > 0) {
|
|
await sendEmail({
|
|
to: emailList,
|
|
subject: 'New Inquiry Received',
|
|
html: `
|
|
<div style="font-family: Arial, sans-serif; background-color: #f4f6f8; padding: 20px;">
|
|
|
|
<div style="max-width: 600px; margin: auto; background: #ffffff; border-radius: 10px; overflow: hidden; box-shadow: 0 4px 10px rgba(0,0,0,0.05);">
|
|
|
|
<!-- Header -->
|
|
<div style="background-color: #0d6efd; color: #ffffff; padding: 20px;">
|
|
<h2 style="margin: 0;">GG Hospital</h2>
|
|
<p style="margin: 5px 0 0; font-size: 14px;">New Inquiry Received</p>
|
|
</div>
|
|
|
|
<!-- Body -->
|
|
<div style="padding: 20px; color: #333;">
|
|
|
|
<h3 style="margin-top: 0;">Contact Details</h3>
|
|
|
|
<table style="width: 100%; border-collapse: collapse;">
|
|
<tr>
|
|
<td style="padding: 8px 0;"><b>Name:</b></td>
|
|
<td style="padding: 8px 0;">${fullName}</td>
|
|
</tr>
|
|
<tr>
|
|
<td style="padding: 8px 0;"><b>Phone:</b></td>
|
|
<td style="padding: 8px 0;">${number}</td>
|
|
</tr>
|
|
<tr>
|
|
<td style="padding: 8px 0;"><b>Email:</b></td>
|
|
<td style="padding: 8px 0;">${emailId}</td>
|
|
</tr>
|
|
<tr>
|
|
<td style="padding: 8px 0;"><b>Subject:</b></td>
|
|
<td style="padding: 8px 0;">${subject}</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<!-- Message Box -->
|
|
<div style="margin-top: 20px;">
|
|
<h3>Message</h3>
|
|
<div style="
|
|
background: #f8f9fa;
|
|
padding: 15px;
|
|
border-radius: 6px;
|
|
line-height: 1.6;
|
|
white-space: pre-wrap;
|
|
word-break: break-word;
|
|
overflow-wrap: anywhere;
|
|
">
|
|
${message ? message.replace(/\n/g, '<br/>') : '-'}
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<!-- Footer -->
|
|
<div style="background: #f1f1f1; padding: 15px; text-align: center; font-size: 12px; color: #666;">
|
|
This message was sent from the GG Hospital website contact form.
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
`,
|
|
});
|
|
}
|
|
} catch (err) {
|
|
console.error('Inquiry email failed:', err);
|
|
}
|
|
|
|
res.status(200).json({
|
|
success: true,
|
|
status: 200,
|
|
data: inquiry,
|
|
message: 'Inquiry added successfully',
|
|
});
|
|
} catch (error) {
|
|
console.error(error);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: 'Failed to add inquiry',
|
|
});
|
|
}
|
|
};
|
|
|
|
/* GET ALL */
|
|
export const getInquiries = async (req, res) => {
|
|
try {
|
|
const inquiries = await prisma.inquiry.findMany({
|
|
orderBy: {
|
|
createdAt: 'desc',
|
|
},
|
|
});
|
|
|
|
res.json({
|
|
success: true,
|
|
data: inquiries,
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({
|
|
success: false,
|
|
message: 'Failed to fetch inquiries',
|
|
});
|
|
}
|
|
};
|
|
|
|
/* GET SINGLE */
|
|
export const getInquiry = async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
|
|
const inquiry = await prisma.inquiry.findUnique({
|
|
where: { id: Number(id) },
|
|
});
|
|
|
|
if (!inquiry) {
|
|
return res.status(404).json({
|
|
success: false,
|
|
message: 'Inquiry not found',
|
|
});
|
|
}
|
|
|
|
res.json({
|
|
success: true,
|
|
data: inquiry,
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({
|
|
success: false,
|
|
message: 'Failed to fetch inquiry',
|
|
});
|
|
}
|
|
};
|
|
|
|
/* DELETE */
|
|
export const deleteInquiry = async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
|
|
await prisma.inquiry.delete({
|
|
where: { id: Number(id) },
|
|
});
|
|
|
|
res.json({
|
|
success: true,
|
|
message: 'Inquiry deleted successfully',
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({
|
|
success: false,
|
|
message: 'Failed to delete inquiry',
|
|
});
|
|
}
|
|
};
|