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

@@ -4,11 +4,11 @@ import { IUser } from 'src/model/interface';
import {
ActivityRepository,
CylinderRepository,
KeyActivityRepository,
KeyRepository,
KeySystemRepository,
} from 'src/model/repositories';
import { KeyHandoutRepository } from 'src/model/repositories/key-handout.repository';
import { ActivityHelperService } from 'src/shared/service/activity.logger.service';
import { IsNull, Not } from 'typeorm';
@Injectable()
@@ -17,9 +17,8 @@ export class KeyService {
private readonly keyrepository: KeyRepository,
private readonly cylinderRepository: CylinderRepository,
private readonly systemRepo: KeySystemRepository,
private activityRepo: KeyActivityRepository,
private handoverRepo: KeyHandoutRepository,
private systemActivityRepo: ActivityRepository,
private readonly activityService: ActivityHelperService
) {}
async getUsersKeys(user: User): Promise<Key[]> {
@@ -41,25 +40,9 @@ export class KeyService {
where: { cylinder: { system: { managers: { id: user.id } } } },
});
const original = await this.keyrepository.findOne({
where: { id: key.id },
relations: ['cylinder'],
});
this.saveLog(original, user);
return this.keyrepository.save(this.keyrepository.create(key));
}
private saveLog(key: Key, user: User) {
const l = this.activityRepo.create({
cylinder: key.cylinder,
handedOut: key.handedOut,
name: key.name,
id: key.id,
nr: key.nr,
user: user,
});
this.activityRepo.save(l);
}
async getUsersCylinders(user: User): Promise<Cylinder[]> {
if (!user || !user.id) {
@@ -73,7 +56,7 @@ export class KeyService {
});
}
async handoverKey(user: IUser, data: any, keyID: string) {
async handoverKey(user: User, data: any, keyID: string) {
const key: Key = await this.keyrepository.findOneOrFail({
where: { id: keyID, cylinder: { system: { managers: { id: user.id } } } },
relations: [ 'cylinder', 'cylinder.system' ]
@@ -92,13 +75,7 @@ export class KeyService {
}),
);
const msg = `Schlüssel ${key.nr} ${res.direction == 'out' ? 'ausgegeben an ' : 'zurückgegeben von '}${res.customer.name}`
this.systemActivityRepo.save(
this.systemActivityRepo.create({
system: key.cylinder[0].system,
user: user as any,
message: msg,
}))
this.activityService.logKeyHandover(user, key, key.cylinder[0].system, res);
return res;
}
@@ -136,11 +113,7 @@ export class KeyService {
async createKey(user: User, key: any) {
const k = await this.keyrepository.save(this.keyrepository.create(key));
this.systemActivityRepo.save({
message: `Schlüssel ${(k as any).nr} angelegt`,
user: user,
system: (k as any).cylinder[0].system
});
this.activityService.logKeyCreated(user, key, key.cylinder[0].system);
return k;
}