diff --git a/backend/api/Controllers/InspectionController.cs b/backend/api/Controllers/InspectionController.cs index ce8fa303..ad285ae8 100644 --- a/backend/api/Controllers/InspectionController.cs +++ b/backend/api/Controllers/InspectionController.cs @@ -1,5 +1,4 @@ -using System.Globalization; -using Api.Controllers.Models; +using Api.Controllers.Models; using Api.Database.Models; using Api.Services; @@ -61,48 +60,19 @@ public async Task> Create([FromRoute] string /// [HttpGet] [Authorize(Roles = Role.User)] - [Route("{installationCode}/{taskId}/taskId")] + [Route("{installationCode}/{isarInspectionId}/isarInspectionId")] [ProducesResponseType(typeof(Inspection), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] [ProducesResponseType(StatusCodes.Status409Conflict)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] - public async Task> GetInspectionImageById([FromRoute] string installationCode, string taskId) + public async Task> GetInspectionImageByIsarInspectionId([FromRoute] string installationCode, string isarInspectionId) { - Inspection? inspection; - try - { - inspection = await inspectionService.ReadByIsarTaskId(taskId, readOnly: true); - if (inspection == null) return NotFound($"Could not find inspection for task with Id {taskId}."); - - } - catch (Exception e) - { - logger.LogError(e, $"Error while finding an inspection with task Id {taskId}"); - return StatusCode(StatusCodes.Status500InternalServerError); - } - - if (inspection.IsarInspectionId == null) return NotFound($"Could not find isar inspection Id {inspection.IsarInspectionId} for Inspection with task ID {taskId}."); - - var inspectionData = await inspectionService.GetInspectionStorageInfo(inspection.IsarInspectionId); + byte[]? inspectionStream = await inspectionService.GetInspectionData(installationCode, isarInspectionId); + if (inspectionStream == null) { return NotFound($"Could not find inspection data for inspection with isar Id {isarInspectionId}."); } - if (inspectionData == null) return NotFound($"Could not find inspection data for inspection with isar Id {inspection.IsarInspectionId}."); - - if (!inspectionData.BlobContainer.ToLower(CultureInfo.CurrentCulture).Equals(installationCode.ToLower(CultureInfo.CurrentCulture), StringComparison.Ordinal)) - { - return NotFound($"Could not find inspection data for inspection with isar Id {inspection.IsarInspectionId} because blob name {inspectionData.BlobName} does not match installation {installationCode}."); - } - - try - { - byte[] inspectionStream = await inspectionService.FetchInpectionImage(inspectionData.BlobName, inspectionData.BlobContainer, inspectionData.StorageAccount); - return File(inspectionStream, "image/png"); - } - catch (Azure.RequestFailedException) - { - return NotFound($"Could not find inspection blob {inspectionData.BlobName} in container {inspectionData.BlobContainer} and storage account {inspectionData.StorageAccount}."); - } + return File(inspectionStream, "image/png"); } } } diff --git a/backend/api/Services/InspectionService.cs b/backend/api/Services/InspectionService.cs index c51df67b..3322c616 100644 --- a/backend/api/Services/InspectionService.cs +++ b/backend/api/Services/InspectionService.cs @@ -9,15 +9,20 @@ using Api.Utilities; using Microsoft.EntityFrameworkCore; using Microsoft.Identity.Abstractions; + namespace Api.Services + { public interface IInspectionService { public Task FetchInpectionImage(string inpectionName, string installationCode, string storageAccount); public Task UpdateInspectionStatus(string isarTaskId, IsarTaskStatus isarTaskStatus); public Task ReadByIsarTaskId(string id, bool readOnly = true); + public Task ReadByIsarInspectionId(string id, bool readOnly = true); public Task AddFinding(InspectionFindingQuery inspectionFindingsQuery, string isarTaskId); public Task GetInspectionStorageInfo(string inspectionId); + public Task GetInspectionData(string installationCode, string isarInspectionId); + } @@ -81,6 +86,11 @@ private async Task Update(Inspection inspection) return await GetInspections(readOnly: readOnly).FirstOrDefaultAsync(inspection => inspection.IsarTaskId != null && inspection.IsarTaskId.Equals(id)); } + public async Task ReadByIsarInspectionId(string id, bool readOnly = true) + { + return await GetInspections(readOnly: readOnly).FirstOrDefaultAsync(inspection => inspection.IsarInspectionId != null && inspection.IsarInspectionId.Equals(id)); + } + private IQueryable GetInspections(bool readOnly = true) { if (accessRoleService.IsUserAdmin() || !accessRoleService.IsAuthenticationAvailable()) @@ -142,11 +152,61 @@ private IQueryable GetInspections(bool readOnly = true) return null; } + if (response.StatusCode != HttpStatusCode.OK) + { + logger.LogError("Could not get inspection data for inspection with Id {inspectionId}", inspectionId); + return null; + } + var inspectionData = await response.Content.ReadFromJsonAsync< IDAInspectionDataResponse >() ?? throw new JsonException("Failed to deserialize inspection data from IDA."); return inspectionData; } + + public async Task GetInspectionData(string installationCode, string isarInspectionId) + { + Inspection? inspection; + try + { + inspection = await ReadByIsarInspectionId(isarInspectionId, readOnly: true); + if (inspection == null) + { + logger.LogError($"Could not find inspection with isar inspection Id {isarInspectionId}."); + return null; + } + } + catch (Exception e) + { + logger.LogError(e, $"Error while finding an inspection with isar inspection Id {isarInspectionId}. {e.Message}"); + return null; + } + + var inspectionData = await GetInspectionStorageInfo(inspection.IsarInspectionId); + + if (inspectionData == null) + { + logger.LogError($"Could not find inspection data for inspection with isar Id {inspection.IsarInspectionId}."); + return null; + } + + if (!inspectionData.BlobContainer.ToLower(CultureInfo.CurrentCulture).Equals(installationCode.ToLower(CultureInfo.CurrentCulture), StringComparison.Ordinal)) + { + logger.LogError($"Could not find inspection data for inspection with isar Id {inspection.IsarInspectionId} because blob name {inspectionData.BlobContainer} does not match installation {installationCode}."); + return null; + } + + try + { + byte[] inspectionStream = await FetchInpectionImage(inspectionData.BlobName, inspectionData.BlobContainer, inspectionData.StorageAccount); + return inspectionStream; + } + catch (Azure.RequestFailedException e) + { + logger.LogError($"Could not find inspection blob {inspectionData.BlobName} in container {inspectionData.BlobContainer} and storage account {inspectionData.StorageAccount}. {e.Message}"); + return null; + } + } } } diff --git a/frontend/src/api/ApiCaller.tsx b/frontend/src/api/ApiCaller.tsx index 2d11bfaf..5519ded1 100644 --- a/frontend/src/api/ApiCaller.tsx +++ b/frontend/src/api/ApiCaller.tsx @@ -430,8 +430,8 @@ export class BackendAPICaller { return result.content } - static async getInspection(installationCode: string, taskId: string): Promise { - const path: string = 'inspection/' + installationCode + '/' + taskId + '/taskId' + static async getInspection(installationCode: string, isarInspectionId: string): Promise { + const path: string = 'inspection/' + installationCode + '/' + isarInspectionId + '/isarInspectionId' return BackendAPICaller.GET(path, 'image/png') .then((response) => response.content) diff --git a/frontend/src/components/Contexts/InpectionsContext.tsx b/frontend/src/components/Contexts/InpectionsContext.tsx index e8019fb5..d7fb9596 100644 --- a/frontend/src/components/Contexts/InpectionsContext.tsx +++ b/frontend/src/components/Contexts/InpectionsContext.tsx @@ -51,8 +51,8 @@ export const InspectionsProvider: FC = ({ children }) => { useEffect(() => { Object.values(selectedInspectionTasksToFetch).forEach((task, index) => { - if (task.isarTaskId) { - BackendAPICaller.getInspection(installationCode, task.isarTaskId!) + if (task.inspection.isarInspectionId && task.isarTaskId) { + BackendAPICaller.getInspection(installationCode, task.inspection.isarInspectionId) .then((imageBlob) => { imageObjectURL.current = URL.createObjectURL(imageBlob) }) diff --git a/frontend/src/components/Pages/MissionPage/TaskOverview/TaskTable.tsx b/frontend/src/components/Pages/MissionPage/TaskOverview/TaskTable.tsx index f2106a3c..d46a8c22 100644 --- a/frontend/src/components/Pages/MissionPage/TaskOverview/TaskTable.tsx +++ b/frontend/src/components/Pages/MissionPage/TaskOverview/TaskTable.tsx @@ -35,23 +35,21 @@ export const TaskTable = ({ tasks }: TaskTableProps) => { const { TranslateText } = useLanguageContext() return ( - <> - - - {TranslateText('Tasks')} - - - - # - {TranslateText('Tag-ID')} - {TranslateText('Description')} - {TranslateText('Inspection Types')} - {TranslateText('Status')} - - - {tasks && } - - + + + {TranslateText('Tasks')} + + + + # + {TranslateText('Tag-ID')} + {TranslateText('Description')} + {TranslateText('Inspection Types')} + {TranslateText('Status')} + + + {tasks && } + ) } diff --git a/frontend/src/models/Inspection.ts b/frontend/src/models/Inspection.ts index 92dd2df8..ab3b6c52 100644 --- a/frontend/src/models/Inspection.ts +++ b/frontend/src/models/Inspection.ts @@ -3,6 +3,7 @@ import { Position } from './Position' export interface Inspection { id: string isarTaskId?: string + isarInspectionId?: string status: InspectionStatus isCompleted: boolean inspectionType: InspectionType