Reduce controller logic scope
This commit is contained in:
50
app/Helpers/Replays.ts
Normal file
50
app/Helpers/Replays.ts
Normal 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>)
|
||||
}
|
28
app/Helpers/Sentry.ts
Normal file
28
app/Helpers/Sentry.ts
Normal file
@ -0,0 +1,28 @@
|
||||
|
||||
export interface SentryPagination {
|
||||
previous: string;
|
||||
hasPreviousResults: boolean;
|
||||
hasNextResults: boolean;
|
||||
next: string
|
||||
}
|
||||
export function parseSentryLinkHeader(header: string): SentryPagination {
|
||||
const links = header.split(',').map(part => part.trim())
|
||||
|
||||
let result = {} as SentryPagination
|
||||
for (const link of links) {
|
||||
const match = link.match(/<([^>]+)>;\s*rel="([^"]+)";\s*results="([^"]+)";\s*cursor="([^"]+)"/)
|
||||
if (!match) continue
|
||||
|
||||
const [, url, rel, results] = match
|
||||
|
||||
if (rel === 'previous') {
|
||||
result.previous = url
|
||||
result.hasPreviousResults = results === 'true'
|
||||
} else if (rel === 'next') {
|
||||
result.next = url
|
||||
result.hasNextResults = results === 'true'
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
19
app/Helpers/Webhook.ts
Normal file
19
app/Helpers/Webhook.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import env from '#start/env'
|
||||
|
||||
export async function sendDataToWebhook(responseData:{ version: number, updatedAt: Date, numberOfRecords: number, data: unknown}) {
|
||||
try {
|
||||
console.log('syncing to webhook')
|
||||
await fetch(env.get('WEBHOOK_URL'),
|
||||
{
|
||||
headers:
|
||||
{
|
||||
'content-type': 'application/json'
|
||||
},
|
||||
method: 'POST',
|
||||
body: JSON.stringify(responseData)
|
||||
}
|
||||
)
|
||||
} catch (e) {
|
||||
console.error('error sending webhook data', e)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user