fp/packages/fetchers/src/updateSegmentInDatabase.ts

52 lines
1.6 KiB
TypeScript

import type { SegmentResponse } from '@futureporn/types'
import { configs } from './config.ts'
/**
* updateSegmentInDatabase
*
* updates the segment in the database with the new filesize
*
* resolves with the updated segment and the is_recording_aborted column of the related vod
*/
export default async function updateSegmentInDatabase({
segment_id,
bytes,
bytes_uploaded,
}: {
segment_id: string,
bytes: number,
bytes_uploaded: number,
}): Promise<SegmentResponse> {
const payload: any = {
bytes,
bytes_uploaded
}
const fetchUrl =`${configs.postgrestUrl}/segments?id=eq.${segment_id}&select=vod:vods(recording:recordings(is_aborted))`
// console.info(`updateSegmentInDatabase > fetchUrl=${fetchUrl}`)
const res = await fetch(fetchUrl, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Prefer': 'return=representation',
'Authorization': `Bearer ${configs.automationUserJwt}`
},
body: JSON.stringify(payload)
})
if (!res.ok) {
const body = await res.text()
const msg = `failed to updateSegmentInDatabase. status=${res.status}, statusText=${res.statusText}, body=${body}`
console.error(msg)
throw new Error(msg);
}
// console.info(`response was OK~`)
const body = await res.json() as SegmentResponse[];
if (!body[0]) throw new Error(`failed to get a segment that matched segment_id=${segment_id}`);
const bod = body[0]
// console.info('the following was the response from PATCH-ing /segments')
// console.info(JSON.stringify(bod))
return bod
}