diff --git a/apps/nextjs/src/app/(private)/orders/page.tsx b/apps/nextjs/src/app/(private)/orders/page.tsx index d77a12c..a8c2b7b 100644 --- a/apps/nextjs/src/app/(private)/orders/page.tsx +++ b/apps/nextjs/src/app/(private)/orders/page.tsx @@ -13,8 +13,13 @@ import { TableHeader, TableRow, } from "@/components/ui/table"; +import { api } from "@/lib/server"; +import rupiahFormatter, { cn } from "@/lib/utils"; +import { IoEllipsisVerticalSharp } from "react-icons/io5"; + +export default async function OrdersPage() { + const data = await api.admin.getOrders.query(); -export default function OrdersPage() { return (
@@ -23,40 +28,56 @@ export default function OrdersPage() { Order Customer - Channel Date - Total - Status + + Total + + Status Actions - - #3210 - Olivia Martin - - Online Store - - - February 20, 2022 - - $42.25 - Shipped - - - - - - - View order - Customer details - - - - + {data.map((order) => ( + + + {order.id} + + {order.buyer.name} + + {new Date(order.createdAt).toLocaleDateString()} + + + {rupiahFormatter(order.totalPrice)} + + + + {order.status} + + + + + + + + View order + Customer details + + + + + ))}
diff --git a/apps/nextjs/src/lib/utils.ts b/apps/nextjs/src/lib/utils.ts index e1944ec..bd9c48e 100644 --- a/apps/nextjs/src/lib/utils.ts +++ b/apps/nextjs/src/lib/utils.ts @@ -1,7 +1,17 @@ -import { clsx } from "clsx" -import type {ClassValue} from "clsx"; -import { twMerge } from "tailwind-merge" +import { clsx } from "clsx"; +import type { ClassValue } from "clsx"; +import { twMerge } from "tailwind-merge"; export function cn(...inputs: ClassValue[]) { - return twMerge(clsx(inputs)) + return twMerge(clsx(inputs)); +} + +export default function rupiahFormatter(value?: number): string { + if (!value) return ""; + + return new Intl.NumberFormat("id-ID", { + style: "currency", + currency: "IDR", + minimumFractionDigits: 0, + }).format(value); } diff --git a/packages/api/src/root.ts b/packages/api/src/root.ts index 58ce4e8..08e8771 100644 --- a/packages/api/src/root.ts +++ b/packages/api/src/root.ts @@ -1,4 +1,4 @@ -import { authRouter } from "./router/auth"; +import { adminRouter } from "./router/admin"; import { categoryRouter } from "./router/category"; import { orderRouter } from "./router/orders"; import { productRouter } from "./router/product"; @@ -6,7 +6,7 @@ import { userRouter } from "./router/user"; import { createTRPCRouter } from "./trpc"; export const appRouter = createTRPCRouter({ - auth: authRouter, + admin: adminRouter, product: productRouter, category: categoryRouter, user: userRouter, diff --git a/packages/api/src/router/admin.ts b/packages/api/src/router/admin.ts new file mode 100644 index 0000000..6281bbc --- /dev/null +++ b/packages/api/src/router/admin.ts @@ -0,0 +1,32 @@ +import { alias } from "drizzle-orm/mysql-core"; + +import { eq, sql } from "@vivat/db"; +import { addresses } from "@vivat/db/schema/addresses"; +import { orders } from "@vivat/db/schema/orders"; +import { products } from "@vivat/db/schema/products"; +import { users } from "@vivat/db/schema/users"; + +import { adminProcedure, createTRPCRouter } from "../trpc"; + +export const adminRouter = createTRPCRouter({ + getOrders: adminProcedure.query(async ({ ctx }) => { + const seller = alias(users, "seller"); + + return await ctx.db + .select({ + createdAt: orders.createdAt, + id: orders.id, + status: orders.status, + products, + buyer: users, + seller, + totalPrice: orders.totalPrice, + }) + .from(orders) + .innerJoin(addresses, eq(orders.addressId, addresses.id)) + .innerJoin(users, eq(addresses.userId, users.id)) + .innerJoin(products, eq(orders.productId, products.id)) + .innerJoin(seller, eq(products.sellerId, seller.id)) + .orderBy(sql`(CASE ${orders.status} WHEN 'payment' THEN 0 ELSE 1 END), ${orders.createdAt} DESC`) + }), +}); diff --git a/packages/api/src/router/auth.ts b/packages/api/src/router/auth.ts deleted file mode 100644 index b418d59..0000000 --- a/packages/api/src/router/auth.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { createTRPCRouter, protectedProcedure, publicProcedure } from "../trpc"; - -export const authRouter = createTRPCRouter({ - getSession: publicProcedure.query(({ ctx }) => { - return ctx.auth; - }), - getSecretMessage: protectedProcedure.query(() => { - // testing type validation of overridden next-auth Session in @vivat/auth package - return "you can see this secret message!"; - }), -});