Logging & Styling

This commit is contained in:
Bastian Wagner
2026-02-16 12:11:30 +01:00
parent 2eafa21baf
commit 9ea2229f5a
29 changed files with 169 additions and 90 deletions

View File

@@ -5,6 +5,7 @@ import { DatabaseModule } from 'src/shared/database/database.module';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { HttpModule } from '@nestjs/axios';
import { JwtModule, JwtService } from '@nestjs/jwt';
import { LogModule } from '../log/log.module';
@Module({
controllers: [AuthController],
@@ -21,6 +22,7 @@ import { JwtModule, JwtService } from '@nestjs/jwt';
signOptions: { expiresIn: config.get('JWT_EXPIRES_IN') },
}),
}),
LogModule
],
exports: [JwtModule, AuthService],
})

View File

@@ -7,6 +7,7 @@ import { ConfigService } from '@nestjs/config';
import { JwtService } from '@nestjs/jwt';
import { IExternalAccessPayload, IPayload } from 'src/model/interface';
import { User } from 'src/model/entitites';
import { LogService, LogType } from '../log/log.service';
@Injectable()
export class AuthService {
@@ -15,9 +16,12 @@ export class AuthService {
private readonly http: HttpService,
private configService: ConfigService,
private jwt: JwtService,
private log: LogService
) {}
register(register: CreateUserDto) {
return this.userRepo.createUser(register);
const user = this.userRepo.createUser(register);
this.log.log(LogType.Auth, user);
}
async registerOrLoginWithAuthCode(auth: AuthCodeDto): Promise<User> {
@@ -31,6 +35,7 @@ export class AuthService {
return resolve(null);
}
this.generateTokens(user);
this.log.log(LogType.Auth, user);
resolve(user);
},
error: () => {
@@ -116,6 +121,7 @@ export class AuthService {
}
getUserById(id: string): Promise<User> {
this.log.log(LogType.Auth, null);
return this.userRepo.findById(id);
}

View File

@@ -1,4 +1,5 @@
import { Injectable } from '@nestjs/common';
import { User } from 'src/model/entitites';
import { EmailLogRepository } from 'src/model/repositories/log';
@Injectable()
@@ -11,6 +12,8 @@ export class LogService {
log(type: LogType, data: any) {
if (type == LogType.Mail) {
return this.logEmail(data);
} else if (type == LogType.Auth) {
return this.logAuthEvent(data);
}
}
@@ -18,10 +21,15 @@ export class LogService {
const log = this.emailLogRepo.create(data);
const logEntry = await this.emailLogRepo.save(log);
}
private async logAuthEvent(data: User) {
console.error("auth logging not implemented")
}
}
export enum LogType {
Mail
Mail,
Auth
}
export enum EmailEvent {

View File

@@ -2,6 +2,8 @@ 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 { User } from "src/model/entitites";
@Injectable()
export class MailService {
@@ -12,56 +14,60 @@ export class MailService {
) {
}
async sendAccessGrantedMail({to, firstName, systemName}: {to: string, firstName: string, systemName: string}) {
async sendAccessGrantedMail({to, system}: {to: User, system: KeySystem}) {
this.mailserService.sendMail({
template: './access',
to: to,
to: to.username,
from: this.configService.get<string>('MAILER_FROM'),
subject: 'Zugriff gewährt',
context: {
firstName,
systemName
firstName: to.firstName,
systemName: system.name
}
}).then(v => {
this.logService.log(LogType.Mail, {
to,
success: true,
message: v.response,
type: EmailEvent.GrantSystemAccess
type: EmailEvent.GrantSystemAccess,
system
})
}).catch(e => {
this.logService.log(LogType.Mail, {
to,
success: false,
message: e.response,
type: EmailEvent.GrantSystemAccess
type: EmailEvent.GrantSystemAccess,
system
})
})
}
sendAccessRemovedMail({to, firstName, systemName}: {to: string, firstName: string, systemName: string}) {
sendAccessRemovedMail({to, system}: {to: User, system: KeySystem}) {
this.mailserService.sendMail({
template: './access-removed',
to: to,
to: to.username,
from: this.configService.get<string>('MAILER_FROM'),
subject: 'Zugriff entzogen',
context: {
firstName,
systemName
firstName: to.firstName,
systemName: system.name
}
}).then(v => {
this.logService.log(LogType.Mail, {
to,
success: true,
message: v.response,
type: EmailEvent.RemoveSystemAccess
type: EmailEvent.RemoveSystemAccess,
system
})
}).catch(e => {
this.logService.log(LogType.Mail, {
to,
success: false,
message: e.response,
type: EmailEvent.RemoveSystemAccess
type: EmailEvent.RemoveSystemAccess,
system
})
})
}

View File

@@ -87,7 +87,7 @@ export class SystemService {
relations: ['settings']
});
if (user.settings.sendSystemAccessMails) {
this.mailService.sendAccessRemovedMail({to: manageObject.email, firstName: manageObject.email, systemName: sys.name})
this.mailService.sendAccessRemovedMail({to: user, system: sys})
}
return sys.managers;
}
@@ -107,7 +107,7 @@ export class SystemService {
sys.managers.push(user);
await this.systemRepo.save(sys);
if (user.settings.sendSystemAccessMails) {
this.mailService.sendAccessGrantedMail({to: user.username, firstName: user.firstName, systemName: sys.name})
this.mailService.sendAccessGrantedMail({to: user, system: sys})
}
return sys.managers;