From 1fb80471382d883db23909b4ae58beab05d1cfca Mon Sep 17 00:00:00 2001
From: Hugo SUBTIL <ext.sopra.husubtil@grandlyon.com>
Date: Thu, 18 Feb 2021 16:55:25 +0100
Subject: [PATCH] fix: MR return + issue on delete owner endpoint

---
 src/structures/services/structures.service.ts |  2 +-
 src/structures/structures.controller.ts       | 11 ++++++-----
 src/temp-user/temp-user.controller.ts         |  3 ++-
 3 files changed, 9 insertions(+), 7 deletions(-)

diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts
index 5d584b075..d25aa1e27 100644
--- a/src/structures/services/structures.service.ts
+++ b/src/structures/services/structures.service.ts
@@ -311,7 +311,7 @@ export class StructuresService {
    * Send an email to structure owner's in order to accept or decline a join request
    * @param user User
    */
-  public async sendStructureJoinRequest(user: IUser, structure: StructureDocument): Promise<any> {
+  public async sendStructureJoinRequest(user: IUser, structure: StructureDocument): Promise<void> {
     const config = this.mailerService.config;
     const ejsPath = this.mailerService.getTemplateLocation(config.templates.structureJoinRequest.ejs);
     const jsonConfig = this.mailerService.loadJsonConfig(config.templates.structureJoinRequest.json);
diff --git a/src/structures/structures.controller.ts b/src/structures/structures.controller.ts
index 4a58d7162..2e84676d7 100644
--- a/src/structures/structures.controller.ts
+++ b/src/structures/structures.controller.ts
@@ -141,7 +141,7 @@ export class StructuresController {
 
   @Post(':id/join')
   @ApiParam({ name: 'id', type: String, required: true })
-  public async join(@Param('id') id: string, @Body() user: User): Promise<any> {
+  public async join(@Param('id') id: string, @Body() user: User): Promise<void> {
     // Get structure name
     const structure = await this.structureService.findOne(id);
     if (!structure) {
@@ -197,19 +197,20 @@ export class StructuresController {
     }
   }
 
-  @Delete(':id/removeOwner')
+  @Delete(':id/owner/:userId')
   @UseGuards(JwtAuthGuard, IsStructureOwnerGuard)
   @Roles('admin')
   @ApiParam({ name: 'id', type: String, required: true })
-  public async removeOwner(@Param('id') id: string, @Body() user: User): Promise<any> {
+  @ApiParam({ name: 'userId', type: String, required: true })
+  public async removeOwner(@Param('id') id: string, @Param('userId') userId: string): Promise<void> {
     // Get structure
     const structure = await this.structureService.findOne(id);
     if (!structure) {
       throw new HttpException('Invalid Structure', HttpStatus.NOT_FOUND);
     }
     // Get user
-    const userFromDb = await this.userService.findOne(user.email);
-    if (!userFromDb) {
+    const userFromDb = await this.userService.findById(userId);
+    if (!userFromDb || !userFromDb.structuresLink.includes(Types.ObjectId(id))) {
       throw new HttpException('Invalid User', HttpStatus.NOT_FOUND);
     }
     this.userService.removeFromStructureLinked(userFromDb.email, id);
diff --git a/src/temp-user/temp-user.controller.ts b/src/temp-user/temp-user.controller.ts
index 7ff27ce27..9dc21a559 100644
--- a/src/temp-user/temp-user.controller.ts
+++ b/src/temp-user/temp-user.controller.ts
@@ -1,5 +1,6 @@
 import { Controller, Get, HttpException, HttpStatus, Param } from '@nestjs/common';
 import { ApiParam } from '@nestjs/swagger';
+import { TempUser } from './temp-user.schema';
 import { TempUserService } from './temp-user.service';
 
 @Controller('temp-user')
@@ -8,7 +9,7 @@ export class TempUserController {
 
   @Get(':id')
   @ApiParam({ name: 'id', type: String, required: true })
-  public async getTempUser(@Param('id') id: string) {
+  public async getTempUser(@Param('id') id: string): Promise<TempUser> {
     const user = await this.tempUserSercice.findById(id);
     if (!user) {
       throw new HttpException('User does not exists', HttpStatus.BAD_REQUEST);
-- 
GitLab