refactoring
This commit is contained in:
@@ -8,9 +8,16 @@ export class CustomerService {
|
||||
constructor(private customerRepository: CustomerRepository) {}
|
||||
|
||||
createCustomer(user: IUser, data: any) {
|
||||
if (!user || !data.name || data.name.length == 0 || !data.system) {
|
||||
throw new HttpException('invalid', HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
if (!user) {
|
||||
throw new HttpException({ message: 'Der Benutzer ist nicht verfügbar.', field: 'user' }, HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
if (!data.name || data.name.length === 0) {
|
||||
throw new HttpException({ message: 'Der Name des Kunden ist erforderlich.', field: 'name' }, HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
if (!data.system) {
|
||||
throw new HttpException({ message: 'Die Schließanlage ist nicht gefüllt.', field: 'system' }, HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
|
||||
return this.customerRepository.save(this.customerRepository.create(data));
|
||||
}
|
||||
|
||||
@@ -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];
|
||||
|
||||
@@ -3,10 +3,11 @@ import { KeyController } from './key.controller';
|
||||
import { KeyService } from './key.service';
|
||||
import { DatabaseModule } from 'src/shared/database/database.module';
|
||||
import { AuthModule } from '../auth/auth.module';
|
||||
import { SharedServiceModule } from 'src/shared/service/shared.service.module';
|
||||
|
||||
@Module({
|
||||
controllers: [KeyController],
|
||||
providers: [KeyService],
|
||||
imports: [DatabaseModule, AuthModule],
|
||||
imports: [DatabaseModule, AuthModule, SharedServiceModule],
|
||||
})
|
||||
export class KeyModule {}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ export class SystemService {
|
||||
constructor(
|
||||
private systemRepo: KeySystemRepository,
|
||||
private userRepo: UserRepository,
|
||||
private systemActivityRepo: ActivityRepository,
|
||||
private systemActivityRepo: ActivityRepository,
|
||||
) {}
|
||||
|
||||
async create(user: User, createSystemDto: CreateSystemDto) {
|
||||
|
||||
@@ -3,10 +3,11 @@ import { UserController } from './user.controller';
|
||||
import { UserService } from './user.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: [UserController],
|
||||
providers: [UserService],
|
||||
imports: [AuthModule, DatabaseModule],
|
||||
imports: [AuthModule, DatabaseModule, SharedServiceModule],
|
||||
})
|
||||
export class UserModule {}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { User } from 'src/model/entitites';
|
||||
import { IUser } from 'src/model/interface';
|
||||
import { ActivityRepository, KeyActivityRepository, KeySystemRepository, RoleRepository, UserRepository } from 'src/model/repositories';
|
||||
import { FindOperator, FindOperators } from 'typeorm';
|
||||
|
||||
import { ActivityRepository, KeySystemRepository, RoleRepository, UserRepository } from 'src/model/repositories';
|
||||
import { ManageHelperService } from 'src/shared/service/system.helper.service';
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
constructor(
|
||||
private readonly userRepo: UserRepository,
|
||||
private readonly roleRepo: RoleRepository,
|
||||
private readonly systemRepo: KeySystemRepository,
|
||||
private readonly systemActivityRepo: ActivityRepository
|
||||
private readonly systemActivityRepo: ActivityRepository,
|
||||
private readonly helper: ManageHelperService,
|
||||
) {}
|
||||
|
||||
getAllUsers(): Promise<User[]> {
|
||||
@@ -31,7 +31,7 @@ export class UserService {
|
||||
return this.userRepo.deleteUserById(id);
|
||||
}
|
||||
|
||||
async getUserStats(user: IUser) {
|
||||
async getUserStats(user: User) {
|
||||
const systems = await this.systemRepo.find({
|
||||
where: { managers: { id: user.id }},
|
||||
relations: ['cylinders', 'cylinders.keys'],
|
||||
@@ -42,10 +42,12 @@ export class UserService {
|
||||
const keys = cylinders.map(c => c.keys).flat().map(k => k.id);
|
||||
const keycount = [...new Set(keys)]
|
||||
|
||||
const handedOut = (await this.helper.getUsersKeys(user)).filter(k => k.handedOut).length;
|
||||
return {
|
||||
keys: keycount.length,
|
||||
cylinders: cylinders.length,
|
||||
systems: systems.length
|
||||
systems: systems.length,
|
||||
handedOut: handedOut
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user