Skip to content

Commit

Permalink
fix: handle missing srt-source in api on delete (#89)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
malmen237 authored Nov 12, 2024
1 parent 2e95afc commit a70aa2b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -22,23 +18,15 @@ 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));
})
.catch((error) => {
Log().error(error);
const errorResponse = {
ok: false,
error: 'unexpected'
error: 'Failed to delete SRT source'
};
return new NextResponse(JSON.stringify(errorResponse), { status: 500 });
});
Expand Down
37 changes: 26 additions & 11 deletions src/hooks/sources/useDeleteSource.tsx
Original file line number Diff line number Diff line change
@@ -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<boolean>(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];
}

0 comments on commit a70aa2b

Please sign in to comment.