import { MailerService } from "@nestjs-modules/mailer"; import { Injectable } from "@nestjs/common"; import { ConfigService } from "@nestjs/config"; import { EmailEvent, LogService, LogType } from "../log/log.service"; import { KeySystem } from "src/model/entitites/system.entity"; import { Key, KeyHandout, User } from "src/model/entitites"; @Injectable() export class MailService { constructor( private mailserService: MailerService, private readonly configService: ConfigService, private readonly logService: LogService ) { } async sendKeyLostOrFoundMail({to, key}: {to: User, key: Key}) { // const subject const keyAction = key.keyLost == null ? 'wurde gefunden' : 'wurde als verloren gemeldet'; const keyExtendedAction = key.keyLost == null ? `wurde als gefunden gemeldet` : `wurde am ${new Date(key.keyLost).toLocaleDateString()} als verloren gemeldet`; const subject = key.keyLost == null ? 'Schlüssel gefunden' : 'Schlüssel verloren'; const context = { keyAction, keyExtendedAction, firstName: to.firstName, keyNr: key.nr, keyName: key.name, url: 'https://keyvaultpro.de/keys?nr=' + key.nr } this.mailserService.sendMail({ template: './key-handout-changed', to: to.username, from: this.configService.get('MAILER_FROM'), subject: subject, context }).then(v => { this.logService.log(LogType.Mail, { to: to.username, success: true, message: v.response, type: EmailEvent.KeyLostOrFound, system: key.cylinder[0].system, context: JSON.stringify(key) }) }).catch(e => { this.logService.log(LogType.Mail, { to, success: false, message: e.response, type: EmailEvent.KeyLostOrFound, system: key.cylinder[0].system, context: JSON.stringify(key) }) }) } async sendKeyHandoutMail({to, key, handoutAction}: {to: User, key: Key, handoutAction: KeyHandout}) { const keyAction = handoutAction.direction == 'out' ? 'wurde ausgegeben' : 'wurde zurückgegeben'; const keyExtendedAction = handoutAction.direction == 'return' ? `wurde von ${handoutAction.customer.name} zurückgegeben` : `wurde an ${handoutAction.customer.name} ausgegeben`; const subject = handoutAction.direction == 'out' ? 'Schlüssel ausgegeben' : 'Schlüssel zurückgegeben'; const context = { keyAction, keyExtendedAction, firstName: to.firstName, keyNr: key.nr, keyName: key.name, url: 'https://keyvaultpro.de/keys?nr=' + key.nr } this.mailserService.sendMail({ template: './key-handout-changed', to: to.username, from: this.configService.get('MAILER_FROM'), subject: subject, context }).then(v => { this.logService.log(LogType.Mail, { to: to.username, success: true, message: v.response, type: EmailEvent.KeyHandout, system: key.cylinder[0].system, context: JSON.stringify(handoutAction) }) }).catch(e => { this.logService.log(LogType.Mail, { to, success: false, message: e.response, type: EmailEvent.KeyHandout, system: key.cylinder[0].system, context: JSON.stringify(handoutAction) }) }) } async sendAccessGrantedMail({to, system}: {to: User, system: KeySystem}) { this.mailserService.sendMail({ template: './access', to: to.username, from: this.configService.get('MAILER_FROM'), subject: 'Zugriff gewährt', context: { firstName: to.firstName, systemName: system.name } }).then(v => { this.logService.log(LogType.Mail, { to, success: true, message: v.response, type: EmailEvent.GrantSystemAccess, system }) }).catch(e => { this.logService.log(LogType.Mail, { to, success: false, message: e.response, type: EmailEvent.GrantSystemAccess, system }) }) } sendAccessRemovedMail({to, system}: {to: User, system: KeySystem}) { this.mailserService.sendMail({ template: './access-removed', to: to.username, from: this.configService.get('MAILER_FROM'), subject: 'Zugriff entzogen', context: { firstName: to.firstName, systemName: system.name } }).then(v => { this.logService.log(LogType.Mail, { to, success: true, message: v.response, type: EmailEvent.RemoveSystemAccess, system }) }).catch(e => { this.logService.log(LogType.Mail, { to, success: false, message: e.response, type: EmailEvent.RemoveSystemAccess, system }) }) } }