diff --git a/src/sections/Profile/Admin/Form/CertificatesForm/SendCertificate.jsx b/src/sections/Profile/Admin/Form/CertificatesForm/SendCertificate.jsx index f1a936e5..178b4442 100644 --- a/src/sections/Profile/Admin/Form/CertificatesForm/SendCertificate.jsx +++ b/src/sections/Profile/Admin/Form/CertificatesForm/SendCertificate.jsx @@ -3,13 +3,13 @@ import { useParams } from "react-router-dom"; import { Button, Input } from "../../../../../components"; import { api } from "../../../../../services"; import * as XLSX from "xlsx"; -import { sendBatchMail } from "./tools/certificateTools"; -import { Alert, MicroLoading } from "../../../../../microInteraction"; import { getCertificatePreview, generatedAndSendCertificate, accessOrCreateEventByFormId, -} from "../CertificatesForm/tools/certificateTools"; + testCertificateSending, +} from "./tools/certificateTools"; +import { Alert, MicroLoading } from "../../../../../microInteraction"; import AuthContext from "../../../../../context/AuthContext.jsx"; const Checkbox = ({ id, checked, onCheckedChange }) => { @@ -41,6 +41,7 @@ const SendCertificate = () => { const [fileUploading, setFileUploading] = useState(false); const [certificatePreview, setCertificatePreview] = useState("Loading..."); const [alert, setAlert] = useState(null); + const [failedEmails, setFailedEmails] = useState([]); useEffect(() => { const fetchCertificatePreview = async () => { @@ -211,6 +212,7 @@ const SendCertificate = () => { } setSendingMail(true); + setFailedEmails([]); try { const eventData = await accessOrCreateEventByFormId( @@ -248,13 +250,22 @@ const SendCertificate = () => { token: authCtx.token, }); - if (response?.status === 200 || response?.status === 207) { + if (response?.status === 200) { setAlert({ - type: response.status === 200 ? "success" : "warning", - message: response.data?.message || "Certificates processed.", + type: "success", + message: response.data?.message || "All certificates sent successfully!", position: "top-right", duration: 4000, }); + } else if (response?.status === 207) { + const failed = response.data?.failed || []; + setFailedEmails(failed); + setAlert({ + type: "warning", + message: response.data?.message || "Some certificates failed to send.", + position: "top-right", + duration: 5000, + }); } else { throw new Error(response?.data?.error || "Failed to send certificates"); } @@ -284,20 +295,23 @@ const SendCertificate = () => { setSendingMail(true); try { - await sendBatchMail({ - batchSize: 1, - formId: eventId, + const eventData = await accessOrCreateEventByFormId( + eventId, + authCtx.token + ); + if (!eventData || !eventData.id) { + throw new Error("Event data retrieval failed"); + } + + const response = await testCertificateSending({ + eventId: eventData.id, + email: checkedAttendees[0].email, + name: checkedAttendees[0].name || "", subject: `[TEST] ${subject}`, - htmlContent: description, - recipients: [ - { - email: checkedAttendees[0].email, - name: checkedAttendees[0].name || "", - }, - ], + token: authCtx.token, }); - if (response.status === 200) { + if (response && response.status === 200) { setAlert({ type: "success", message: "Test mail sent successfully!", @@ -305,7 +319,7 @@ const SendCertificate = () => { duration: 3000, }); } else { - throw new Error(response.data?.error || "Failed to send test mail"); + throw new Error(response?.data?.error || "Failed to send test mail"); } } catch (error) { setAlert({ @@ -580,6 +594,99 @@ const SendCertificate = () => { + + {failedEmails.length > 0 && ( +