// Public REST: website store packages (website_shop_articles). // AtomCMS JSON API parity — read-only list of buyable packages, paginated and // ordered by `position` (then name), matching the admin /admin/shop query. import { apiError, apiJson, pagination, positiveBigInt } from "@/lib/api"; import { prisma } from "@/lib/prisma"; export const dynamic = "force-dynamic"; export async function GET(req: Request) { const sp = new URL(req.url).searchParams; const { page, perPage, skip, take } = pagination(sp); // Optional ?category= filter (website_shop_category_id is a BigInt). const categoryRaw = sp.get("category"); let where: { categoryId?: bigint } = {}; if (categoryRaw !== null) { const categoryId = positiveBigInt(categoryRaw); if (!categoryId) return apiError("A valid category id is required", 422); where = { categoryId }; } try { const [total, data] = await Promise.all([ prisma.websiteShopArticles.count({ where }), prisma.websiteShopArticles.findMany({ where, orderBy: [{ position: "asc" }, { name: "asc" }], skip, take, select: { id: true, categoryId: true, name: true, info: true, iconUrl: true, color: true, costs: true, giveRank: true, isGiftable: true, credits: true, duckets: true, diamonds: true, badges: true, furniture: true, position: true, }, }), ]); return apiJson({ data, meta: { page, perPage, total, lastPage: Math.max(1, Math.ceil(total / perPage)), }, }); } catch { // DB unreachable — never 500; return an empty, well-formed payload. return apiJson( { data: [], meta: { page, perPage, total: 0, lastPage: 1 } }, { status: 200 }, ); } }