Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions packages/ai-database/src/app/[type]/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import config from '@payload-config'
import { waitUntil } from '@vercel/functions'
import { getPayload } from 'payload'

export const maxDuration = 800

export async function GET(request: Request) {
const start = Date.now()
const { headers } = request
const { origin, searchParams, pathname } = new URL(request.url)

const paths = pathname.split('/')
const id = paths.pop() || ''
const type = paths.pop() || ''

const payload = await getPayload({ config })
const { user } = await payload.auth({ headers })

try {
const thing = await payload.find({
collection: 'things',
where: {
type: { equals: type },
id: { equals: id }
}
})

if (thing.docs.length > 0) {
return Response.json({
...thing.docs[0],
$context: origin,
links: {
home: origin,
admin: origin + '/admin',
}
})
} else {
const job = await payload.jobs.queue({
workflow: 'generateThing',
input: {
id,
type
}
})

return Response.json({
id,
type,
status: 'generating',
jobId: job.id,
message: 'Resource is being generated. Please check back soon.',
$context: origin,
links: {
home: origin,
admin: origin + '/admin',
job: `${origin}/admin/collections/payload-jobs/${job.id}`,
}
}, { status: 202 }) // 202 Accepted indicates the request has been accepted for processing
}
} catch (error) {
return Response.json({
error: 'Failed to process request',
message: error.message,
}, { status: 500 })
}
}
Loading