import type { Context } from '@netlify/functions' import { guardMethod } from '../utils/method.js' import { ok } from '../utils/http.js' import { prisma } from '../utils/prisma.js' import { withErrorBoundary } from '../utils/handler.js' const categories = [ { id: 'recommend', name: '推荐' }, { id: 'homeLiving', name: '居家生活' }, { id: 'digitalAppliance', name: '数码家电' }, { id: 'beautyCare', name: '美妆个护' }, { id: 'foodDrink', name: '食品饮料' }, { id: 'motherBaby', name: '母婴玩具' }, { id: 'fashionBag', name: '服饰鞋包' }, { id: 'sportsOutdoor', name: '运动户外' } ] const toProductItem = (product: Awaited>[number]) => { const picInfo = Array.isArray(product.picInfo) ? product.picInfo : [] const image = product.imageUrl || String(picInfo[0] || '') const price = product.price.toFixed(2) return { id: product.id, sku: product.sku, number: product.number, price, name: product.name, taxRate: Number(product.taxRate), picInfo, desc: product.description || '', detail: product.detail || product.description || '', originPrice: product.originPrice ? product.originPrice.toFixed(2) : price, image, imageUrl: image, categoryId: product.categoryId || '', category: product.category || '', tag: product.tag || '', sales: product.sales || '', stock: product.stock } } export default withErrorBoundary(async (request: Request, _context: Context) => { const methodResponse = guardMethod(request, ['GET']) if (methodResponse) return methodResponse const url = new URL(request.url) const keyword = (url.searchParams.get('keyword') || '').trim() const categoryId = (url.searchParams.get('categoryId') || '').trim() const products = await prisma.supplierProduct.findMany({ where: { enabled: true, deleted: 0, ...(keyword ? { name: { contains: keyword } } : {}), ...(categoryId && categoryId !== 'recommend' ? { categoryId } : {}) }, orderBy: [ { sortOrder: 'asc' }, { createdAt: 'desc' } ] }) return ok({ categories, products: products.map(toProductItem) }) })