import { Injectable } from "@nestjs/common"; import { Cylinder, Key, KeyHandout, User } from "src/model/entitites"; import { KeySystem } from "src/model/entitites/system.entity"; import { ActivityRepository, CylinderRepository, KeyRepository } from "src/model/repositories"; import { HelperService } from "./system.helper.service"; @Injectable() export class ActivityHelperService { constructor( private readonly helper: HelperService, 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, oldName?: string) { let msg = `Schlüssel ${key.nr} ${oldName ? 'von ' + oldName + ' in ' : ''}in ${key.name} umbenannt`; this.activityRepo.save( this.activityRepo.create({ system, user, message: msg, })) } /** * Logs when a key is reported as lost or misplaced in the system. * * @param user - The user who reported the key as lost * @param key - The key that was reported as lost * @param system - The key system the lost key belongs to * @param lostDate - The date when the key was reported as lost */ async logKeyLostUpdate(user: User, key: Key,lostDate: Date | string) { const sys = await this.helper.getSystemOfKey(key); let msg; if (lostDate == null) { msg = `Schlüssel ${key.nr} als gefunden gemeldet`; } else { msg = `Schlüssel ${key.nr} als verloren gemeldet` } this.activityRepo.save( this.activityRepo.create({ system: sys, 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, })) } async logKeyRestored(user: User, key: Key) { let msg = `Schlüssel ${key.nr} wiederhergestellt`; const system: KeySystem = await this.helper.getSystemOfKey(key); this.activityRepo.save( this.activityRepo.create({ system, user, message: msg, })) } async logCylinderRestored(user: User, cylinder: Cylinder) { let msg = `Zylinder ${cylinder.name} wiederhergestellt`; const system: KeySystem = await this.helper.getSystemOCylinder(cylinder); this.activityRepo.save( this.activityRepo.create({ system, user, message: msg, })) } async logCylinderDeleted(user: User, cylinder: Cylinder) { let msg = `Zylinder ${cylinder.name} gelöscht`; const system: KeySystem = await this.helper.getSystemOCylinder(cylinder); this.activityRepo.save( this.activityRepo.create({ system, user, message: msg, })) } }