refactoring

This commit is contained in:
Bastian Wagner
2025-01-03 11:00:02 +01:00
parent a47bbe29fe
commit c8c2ee18cb
35 changed files with 285 additions and 167 deletions

View File

@@ -5,7 +5,6 @@ import {
Customer,
Cylinder,
Key,
KeyActivity,
KeyHandout,
Role,
SSOUser,
@@ -16,7 +15,6 @@ import {
ActivityRepository,
CustomerRepository,
CylinderRepository,
KeyActivityRepository,
KeyRepository,
KeySystemRepository,
RoleRepository,
@@ -32,7 +30,6 @@ const ENTITIES = [
KeySystem,
Key,
Cylinder,
KeyActivity,
Customer,
KeyHandout,
Activity,
@@ -44,7 +41,6 @@ const REPOSITORIES = [
KeySystemRepository,
KeyRepository,
CylinderRepository,
KeyActivityRepository,
CustomerRepository,
KeyHandoutRepository,
ActivityRepository,

View File

@@ -0,0 +1,78 @@
import { Injectable } from "@nestjs/common";
import { Key, KeyHandout, User } from "src/model/entitites";
import { KeySystem } from "src/model/entitites/system.entity";
import { ActivityRepository, CylinderRepository, KeyRepository } from "src/model/repositories";
@Injectable()
export class ActivityHelperService {
constructor(
private readonly activityRepo: ActivityRepository,
private readonly keyRepository: KeyRepository,
private readonly cylinderRepo: CylinderRepository,
) {}
async logDeleteKey(user: User, key: Key, system?: KeySystem) {
if (!key || !user) { return; }
if (!system && !key.cylinder) {
key = await this.keyRepository.findOne({
where: { id: key.id },
relations: ['cylinder', 'cylinder.system']
})
system = key.cylinder[0].system;
} else if (!system && !key.cylinder[0].system) {
const c = await this.cylinderRepo.findOne({
where: { id: key.cylinder[0].id },
relations: ['system']
});
if (!c) { return; }
system = c.system;
}
if (!system) { return; }
let msg = `Schlüssel ${key.nr} entfernt`;
this.activityRepo.save(
this.activityRepo.create({
system,
user,
message: msg,
}))
}
async logKeyCreated(user: User, key: Key, system: KeySystem) {
let msg = `Schlüssel ${key.nr} angelegt`;
this.activityRepo.save(
this.activityRepo.create({
system,
user,
message: msg,
}))
}
async logKeyRenamed(user: User, key: Key, system: KeySystem) {
let msg = `Schlüssel ${key.nr} in ${key.name} umbenannt`;
this.activityRepo.save(
this.activityRepo.create({
system,
user,
message: msg,
}))
}
logKeyHandover(user: User, key: Key, system: KeySystem, handover: KeyHandout) {
const msg = `Schlüssel ${key.nr} ${handover.direction == 'out' ? 'ausgegeben an ' : 'zurückgegeben von '}${handover.customer.name}`
this.activityRepo.save(
this.activityRepo.create({
system,
user,
message: msg,
}))
}
}

View File

@@ -0,0 +1,18 @@
import { Module } from "@nestjs/common";
import { DatabaseModule } from "../database/database.module";
import { ManageHelperService } from "./system.helper.service";
import { CylinderRepository, KeySystemRepository } from "src/model/repositories";
import { Cylinder, User } from "src/model/entitites";
import { ActivityHelperService } from "./activity.logger.service";
@Module({
imports: [ DatabaseModule ],
providers: [ ManageHelperService, ActivityHelperService ],
exports: [ ManageHelperService, ActivityHelperService ],
})
export class SharedServiceModule {
}

View File

@@ -0,0 +1,38 @@
import { Injectable } from "@nestjs/common";
import { User, Cylinder, Key } from "src/model/entitites";
import { KeySystemRepository, CylinderRepository, KeyRepository } from "src/model/repositories";
@Injectable()
export class ManageHelperService {
constructor(
private readonly systemRepository: KeySystemRepository,
private readonly cylinderRepository: CylinderRepository,
private readonly keyRepo: KeyRepository,
) {}
/**
* Gibt den Cylinder zurück wenn der user ein Manager ist. Gibt null zurück wenn er nicht existiert oder der
* User den Cylinder nicht managen darf.
* @param user User
* @param id ID vom Cylinder
* @returns Cylinder | null
*/
async getCylinderIfUserCanManage(user: User, id: string, relations: string[] = ['keys', 'system']): Promise<Cylinder> {
const cylinder = await this.cylinderRepository.findOne({
where: { id: id, system: { managers: { id: user.id } } },
relations: relations,
});
return cylinder;
}
async getUsersKeys(user: User): Promise<Key[]> {
const keys = await this.keyRepo.find({
where: { cylinder: { system: { managers: { id: user.id } } } },
relations: ['cylinder', 'cylinder.system', 'customer'],
});
return keys;
}
}