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

@@ -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,
}))
}
}