Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 034293a

Browse files
server/utils/mail: add types & update to named export
1 parent 1ca9c14 commit 034293a

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

‎server/controllers/user.controller.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import crypto from 'crypto';
22

33
import User from '../models/user';
4-
import mail from '../utils/mail';
4+
import {mailerService} from '../utils/mail';
55
import { renderEmailConfirmation, renderResetPassword } from '../views/mail';
66

77
export * from './user.controller/apiKey';
@@ -83,7 +83,7 @@ export async function createUser(req, res) {
8383
});
8484

8585
try {
86-
await mail.send(mailOptions);
86+
await mailerService.send(mailOptions);
8787
res.json(userResponse(user));
8888
} catch (mailErr) {
8989
console.error(mailErr);
@@ -155,7 +155,7 @@ export async function resetPasswordInitiate(req, res) {
155155
to: user.email
156156
});
157157

158-
await mail.send(mailOptions);
158+
await mailerService.send(mailOptions);
159159
res.json({
160160
success: true,
161161
message:
@@ -203,7 +203,7 @@ export async function emailVerificationInitiate(req, res) {
203203
to: user.email
204204
});
205205
try {
206-
await mail.send(mailOptions);
206+
await mailerService.send(mailOptions);
207207
} catch (mailErr) {
208208
res.status(500).send({ error: 'Error sending mail' });
209209
return;
@@ -334,7 +334,7 @@ export async function updateSettings(req, res) {
334334
to: user.email
335335
});
336336

337-
await mail.send(mailOptions);
337+
await mailerService.send(mailOptions);
338338
} else {
339339
await saveUser(res, user);
340340
}

‎server/migrations/emailConsolidation.js‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
moveObjectToUserInS3,
88
copyObjectInS3
99
} from '../controllers/aws.controller';
10-
import mail from '../utils/mail';
10+
import {mailerService} from '../utils/mail';
1111
import { renderAccountConsolidation } from '../views/mail';
1212

1313
const mongoConnectionString = process.env.MONGO_URL;
@@ -31,7 +31,8 @@ mongoose.connection.on('error', () => {
3131
* https://mongodb.github.io/node-mongodb-native
3232
*/
3333

34-
const agg = [ // eslint-disable-line
34+
const agg = [
35+
// eslint-disable-line
3536
{
3637
$project: {
3738
email: {
@@ -187,7 +188,7 @@ async function consolidateAccount(email) {
187188
});
188189

189190
return new Promise((resolve, reject) => {
190-
mail.send(mailOptions, (mailErr, result) => {
191+
mailerService.send(mailOptions, (mailErr, result) => {
191192
console.log('Sent email.');
192193
if (mailErr) {
193194
return reject(mailErr);
@@ -226,7 +227,7 @@ async function consolidateAccount(email) {
226227
// });
227228

228229
// return new Promise((resolve, reject) => {
229-
// mail.send(mailOptions, (mailErr, result) => {
230+
// mailerService.send(mailOptions, (mailErr, result) => {
230231
// console.log('Sent email.');
231232
// if (mailErr) {
232233
// return reject(mailErr);

‎server/utils/mail.ts‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
1-
/**
2-
* Mail service wrapping around mailgun
3-
*/
4-
51
import nodemailer from 'nodemailer';
62
import mg from 'nodemailer-mailgun-transport';
3+
import { RenderedMailerData } from '../types/email';
4+
5+
if (!process.env.MAILGUN_KEY) {
6+
throw new Error('Mailgun key missing');
7+
}
78

89
const auth = {
910
api_key: process.env.MAILGUN_KEY,
1011
domain: process.env.MAILGUN_DOMAIN
1112
};
1213

14+
/** Mail service class wrapping around mailgun */
1315
class Mail {
16+
client: nodemailer.Transporter;
17+
18+
sendOptions: Pick<nodemailer.SendMailOptions, 'from'>;
19+
1420
constructor() {
1521
this.client = nodemailer.createTransport(mg({ auth }));
1622
this.sendOptions = {
1723
from: process.env.EMAIL_SENDER
1824
};
1925
}
2026

21-
async sendMail(mailOptions) {
27+
async sendMail(mailOptions: nodemailer.SendMailOptions) {
2228
try {
2329
const response = await this.client.sendMail(mailOptions);
2430
return response;
@@ -28,8 +34,8 @@ class Mail {
2834
}
2935
}
3036

31-
async send(data) {
32-
const mailOptions = {
37+
async send(data: RenderedMailerData) {
38+
const mailOptions: nodemailer.SendMailOptions = {
3339
from: this.sendOptions.from,
3440
to: data.to,
3541
subject: data.subject,
@@ -46,4 +52,7 @@ class Mail {
4652
}
4753
}
4854

49-
export default new Mail();
55+
/**
56+
* Mail service wrapping around mailgun
57+
*/
58+
export const mailerService = new Mail();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /