Mailservice
This commit is contained in:
10
api/src/modules/log/log.module.ts
Normal file
10
api/src/modules/log/log.module.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { LogService } from './log.service';
|
||||
import { DatabaseModule } from 'src/shared/database/database.module';
|
||||
|
||||
@Module({
|
||||
imports: [DatabaseModule],
|
||||
providers: [LogService],
|
||||
exports: [LogService]
|
||||
})
|
||||
export class LogModule {}
|
||||
18
api/src/modules/log/log.service.spec.ts
Normal file
18
api/src/modules/log/log.service.spec.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { LogService } from './log.service';
|
||||
|
||||
describe('LogService', () => {
|
||||
let service: LogService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [LogService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<LogService>(LogService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
37
api/src/modules/log/log.service.ts
Normal file
37
api/src/modules/log/log.service.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { EmailLogRepository } from 'src/model/repositories/log';
|
||||
|
||||
@Injectable()
|
||||
export class LogService {
|
||||
constructor(private readonly emailLogRepo: EmailLogRepository) {}
|
||||
|
||||
|
||||
|
||||
log(type: LogType, data: any) {
|
||||
if (type == LogType.Mail) {
|
||||
return this.logEmail(data);
|
||||
}
|
||||
}
|
||||
|
||||
private async logEmail(data: EmailLogDto) {
|
||||
const log = this.emailLogRepo.create(data);
|
||||
const logEntry = await this.emailLogRepo.save(log);
|
||||
console.log(logEntry);
|
||||
}
|
||||
}
|
||||
|
||||
export enum LogType {
|
||||
Mail
|
||||
}
|
||||
|
||||
export enum EmailEvent {
|
||||
GrantSystemAccess,
|
||||
RemoveSystemAccess
|
||||
}
|
||||
|
||||
export interface EmailLogDto {
|
||||
success: boolean;
|
||||
message: string;
|
||||
to: string;
|
||||
type: EmailEvent;
|
||||
}
|
||||
45
api/src/modules/mail/mail.module.ts
Normal file
45
api/src/modules/mail/mail.module.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { MailerModule } from '@nestjs-modules/mailer';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter';
|
||||
import { join } from 'path';
|
||||
import { MailService } from './mail.service';
|
||||
import { LogModule } from '../log/log.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
LogModule,
|
||||
MailerModule.forRootAsync({
|
||||
imports: [ ],
|
||||
inject: [ ConfigService ],
|
||||
useFactory: async (config: ConfigService) => ({
|
||||
transport: {
|
||||
host: config.get('MAILER_HOST'),
|
||||
secure: config.get('MAILER_SECURE'),
|
||||
port: config.get('MAILER_PORT'),
|
||||
auth: {
|
||||
user: config.get('MAILER_USERNAME'),
|
||||
pass: config.get('MAILER_PASSWORD'),
|
||||
},
|
||||
},
|
||||
defaults: {
|
||||
from: config.get('MAILER_FROM'),
|
||||
},
|
||||
template: {
|
||||
dir: join(__dirname, '../../../templates'),
|
||||
adapter: new HandlebarsAdapter(), // or new PugAdapter() or new EjsAdapter()
|
||||
options: {
|
||||
strict: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
],
|
||||
providers: [MailService],
|
||||
exports: [MailService]
|
||||
})
|
||||
export class MailModule {
|
||||
constructor() {
|
||||
console.log(join(__dirname, '../../../templates'))
|
||||
}
|
||||
}
|
||||
68
api/src/modules/mail/mail.service.ts
Normal file
68
api/src/modules/mail/mail.service.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import { MailerService } from "@nestjs-modules/mailer";
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { EmailEvent, LogService, LogType } from "../log/log.service";
|
||||
|
||||
@Injectable()
|
||||
export class MailService {
|
||||
constructor(
|
||||
private mailserService: MailerService,
|
||||
private readonly configService: ConfigService,
|
||||
private readonly logService: LogService,
|
||||
) {
|
||||
}
|
||||
|
||||
async sendAccessGrantedMail({to, firstName, systemName}: {to: string, firstName: string, systemName: string}) {
|
||||
this.mailserService.sendMail({
|
||||
template: './access',
|
||||
to: to,
|
||||
from: this.configService.get<string>('MAILER_FROM'),
|
||||
subject: 'Zugriff gewährt',
|
||||
context: {
|
||||
firstName,
|
||||
systemName
|
||||
}
|
||||
}).then(v => {
|
||||
this.logService.log(LogType.Mail, {
|
||||
to,
|
||||
success: true,
|
||||
message: v.response,
|
||||
type: EmailEvent.GrantSystemAccess
|
||||
})
|
||||
}).catch(e => {
|
||||
this.logService.log(LogType.Mail, {
|
||||
to,
|
||||
success: false,
|
||||
message: e.response,
|
||||
type: EmailEvent.GrantSystemAccess
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
sendAccessRemovedMail({to, firstName, systemName}: {to: string, firstName: string, systemName: string}) {
|
||||
this.mailserService.sendMail({
|
||||
template: './access-removed',
|
||||
to: to,
|
||||
from: this.configService.get<string>('MAILER_FROM'),
|
||||
subject: 'Zugriff entzogen',
|
||||
context: {
|
||||
firstName,
|
||||
systemName
|
||||
}
|
||||
}).then(v => {
|
||||
this.logService.log(LogType.Mail, {
|
||||
to,
|
||||
success: true,
|
||||
message: v.response,
|
||||
type: EmailEvent.RemoveSystemAccess
|
||||
})
|
||||
}).catch(e => {
|
||||
this.logService.log(LogType.Mail, {
|
||||
to,
|
||||
success: false,
|
||||
message: e.response,
|
||||
type: EmailEvent.RemoveSystemAccess
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -3,10 +3,11 @@ import { SystemService } from './system.service';
|
||||
import { SystemController } from './system.controller';
|
||||
import { AuthModule } from '../auth/auth.module';
|
||||
import { DatabaseModule } from 'src/shared/database/database.module';
|
||||
import { MailModule } from '../mail/mail.module';
|
||||
|
||||
@Module({
|
||||
controllers: [SystemController],
|
||||
providers: [SystemService],
|
||||
imports: [AuthModule, DatabaseModule],
|
||||
imports: [AuthModule, DatabaseModule, MailModule],
|
||||
})
|
||||
export class SystemModule {}
|
||||
|
||||
@@ -4,6 +4,7 @@ import { UpdateSystemDto } from './dto/update-system.dto';
|
||||
import { ActivityRepository, KeySystemRepository, UserRepository } from 'src/model/repositories';
|
||||
import { User } from 'src/model/entitites';
|
||||
import { IUser } from 'src/model/interface';
|
||||
import { MailService } from '../mail/mail.service';
|
||||
|
||||
@Injectable()
|
||||
export class SystemService {
|
||||
@@ -11,6 +12,7 @@ export class SystemService {
|
||||
private systemRepo: KeySystemRepository,
|
||||
private userRepo: UserRepository,
|
||||
private systemActivityRepo: ActivityRepository,
|
||||
private mailService: MailService
|
||||
) {}
|
||||
|
||||
async create(user: User, createSystemDto: CreateSystemDto) {
|
||||
@@ -80,6 +82,7 @@ export class SystemService {
|
||||
sys.managers = sys.managers.filter( m => m.username != manageObject.email);
|
||||
|
||||
await this.systemRepo.save(sys);
|
||||
this.mailService.sendAccessRemovedMail({to: manageObject.email, firstName: manageObject.email, systemName: sys.name})
|
||||
return sys.managers;
|
||||
}
|
||||
|
||||
@@ -94,6 +97,7 @@ export class SystemService {
|
||||
|
||||
sys.managers.push(user);
|
||||
await this.systemRepo.save(sys);
|
||||
this.mailService.sendAccessGrantedMail({to: user.username, firstName: user.firstName, systemName: sys.name})
|
||||
return sys.managers;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user