From 8d4994c8c723b96bf386667d5020f49b58fcf045 Mon Sep 17 00:00:00 2001 From: AmitChauhan63390 Date: Fri, 15 Nov 2024 22:26:36 +0530 Subject: [PATCH] Route corrected --- server/src/routes/storage.ts | 55 ++++++++++++++++++++ src/api/storage.ts | 26 ++++----- src/components/molecules/RecordingsTable.tsx | 39 ++++++++------ src/components/organisms/ApiKey.tsx | 5 +- 4 files changed, 93 insertions(+), 32 deletions(-) diff --git a/server/src/routes/storage.ts b/server/src/routes/storage.ts index 228f60de..833e6e6b 100644 --- a/server/src/routes/storage.ts +++ b/server/src/routes/storage.ts @@ -16,6 +16,7 @@ import { workflowQueue } from '../worker'; import { AuthenticatedRequest } from './record'; import { computeNextRun } from '../utils/schedule'; import { capture } from "../utils/analytics"; +import { tryCatch } from 'bullmq'; export const router = Router(); @@ -57,6 +58,60 @@ router.get('/recordings/:id', requireSignIn, async (req, res) => { } }) +router.get(('/recordings/:id/runs'), requireSignIn, async (req, res) => { + try { + const runs = await Run.findAll({ + where: { + robotMetaId: req.params.id + }, + raw: true + }); + const formattedRuns = runs.map(formatRunResponse); + const response = { + statusCode: 200, + messageCode: "success", + runs: { + totalCount: formattedRuns.length, + items: formattedRuns, + }, + }; + + res.status(200).json(response); +} catch (error) { + console.error("Error fetching runs:", error); + res.status(500).json({ + statusCode: 500, + messageCode: "error", + message: "Failed to retrieve runs", + }); +} +}) + +function formatRunResponse(run: any) { + const formattedRun = { + id: run.id, + status: run.status, + name: run.name, + robotId: run.robotMetaId, // Renaming robotMetaId to robotId + startedAt: run.startedAt, + finishedAt: run.finishedAt, + runId: run.runId, + runByUserId: run.runByUserId, + runByScheduleId: run.runByScheduleId, + runByAPI: run.runByAPI, + data: {}, + screenshot: null, + }; + + if (run.serializableOutput && run.serializableOutput['item-0']) { + formattedRun.data = run.serializableOutput['item-0']; + } else if (run.binaryOutput && run.binaryOutput['item-0']) { + formattedRun.screenshot = run.binaryOutput['item-0']; + } + + return formattedRun; +} + /** * DELETE endpoint for deleting a recording from the storage. */ diff --git a/src/api/storage.ts b/src/api/storage.ts index 22cd9a28..e48c7091 100644 --- a/src/api/storage.ts +++ b/src/api/storage.ts @@ -9,6 +9,7 @@ import { apiUrl } from "../apiConfig"; + export const getStoredRecordings = async (): Promise => { try { const response = await axios.get(`${apiUrl}/storage/recordings`); @@ -52,24 +53,15 @@ export const getStoredRecording = async (id: string) => { } + export const checkRunsForRecording = async (id: string): Promise => { - const apiKey = localStorage.getItem('x-api-key'); - - // Check if the API key exists - if (!apiKey) { - console.error('API key is missing.'); - return false; - } - + + try { - const response = await axios.get(`${apiUrl}/api/robots/${id}/runs`, { - headers: { - 'x-api-key': apiKey, // Pass the valid API key in the header - }, - withCredentials: true, - }); + const response = await axios.get(`${apiUrl}/storage/recordings/${id}/runs`); const runs = response.data; + console.log(runs.runs.totalCount) return runs.runs.totalCount > 0; } catch (error) { console.error('Error checking runs for recording:', error); @@ -77,6 +69,7 @@ export const checkRunsForRecording = async (id: string): Promise => { } }; + export const deleteRecordingFromStorage = async (id: string): Promise => { const hasRuns = await checkRunsForRecording(id); @@ -85,7 +78,6 @@ export const deleteRecordingFromStorage = async (id: string): Promise = return false; } - try { const response = await axios.delete(`${apiUrl}/storage/recordings/${id}`); if (response.status === 200) { @@ -99,6 +91,10 @@ export const deleteRecordingFromStorage = async (id: string): Promise = return false; } + + + + }; export const deleteRunFromStorage = async (id: string): Promise => { diff --git a/src/components/molecules/RecordingsTable.tsx b/src/components/molecules/RecordingsTable.tsx index 557b0b70..36a66fbe 100644 --- a/src/components/molecules/RecordingsTable.tsx +++ b/src/components/molecules/RecordingsTable.tsx @@ -18,6 +18,8 @@ import { Add } from "@mui/icons-material"; import { useNavigate } from 'react-router-dom'; import { stopRecording } from "../../api/recording"; import { GenericModal } from '../atoms/GenericModal'; +import axios from 'axios'; +import { apiUrl } from '../../apiConfig'; /** TODO: @@ -159,12 +161,7 @@ export const RecordingsTable = ({ handleEditRecording, handleRunRecording, handl } }, []); - const hasAssociatedRuns = async (robotId: string): Promise => { - - const associatedRuns = await fetch(`/api/robot/${robotId}/runs`); - const data = await associatedRuns.json(); - return data.length > 0; - }; + return ( @@ -258,21 +255,31 @@ export const RecordingsTable = ({ handleEditRecording, handleRunRecording, handl case 'delete': return ( - { + { + checkRunsForRecording(row.id).then((result: boolean) => { if (result) { - notify('warning', 'Recording has associated runs, please delete them first'); + notify('warning', 'Cannot delete recording as it has active runs'); } - }) + + + deleteRecordingFromStorage(row.id).then((result: boolean) => { + if (result) { + setRows([]); + notify('success', 'Recording deleted successfully'); + fetchRecordings(); + } + }) + - deleteRecordingFromStorage(row.id).then((result: boolean) => { - if (result) { - setRows([]); - notify('success', 'Recording deleted successfully'); - fetchRecordings(); - } - }) + + + + + + + }}> diff --git a/src/components/organisms/ApiKey.tsx b/src/components/organisms/ApiKey.tsx index 49675538..675edb72 100644 --- a/src/components/organisms/ApiKey.tsx +++ b/src/components/organisms/ApiKey.tsx @@ -38,6 +38,8 @@ const ApiKeyManager = () => { + + useEffect(() => { const fetchApiKey = async () => { try { @@ -51,6 +53,7 @@ const ApiKeyManager = () => { }; fetchApiKey(); + }, []); const generateApiKey = async () => { @@ -58,7 +61,7 @@ const ApiKeyManager = () => { try { const { data } = await axios.post(`${apiUrl}/auth/generate-api-key`); setApiKey(data.api_key); - localStorage.setItem('x-api-key', data.api_key); + notify('success', `Generated API Key successfully`); } catch (error: any) { notify('error', `Failed to generate API Key - ${error.message}`);