1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
import nodemailer from 'nodemailer';
import handlebars from 'handlebars';
import fs from 'fs';
import path from 'path';
import i18next from 'i18next';
// Nodemailer Transporter 생성
const transporter = nodemailer.createTransport({
host: process.env.Email_Host,
port: parseInt(process.env.Email_Port || '465'),
secure: process.env.Email_Secure === 'true',
auth: {
user: process.env.Email_User_Name,
pass: process.env.Email_Password,
},
});
// 헬퍼 함수들 등록
function registerHandlebarsHelpers() {
// i18next 헬퍼 등록
handlebars.registerHelper('t', function(key: string, options: { hash?: Record<string, unknown> }) {
// options.hash에는 Handlebars에서 넘긴 named parameter들이 들어있음
return i18next.t(key, options.hash || {});
});
// eq 헬퍼 등록 - 인라인과 블록 헬퍼 둘 다 지원
handlebars.registerHelper('eq', function(a: any, b: any, options?: any) {
const result = a === b;
// 블록 헬퍼로 사용된 경우 (options가 있고 fn 함수가 있는 경우)
if (options && typeof options.fn === 'function') {
if (result) {
return options.fn(this);
} else {
return options.inverse(this);
}
}
// 인라인 헬퍼로 사용된 경우
return result;
});
// ne 헬퍼 - 인라인과 블록 헬퍼 둘 다 지원
handlebars.registerHelper('ne', function(a: any, b: any, options?: any) {
const result = a !== b;
if (options && typeof options.fn === 'function') {
if (result) {
return options.fn(this);
} else {
return options.inverse(this);
}
}
return result;
});
// gt 헬퍼 - 인라인과 블록 헬퍼 둘 다 지원
handlebars.registerHelper('gt', function(a: any, b: any, options?: any) {
const result = a > b;
if (options && typeof options.fn === 'function') {
if (result) {
return options.fn(this);
} else {
return options.inverse(this);
}
}
return result;
});
// gte 헬퍼 - 인라인과 블록 헬퍼 둘 다 지원
handlebars.registerHelper('gte', function(a: any, b: any, options?: any) {
const result = a >= b;
if (options && typeof options.fn === 'function') {
if (result) {
return options.fn(this);
} else {
return options.inverse(this);
}
}
return result;
});
// lt 헬퍼 - 인라인과 블록 헬퍼 둘 다 지원
handlebars.registerHelper('lt', function(a: any, b: any, options?: any) {
const result = a < b;
if (options && typeof options.fn === 'function') {
if (result) {
return options.fn(this);
} else {
return options.inverse(this);
}
}
return result;
});
// lte 헬퍼 - 인라인과 블록 헬퍼 둘 다 지원
handlebars.registerHelper('lte', function(a: any, b: any, options?: any) {
const result = a <= b;
if (options && typeof options.fn === 'function') {
if (result) {
return options.fn(this);
} else {
return options.inverse(this);
}
}
return result;
});
// and 헬퍼 - 모든 조건이 true인지 확인 (블록 헬퍼)
handlebars.registerHelper('and', function(...args: any[]) {
// 마지막 인자는 Handlebars 옵션
const options = args[args.length - 1];
const values = args.slice(0, -1);
const result = values.every(Boolean);
if (typeof options.fn === 'function') {
if (result) {
return options.fn(this);
} else {
return options.inverse(this);
}
}
return result;
});
// or 헬퍼 - 하나라도 true인지 확인 (블록 헬퍼)
handlebars.registerHelper('or', function(...args: any[]) {
// 마지막 인자는 Handlebars 옵션
const options = args[args.length - 1];
const values = args.slice(0, -1);
const result = values.some(Boolean);
if (typeof options.fn === 'function') {
if (result) {
return options.fn(this);
} else {
return options.inverse(this);
}
}
return result;
});
// not 헬퍼 - 값 반전 (블록 헬퍼)
handlebars.registerHelper('not', function(value: any, options: any) {
const result = !value;
if (typeof options.fn === 'function') {
if (result) {
return options.fn(this);
} else {
return options.inverse(this);
}
}
return result;
});
// formatDate 헬퍼 - 날짜 포맷팅
handlebars.registerHelper('formatDate', function(date: string | Date, format: string = 'YYYY-MM-DD') {
if (!date) return '';
const dateObj = new Date(date);
if (isNaN(dateObj.getTime())) return '';
// 간단한 날짜 포맷팅 (더 복잡한 경우 moment.js나 date-fns 사용)
const year = dateObj.getFullYear();
const month = String(dateObj.getMonth() + 1).padStart(2, '0');
const day = String(dateObj.getDate()).padStart(2, '0');
return format
.replace('YYYY', String(year))
.replace('MM', month)
.replace('DD', day);
});
// formatNumber 헬퍼 - 숫자 포맷팅
handlebars.registerHelper('formatNumber', function(number: number, locale: string = 'ko-KR') {
if (typeof number !== 'number') return number;
return new Intl.NumberFormat(locale).format(number);
});
}
// 헬퍼 등록 실행
registerHandlebarsHelpers();
// 템플릿 로더 함수
function loadTemplate(templateName: string, data: Record<string, unknown>) {
const templatePath = path.join(process.cwd(), 'lib', 'mail', 'templates', `${templateName}.hbs`);
if (!fs.existsSync(templatePath)) {
throw new Error(`Template not found: ${templatePath}`);
}
const source = fs.readFileSync(templatePath, 'utf8');
const template = handlebars.compile(source);
return template(data);
}
export { transporter, loadTemplate };
|