From f968c6d2329060737ac884e652602dc4cd6eb226 Mon Sep 17 00:00:00 2001
From: Etienne Loupias <eloupias@grandlyon.com>
Date: Thu, 13 Mar 2025 11:37:12 +0100
Subject: [PATCH] find orientation by id

---
 ...on.dto.ts => orientationsDashboard.dto.ts} |  0
 src/orientation/orientation.controller.ts     | 38 +++++++++++++++++--
 src/orientation/orientation.service.ts        |  9 ++++-
 3 files changed, 41 insertions(+), 6 deletions(-)
 rename src/orientation/dto/{findOrientation.dto.ts => orientationsDashboard.dto.ts} (100%)

diff --git a/src/orientation/dto/findOrientation.dto.ts b/src/orientation/dto/orientationsDashboard.dto.ts
similarity index 100%
rename from src/orientation/dto/findOrientation.dto.ts
rename to src/orientation/dto/orientationsDashboard.dto.ts
diff --git a/src/orientation/orientation.controller.ts b/src/orientation/orientation.controller.ts
index 997488c34..1c2f89af4 100644
--- a/src/orientation/orientation.controller.ts
+++ b/src/orientation/orientation.controller.ts
@@ -1,4 +1,16 @@
-import { Body, Controller, Get, Logger, Post, Query, Request, UseGuards } from '@nestjs/common';
+import {
+  Body,
+  Controller,
+  Get,
+  HttpException,
+  HttpStatus,
+  Logger,
+  Param,
+  Post,
+  Query,
+  Request,
+  UseGuards,
+} from '@nestjs/common';
 import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
 import { OrientationDto } from './dto/orientation.dto';
 import { IOrientation } from './interfaces/orientation.interface';
@@ -6,7 +18,7 @@ import { OrientationService } from './orientation.service';
 import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
 import { RolesGuard } from '../users/guards/roles.guard';
 import { Roles } from '../users/decorators/roles.decorator';
-import { FindOrientationDto } from './dto/findOrientation.dto';
+import { FindOrientationDto as OrientationsDashboardDto } from './dto/orientationsDashboard.dto';
 
 @ApiTags('orientation')
 @Controller('orientation')
@@ -26,14 +38,32 @@ export class OrientationController {
     return this.orientationService.findAll();
   }
 
+  @Get(':id')
+  @UseGuards(JwtAuthGuard)
+  @ApiOperation({ summary: 'Get orientation' })
+  @ApiResponse({ status: 200, description: 'Return orientation' })
+  @ApiResponse({ status: 500, description: 'Internal server error.' })
+  public async find(@Param('id') id: string) {
+    this.logger.debug(`find with ${id}`);
+    const orientation = await this.orientationService.findOne(id);
+    if (!orientation) {
+      this.logger.log(`orientation with ${id} does not exist`);
+      throw new HttpException('Orientation does not exist', HttpStatus.NOT_FOUND);
+    }
+    return orientation;
+  }
+
   @Get('dashboard')
   @UseGuards(JwtAuthGuard)
   @ApiOperation({ summary: 'Get user orientations' })
   @ApiResponse({ status: 200, description: 'Return user orientations with populated details.' })
   @ApiResponse({ status: 500, description: 'Internal server error.' })
-  public async getDashboard(@Request() req, @Query('structureId') structureId?: string): Promise<FindOrientationDto> {
+  public async getDashboard(
+    @Request() req,
+    @Query('structureId') structureId?: string
+  ): Promise<OrientationsDashboardDto> {
     this.logger.debug(`find for user=${req.user._id}, structureId=${structureId}`);
-    return this.orientationService.find(req.user._id, structureId);
+    return this.orientationService.getOrientationsDashboard(req.user._id, structureId);
   }
 
   @Post()
diff --git a/src/orientation/orientation.service.ts b/src/orientation/orientation.service.ts
index 91c1cd683..c7957f40b 100644
--- a/src/orientation/orientation.service.ts
+++ b/src/orientation/orientation.service.ts
@@ -6,7 +6,7 @@ import { IOrientation } from './interfaces/orientation.interface';
 import { Orientation, OrientationDocument } from './orientation.schema';
 import { CategoriesService } from '../categories/services/categories.service';
 import { UsersService } from '../users/services/users.service';
-import { FindOrientationDto } from './dto/findOrientation.dto';
+import { FindOrientationDto } from './dto/orientationsDashboard.dto';
 
 @Injectable()
 export class OrientationService {
@@ -38,7 +38,12 @@ export class OrientationService {
     }
   }
 
-  public async find(userId: string, structureId: string = null): Promise<FindOrientationDto> {
+  public async findOne(idParam: string): Promise<IOrientation> {
+    this.logger.debug(`findOne: ${idParam}`);
+    return this.OrientationModel.findById(new Types.ObjectId(idParam)).exec();
+  }
+
+  public async getOrientationsDashboard(userId: string, structureId: string = null): Promise<FindOrientationDto> {
     const user = await this.userService.findById(userId, true);
     if (structureId && user.structuresLink.indexOf(new Types.ObjectId(structureId)) == -1) {
       throw new HttpException('User does not have access to this structure', HttpStatus.FORBIDDEN);
-- 
GitLab