|
| 1 | +import { extractActionModuleInfo } from "@/lib/actionExtraction" |
| 2 | +import { getPayloadClient } from "@/lib/payloadClient" |
| 3 | +import type { Action } from "@/payload-types" |
| 4 | +import { NextResponse } from "next/server" |
| 5 | + |
| 6 | +function getBearerToken(request: Request) { |
| 7 | + const authorization = request.headers.get("authorization")?.trim() |
| 8 | + if (!authorization?.toLowerCase().startsWith("bearer ")) return null |
| 9 | + |
| 10 | + return authorization.slice("bearer ".length).trim() |
| 11 | +} |
| 12 | + |
| 13 | +function createJsonUploadFile(identifier: string, module: unknown) { |
| 14 | + const json = JSON.stringify(module, null, 2) |
| 15 | + return { |
| 16 | + data: Buffer.from(json), |
| 17 | + mimetype: "application/json", |
| 18 | + name: `${identifier}.json`, |
| 19 | + size: Buffer.byteLength(json), |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +export async function POST(request: Request) { |
| 24 | + const expectedToken = process.env.ACTIONS_IMPORT_SECRET?.trim() |
| 25 | + const receivedToken = getBearerToken(request) |
| 26 | + |
| 27 | + if (!expectedToken || receivedToken !== expectedToken) { |
| 28 | + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }) |
| 29 | + } |
| 30 | + |
| 31 | + const module = await request.json().catch(() => null) |
| 32 | + if (!module) { |
| 33 | + return NextResponse.json({ error: "Missing module JSON." }, { status: 400 }) |
| 34 | + } |
| 35 | + |
| 36 | + const moduleInfo = extractActionModuleInfo(module) |
| 37 | + if (!moduleInfo?.identifier) { |
| 38 | + return NextResponse.json({ error: "Module identifier is required." }, { status: 400 }) |
| 39 | + } |
| 40 | + |
| 41 | + if (!moduleInfo.title) { |
| 42 | + return NextResponse.json({ error: "Module name is required." }, { status: 400 }) |
| 43 | + } |
| 44 | + |
| 45 | + const payload = await getPayloadClient() |
| 46 | + const existingActions = await payload.find({ |
| 47 | + collection: "actions", |
| 48 | + depth: 0, |
| 49 | + limit: 1, |
| 50 | + overrideAccess: true, |
| 51 | + pagination: false, |
| 52 | + where: { |
| 53 | + identifier: { |
| 54 | + equals: moduleInfo.identifier, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }) |
| 58 | + const existingAction = existingActions.docs[0] as Action | undefined |
| 59 | + |
| 60 | + const media = await payload.create({ |
| 61 | + collection: "media", |
| 62 | + data: { |
| 63 | + alt: `${moduleInfo.title} module`, |
| 64 | + }, |
| 65 | + file: createJsonUploadFile(moduleInfo.identifier, module), |
| 66 | + overrideAccess: true, |
| 67 | + }) |
| 68 | + |
| 69 | + const actionData = { |
| 70 | + identifier: moduleInfo.identifier, |
| 71 | + module: media.id, |
| 72 | + } |
| 73 | + const action = existingAction |
| 74 | + ? await payload.update({ |
| 75 | + id: existingAction.id, |
| 76 | + collection: "actions", |
| 77 | + data: actionData, |
| 78 | + overrideAccess: true, |
| 79 | + }) |
| 80 | + : await payload.create({ |
| 81 | + collection: "actions", |
| 82 | + data: actionData, |
| 83 | + overrideAccess: true, |
| 84 | + }) |
| 85 | + |
| 86 | + return NextResponse.json({ |
| 87 | + id: action.id, |
| 88 | + identifier: moduleInfo.identifier, |
| 89 | + mediaId: media.id, |
| 90 | + status: existingAction ? "updated" : "created", |
| 91 | + }) |
| 92 | +} |
0 commit comments