summaryrefslogtreecommitdiff
path: root/lib/email-template/service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'lib/email-template/service.ts')
-rw-r--r--lib/email-template/service.ts123
1 files changed, 123 insertions, 0 deletions
diff --git a/lib/email-template/service.ts b/lib/email-template/service.ts
index e3ab9bed..c492410a 100644
--- a/lib/email-template/service.ts
+++ b/lib/email-template/service.ts
@@ -520,6 +520,85 @@ export async function addTemplateVariable(slug: string, variable: {
}
/**
+ * 템플릿 변수 수정
+ */
+export async function modifyTemplateVariable(
+ slug: string,
+ variableId: string,
+ updates: {
+ variableName?: string;
+ variableType?: string;
+ defaultValue?: string;
+ isRequired?: boolean;
+ description?: string;
+ }
+): Promise<TemplateVariable> {
+ try {
+ const template = await getTemplate(slug);
+ if (!template) {
+ throw new Error('템플릿을 찾을 수 없습니다.');
+ }
+
+ // 수정 대상 변수 존재 여부 확인
+ const existingVariable = template.variables.find(v => v.id === variableId);
+ if (!existingVariable) {
+ throw new Error('해당 변수를 찾을 수 없습니다.');
+ }
+
+ // 변수명 중복 확인
+ if (
+ updates.variableName &&
+ template.variables.some(
+ v => v.variableName === updates.variableName && v.id !== variableId
+ )
+ ) {
+ throw new Error('이미 존재하는 변수명입니다.');
+ }
+
+ // 업데이트 실행
+ const [updatedVariable] = await db
+ .update(templateVariables)
+ .set(updates)
+ .where(eq(templateVariables.id, variableId))
+ .returning();
+
+ return updatedVariable;
+ } catch (error) {
+ console.error('Error updating template variable:', error);
+ throw error;
+ }
+}
+
+/**
+ * 템플릿 변수 삭제
+ */
+export async function removeTemplateVariable(
+ slug: string,
+ variableId: string
+): Promise<void> {
+ try {
+ const template = await getTemplate(slug);
+ if (!template) {
+ throw new Error('템플릿을 찾을 수 없습니다.');
+ }
+
+ // 삭제 대상 변수 확인
+ const existingVariable = template.variables.find(v => v.id === variableId);
+ if (!existingVariable) {
+ throw new Error('해당 변수를 찾을 수 없습니다.');
+ }
+
+ // DB에서 삭제 실행
+ await db
+ .delete(templateVariables)
+ .where(eq(templateVariables.id, variableId));
+ } catch (error) {
+ console.error('Error removing template variable:', error);
+ throw error;
+ }
+}
+
+/**
* 템플릿 삭제 (소프트 삭제)
*/
export async function deleteTemplate(id: string): Promise<{ success: boolean; error?: string }> {
@@ -846,6 +925,50 @@ export async function addTemplateVariableAction(slug: string, variable: {
}
}
+export async function modifyTemplateVariableAction(
+ slug: string,
+ variableId: string,
+ updates: {
+ variableName?: string;
+ variableType?: string;
+ defaultValue?: string;
+ isRequired?: boolean;
+ description?: string;
+ }
+) {
+ try {
+ const modifiedVariable = await modifyTemplateVariable(slug, variableId, updates);
+
+ return {
+ success: true,
+ data: modifiedVariable,
+ };
+ } catch (error) {
+ return {
+ success: false,
+ error: error instanceof Error ? error.message : '변수 수정에 실패했습니다.',
+ };
+ }
+}
+
+export async function removeTemplateVariableAction(
+ slug: string,
+ variableId: string
+) {
+ try {
+ await removeTemplateVariable(slug, variableId);
+
+ return {
+ success: true,
+ };
+ } catch (error) {
+ return {
+ success: false,
+ error: error instanceof Error ? error.message : '변수 삭제에 실패했습니다.',
+ };
+ }
+}
+
export async function previewTemplateAction(
slug: string,
data: Record<string, any>,