diff options
Diffstat (limited to 'lib/email-template/service.ts')
| -rw-r--r-- | lib/email-template/service.ts | 64 |
1 files changed, 35 insertions, 29 deletions
diff --git a/lib/email-template/service.ts b/lib/email-template/service.ts index 7c92b377..946e0a26 100644 --- a/lib/email-template/service.ts +++ b/lib/email-template/service.ts @@ -1,40 +1,31 @@ 'use server'; +/* IMPORT */ +import { and, asc, count, desc, eq, ilike, or, sql, type SQL } from 'drizzle-orm'; +import { authOptions } from '@/app/api/auth/[...nextauth]/route'; import db from '@/db/db'; -import { eq, and, desc, sql, count, ilike, asc, or, SQL } from 'drizzle-orm'; +import { filterColumns } from '../filter-columns'; +import { getOptions } from '@/i18n/settings'; +import { getServerSession } from 'next-auth'; import handlebars from 'handlebars'; import i18next from 'i18next'; import resourcesToBackend from 'i18next-resources-to-backend'; -import { getOptions } from '@/i18n/settings'; - -// Schema imports -import { - templateListView, - templateDetailView, - type TemplateListView, - type TemplateDetailView -} from '@/db/schema'; import { templates, templateVariables, templateHistory, type Template, type TemplateVariable, - type TemplateHistory } from '@/db/schema/templates'; +import { templateListView } from '@/db/schema'; +import { type GetEmailTemplateSchema } from './validations'; -// Validation imports -import { GetEmailTemplateSchema } from './validations'; -import { filterColumns } from '../filter-columns'; - -// =========================================== -// Types -// =========================================== +// ---------------------------------------------------------------------------------------------------- +/* TYPES */ export interface TemplateWithVariables extends Template { variables: TemplateVariable[]; } - interface ValidationResult { isValid: boolean; errors: string[]; @@ -225,6 +216,17 @@ function registerHandlebarsHelpers(): void { // Core Service Functions // =========================================== +/* HELPER FUNCTION FOR GETTING CURRENT USER ID */ +export async function getCurrentUserId(): Promise<number> { + try { + const session = await getServerSession(authOptions); + return session?.user?.id ? Number(session.user.id) : 3; // 기본값 3, 실제 환경에서는 적절한 기본값 설정 + } catch (error) { + console.error('Error in Getting Current Session User ID:', error); + return 3; // 기본값 3 + } +} + /** * 템플릿 목록 조회 (View 테이블 사용) */ @@ -433,6 +435,7 @@ export async function updateTemplate(slug: string, data: { subject?: string; content?: string; description?: string; + category?: string; sampleData?: Record<string, any>; updatedBy: number; }): Promise<TemplateWithVariables> { @@ -444,10 +447,10 @@ export async function updateTemplate(slug: string, data: { // 버전 히스토리 저장 await db.insert(templateHistory).values({ - templateId: existingTemplate.id, - version: existingTemplate.version, - subject: existingTemplate.subject, - content: existingTemplate.content, + templateId: existingTemplate.id!, + version: existingTemplate.version!, + subject: existingTemplate.subject!, + content: existingTemplate.content!, changeDescription: '템플릿 업데이트', changedBy: data.updatedBy, }); @@ -460,8 +463,9 @@ export async function updateTemplate(slug: string, data: { content: data.content || existingTemplate.content, subject: data.subject || existingTemplate.subject, description: data.description || existingTemplate.description, + category: data.category || existingTemplate.category, sampleData: data.sampleData || existingTemplate.sampleData, - version: existingTemplate.version + 1, + version: existingTemplate.version ? existingTemplate.version + 1 : 1, updatedAt: new Date(), }) .where(eq(templates.id, existingTemplate.id)); @@ -653,7 +657,7 @@ export async function duplicateTemplate( id: string, newName: string, newSlug: string, - userId: string + userId: number, ): Promise<{ success: boolean; error?: string; data?: any }> { try { // 원본 템플릿 조회 (변수 포함) @@ -691,12 +695,13 @@ export async function duplicateTemplate( .values({ name: newName, slug: newSlug, + subject: originalTemplate.subject, content: originalTemplate.content, description: originalTemplate.description ? `${originalTemplate.description} (복사본)` : undefined, category: originalTemplate.category, sampleData: originalTemplate.sampleData, + version: 1, createdBy: userId, - version: 1 }) .returning(); @@ -869,6 +874,7 @@ export async function updateTemplateAction(slug: string, data: { subject?: string; content?: string; description?: string; + category?: string; sampleData?: Record<string, any>; updatedBy: number; }) { @@ -883,7 +889,7 @@ export async function updateTemplateAction(slug: string, data: { if (!validation.isValid) { return { success: false, - error: `보안 검증 실패: ${validation.errors.join(', ')}` + error: `보안 검증 실패: ${validation.errors.join(', ')}`, }; } @@ -895,12 +901,12 @@ export async function updateTemplateAction(slug: string, data: { return { success: true, - data: template + data: template, }; } catch (error) { return { success: false, - error: error instanceof Error ? error.message : '템플릿 수정에 실패했습니다.' + error: error instanceof Error ? error.message : '템플릿 수정에 실패했습니다.', }; } } |
