Files
keyvault/api/src/shared/service/system.helper.service.ts
2026-03-05 14:51:45 +01:00

81 lines
2.4 KiB
TypeScript

import { Inject, Injectable } from "@nestjs/common";
import { User, Cylinder, Key } from "src/model/entitites";
import { KeySystem } from "src/model/entitites/system.entity";
import { KeySystemRepository, CylinderRepository, KeyRepository } from "src/model/repositories";
import { Cache } from "@nestjs/cache-manager";
@Injectable()
export class HelperService {
constructor(
private readonly systemRepository: KeySystemRepository,
private readonly cylinderRepository: CylinderRepository,
private readonly keyRepo: KeyRepository,
// private cacheManager: Cache
) {}
/**
* 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;
}
/**
* Sucht das System eines Schlüssels und gibt es als Promise zurück
* @param key key
* @returns system: KeySystem
*/
async getSystemOfKey(key: Key): Promise<KeySystem> {
const k = await this.keyRepo.findOne({
where: { id: key.id },
relations: ['cylinder', 'cylinder.system', 'cylinder.system.managers'],
withDeleted: true,
});
this.cache()
const found = k.cylinder.find(c => c.system != null);
return found.system;
}
/**
* Gibt das System eines Zylinders und gibt es als Promise zurück
* @param cylinder Zylinder
* @returns Promise<KeySystem>
*/
async getSystemOfCylinder(cylinder: Cylinder): Promise<KeySystem> {
const k = await this.cylinderRepository.findOne({
where: { id: cylinder.id },
relations: ['system'],
withDeleted: true,
});
this.cache()
return k.system;
}
async cache() {
// const value = await this.cacheManager.store.keys()
// console.log(value)
}
async deleteKeyArchiveCache() {
// await this.cacheManager.del('/key/archive');
}
}