Manage SystemManagers
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { CreateSystemDto } from './dto/create-system.dto';
|
||||
import { UpdateSystemDto } from './dto/update-system.dto';
|
||||
import { KeySystemRepository } from 'src/model/repositories';
|
||||
import { KeySystemRepository, UserRepository } from 'src/model/repositories';
|
||||
import { User } from 'src/model/entitites';
|
||||
|
||||
@Injectable()
|
||||
export class SystemService {
|
||||
constructor(private systemRepo: KeySystemRepository) {}
|
||||
constructor(private systemRepo: KeySystemRepository, private userRepo: UserRepository) {}
|
||||
|
||||
async create(user: User, createSystemDto: CreateSystemDto) {
|
||||
const sys = this.systemRepo.create(createSystemDto);
|
||||
@@ -52,4 +52,36 @@ export class SystemService {
|
||||
|
||||
return system.managers;
|
||||
}
|
||||
|
||||
async manageManagers(systemID: string, manageObject: { email: string, action: 'add' | 'remove'}) {
|
||||
const sys = await this.systemRepo.findOne({
|
||||
where: { id: systemID },
|
||||
relations: ['managers']
|
||||
});
|
||||
|
||||
if (!sys) {
|
||||
throw new HttpException('Das System wurde nicht im System gefunden', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
if (manageObject.action == 'remove') {
|
||||
sys.managers = sys.managers.filter( m => m.username != manageObject.email);
|
||||
|
||||
await this.systemRepo.save(sys);
|
||||
return sys.managers;
|
||||
}
|
||||
|
||||
if (sys.managers.some(m => m.username == manageObject.email)) {
|
||||
return sys.managers;
|
||||
}
|
||||
|
||||
const user = await this.userRepo.findOneBy({ username: manageObject.email.trim() });
|
||||
if (!user) {
|
||||
throw new HttpException('Es wurde kein User mit dieser Emailadresse gefunden. Bitte prüfe die Emailadresse und versuche es erneut.', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
sys.managers.push(user);
|
||||
await this.systemRepo.save(sys);
|
||||
return sys.managers;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user