import { NextResponse } from "next/server"; import { STORED_CHANNEL_TIERS, PRIORITY_OPERATIONS, type PriorityOperation, type StoredChannelTier, } from "yt-dlp-transcript-common/lib/channelPriority"; import { applyChannelPriorityPresetAction, setChannelOperationTierAction, setChannelTierAction, } from "../../../channels/actions"; import { actionResponse, OpsInputError, ops, optString, reqSlugs, } from "../_lib"; export const dynamic = "force-dynamic"; // POST { slugs: string[], tier?: "normal"|"low"|"paused"|null, operation?: one // of sync|transcription|download|digest|backfill, preset?: "sync-only"|"clear" } // // Three named gestures, one route, because they are one gesture in the UI: the // /channels deck's tier control. `operation` present pins that operation's // override (null clears it back to the base tier); absent sets the BASE tier. // `preset` is the deck's two shortcuts and takes no tier. export async function POST(request: Request) { return ops( request, ["slugs", "tier", "operation", "preset"], async (body) => { const slugs = reqSlugs(body, "slugs"); const preset = optString(body, "preset"); if (preset !== undefined) { if (preset !== "sync-only" && preset !== "clear") { throw new OpsInputError('"preset" must be "sync-only" or "clear"'); } return actionResponse( await applyChannelPriorityPresetAction(slugs, preset), ); } const rawTier = body.tier; if (rawTier !== null && typeof rawTier !== "string") { throw new OpsInputError( `"tier" must be one of ${STORED_CHANNEL_TIERS.join(", ")} (or null with an "operation", to clear the override)`, ); } if ( rawTier !== null && !(STORED_CHANNEL_TIERS as readonly string[]).includes(rawTier) ) { throw new OpsInputError( `"tier" must be one of ${STORED_CHANNEL_TIERS.join(", ")}`, ); } const operation = optString(body, "operation"); if (operation !== undefined) { if (!(PRIORITY_OPERATIONS as readonly string[]).includes(operation)) { throw new OpsInputError( `"operation" must be one of ${PRIORITY_OPERATIONS.join(", ")}`, ); } return actionResponse( await setChannelOperationTierAction( slugs, operation as PriorityOperation, rawTier as StoredChannelTier | null, ), ); } if (rawTier === null) { throw new OpsInputError( 'a null "tier" clears an operation override — name an "operation", or send a stored tier', ); } return actionResponse( await setChannelTierAction(slugs, rawTier as StoredChannelTier), ); }, ); } export function GET() { return NextResponse.json( { ok: false, error: "POST only" }, { status: 405 }, ); }