Reduce controller logic scope

This commit is contained in:
Mike Conrad
2025-05-20 14:00:44 -04:00
parent 5df94eaafa
commit 3c44fcc062
4 changed files with 114 additions and 108 deletions

50
app/Helpers/Replays.ts Normal file
View File

@ -0,0 +1,50 @@
import Replay from '#models/replay'
import {parseSentryLinkHeader, SentryPagination} from './Sentry.js'
import env from '#start/env'
let recordsUpdated = 0
const SENTRY_TOKEN = env.get('SENTRY_TOKEN')
interface ApiResponse<T> {
data: T;
// optionally, you can define `meta`, `errors`, etc. if your API returns them
}
export async function fetchBatch(url: string) {
const options: RequestInit = {
headers: {
Authorization: `Bearer ${SENTRY_TOKEN}`
}
}
const req = await fetch(url, options)
if (!req.ok) {
throw new Error(`Request failed with status ${req.status}`);
}
const resp = await req.json() as ApiResponse<Replay[]>;
const replays = resp.data;
const headers = req.headers
const cleanedData = replays.map(record => sanitizeInput(record, Replay.allowedFields))
let updated = await Replay.updateOrCreateMany('id', cleanedData)
recordsUpdated = recordsUpdated + updated.length
const linkHeader = headers.get('link')
if (!linkHeader) {
return { error: 'link header missing from Sentry API response' }
}
const pagination: SentryPagination = parseSentryLinkHeader(linkHeader)
if (pagination.hasNextResults == true) {
console.log('fetching', pagination.next)
await fetchBatch(pagination.next)
}
console.log('no more results')
return { recordsUpdated }
}
function sanitizeInput(data: Record<string, any>, allowedFields: string[]) {
return allowedFields.reduce((acc, key) => {
if (key in data) acc[key] = data[key]
return acc
}, {} as Record<string, any>)
}