Route corrected

This commit is contained in:
AmitChauhan63390
2024-11-15 22:26:36 +05:30
parent 8eedca1b1e
commit 8d4994c8c7
4 changed files with 93 additions and 32 deletions

View File

@@ -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.
*/

View File

@@ -9,6 +9,7 @@ import { apiUrl } from "../apiConfig";
export const getStoredRecordings = async (): Promise<string[] | null> => {
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<boolean> => {
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<boolean> => {
}
};
export const deleteRecordingFromStorage = async (id: string): Promise<boolean> => {
const hasRuns = await checkRunsForRecording(id);
@@ -85,7 +78,6 @@ export const deleteRecordingFromStorage = async (id: string): Promise<boolean> =
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<boolean> =
return false;
}
};
export const deleteRunFromStorage = async (id: string): Promise<boolean> => {

View File

@@ -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<boolean> => {
const associatedRuns = await fetch(`/api/robot/${robotId}/runs`);
const data = await associatedRuns.json();
return data.length > 0;
};
return (
<React.Fragment>
@@ -258,21 +255,31 @@ export const RecordingsTable = ({ handleEditRecording, handleRunRecording, handl
case 'delete':
return (
<TableCell key={column.id} align={column.align}>
<IconButton aria-label="add" size="small" onClick={() => {
<IconButton aria-label="add" size="small" onClick={async () => {
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();
}
})
}}>
<DeleteForever />
</IconButton>

View File

@@ -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}`);