feat: PDF 첨부 파일을 지원하도록 조사매물 제출 팝업 및 mailer.ts 개선

This commit is contained in:
Dayoung 2025-07-04 16:11:35 +09:00
parent 9ea58d3aea
commit 2db1357558
2 changed files with 48 additions and 15 deletions

View File

@ -19,6 +19,7 @@ interface SubmitFormData {
reference: string[] | null reference: string[] | null
title: string title: string
contents: string | null contents: string | null
srlNo: string | null
} }
interface FormField { interface FormField {
@ -35,7 +36,7 @@ export default function SurveySaleSubmitPopup() {
const { setIsShow } = useSpinnerStore() const { setIsShow } = useSpinnerStore()
const { getCommCode } = useCommCode() const { getCommCode } = useCommCode()
const { surveyDetail, getSubmitTarget } = useSurvey(Number(routeId)) const { surveyDetail, getSubmitTarget, isSubmittingSurvey, submitSurvey } = useSurvey(Number(routeId))
const { showErrorAlert, showSuccessAlert, showConfirm } = useAlertMsg() const { showErrorAlert, showSuccessAlert, showConfirm } = useAlertMsg()
const [submitData, setSubmitData] = useState<SubmitFormData>({ const [submitData, setSubmitData] = useState<SubmitFormData>({
@ -47,6 +48,7 @@ export default function SurveySaleSubmitPopup() {
reference: null, reference: null,
title: '', title: '',
contents: '', contents: '',
srlNo: null,
}) })
const [commCodeList, setCommCodeList] = useState<CommCode[]>([]) const [commCodeList, setCommCodeList] = useState<CommCode[]>([])
@ -56,6 +58,8 @@ export default function SurveySaleSubmitPopup() {
const baseUpdate = { const baseUpdate = {
sender: session?.email ?? '', sender: session?.email ?? '',
title: '[HANASYS現地調査] 調査物件が提出. (' + surveyDetail?.srlNo + ')', title: '[HANASYS現地調査] 調査物件が提出. (' + surveyDetail?.srlNo + ')',
srlNo: surveyDetail?.srlNo ?? null,
surveyId: surveyDetail?.id ?? null,
} }
/** Admin 제출 폼 데이터 삽입 - 1차 판매점*/ /** Admin 제출 폼 데이터 삽입 - 1차 판매점*/
if (session?.role === 'Admin') { if (session?.role === 'Admin') {
@ -106,8 +110,6 @@ export default function SurveySaleSubmitPopup() {
{ id: 'contents', name: '内容', required: false }, { id: 'contents', name: '内容', required: false },
] ]
const { submitSurvey, isSubmittingSurvey } = useSurvey(Number(routeId))
const handleInputChange = (field: keyof SubmitFormData, value: string) => { const handleInputChange = (field: keyof SubmitFormData, value: string) => {
setSubmitData((prev) => ({ ...prev, [field]: value })) setSubmitData((prev) => ({ ...prev, [field]: value }))
} }
@ -140,11 +142,15 @@ export default function SurveySaleSubmitPopup() {
cc: submitData.reference ?? '', cc: submitData.reference ?? '',
subject: submitData.title, subject: submitData.title,
content: generateEmailContent(), content: generateEmailContent(),
surveyPdf: {
id: surveyDetail?.id ?? 0,
filename: surveyDetail?.srlNo ?? 'hanasys_survey',
},
}) })
.then(() => { .then(() => {
submitSurvey({ targetId: submitData.targetId, targetNm: submitData.targetNm })
if (!isSubmittingSurvey) { if (!isSubmittingSurvey) {
showSuccessAlert(SUCCESS_MESSAGE.SUBMIT_SUCCESS) showSuccessAlert(SUCCESS_MESSAGE.SUBMIT_SUCCESS)
submitSurvey({ targetId: submitData.targetId, targetNm: submitData.targetNm })
popupController.setSurveySaleSubmitPopup(false) popupController.setSurveySaleSubmitPopup(false)
} }
}) })

View File

@ -3,6 +3,15 @@
import nodemailer from 'nodemailer' import nodemailer from 'nodemailer'
import { Attachment } from 'nodemailer/lib/mailer' 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 { interface EmailParams {
from: string from: string
to: string | string[] to: string | string[]
@ -10,10 +19,32 @@ interface EmailParams {
subject: string subject: string
content: string content: string
attachments?: Attachment[] attachments?: Attachment[]
surveyPdf?: {
id: number
filename: string
}
} }
export async function sendEmail({ from, to, cc, subject, content, attachments }: EmailParams): Promise<void> { export async function sendEmail({ from, to, cc, subject, content, attachments, surveyPdf }: EmailParams): Promise<void> {
// Create a transporter using SMTP /**
* @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({ const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST, host: process.env.SMTP_HOST,
port: Number(process.env.SMTP_PORT), port: Number(process.env.SMTP_PORT),
@ -22,21 +53,17 @@ export async function sendEmail({ from, to, cc, subject, content, attachments }:
tls: { rejectUnauthorized: false }, tls: { rejectUnauthorized: false },
}) })
// Email options /**
* @description
*/
const mailOptions = { const mailOptions = {
from, from,
to: Array.isArray(to) ? to.join(', ') : to, to: Array.isArray(to) ? to.join(', ') : to,
cc: cc ? (Array.isArray(cc) ? cc.join(', ') : cc) : undefined, cc: cc ? (Array.isArray(cc) ? cc.join(', ') : cc) : undefined,
subject, subject,
html: content, html: content,
attachments: attachments || [], attachments: surveyPdf ? surveyPdfAttachment : attachments || [],
} }
try {
// Send email
await transporter.sendMail(mailOptions) await transporter.sendMail(mailOptions)
} catch (error) {
console.error('Error sending email:', error)
throw new Error('Failed to send email')
}
} }