refactoring
This commit is contained in:
@@ -3,10 +3,11 @@ import { CylinderController } from './cylinder.controller';
|
||||
import { CylinderService } from './cylinder.service';
|
||||
import { AuthModule } from '../auth/auth.module';
|
||||
import { DatabaseModule } from 'src/shared/database/database.module';
|
||||
import { SharedServiceModule } from 'src/shared/service/shared.service.module';
|
||||
|
||||
@Module({
|
||||
controllers: [CylinderController],
|
||||
providers: [CylinderService],
|
||||
imports: [AuthModule, DatabaseModule],
|
||||
imports: [AuthModule, DatabaseModule, SharedServiceModule],
|
||||
})
|
||||
export class CylinderModule {}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { Cylinder, User } from 'src/model/entitites';
|
||||
import { ActivityRepository, CylinderRepository, KeyRepository } from 'src/model/repositories';
|
||||
import { ManageHelperService } from 'src/shared/service/system.helper.service';
|
||||
|
||||
@Injectable()
|
||||
export class CylinderService {
|
||||
@@ -8,6 +9,7 @@ export class CylinderService {
|
||||
private readonly cylinderRepo: CylinderRepository,
|
||||
private readonly keyRepo: KeyRepository,
|
||||
private systemActivityRepo: ActivityRepository,
|
||||
private readonly helper: ManageHelperService
|
||||
) {}
|
||||
|
||||
async getCylinders(user: User): Promise<Cylinder[]> {
|
||||
@@ -16,24 +18,26 @@ export class CylinderService {
|
||||
order: { name: { direction: 'ASC' } },
|
||||
relations: ['system', 'keys'],
|
||||
});
|
||||
c.map((cc) => (cc.keys = []));
|
||||
return c;
|
||||
}
|
||||
|
||||
async deleteCylinder(user: User, cylinderid: string) {
|
||||
const cylinder = await this.cylinderRepo.findOneOrFail({
|
||||
where: { id: cylinderid, system: { managers: { id: user.id } } },
|
||||
relations: ['keys'],
|
||||
});
|
||||
await this.keyRepo.softRemove(cylinder.keys);
|
||||
return this.cylinderRepo.softRemove(cylinder);
|
||||
const cylinder = await this.helper.getCylinderIfUserCanManage(user, cylinderid, ['keys', 'system', 'keys.cylinder']);
|
||||
if (!cylinder) {
|
||||
throw new HttpException('Zylinder nicht gefunden', HttpStatus.BAD_REQUEST)
|
||||
}
|
||||
|
||||
const keysToDelete = cylinder.keys.filter(k => k.cylinder.length == 1);
|
||||
await this.keyRepo.softRemove(keysToDelete);
|
||||
await this.cylinderRepo.softDelete({id: cylinder.id})
|
||||
return;
|
||||
}
|
||||
|
||||
async updateCylinder(user: User, cylinder: Partial<Cylinder>) {
|
||||
const original = await this.cylinderRepo.findOneOrFail({
|
||||
where: { id: cylinder.id, system: { managers: { id: user.id } } },
|
||||
relations: ['keys', 'system'],
|
||||
});
|
||||
const original = await this.helper.getCylinderIfUserCanManage(user, cylinder.id);
|
||||
if (!original) {
|
||||
throw new HttpException('Zylinder nicht gefunden', HttpStatus.BAD_REQUEST)
|
||||
}
|
||||
|
||||
Object.keys(cylinder).forEach((k: string) => {
|
||||
original[k] = cylinder[k];
|
||||
|
||||
Reference in New Issue
Block a user