Files
gg-backend/backend/src/controllers/inquiry.controller.js
T

186 lines
4.1 KiB
JavaScript
Raw Normal View History

2026-03-16 12:39:41 +05:30
import prisma from "../prisma/client.js";
2026-04-16 14:03:50 +05:30
import {sendEmail} from "../utils/sendEmail.js";
import {getEmailsByType} from "../utils/getEmailByTypes.js";
2026-03-16 12:39:41 +05:30
/* 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,
},
});
2026-04-16 14:03:50 +05:30
try {
const emailList = await getEmailsByType("INQUIRY");
if (emailList && emailList.length > 0) {
await sendEmail({
to: emailList,
subject: "New Inquiry Received",
html: `
2026-04-27 17:29:33 +05:30
<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>
2026-04-16 14:03:50 +05:30
2026-04-27 17:29:33 +05:30
<!-- 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>
2026-04-16 14:03:50 +05:30
2026-04-27 17:29:33 +05:30
<!-- 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>
2026-04-16 14:03:50 +05:30
`,
});
}
} catch (err) {
console.error("Inquiry email failed:", err);
}
2026-03-16 12:39:41 +05:30
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",
});
}
};