Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5 from matejbucek/main
Browse files Browse the repository at this point in the history
Pohles-Backend: Added parsing throught EJS.
  • Loading branch information
lukynmatuska authored Oct 3, 2021
2 parents aa47728 + db89cff commit 4870505
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@tsed/socketio": "^6.63.2",
"@tsed/swagger": "^6.63.2",
"@types/bluebird": "^3.5.36",
"@types/ejs": "^3.1.0",
"@types/nodemailer": "^6.4.4",
"@types/nodemailer-direct-transport": "^1.0.32",
"@types/nodemailer-smtp-transport": "^2.7.5",
Expand All @@ -32,6 +33,7 @@
"cors": "^2.8.5",
"cross-env": "^7.0.3",
"dotenv": "^10.0.0",
"ejs": "^3.1.6",
"express": "^4.17.1",
"express-session": "^1.17.2",
"keycloak-connect": "^15.0.2",
Expand Down
29 changes: 19 additions & 10 deletions src/services/Nodemailer.service.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { Constant, Injectable } from "@tsed/di";
import { Constant, Injectable, OnInit } from "@tsed/di";
import { NodemailerConfig } from "./Nodemailer.config";
import * as nodemailer from 'nodemailer';
import SMTPTransport from "nodemailer/lib/smtp-transport";
import Mail from "nodemailer/lib/mailer";
import moment from "moment";
import ejs from "ejs";
import path from "path";

@Injectable()
export class NodemailerService {
export class NodemailerService implements OnInit{
@Constant("nodemailer")
config: NodemailerConfig;

private _transporter: nodemailer.Transporter<SMTPTransport.SentMessageInfo>;

private _testAccount: nodemailer.TestAccount;


constructor() {
if (!this.config?.transport) {
$onInit(){
if (!this.config.transport) {
// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
nodemailer.createTestAccount((err, testAccount) => {
Expand Down Expand Up @@ -73,14 +74,18 @@ export class NodemailerService {
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
}

public async send(id: string, callback: Function) {
//this.nodeMailerService.sendAndParse("email@seznam.cz", "Testovací email", "test.ejs", {"text": "Hello, World!"});
public async sendAndParse(to: string, subject: string, fileName: string, data: object, options?: object){
this.sendHtml(to, subject, await this.parse(fileName, data, options));
}

public async sendHtml(to: string, subject: string, html: string) {
// send mail with defined transport object
let info = await this._transporter.sendMail({
from: this.config.sender, // sender address
to: "matuska.lukas@lukasmatuska.cz", // list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world?", // plain text body
html: "<b>Hello world?</b>", // html body
to: to, // list of receivers
subject: subject, // Subject line
html: html, // html body
});

console.log("Message sent: %s", info.messageId);
Expand All @@ -93,4 +98,8 @@ export class NodemailerService {
this._transporter.sendMail(mailOptions);
}*/


private parse(fileName: string, data: object, options?: object){
return ejs.renderFile(path.resolve(`src/templates/${fileName}`), data, options);
}
}
2 changes: 1 addition & 1 deletion src/services/Ticket.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Inject, Injectable } from "@tsed/di";
import { BadRequest } from "@tsed/exceptions";
import { MongooseModel } from "@tsed/mongoose";
import { CustomerModel } from "src/models/Customer.model";
import { TicketEasyModel, TicketModel, TicketUpdateModel } from "src/models/Ticket.model";
Expand Down Expand Up @@ -42,7 +43,6 @@ export class TicketService {
owner = await this.customerService.save(obj.buyer);
}


// Get active year
let year = await this.yearModel.findOne({ status: 'active' });
if (!year) {
Expand Down

0 comments on commit 4870505

Please sign in to comment.