-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
211 lines (180 loc) · 6.39 KB
/
index.ts
File metadata and controls
211 lines (180 loc) · 6.39 KB
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
210
211
import {sendMail} from './mailer';
import {env} from '../../constants';
import {CommonError} from '../../util/errors';
import {approvePayout} from './templates/approvePayout';
import {userCommonCreated} from './templates/userCommonCreated';
import {userJoinedSuccess} from './templates/userJoinedSuccess';
import {adminPayInSuccess} from './templates/adminPayInSuccess';
import {adminCommonCreated} from './templates/adminCommonCreated';
import {userCommonFeatured} from './templates/userCommonFeatured';
import {requestToJoinSubmitted} from './templates/requestToJoinSubmitted';
import {userFundingRequestAcceptedUnknown} from './templates/userFundingRequestAcceptedUnknown';
import {userFundingRequestAcceptedIsraeli} from './templates/userFundingRequestAcceptedIsraeli';
import {userFundingRequestAcceptedForeign} from './templates/userFundingRequestAcceptedForeign';
import {userFundingRequestAcceptedZeroAmount} from './templates/userFundingRequestAcceptedZeroAmount';
import {userJoinedButFailedPayment} from './templates/userJoinedButFailedPayment';
import {adminFundingRequestAccepted} from './templates/adminFundingRequestAccepted';
import {adminPreauthorizationFailed} from './templates/adminPreauthorizationFailed';
import {adminJoinedButPaymentFailed} from './templates/adminJoinedButFailedPayment';
import {subscriptionCanceled} from './templates/subscriptionCanceled';
import {subscriptionChargeFailed} from './templates/subscriptionChargeFailed';
import {subscriptionCharged} from './templates/subscriptionCharged';
import {userFundingRequestAcceptedInsufficientFunds} from './templates/userFundingRequestAcceptedInsufficientFunds';
const templates = {
requestToJoinSubmitted,
adminCommonCreated,
adminFundingRequestAccepted,
adminPreauthorizationFailed,
userCommonCreated,
userCommonFeatured,
userFundingRequestAcceptedUnknown,
userFundingRequestAcceptedIsraeli,
userFundingRequestAcceptedForeign,
userFundingRequestAcceptedZeroAmount,
userJoinedButFailedPayment,
userJoinedSuccess,
adminJoinedButPaymentFailed,
adminPayInSuccess,
approvePayout,
subscriptionCanceled,
subscriptionCharged,
subscriptionChargeFailed,
userFundingRequestAcceptedInsufficientFunds,
};
const globalDefaultStubs = {
supportChatLink: 'https://common.io/help',
};
const replaceAll = (string, search, replace) => {
return string.split(search).join(replace);
};
const isNullOrUndefined = (val) => val === null || val === undefined;
interface IStub {
required: boolean;
default?: string;
}
export interface IEmailTemplate {
template: string;
subject: string;
emailStubs?: {
[key: string]: IStub;
};
subjectStubs?: {
[key: string]: IStub;
};
}
interface ITemplatedEmail {
body: string;
subject: string;
}
// @todo Make the payload type based on the templateKey
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const getTemplatedEmail = (
templateKey: keyof typeof templates,
payload: any,
): ITemplatedEmail => {
let {template, subject} = templates[templateKey];
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const {emailStubs, subjectStubs} = templates[templateKey];
// @todo Logger is not definded here because the file is JS. Move it to TS
// eslint-disable-next-line no-console
console.debug('Email templating started');
if (isNullOrUndefined(template)) {
throw new CommonError(
`The requested template (${templateKey}) cannot be found`,
);
}
// Validate and add default values for the email template
for (const stub in emailStubs || {}) {
// Check if the stub is required. If it is check is there is value either in the
// global stubs, the default stubs or the user provided ones
if (
emailStubs[stub].required &&
isNullOrUndefined(payload.emailStubs[stub]) &&
isNullOrUndefined(emailStubs[stub].default) &&
isNullOrUndefined(globalDefaultStubs[stub])
) {
throw new CommonError(
`Required stub ${stub} was not provided for ${templateKey} template`,
);
}
// If there is a default value for the stub and has not been replaced add it here
if (
!isNullOrUndefined(emailStubs[stub].default) &&
isNullOrUndefined(payload.emailStubs[stub])
) {
template = template.replace(`{{${stub}}}`, emailStubs[stub]);
} else if (
!isNullOrUndefined(globalDefaultStubs[stub]) &&
isNullOrUndefined(payload.emailStubs[stub])
) {
template = template.replace(`{{${stub}}}`, globalDefaultStubs[stub]);
}
}
// Replace all provided stubs in the template
for (const stub in payload.emailStubs) {
template = replaceAll(template, `{{${stub}}}`, payload.emailStubs[stub]);
}
// Validate the email subject
for (const stub in subjectStubs) {
if (
subjectStubs[stub].required &&
isNullOrUndefined(payload.subjectStubs[stub])
) {
throw new CommonError(
`Required stub ${stub} was not provided for subject template`,
);
}
}
for (const stub in payload.subjectStubs) {
subject = replaceAll(subject, `{{${stub}}}`, payload.subjectStubs[stub]);
}
// eslint-disable-next-line no-console
console.debug(`Email templating finished for template ${templateKey}`);
return {
body: template,
subject,
};
};
export interface ISendTemplatedEmailData {
templateKey: keyof typeof templates;
emailStubs?: any;
subjectStubs?: any;
to: string | string[];
from?: string;
bcc?: string;
}
type SendTemplatedEmail = (data: ISendTemplatedEmailData) => Promise<void>;
export const sendTemplatedEmail: SendTemplatedEmail = async ({
templateKey,
emailStubs,
subjectStubs,
to,
from,
bcc,
}) => {
to === 'admin' && (to = env.mail.adminMail);
const {body, subject} = getTemplatedEmail(templateKey, {
emailStubs,
subjectStubs,
});
if (Array.isArray(to)) {
const emailPromises = [];
to.forEach((emailTo) => {
// eslint-disable-next-line no-console
console.log(`Sending ${templateKey} to ${emailTo}.`);
emailPromises.push(sendMail(emailTo, subject, body, from, bcc));
});
await Promise.all(emailPromises);
} else {
// eslint-disable-next-line no-console
console.log(`Sending ${templateKey} to ${to}.`);
await sendMail(to, subject, body, from, bcc);
}
// eslint-disable-next-line no-console
console.log('Templated email send successfully');
};
export default {
getTemplatedEmail,
sendTemplatedEmail,
};