-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PDT24-24 | Create logger wrapper (#10)
* feat: add logging service with winston * chore(logging): use and fix logging information * PDT24-34 | Configure nodemailer service (#12) * feat: configure nodemailer servie for sending email * feat: create a basic email template for sending signup otp code * chore: rename param names and add function documentation * fix: use secure mailing port * refactor: remove sendEmail call from main and improve sendEmail method * refactor: remove ternary operator
- Loading branch information
1 parent
030b1ba
commit 0947ed2
Showing
10 changed files
with
909 additions
and
645 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { format, transports } from 'winston'; | ||
const isProduction = process.env.APP_ENV === 'production'; | ||
export const loggerConfig = { | ||
format: format.combine( | ||
format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), | ||
format.printf(({ level, message, timestamp }) => { | ||
return `${timestamp} [${level}]: ${message}`; | ||
}), | ||
), | ||
transports: isProduction | ||
? [ | ||
new transports.File({ filename: 'logs/errors.log', level: 'error' }), | ||
new transports.File({ filename: 'logs/all.log' }), | ||
] | ||
: [new transports.Console({ format: format.combine(format.colorize({ all: true })) })], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Address } from 'nodemailer/lib/mailer'; | ||
export class MailerDto { | ||
sender?: Address; | ||
recipients: Address[]; | ||
subject: string; | ||
html: string; | ||
text?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { MailerService } from './mailer.service'; | ||
|
||
@Module({ | ||
providers: [MailerService], | ||
exports: [MailerService], | ||
}) | ||
export class MailerModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { createTransport, SendMailOptions, Transporter } from 'nodemailer'; | ||
import { Logger } from '@nestjs/common'; | ||
import { MailerDto } from '@/mailer/dto/mailer.dto'; | ||
|
||
export class MailerService { | ||
private readonly logger = new Logger(); | ||
transporter: Transporter = createTransport({ | ||
host: process.env.EMAIL_HOST, | ||
port: 465, | ||
secure: true, | ||
auth: { | ||
user: process.env.EMAIL_USER, | ||
pass: process.env.EMAIL_PASS, | ||
}, | ||
}); | ||
|
||
async sendEmail(data: MailerDto): Promise<void> { | ||
try { | ||
const { sender, recipients, subject, html, text } = data; | ||
const mailOptions: SendMailOptions = { | ||
from: sender ?? { | ||
name: process.env.EMAIL_SENDER_NAME as string, | ||
address: process.env.EMAIL_USER as string, | ||
}, | ||
to: recipients, | ||
subject, | ||
html, | ||
text, | ||
}; | ||
await this.transporter.sendMail(mailOptions); | ||
this.logger.log('Mail sent successfully'); | ||
} catch (error) { | ||
this.logger.error(`Error sending email:`, { error }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export const signupOtpMailTemplate = { | ||
subject: 'Verify your account', | ||
/** | ||
* Generates a random OTP of the specified size. | ||
* @param otpCode - code to send to through email. | ||
* @param userName - name of the user to send the email to. | ||
*/ | ||
body: (otpCode: number, userName: string): string => ` | ||
<div> | ||
<p>Welcome ${userName}</p> | ||
<p>Use this code to verify your account:<br><b>${otpCode}</b> | ||
</div> | ||
`, | ||
}; |
Oops, something went wrong.