Manage SystemManagers
This commit is contained in:
@@ -52,4 +52,9 @@ export class SystemController {
|
||||
remove(@Param('id') id: string) {
|
||||
return this.systemService.remove(id);
|
||||
}
|
||||
|
||||
@Post(':id/manager')
|
||||
manaManager(@Param('id') id: string, @Body() body: any){
|
||||
return this.systemService.manageManagers(id, body);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common';
|
||||
import { Body, Controller, Delete, Get, HttpException, HttpStatus, Param, Post, Req, UseGuards } from '@nestjs/common';
|
||||
import { AuthGuard } from 'src/core/guards/auth.guard';
|
||||
import { UserService } from './user.service';
|
||||
import { User } from 'src/model/entitites';
|
||||
import { IUser } from 'src/model/interface';
|
||||
import { AuthenticatedRequest } from 'src/model/interface/authenticated-request.interface';
|
||||
import { HttpErrorByCode } from '@nestjs/common/utils/http-error-by-code.util';
|
||||
import { HttpStatusCode } from 'axios';
|
||||
|
||||
@UseGuards(AuthGuard)
|
||||
@Controller('user')
|
||||
@@ -18,4 +21,12 @@ export class UserController {
|
||||
saveUser(@Body() user: IUser) {
|
||||
return this.userService.saveUser(user);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
deleteUserWithId(@Req() req: AuthenticatedRequest, @Param('id') id: string) {
|
||||
if (req.user.role.name != "admin") {
|
||||
throw new HttpException('no admin', HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
return this.userService.deleteUserById(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,4 +23,9 @@ export class UserService {
|
||||
}
|
||||
return this.userRepo.save(user as any);
|
||||
}
|
||||
|
||||
async deleteUserById(id: string) {
|
||||
return this.userRepo.deleteUserById(id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user