Files
gg-backend/backend/src/controllers/candidate.controller.js
T
2026-04-27 17:29:33 +05:30

281 lines
6.1 KiB
JavaScript

import prisma from "../prisma/client.js";
import { sendEmail } from "../utils/sendEmail.js";
import { getEmailsByType } from "../utils/getEmailByTypes.js";
// CREATE CANDIDATE
export const createCandidate = async (req, res) => {
try {
const { fullName, mobile, email, subject, coverLetter, careerId } =
req.body;
if (!fullName || !mobile || !email || !careerId) {
return res.status(400).json({
success: false,
message: "Required fields missing",
});
}
const candidate = await prisma.candidate.create({
data: {
fullName,
mobile,
email,
subject,
coverLetter,
careerId: Number(careerId),
},
include: {
career: true,
},
});
try {
const emailList = await getEmailsByType("CANDIDATE");
if (emailList && emailList.length > 0) {
await sendEmail({
to: emailList,
subject: "New Job Application 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 Job Application Received
</p>
</div>
<!-- Body -->
<div style="padding: 20px; color: #333;">
<h3 style="margin-top: 0;">Candidate 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;">${mobile}</td>
</tr>
<tr>
<td style="padding: 8px 0;"><b>Email:</b></td>
<td style="padding: 8px 0;">${email}</td>
</tr>
</table>
<h3 style="margin-top: 20px;">Application Details</h3>
<table style="width: 100%; border-collapse: collapse;">
<tr>
<td style="padding: 8px 0;"><b>Applied For:</b></td>
<td style="padding: 8px 0;">${candidate.career?.post || "-"}</td>
</tr>
<tr>
<td style="padding: 8px 0;"><b>Designation:</b></td>
<td style="padding: 8px 0;">${candidate.career?.designation || "-"}</td>
</tr>
<tr>
<td style="padding: 8px 0;"><b>Subject:</b></td>
<td style="padding: 8px 0;">${subject || "-"}</td>
</tr>
</table>
<!-- Cover Letter -->
<div style="margin-top: 20px;">
<h3>Cover Letter</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;
">
${coverLetter ? coverLetter.replace(/\n/g, "<br/>") : "-"}
</div>
</div>
</div>
<!-- Footer -->
<div style="background: #f1f1f1; padding: 15px; text-align: center; font-size: 12px; color: #666;">
This application was submitted via the GG Hospital careers page.
</div>
</div>
</div>
`,
});
}
} catch (err) {
console.error("Candidate email failed:", err);
}
res.status(201).json({
success: true,
message: "Application submitted successfully",
data: candidate,
});
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Failed to create candidate",
});
}
};
// GET ALL CANDIDATES
export const getCandidates = async (req, res) => {
try {
const candidates = await prisma.candidate.findMany({
include: {
career: true,
},
orderBy: {
createdAt: "desc",
},
});
res.status(200).json({
success: true,
data: candidates,
});
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Failed to fetch candidates",
});
}
};
// GET SINGLE CANDIDATE
export const getCandidate = async (req, res) => {
try {
const { id } = req.params;
const candidate = await prisma.candidate.findUnique({
where: {
id: Number(id),
},
include: {
career: true,
},
});
if (!candidate) {
return res.status(404).json({
success: false,
message: "Candidate not found",
});
}
res.status(200).json({
success: true,
data: candidate,
});
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Failed to fetch candidate",
});
}
};
// GET CANDIDATES BY CAREER
export const getCandidatesByCareer = async (req, res) => {
try {
const { careerId } = req.params;
const candidates = await prisma.candidate.findMany({
where: {
careerId: Number(careerId),
},
include: {
career: true,
},
orderBy: {
createdAt: "desc",
},
});
res.status(200).json({
success: true,
data: candidates,
});
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Failed to fetch candidates",
});
}
};
// UPDATE CANDIDATE
export const updateCandidate = async (req, res) => {
try {
const { id } = req.params;
const candidate = await prisma.candidate.update({
where: {
id: Number(id),
},
data: req.body,
});
res.status(200).json({
success: true,
message: "Candidate updated successfully",
data: candidate,
});
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Failed to update candidate",
});
}
};
// DELETE CANDIDATE
export const deleteCandidate = async (req, res) => {
try {
const { id } = req.params;
await prisma.candidate.delete({
where: {
id: Number(id),
},
});
res.status(200).json({
success: true,
message: "Candidate deleted successfully",
});
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Failed to delete candidate",
});
}
};