From e6d235880bc5de7782393220103e7e1be28d2015 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 26 Sep 2023 10:32:49 +0200 Subject: [PATCH 1/3] added catch for invalid operation exception to controllers where this is throwable. --- .../Requests/InternalRequestsController.cs | 16 +++++++++++ .../Requests/SecondOpinionController.cs | 1 + .../Utilities/UtilitiesController.cs | 27 +++++++++++++------ 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/backend/api/Fusion.Resources.Api/Controllers/Requests/InternalRequestsController.cs b/src/backend/api/Fusion.Resources.Api/Controllers/Requests/InternalRequestsController.cs index 51bd0bc85..8d708a038 100644 --- a/src/backend/api/Fusion.Resources.Api/Controllers/Requests/InternalRequestsController.cs +++ b/src/backend/api/Fusion.Resources.Api/Controllers/Requests/InternalRequestsController.cs @@ -109,6 +109,10 @@ public async Task> CreateProjectAlloc return Created($"/projects/{projectIdentifier}/requests/{newRequest!.RequestId}", new ApiResourceAllocationRequest(newRequest)); } + catch (InvalidOperationException iv) + { + return ApiErrors.InvalidOperation(iv); + } catch (ValidationException ex) { return ApiErrors.InvalidOperation(ex); @@ -193,6 +197,10 @@ public async Task> CreateProjectAlloc // Using the requests for position endpoint as created ref.. This is not completely accurate as it could return more than those created. Best option though. return Created($"/projects/{projectIdentifier}/positions/{request.OrgPositionId}/requests", requests.Select(x => new ApiResourceAllocationRequest(x)).ToList()); } + catch (InvalidOperationException iv) + { + return ApiErrors.InvalidOperation(iv); + } catch (ValidationException ex) { return ApiErrors.InvalidOperation(ex); @@ -287,6 +295,10 @@ public async Task> CreateResourceOwne return Created($"/departments/{departmentPath}/resources/requests/{newRequest!.RequestId}", new ApiResourceAllocationRequest(newRequest)); } + catch (InvalidOperationException iv) + { + return ApiErrors.InvalidOperation(iv); + } catch (ValidationException ex) { return ApiErrors.InvalidOperation(ex); @@ -363,6 +375,10 @@ public async Task> PatchInternalReque return new ApiResourceAllocationRequest(updatedRequest!); } + catch (InvalidOperationException iv) + { + return ApiErrors.InvalidOperation(iv); + } catch (ValidationException ve) { return ApiErrors.InvalidOperation(ve); diff --git a/src/backend/api/Fusion.Resources.Api/Controllers/Requests/SecondOpinionController.cs b/src/backend/api/Fusion.Resources.Api/Controllers/Requests/SecondOpinionController.cs index 91e3ee21b..424ea1787 100644 --- a/src/backend/api/Fusion.Resources.Api/Controllers/Requests/SecondOpinionController.cs +++ b/src/backend/api/Fusion.Resources.Api/Controllers/Requests/SecondOpinionController.cs @@ -15,6 +15,7 @@ namespace Fusion.Resources.Api.Controllers.Requests [ApiController] public class SecondOpinionController : ResourceControllerBase { + // projects/d5c79bb2-5135-47f0-81f2-739ebe46f752/requests/32d3a26c-a13c-473b-889e-beb8d0a97992 [HttpOptions("/resources/requests/internal/{requestId}/second-opinions")] public async Task CheckSecondOpinionAccess(Guid requestId) { diff --git a/src/backend/api/Fusion.Resources.Api/Controllers/Utilities/UtilitiesController.cs b/src/backend/api/Fusion.Resources.Api/Controllers/Utilities/UtilitiesController.cs index 67f57a4d6..9ddbba03d 100644 --- a/src/backend/api/Fusion.Resources.Api/Controllers/Utilities/UtilitiesController.cs +++ b/src/backend/api/Fusion.Resources.Api/Controllers/Utilities/UtilitiesController.cs @@ -22,7 +22,8 @@ public class UtilitiesController : ResourceControllerBase private readonly IFusionTokenProvider tokenProvider; private readonly IOptions fusionOptions; - public UtilitiesController(IHttpClientFactory httpClientFactory, IFusionTokenProvider tokenProvider, IOptions fusionOptions) + public UtilitiesController(IHttpClientFactory httpClientFactory, IFusionTokenProvider tokenProvider, + IOptions fusionOptions) { this.httpClientFactory = httpClientFactory; this.tokenProvider = tokenProvider; @@ -30,7 +31,8 @@ public UtilitiesController(IHttpClientFactory httpClientFactory, IFusionTokenPro } [HttpPost("/utilities/parse-spreadsheet")] - public async Task> ValidateContractorImportSpreadsheet([FromForm] ConvertSpreadsheetRequest request) + public async Task> ValidateContractorImportSpreadsheet( + [FromForm] ConvertSpreadsheetRequest request) { if (request == null) return FusionApiError.InvalidOperation("MissingBody", "Could not locate any body payload"); @@ -43,7 +45,8 @@ public async Task> ValidateContractorImportSpreads if (string.IsNullOrEmpty(url)) - throw new InvalidOperationException("Missing configuration for fusion utility function"); + return ApiErrors.InvalidOperation( + new InvalidOperationException("Missing configuration for fusion utility function")); using var streamContent = new StreamContent(request.File!.OpenReadStream()); @@ -57,14 +60,17 @@ public async Task> ValidateContractorImportSpreads if (response.IsSuccessStatusCode) return JsonConvert.DeserializeObject(content)!; - throw new InvalidOperationException($"Parser function returned non-successfull response ({response.StatusCode})."); + return ApiErrors.InvalidOperation( + new InvalidOperationException( + $"Parser function returned non-successfull response ({response.StatusCode}).")); } [HttpGet("/utilities/templates/import-personnel")] public async Task DownloadImportPersonnelTemplate() { const string fileName = "fusion personnel import.xlsx"; - using var templateFile = Assembly.GetExecutingAssembly().GetManifestResourceStream("Fusion.Resources.Api.Data.personnel-import-template.xlsx"); + using var templateFile = Assembly.GetExecutingAssembly() + .GetManifestResourceStream("Fusion.Resources.Api.Data.personnel-import-template.xlsx"); using var memoryStream = new MemoryStream(); if (templateFile == null) @@ -117,6 +123,7 @@ public class ExcelHeader /// public int ColIndex { get; set; } } + public class ExcelDataRow { /// @@ -146,10 +153,14 @@ public class ExcelParserMessage /// public string Cell { get; set; } = null!; - public enum ExcelParserMessageLevel { Information, Warning, Error } - + public enum ExcelParserMessageLevel + { + Information, + Warning, + Error + } } #endregion } -} +} \ No newline at end of file From 01775e22124a46ea6f98239a1b335052a494c62f Mon Sep 17 00:00:00 2001 From: David Date: Thu, 28 Sep 2023 09:09:37 +0200 Subject: [PATCH 2/3] removed unnecessary comment --- .../Controllers/Requests/SecondOpinionController.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/backend/api/Fusion.Resources.Api/Controllers/Requests/SecondOpinionController.cs b/src/backend/api/Fusion.Resources.Api/Controllers/Requests/SecondOpinionController.cs index 424ea1787..91e3ee21b 100644 --- a/src/backend/api/Fusion.Resources.Api/Controllers/Requests/SecondOpinionController.cs +++ b/src/backend/api/Fusion.Resources.Api/Controllers/Requests/SecondOpinionController.cs @@ -15,7 +15,6 @@ namespace Fusion.Resources.Api.Controllers.Requests [ApiController] public class SecondOpinionController : ResourceControllerBase { - // projects/d5c79bb2-5135-47f0-81f2-739ebe46f752/requests/32d3a26c-a13c-473b-889e-beb8d0a97992 [HttpOptions("/resources/requests/internal/{requestId}/second-opinions")] public async Task CheckSecondOpinionAccess(Guid requestId) { From d435441ae570d4b6d9c457a2115c811c1a65db9d Mon Sep 17 00:00:00 2001 From: David Date: Thu, 28 Sep 2023 14:11:03 +0200 Subject: [PATCH 3/3] revert chages to utillitiesController --- .../Controllers/Utilities/UtilitiesController.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/backend/api/Fusion.Resources.Api/Controllers/Utilities/UtilitiesController.cs b/src/backend/api/Fusion.Resources.Api/Controllers/Utilities/UtilitiesController.cs index 9ddbba03d..f61125f91 100644 --- a/src/backend/api/Fusion.Resources.Api/Controllers/Utilities/UtilitiesController.cs +++ b/src/backend/api/Fusion.Resources.Api/Controllers/Utilities/UtilitiesController.cs @@ -45,8 +45,7 @@ public async Task> ValidateContractorImportSpreads if (string.IsNullOrEmpty(url)) - return ApiErrors.InvalidOperation( - new InvalidOperationException("Missing configuration for fusion utility function")); + throw new InvalidOperationException("Missing configuration for fusion utility function"); using var streamContent = new StreamContent(request.File!.OpenReadStream()); @@ -60,9 +59,8 @@ public async Task> ValidateContractorImportSpreads if (response.IsSuccessStatusCode) return JsonConvert.DeserializeObject(content)!; - return ApiErrors.InvalidOperation( - new InvalidOperationException( - $"Parser function returned non-successfull response ({response.StatusCode}).")); + throw new InvalidOperationException( + $"Parser function returned non-successfull response ({response.StatusCode})."); } [HttpGet("/utilities/templates/import-personnel")]