Skip to content

Commit

Permalink
registration email, handlebars templates
Browse files Browse the repository at this point in the history
  • Loading branch information
demshy committed Sep 13, 2020
1 parent 234586e commit 1b10286
Show file tree
Hide file tree
Showing 20 changed files with 340 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ DATABASE_USER=test
DATABASE_PASSWORD=test

JWT_SECRET=1i03RjH85jL4HTk1XHXmhwJjM-8ZfDxzOaN1Hnjx4XzDm0RcH6xmnGBlO

SMTP_HOST=smtp.gmail.com
SMTP_PORT=465
SMTP_USERNAME=[email protected]
SMTP_PASSWORD=zvbquyhagnjchutw
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
10 changes: 8 additions & 2 deletions nest-cli.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"collection": "@nestjs/schematics",
"sourceRoot": "src"
}
"sourceRoot": "src",
"compilerOptions": {
"assets": [
"**/*.hbs"
],
"watchAssets": true
}
}
86 changes: 81 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@
"@nestjs/passport": "^7.1.0",
"@nestjs/platform-express": "^7.0.0",
"@nestjs/typeorm": "^7.1.0",
"@types/nodemailer": "^6.4.0",
"apollo-server-express": "^2.16.1",
"bcrypt": "^5.0.0",
"class-transformer": "^0.3.1",
"class-validator": "^0.12.2",
"dotenv": "^8.2.0",
"fs": "0.0.1-security",
"graphql": "^15.3.0",
"graphql-tools": "^6.0.18",
"handlebars": "^4.7.6",
"hbs": "^4.1.1",
"nodemailer": "^6.4.11",
"passport": "^0.4.1",
"passport-jwt": "^4.0.0",
"pg": "^8.3.0",
Expand Down
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { Route } from './crags/entities/route.entity';
import { Area } from './crags/entities/area.entity';
import { Book } from './crags/entities/book.entity';
import { Grade } from './crags/entities/grade.entity';
import { NotificationModule } from './notification/notification.module';

@Module({
imports: [
Expand All @@ -41,6 +42,7 @@ import { Grade } from './crags/entities/grade.entity';
UsersModule,
CragsModule,
AuditModule,
NotificationModule,
],
controllers: [],
providers: [],
Expand Down
1 change: 0 additions & 1 deletion src/crags/services/crags.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ export class CragsService {
.addOrderBy('route.grade', 'DESC')
.getOne().then((route) => {
if (route != null && route.grade != null) {
console.log(route.id, route.grade);
return route.grade;
}

Expand Down
6 changes: 6 additions & 0 deletions src/notification/interfaces/mail-options.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface MailOptions {
to: string;
subject: string;
template: string;
templateParams?: any;
}
11 changes: 11 additions & 0 deletions src/notification/notification.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { NotificationService } from './services/notification.service';
import { MailService } from './services/mail.service';

@Module({
imports: [ConfigModule],
providers: [NotificationService, MailService, ConfigService],
exports: [NotificationService]
})
export class NotificationModule { }
18 changes: 18 additions & 0 deletions src/notification/services/mail.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
57 changes: 57 additions & 0 deletions src/notification/services/mail.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

import * as nodemailer from 'nodemailer';
import Mail from 'nodemailer/lib/mailer';
import { SentMessageInfo } from 'nodemailer/lib/smtp-transport';
import { MailOptions } from '../interfaces/mail-options.interface';
import { readFileSync } from 'fs';

import { compile } from 'handlebars';

@Injectable()
export class MailService {

transporter: Mail;

constructor(configService: ConfigService) {
this.transporter = nodemailer.createTransport({
host: configService.get("SMTP_HOST"),
port: configService.get("SMTP_PORT"),
secure: true,
auth: {
user: configService.get("SMTP_USERNAME"),
pass: configService.get("SMTP_PASSWORD"),
},
});
}

send(options: MailOptions): Promise<SentMessageInfo> {
return this.transporter.sendMail({
from: '"Plezanje.net" <[email protected]>',
to: options.to,
subject: options.subject,
text: this.render("plain", options.template, options.templateParams),
html: this.render("html", options.template, options.templateParams)
})
}

render(type: string, templateName: string, params: any = {}): string {
const template = compile(
readFileSync(__dirname + '/../templates/' + templateName + '.' + type + '.hbs').toString()
)

if (type == "plain") {
return template(params);
}

const layout = compile(
readFileSync(__dirname + '/../templates/layout.html.hbs').toString()
)

return layout({
content: template(params)
});
}

}
18 changes: 18 additions & 0 deletions src/notification/services/notification.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { NotificationService } from './notification.service';

describe('NotificationService', () => {
let service: NotificationService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [NotificationService],
}).compile();

service = module.get<NotificationService>(NotificationService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
19 changes: 19 additions & 0 deletions src/notification/services/notification.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Injectable } from '@nestjs/common';
import { User } from 'src/users/entities/user.entity';
import { MailService } from './mail.service';

@Injectable()
export class NotificationService {
constructor(private readonly mailService: MailService) { }

public accountConfirmation(user: User): void {
this.mailService.send({
to: user.email,
subject: 'Aktivacija računa',
template: 'account-confirmation',
templateParams: {
user: user
}
});
}
}
14 changes: 14 additions & 0 deletions src/notification/templates/account-confirmation.html.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<h1>Aktivacija uporabniškega računa</h1>

<p>
Na portalu plezanje.net ste
ustvarili nov račun, ki ga je pred uporabo
potrebno še aktivirati.
</p>
<p>
To naredite s klikom na spodnjo povezavo:
</p>
<p>
<a
href="http://localhost:4200/aktivacija/{{ user.id }}/{{ user.confirmationToken }}">https://www.plezanje.net/aktivacija/{{ user.id }}/{{ user.confirmationToken }}</a>
</p>
Loading

0 comments on commit 1b10286

Please sign in to comment.