diff --git a/src/components/popup/SurveySaleSubmitPopup.tsx b/src/components/popup/SurveySaleSubmitPopup.tsx index 2093c38..7e8e7af 100644 --- a/src/components/popup/SurveySaleSubmitPopup.tsx +++ b/src/components/popup/SurveySaleSubmitPopup.tsx @@ -19,6 +19,7 @@ interface SubmitFormData { reference: string[] | null title: string contents: string | null + srlNo: string | null } interface FormField { @@ -35,7 +36,7 @@ export default function SurveySaleSubmitPopup() { const { setIsShow } = useSpinnerStore() const { getCommCode } = useCommCode() - const { surveyDetail, getSubmitTarget } = useSurvey(Number(routeId)) + const { surveyDetail, getSubmitTarget, isSubmittingSurvey, submitSurvey } = useSurvey(Number(routeId)) const { showErrorAlert, showSuccessAlert, showConfirm } = useAlertMsg() const [submitData, setSubmitData] = useState({ @@ -47,6 +48,7 @@ export default function SurveySaleSubmitPopup() { reference: null, title: '', contents: '', + srlNo: null, }) const [commCodeList, setCommCodeList] = useState([]) @@ -56,6 +58,8 @@ export default function SurveySaleSubmitPopup() { const baseUpdate = { sender: session?.email ?? '', title: '[HANASYS現地調査] 調査物件が提出. (' + surveyDetail?.srlNo + ')', + srlNo: surveyDetail?.srlNo ?? null, + surveyId: surveyDetail?.id ?? null, } /** Admin 제출 폼 데이터 삽입 - 1차 판매점*/ if (session?.role === 'Admin') { @@ -106,8 +110,6 @@ export default function SurveySaleSubmitPopup() { { id: 'contents', name: '内容', required: false }, ] - const { submitSurvey, isSubmittingSurvey } = useSurvey(Number(routeId)) - const handleInputChange = (field: keyof SubmitFormData, value: string) => { setSubmitData((prev) => ({ ...prev, [field]: value })) } @@ -140,11 +142,15 @@ export default function SurveySaleSubmitPopup() { cc: submitData.reference ?? '', subject: submitData.title, content: generateEmailContent(), + surveyPdf: { + id: surveyDetail?.id ?? 0, + filename: surveyDetail?.srlNo ?? 'hanasys_survey', + }, }) .then(() => { + submitSurvey({ targetId: submitData.targetId, targetNm: submitData.targetNm }) if (!isSubmittingSurvey) { showSuccessAlert(SUCCESS_MESSAGE.SUBMIT_SUCCESS) - submitSurvey({ targetId: submitData.targetId, targetNm: submitData.targetNm }) popupController.setSurveySaleSubmitPopup(false) } }) diff --git a/src/libs/mailer.ts b/src/libs/mailer.ts index 727e7a3..10baec3 100644 --- a/src/libs/mailer.ts +++ b/src/libs/mailer.ts @@ -3,6 +3,15 @@ import nodemailer from 'nodemailer' import { Attachment } from 'nodemailer/lib/mailer' +/** + * @description 이메일 파라미터 인터페이스 + * @param {string} from 발신자 + * @param {string | string[]} to 수신자 + * @param {string | string[]} cc 참조 + * @param {string} subject 제목 + * @param {string} content 내용 + * @param {Attachment[]} attachments 첨부파일 + */ interface EmailParams { from: string to: string | string[] @@ -10,10 +19,32 @@ interface EmailParams { subject: string content: string attachments?: Attachment[] + surveyPdf?: { + id: number + filename: string + } } -export async function sendEmail({ from, to, cc, subject, content, attachments }: EmailParams): Promise { - // Create a transporter using SMTP +export async function sendEmail({ from, to, cc, subject, content, attachments, surveyPdf }: EmailParams): Promise { + /** + * @description 조사매물 pdf blob 및 buffer 생성 + */ + let surveyPdfBuffer: Buffer | null = null + if (surveyPdf) { + const resp = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/survey-sales/${surveyPdf.id}?isPdf=true`, { + method: 'GET', + headers: { + 'Content-Type': 'application/pdf', + }, + }) + const pdfBlob = await resp.blob() + surveyPdfBuffer = Buffer.from(await pdfBlob.arrayBuffer()) + } + const surveyPdfAttachment = surveyPdfBuffer ? [{ filename: '[HANASYS現地調査]' + surveyPdf?.filename + '.pdf', content: surveyPdfBuffer }] : [] + + /** + * @description SMTP 트랜스포터 생성 + */ const transporter = nodemailer.createTransport({ host: process.env.SMTP_HOST, port: Number(process.env.SMTP_PORT), @@ -22,21 +53,17 @@ export async function sendEmail({ from, to, cc, subject, content, attachments }: tls: { rejectUnauthorized: false }, }) - // Email options + /** + * @description 이메일 옵션 + */ const mailOptions = { from, to: Array.isArray(to) ? to.join(', ') : to, cc: cc ? (Array.isArray(cc) ? cc.join(', ') : cc) : undefined, subject, html: content, - attachments: attachments || [], + attachments: surveyPdf ? surveyPdfAttachment : attachments || [], } - try { - // Send email - await transporter.sendMail(mailOptions) - } catch (error) { - console.error('Error sending email:', error) - throw new Error('Failed to send email') - } + await transporter.sendMail(mailOptions) }