diff --git a/src/admin/admin.controller.ts b/src/admin/admin.controller.ts index 858a0dcdb25829e013a48935db7ac788246edd2a..71cbdbf1fb4ad94fbe421d970949402b7d645140 100644 --- a/src/admin/admin.controller.ts +++ b/src/admin/admin.controller.ts @@ -1,6 +1,6 @@ -import { Body } from '@nestjs/common'; +import { Body, Delete, Param } from '@nestjs/common'; import { Controller, Get, Post, UseGuards } from '@nestjs/common'; -import { ApiOperation } from '@nestjs/swagger'; +import { ApiOperation, ApiParam } from '@nestjs/swagger'; import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; import { StructuresService } from '../structures/services/structures.service'; import { Roles } from '../users/decorators/roles.decorator'; @@ -67,4 +67,29 @@ export class AdminController { }) ); } + + @UseGuards(JwtAuthGuard, RolesGuard) + @Roles('admin') + @Delete('user/:id') + @ApiParam({ name: 'id', type: String, required: true }) + public async deleteUser(@Param() params) { + const user = await this.usersService.deleteOneId(params.id); + user.structuresLink.forEach((structureId) => { + this.usersService.isStructureClaimed(structureId.toString()).then((userFound) => { + if (!userFound) { + this.structuresService.deleteOne(structureId.toString()); + } + }); + }); + return user; + } + + @UseGuards(JwtAuthGuard, RolesGuard) + @Roles('admin') + @Post('searchUsers') + public async searchUsers(@Body() searchString: { searchString: string }) { + if (searchString && searchString.searchString.length > 0) + return this.usersService.searchUsers(searchString.searchString); + else return this.usersService.findAll(); + } } diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index 4bde1ea1bf7a9f8d264dc36dc3f9360145c78e12..b0a31ce0aec2271beacdadc65527ecb4267d3674 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -9,6 +9,8 @@ import { PasswordResetDto } from './dto/reset-password.dto'; import { UsersService } from './users.service'; import { StructuresService } from '../structures/services/structures.service'; import { TempUserService } from '../temp-user/temp-user.service'; +import { RolesGuard } from './guards/roles.guard'; +import { Roles } from './decorators/roles.decorator'; @Controller('users') export class UsersController { diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 7b6915537199610c9c63976c784f6169bec3b571..3086c0c10fb5d0e8cb86e9e4fc7d5ece08973784 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -486,6 +486,14 @@ export class UsersService { return user.deleteOne(); } + public async deleteOneId(id: string): Promise<User> { + const user = await this.userModel.findOne({ _id: id }).exec(); + if (!user) { + throw new HttpException('Invalid user id', HttpStatus.BAD_REQUEST); + } + return user.deleteOne(); + } + public async getStructureOwnersMails(structureId: string, emailUser: string): Promise<OwnerDto[]> { const users = await this.userModel .find({ structuresLink: Types.ObjectId(structureId), email: { $ne: emailUser } }) @@ -499,4 +507,10 @@ export class UsersService { }); return owners; } + + public async searchUsers(searchString: string) { + return this.userModel + .find( {email: new RegExp(searchString, 'i') }) + .exec(); + } }