126 lines
3.4 KiB
TypeScript
126 lines
3.4 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getPayload } from 'payload'
|
|
import config from '@payload-config'
|
|
|
|
/**
|
|
* 获取预购产品的订单列表(从 Medusa 获取)
|
|
* GET /api/preorders/:id/orders
|
|
*/
|
|
export async function GET(
|
|
req: NextRequest,
|
|
{ params }: { params: { id: string } }
|
|
) {
|
|
try {
|
|
const payload = await getPayload({ config })
|
|
const { id } = params
|
|
|
|
// 获取预购产品
|
|
let product: any = null
|
|
|
|
try {
|
|
product = await payload.findByID({
|
|
collection: 'preorder-products',
|
|
id,
|
|
depth: 2,
|
|
})
|
|
} catch (err) {
|
|
const result = await payload.find({
|
|
collection: 'preorder-products',
|
|
where: {
|
|
or: [
|
|
{ medusaId: { equals: id } },
|
|
{ seedId: { equals: id } },
|
|
],
|
|
},
|
|
limit: 1,
|
|
depth: 2,
|
|
})
|
|
|
|
if (result.docs.length > 0) {
|
|
product = result.docs[0]
|
|
}
|
|
}
|
|
|
|
if (!product) {
|
|
return NextResponse.json(
|
|
{ error: 'Preorder product not found' },
|
|
{ status: 404 }
|
|
)
|
|
}
|
|
|
|
if (!product.medusaId) {
|
|
return NextResponse.json(
|
|
{ error: 'Product has no Medusa ID, cannot fetch orders' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// 从 Medusa 获取订单数据
|
|
const medusaUrl = process.env.MEDUSA_BACKEND_URL || 'http://localhost:9000'
|
|
const medusaResponse = await fetch(`${medusaUrl}/admin/orders`, {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
})
|
|
|
|
if (!medusaResponse.ok) {
|
|
throw new Error('Failed to fetch orders from Medusa')
|
|
}
|
|
|
|
const { orders } = await medusaResponse.json()
|
|
|
|
// 筛选包含此产品的订单
|
|
const productOrders = (orders || [])
|
|
.filter((order: any) => {
|
|
return order?.items?.some((item: any) => item.product_id === product.medusaId)
|
|
})
|
|
.map((order: any) => {
|
|
// 提取此产品的订单项
|
|
const items = order.items.filter((item: any) => item.product_id === product.medusaId)
|
|
|
|
return {
|
|
id: order.id,
|
|
display_id: order.display_id,
|
|
status: order.status,
|
|
payment_status: order.payment_status,
|
|
fulfillment_status: order.fulfillment_status,
|
|
customer_id: order.customer_id,
|
|
email: order.email,
|
|
total: order.total,
|
|
currency_code: order.currency_code,
|
|
created_at: order.created_at,
|
|
updated_at: order.updated_at,
|
|
items: items.map((item: any) => ({
|
|
id: item.id,
|
|
variant_id: item.variant_id,
|
|
title: item.title,
|
|
quantity: item.quantity,
|
|
unit_price: item.unit_price,
|
|
total: item.total,
|
|
})),
|
|
}
|
|
})
|
|
|
|
// 按创建时间倒序排序
|
|
productOrders.sort((a: any, b: any) =>
|
|
new Date(b.created_at).getTime() - new Date(a.created_at).getTime()
|
|
)
|
|
|
|
return NextResponse.json({
|
|
orders: productOrders,
|
|
count: productOrders.length,
|
|
product: {
|
|
id: product.id,
|
|
title: product.title,
|
|
medusa_id: product.medusaId,
|
|
},
|
|
})
|
|
} catch (error: any) {
|
|
console.error('[Payload Preorder Orders API] Error:', error?.message || error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch preorder orders', message: error?.message },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|