fp/packages/fetchers/src/updateSegmentInDatabase.ts

52 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-08-31 10:42:28 +00:00
import type { SegmentResponse } from '@futureporn/types'
2024-09-16 16:31:51 +00:00
import { configs } from './config.ts'
2024-08-27 07:11:24 +00:00
2024-08-31 10:42:28 +00:00
/**
* 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
*/
2024-08-27 07:11:24 +00:00
export default async function updateSegmentInDatabase({
segment_id,
2024-10-02 17:38:24 +00:00
bytes,
bytes_uploaded,
2024-08-27 07:11:24 +00:00
}: {
2024-08-31 10:42:28 +00:00
segment_id: string,
2024-10-02 17:38:24 +00:00
bytes: number,
bytes_uploaded: number,
2024-08-31 10:42:28 +00:00
}): Promise<SegmentResponse> {
2024-08-27 07:11:24 +00:00
const payload: any = {
2024-10-02 17:38:24 +00:00
bytes,
bytes_uploaded
2024-08-27 07:11:24 +00:00
}
2024-09-21 03:01:21 +00:00
const fetchUrl =`${configs.postgrestUrl}/segments?id=eq.${segment_id}&select=vod:vods(recording:recordings(is_aborted))`
2024-09-26 02:01:25 +00:00
// console.info(`updateSegmentInDatabase > fetchUrl=${fetchUrl}`)
2024-08-31 10:42:28 +00:00
const res = await fetch(fetchUrl, {
2024-08-27 07:11:24 +00:00
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()
2024-08-31 10:42:28 +00:00
const msg = `failed to updateSegmentInDatabase. status=${res.status}, statusText=${res.statusText}, body=${body}`
2024-09-06 05:39:08 +00:00
console.error(msg)
2024-08-27 07:11:24 +00:00
throw new Error(msg);
}
2024-09-06 05:39:08 +00:00
// console.info(`response was OK~`)
2024-08-31 10:42:28 +00:00
const body = await res.json() as SegmentResponse[];
2024-08-27 07:11:24 +00:00
if (!body[0]) throw new Error(`failed to get a segment that matched segment_id=${segment_id}`);
const bod = body[0]
2024-09-06 05:39:08 +00:00
// console.info('the following was the response from PATCH-ing /segments')
// console.info(JSON.stringify(bod))
2024-08-27 07:11:24 +00:00
return bod
}