fix: Move mail to src/
This commit is contained in:
@@ -14,7 +14,8 @@ import { OrganizationModule } from './organization/organization.module';
|
||||
import { OrganizationMembershipModule } from './organization-membership/organization-membership.module';
|
||||
import { AuthorizationModule } from './authorization/authorization.module';
|
||||
import { CacheModule } from './cache/cache.module';
|
||||
import { MailModule } from 'core/mail/mail.module';
|
||||
import { EventEmitterModule } from '@nestjs/event-emitter';
|
||||
import { MailModule } from './mail/mail.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -29,7 +30,8 @@ import { MailModule } from 'core/mail/mail.module';
|
||||
OrganizationMembershipModule,
|
||||
AuthorizationModule,
|
||||
CacheModule,
|
||||
MailModule
|
||||
MailModule,
|
||||
EventEmitterModule.forRoot()
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [
|
||||
|
||||
@@ -6,7 +6,7 @@ import { AuthGuard } from './guards/auth.guard';
|
||||
import { UserModule } from 'src/user/user.module';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
import { RequestContextModule } from 'core/als/request-context.module';
|
||||
import { MailModule } from 'core/mail/mail.module';
|
||||
import { MailModule } from 'src/mail/mail.module';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
|
||||
@@ -5,8 +5,8 @@ import * as bcrypt from 'bcrypt';
|
||||
import { UserService } from 'src/user/user.service';
|
||||
import { TokenInputType } from './types';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { MailService } from 'core/mail/mail.service';
|
||||
import EmailTemplates from 'common/emails';
|
||||
import { MailService } from 'src/mail/mail.service';
|
||||
|
||||
@Injectable()
|
||||
@Public()
|
||||
|
||||
8
src/mail/mail.module.ts
Normal file
8
src/mail/mail.module.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { MailService } from './mail.service';
|
||||
|
||||
@Module({
|
||||
providers: [MailService],
|
||||
exports: [MailService]
|
||||
})
|
||||
export class MailModule { }
|
||||
18
src/mail/mail.service.spec.ts
Normal file
18
src/mail/mail.service.spec.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { MailService } from './mail.service';
|
||||
|
||||
describe('MailService', () => {
|
||||
let service: MailService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [MailService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<MailService>(MailService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
45
src/mail/mail.service.ts
Normal file
45
src/mail/mail.service.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import * as nodemailer from "nodemailer";
|
||||
|
||||
@Injectable()
|
||||
export class MailService {
|
||||
private transporter: nodemailer.Transporter;
|
||||
private mailServiceAvailable = false;
|
||||
|
||||
constructor(private readonly configService: ConfigService) {
|
||||
const mailId = this.configService.get<string | undefined>("MAIL_ID");
|
||||
const mailPass = this.configService.get<string | undefined>("MAIL_PASS");
|
||||
|
||||
if (!mailId || !mailPass)
|
||||
throw new Error("Make sure MAIL_ID and MAIL_PASS environment variables are set")
|
||||
|
||||
// Use secure in prod
|
||||
// TODO: A table for failed emails to retry later(actually bullmq)
|
||||
this.transporter = nodemailer.createTransport({
|
||||
host: "smtp.gmail.com",
|
||||
port: 587,
|
||||
secure: false, // Use true for port 465, false for port 587
|
||||
auth: {
|
||||
user: mailId,
|
||||
pass: mailPass,
|
||||
},
|
||||
from: mailId
|
||||
});
|
||||
|
||||
this.mailServiceAvailable = true;
|
||||
}
|
||||
|
||||
sendMail({ to, subject, body }: { to: string, subject: string, body: string }) {
|
||||
if (!this.mailServiceAvailable)
|
||||
throw new Error("Mail service not available")
|
||||
|
||||
this.transporter.sendMail(
|
||||
{
|
||||
to,
|
||||
subject,
|
||||
html: body
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user