From c13a0de5adde6b2b2586f94f451b5e202763ec75 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Thu, 26 Sep 2024 22:39:39 +0530 Subject: [PATCH] feat: get all recordings --- server/src/api/index.ts | 62 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 5 deletions(-) diff --git a/server/src/api/index.ts b/server/src/api/index.ts index e1e4e5a4..bc023417 100644 --- a/server/src/api/index.ts +++ b/server/src/api/index.ts @@ -1,5 +1,57 @@ -const genAPIKey = () => { - return [...Array(30)] - .map((e) => ((Math.random() * 36) | 0).toString(36)) - .join(''); -}; \ No newline at end of file +import { deleteFile, readFile, readFiles, saveFile } from "../workflow-management/storage"; +import { Router, Request, Response } from 'express'; +export const router = Router(); + +const formatRecording = (recordingData: any) => { + const recordingMeta = recordingData.recording_meta; + const workflow = recordingData.recording.workflow || []; + const firstWorkflowStep = workflow[0]?.where?.url || ''; + + const inputParameters = [ + { + type: "string", + name: "originUrl", + label: "Origin URL", + required: true, + defaultValue: firstWorkflowStep, + }, + ]; + + return { + id: recordingMeta.id, + name: recordingMeta.name, + createdAt: new Date(recordingMeta.create_date).getTime(), + inputParameters, + }; + }; + + + router.get("/api/recordings", async (req: Request, res: Response) => { + try { + const fileContents = await readFiles('./../storage/recordings/'); + + const formattedRecordings = fileContents.map((fileContent: string) => { + const recordingData = JSON.parse(fileContent); + return formatRecording(recordingData); + }); + + const response = { + statusCode: 200, + messageCode: "success", + robots: { + totalCount: formattedRecordings.length, + items: formattedRecordings, + }, + }; + + res.status(200).json(response); + } catch (error) { + console.error("Error fetching recordings:", error); + res.status(500).json({ + statusCode: 500, + messageCode: "error", + message: "Failed to retrieve recordings", + }); + } + }); + \ No newline at end of file