From a70aa2ba3180cbd5873465abc1b3d8664a77ef31 Mon Sep 17 00:00:00 2001 From: Linda Malm <109201562+malmen237@users.noreply.github.com> Date: Tue, 12 Nov 2024 09:33:45 +0100 Subject: [PATCH] fix: handle missing srt-source in api on delete (#89) * fix: handle missing srt-source in api on delete * fix: updated delete-fetch to return before fetch if source-id is not in api * fix: removed commented out code --- .../[ingest_source_id]}/route.ts | 20 ++-------- src/hooks/sources/useDeleteSource.tsx | 37 +++++++++++++------ 2 files changed, 30 insertions(+), 27 deletions(-) rename src/app/api/manager/srt/{[ingest_name]/[ingest_source_name] => [ingest_uuid]/[ingest_source_id]}/route.ts (61%) diff --git a/src/app/api/manager/srt/[ingest_name]/[ingest_source_name]/route.ts b/src/app/api/manager/srt/[ingest_uuid]/[ingest_source_id]/route.ts similarity index 61% rename from src/app/api/manager/srt/[ingest_name]/[ingest_source_name]/route.ts rename to src/app/api/manager/srt/[ingest_uuid]/[ingest_source_id]/route.ts index 13583a75..20bd44a5 100644 --- a/src/app/api/manager/srt/[ingest_name]/[ingest_source_name]/route.ts +++ b/src/app/api/manager/srt/[ingest_uuid]/[ingest_source_id]/route.ts @@ -2,14 +2,10 @@ import { NextRequest, NextResponse } from 'next/server'; import { isAuthenticated } from '../../../../../../api/manager/auth'; import { deleteSrtSource } from '../../../../../../api/ateliereLive/ingest'; import { Log } from '../../../../../../api/logger'; -import { - getUuidFromIngestName, - getSourceIdFromSourceName -} from '../../../../../../api/ateliereLive/ingest'; type Params = { - ingest_name: string; - ingest_source_name: string; + ingest_uuid: string; + ingest_source_id: number; }; export async function DELETE( @@ -22,15 +18,7 @@ export async function DELETE( }); } - const ingestUuid = await getUuidFromIngestName(params.ingest_name, false); - const sourceId = ingestUuid - ? await getSourceIdFromSourceName( - ingestUuid, - params.ingest_source_name, - false - ) - : 0; - return await deleteSrtSource(ingestUuid || '', sourceId || 0) + return await deleteSrtSource(params.ingest_uuid, params.ingest_source_id) .then((response) => { return new NextResponse(JSON.stringify(response)); }) @@ -38,7 +26,7 @@ export async function DELETE( Log().error(error); const errorResponse = { ok: false, - error: 'unexpected' + error: 'Failed to delete SRT source' }; return new NextResponse(JSON.stringify(errorResponse), { status: 500 }); }); diff --git a/src/hooks/sources/useDeleteSource.tsx b/src/hooks/sources/useDeleteSource.tsx index 760d10c4..804e2395 100644 --- a/src/hooks/sources/useDeleteSource.tsx +++ b/src/hooks/sources/useDeleteSource.tsx @@ -1,28 +1,43 @@ import { useState } from 'react'; import { CallbackHook } from '../types'; import { API_SECRET_KEY } from '../../utils/constants'; +import { useIngests, useIngestSources } from '../ingests'; export function useDeleteSrtSource(): CallbackHook< (ingest_name: string, ingest_source_name: string) => void > { const [deleteSrtLoading, setDeleteSrtLoading] = useState(false); + const ingests = useIngests(); + const [getSources] = useIngestSources(); const deleteSrtSource = async ( ingest_name: string, ingest_source_name: string ) => { - setDeleteSrtLoading(true); - return fetch(`/api/manager/srt/${ingest_name}/${ingest_source_name}/`, { - method: 'DELETE', - headers: [['x-api-key', `Bearer ${API_SECRET_KEY}`]] - }) - .then(async (response) => { - if (response.ok) { - return response.json(); - } - throw await response.text; + const ingestUuid = + ingests.find((ingest) => ingest.name === ingest_name)?.uuid || ''; + + const sources = await getSources(ingest_name); + const sourceId = sources.find( + (source) => source.name === ingest_source_name + )?.source_id; + + if (ingestUuid !== '' && sourceId !== undefined) { + return fetch(`/api/manager/srt/${ingestUuid}/${sourceId}/`, { + method: 'DELETE', + headers: [['x-api-key', `Bearer ${API_SECRET_KEY}`]] }) - .finally(() => setDeleteSrtLoading(false)); + .then(async (response) => { + if (response.ok) { + return response.json(); + } + throw response.text; + }) + .finally(() => setDeleteSrtLoading(false)); + } else { + setDeleteSrtLoading(false); + return; + } }; return [deleteSrtSource, deleteSrtLoading]; }