Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | 1x 1x 1x 1x 1x 1x | import { NextResponse } from 'next/server'
import { prisma } from '@/lib/prisma'
type OrderItemInput = {
id: string
quantity: number
price: number
salePrice?: number
selectedColor?: string | null
selectedSize?: string | null
}
type OrderRequest = {
customer: {
name: string
email: string
phone: string
address: string
city: string
zip: string
notes?: string | null
}
items: OrderItemInput[]
total: number | string
shippingMethod: string
}
// POST /api/orders - Create a new order
export async function POST(request: Request) {
try {
const body = (await request.json()) as OrderRequest
const total =
typeof body.total === 'string' ? parseFloat(body.total) : body.total
const items = Array.isArray(body.items) ? body.items : []
const order = await prisma.order.create({
data: {
customerName: body.customer.name,
customerEmail: body.customer.email,
customerPhone: body.customer.phone,
customerAddress: body.customer.address,
customerCity: body.customer.city,
customerZip: body.customer.zip,
shippingMethod: body.shippingMethod,
notes: body.customer.notes || null,
total,
status: 'pending',
items: {
create: items.map((item) => ({
productId: item.id,
quantity: item.quantity,
price: item.salePrice || item.price,
selectedColor: item.selectedColor || null,
selectedSize: item.selectedSize || null,
})),
},
},
include: {
items: {
include: {
product: true,
},
},
},
})
return NextResponse.json(order, { status: 201 })
} catch (error) {
console.error('Error creating order:', error)
return NextResponse.json({ error: 'Failed to create order' }, { status: 500 })
}
}
|