feat: 문의 메일 발송 구현
This commit is contained in:
parent
6ac9207ec5
commit
fb27e414d8
@ -1,4 +1,4 @@
|
||||
import axios from 'axios'
|
||||
import axios, { HttpStatusCode } from 'axios'
|
||||
import { NextResponse } from 'next/server'
|
||||
import { loggerWrapper } from '@/libs/api-wrapper'
|
||||
import { QnaService } from '../service'
|
||||
@ -12,9 +12,9 @@ import { cookies } from 'next/headers'
|
||||
* @api {POST} /api/qna/save 문의 저장 API
|
||||
* @apiName POST /api/qna/save
|
||||
* @apiGroup Qna
|
||||
* @apiDescription 문의 저장 API
|
||||
* @apiDescription 문의 메일 발송 후 문의 저장 API
|
||||
*
|
||||
* @apiBody {InquiryRequest} inquiryRequest 문의 저장 요청 파라미터
|
||||
* @apiBody {FormData} formData 문의 저장 요청 파라미터
|
||||
*
|
||||
* @apiExample {curl} Example usage:
|
||||
* curl -X POST http://localhost:3000/api/qna/save
|
||||
@ -32,9 +32,14 @@ import { cookies } from 'next/headers'
|
||||
async function setQna(request: Request): Promise<NextResponse> {
|
||||
const cookieStore = await cookies()
|
||||
const session = await getIronSession<SessionData>(cookieStore, sessionOptions)
|
||||
|
||||
const service = new QnaService(session)
|
||||
|
||||
const formData = await request.formData()
|
||||
const mailResult = await service.sendMail(formData)
|
||||
if (mailResult instanceof ApiError) {
|
||||
return NextResponse.json({ error: mailResult.message }, { status: mailResult.statusCode })
|
||||
}
|
||||
|
||||
const result = await service.tryFunction(() =>
|
||||
axios.post(`${process.env.NEXT_PUBLIC_INQUIRY_API_URL}/api/qna/save`, formData, {
|
||||
headers: {
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
import { SessionData } from '@/types/Auth'
|
||||
import { CommonCode } from '@/types/Inquiry'
|
||||
import { CommonCode, InquiryRequest } from '@/types/Inquiry'
|
||||
import { ERROR_MESSAGE } from '@/hooks/useAlertMsg'
|
||||
import { HttpStatusCode } from 'axios'
|
||||
import { ApiError } from 'next/dist/server/api-utils'
|
||||
import { prisma } from '@/libs/prisma'
|
||||
import { sendEmail } from '@/libs/mailer'
|
||||
|
||||
export class QnaService {
|
||||
private session?: SessionData
|
||||
@ -15,6 +17,7 @@ export class QnaService {
|
||||
* @returns {ApiError} 에러 객체
|
||||
*/
|
||||
private handleRouteError(error: any): ApiError {
|
||||
if (error === undefined) return new ApiError(HttpStatusCode.InternalServerError, ERROR_MESSAGE.SERVER_ERROR)
|
||||
console.error('❌ API ROUTE ERROR : ', error)
|
||||
return new ApiError(error.response.status ?? HttpStatusCode.InternalServerError, error.response.data.result.message ?? ERROR_MESSAGE.FETCH_ERROR)
|
||||
}
|
||||
@ -23,13 +26,13 @@ export class QnaService {
|
||||
* @param {() => Promise<any>} func 비동기 함수
|
||||
* @returns {Promise<ApiError | any>} 에러 객체 또는 함수 결과
|
||||
*/
|
||||
async tryFunction(func: () => Promise<any>, isFile?: boolean): Promise<ApiError | any> {
|
||||
async tryFunction(func: () => Promise<any>, shouldThrowResult?: boolean): Promise<ApiError | any> {
|
||||
if (this.session !== undefined && !this.session?.isLoggedIn) {
|
||||
return new ApiError(HttpStatusCode.Unauthorized, ERROR_MESSAGE.UNAUTHORIZED)
|
||||
}
|
||||
try {
|
||||
const response = await func()
|
||||
if (isFile) return response
|
||||
if (shouldThrowResult) return response
|
||||
return this.handleResult(response)
|
||||
} catch (error) {
|
||||
return this.handleRouteError(error)
|
||||
@ -86,4 +89,94 @@ export class QnaService {
|
||||
})
|
||||
return params
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 문의 메일 발송
|
||||
* @param {FormData} formData 문의 메일 발송 요청 파라미터 - qnaClsLrgCd, title, contents, files
|
||||
* @returns {ApiError | null} 에러 객체 또는 null
|
||||
*/
|
||||
async sendMail(formData: FormData): Promise<ApiError | void> {
|
||||
const receivers: string[] = await this.getReceiver(formData.get('qnaClsLrgCd') as string)
|
||||
if (receivers.length === 0) {
|
||||
return new ApiError(HttpStatusCode.InternalServerError, ERROR_MESSAGE.EMAIL_SEND_ERROR)
|
||||
}
|
||||
const files = formData.getAll('files') as File[]
|
||||
const fileBuffers = await Promise.all(files.map((file) => file.arrayBuffer()))
|
||||
|
||||
const attachments = files.map((file, index) => ({
|
||||
filename: file.name,
|
||||
content: Buffer.from(fileBuffers[index]),
|
||||
contentType: file.type || 'application/octet-stream',
|
||||
}))
|
||||
|
||||
return this.tryFunction(() => {
|
||||
return sendEmail({
|
||||
from: 'test@test.com',
|
||||
to: receivers,
|
||||
subject: `[HANASYS お問い合わせ] ${formData.get('title')}`,
|
||||
content: this.generateEmailContent(formData),
|
||||
attachments: attachments,
|
||||
})
|
||||
}, true)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 문의 수신자 조회
|
||||
* @param {string} qnaClsLrgCd 문의 유형 대분류 코드
|
||||
* @returns {string[]} 문의 수신자 목록
|
||||
*/
|
||||
async getReceiver(qnaClsLrgCd: string): Promise<string[]> {
|
||||
const query = `
|
||||
OPEN SYMMETRIC KEY SYMMETRICKEY DECRYPTION BY CERTIFICATE CERTI_QSPJP;
|
||||
SELECT CONVERT(NVARCHAR(100), DecryptByKey(bu.e_mail)) AS email
|
||||
FROM BC_USER bu
|
||||
WHERE bu.user_id IN (
|
||||
SELECT user_id
|
||||
FROM SY_POLICY_U spu
|
||||
WHERE policy_cd = (
|
||||
SELECT bcl.ref_chr2
|
||||
FROM BC_COMM_L bcl
|
||||
WHERE bcl.head_cd = (SELECT head_cd FROM BC_COMM_H bch WHERE head_id = 'QNA_CLS_MID_CD')
|
||||
AND bcl.ref_chr1 = '${qnaClsLrgCd}'
|
||||
GROUP BY bcl.ref_chr2
|
||||
)
|
||||
)
|
||||
CLOSE SYMMETRIC KEY SYMMETRICKEY;`
|
||||
|
||||
const receivers: { email: string }[] = await prisma.$queryRawUnsafe(query)
|
||||
return receivers.map((receiver) => receiver.email)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 문의 이메일 내용 생성
|
||||
* @param {FormData} formData 문의 메일 발송 요청 파라미터 - qnaClsLrgCd, title, contents, files
|
||||
* @returns {string} 문의 이메일 내용
|
||||
*/
|
||||
generateEmailContent = (formData: FormData) => `
|
||||
<div>
|
||||
<p style="font-size: 13px; font-weight: 400; color: #2e3a59; margin-bottom: 15px;">
|
||||
お問い合わせが登録されました。<br/>
|
||||
{ QSP > System mgt. > お問い合わせ } でお問い合わせ内容を確認してください。
|
||||
</p>
|
||||
<p style="font-size: 13px; font-weight: 400; color: #2e3a59; margin-bottom: 3px;">
|
||||
-登録者: <span style="color: #417DDC;">${formData.get('regUserNm')}</span>
|
||||
</p>
|
||||
<p style="font-size: 13px; font-weight: 400; color: #2e3a59; margin-bottom: 3px;">
|
||||
-販売店ID:
|
||||
<span style="color: #417DDC;">
|
||||
${formData.get('storeId')}
|
||||
</span>
|
||||
</p>
|
||||
<p style="font-size: 13px; font-weight: 400; color: #2e3a59; margin-bottom: 15px;">
|
||||
-販売店名:
|
||||
<span style="color: #417DDC;">${this.session?.storeNm ?? ' - '}</span>
|
||||
</p>
|
||||
<p style="font-size: 13px; font-weight: 400; color: #2e3a59; margin-bottom: 15px;">
|
||||
-お問い合わせ内容:
|
||||
</p>
|
||||
<p style="font-size: 13px; font-weight: 400; color: #2e3a59;">
|
||||
${formData.get('contents')}
|
||||
</p>
|
||||
</div>
|
||||
`
|
||||
}
|
||||
|
||||
@ -5,7 +5,7 @@ import { useSessionStore } from '@/store/session'
|
||||
import { InquiryRequest } from '@/types/Inquiry'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { CONFIRM_MESSAGE, SUCCESS_MESSAGE, useAlertMsg, WARNING_MESSAGE } from '@/hooks/useAlertMsg'
|
||||
import { CONFIRM_MESSAGE, ERROR_MESSAGE, SUCCESS_MESSAGE, useAlertMsg, WARNING_MESSAGE } from '@/hooks/useAlertMsg'
|
||||
import { useInquiryFilterStore } from '@/store/inquiryFilterStore'
|
||||
|
||||
export default function RegistForm() {
|
||||
@ -106,9 +106,13 @@ export default function RegistForm() {
|
||||
showConfirm(
|
||||
CONFIRM_MESSAGE.SAVE_INQUIRY_CONFIRM,
|
||||
async () => {
|
||||
const res = await saveInquiry(formData)
|
||||
showSuccessAlert(SUCCESS_MESSAGE.SAVE_SUCCESS)
|
||||
router.push(`/inquiry/${res.qnaNo}`)
|
||||
try {
|
||||
const res = await saveInquiry(formData)
|
||||
showSuccessAlert(SUCCESS_MESSAGE.SAVE_SUCCESS)
|
||||
router.push(`/inquiry/${res.qnaNo}`)
|
||||
} catch (error) {
|
||||
showErrorAlert(ERROR_MESSAGE.SERVER_ERROR)
|
||||
}
|
||||
},
|
||||
() => null,
|
||||
)
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
'use server'
|
||||
|
||||
import nodemailer from 'nodemailer'
|
||||
import { Attachment } from 'nodemailer/lib/mailer'
|
||||
|
||||
interface EmailParams {
|
||||
from: string
|
||||
@ -8,9 +9,10 @@ interface EmailParams {
|
||||
cc?: string | string[]
|
||||
subject: string
|
||||
content: string
|
||||
attachments?: Attachment[]
|
||||
}
|
||||
|
||||
export async function sendEmail({ from, to, cc, subject, content }: EmailParams): Promise<void> {
|
||||
export async function sendEmail({ from, to, cc, subject, content, attachments }: EmailParams): Promise<void> {
|
||||
// Create a transporter using SMTP
|
||||
const transporter = nodemailer.createTransport({
|
||||
host: process.env.SMTP_HOST,
|
||||
@ -27,6 +29,7 @@ export async function sendEmail({ from, to, cc, subject, content }: EmailParams)
|
||||
cc: cc ? (Array.isArray(cc) ? cc.join(', ') : cc) : undefined,
|
||||
subject,
|
||||
html: content,
|
||||
attachments: attachments || [],
|
||||
}
|
||||
|
||||
try {
|
||||
@ -37,13 +40,3 @@ export async function sendEmail({ from, to, cc, subject, content }: EmailParams)
|
||||
throw new Error('Failed to send email')
|
||||
}
|
||||
}
|
||||
|
||||
async function sendEmailTest() {
|
||||
await sendEmail({
|
||||
from: 'from@test.com',
|
||||
to: 'test@test.com',
|
||||
cc: 'test2@test.com',
|
||||
subject: 'Test Email',
|
||||
content: '<h1>Hello</h1><p>This is a test email.</p>',
|
||||
})
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user