refactoring
This commit is contained in:
78
api/src/shared/service/activity.logger.service.ts
Normal file
78
api/src/shared/service/activity.logger.service.ts
Normal 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,
|
||||
}))
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user