import { auth } from "@/auth" import { prisma } from "@/lib/prisma" import { redirect } from "next/navigation" import { ShoppingCart } from "lucide-react" interface AdminOrder { id: string orderNumber: string email: string | null totalPrice: string currency: string | null financialStatus: string | null fulfillmentStatus: string | null createdAt: Date store: { businessName: string shopifyStoreName: string } } async function getOrders(): Promise { const acceptedSyncs = await prisma.productSync.findMany({ where: { status: "ACCEPTED" }, select: { shopifyProductId: true, storeId: true }, }) if (acceptedSyncs.length === 0) return [] const syncsByStore = new Map>() acceptedSyncs.forEach((sync) => { if (!syncsByStore.has(sync.storeId)) syncsByStore.set(sync.storeId, new Set()) syncsByStore.get(sync.storeId)!.add(sync.shopifyProductId) }) const storeIds = Array.from(syncsByStore.keys()) const cachedOrders = await prisma.cachedOrder.findMany({ where: { storeId: { in: storeIds } }, include: { store: { select: { id: true, businessName: true, shopifyStoreName: true } }, }, orderBy: { shopifyCreatedAt: "desc" }, }) const relevantOrders = cachedOrders.filter((order) => { const syncedProductIds = syncsByStore.get(order.storeId) if (!syncedProductIds?.size) return false try { const lineItems = typeof order.lineItems === "string" ? JSON.parse(order.lineItems) : order.lineItems if (!Array.isArray(lineItems)) return false return lineItems.some( (item: { product_id?: number }) => item.product_id != null && syncedProductIds.has(String(item.product_id)) ) } catch { return false } }) return relevantOrders.map((order) => { const store = order.store as { id: string; businessName: string; shopifyStoreName: string } return { id: order.shopifyOrderId, orderNumber: order.orderNumber, email: order.email, totalPrice: order.totalPrice, currency: order.currency, financialStatus: order.financialStatus, fulfillmentStatus: order.fulfillmentStatus, createdAt: order.shopifyCreatedAt || order.createdAt, store: { businessName: store.businessName, shopifyStoreName: store.shopifyStoreName }, } }) as AdminOrder[] } export default async function AdminOrdersPage() { const session = await auth() if (!session || session.user.role !== "ADMIN") { redirect("/login") } const orders = await getOrders() return (

Orders with Synced Products

Orders from vendor stores that contain products synced with suppliers.

{orders.length === 0 && ( )} {orders.map((order) => ( ))}
Order Vendor Store Customer Total Financial Fulfillment Created
No orders with synced products found.
#{order.orderNumber}
Order ID: {order.id}
{order.store.businessName}
{order.store.shopifyStoreName}
{order.email || "—"} {order.totalPrice} {order.currency || ""} {order.financialStatus || "—"} {order.fulfillmentStatus || "—"} {new Date(order.createdAt as Date).toLocaleString("en-US", { dateStyle: "short", timeStyle: "short", })}
) }