From c0567936f9a4fd0825ef9c02c76439205033229a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Wed, 6 Nov 2024 16:22:54 +0100 Subject: [PATCH] feat: history page - handle new order schema --- .../src/pages/dashboard/account/history.tsx | 89 ++++++++++++------- webapp/src/pages/dashboard/wallet/index.tsx | 2 +- webapp/src/server/api/routers/order.ts | 51 +++++++---- webapp/src/server/api/routers/saving.ts | 4 +- 4 files changed, 92 insertions(+), 54 deletions(-) diff --git a/webapp/src/pages/dashboard/account/history.tsx b/webapp/src/pages/dashboard/account/history.tsx index 9789ef04..4f4d8596 100644 --- a/webapp/src/pages/dashboard/account/history.tsx +++ b/webapp/src/pages/dashboard/account/history.tsx @@ -1,3 +1,4 @@ +import { ChevronLeftIcon } from "@chakra-ui/icons"; import { Box, Center, @@ -8,15 +9,16 @@ import { IconButton, Text, } from "@chakra-ui/react"; +import { TinyColor } from "@ctrl/tinycolor"; +import Image from "next/image"; import { useRouter } from "next/router"; -import { useAuth } from "~/providers/Auth"; -import LoadingLoader from "~/components/LoadingLoader"; import { HiMiniCheckCircle, HiMiniClock } from "react-icons/hi2"; -import { api } from "~/utils/api"; +import LoadingLoader from "~/components/LoadingLoader"; +import { useAuth } from "~/providers/Auth"; +import { OrderIncluded } from "~/server/api/routers/order"; +import { CouponExtanded } from "~/server/api/routers/saving"; import { UserIncluded } from "~/server/api/routers/user"; -import Image from "next/image"; -import { ChevronLeftIcon } from "@chakra-ui/icons"; -import { TinyColor } from "@ctrl/tinycolor"; +import { api } from "~/utils/api"; const UserSavingsNoData = () => { return ( @@ -37,16 +39,34 @@ export default function AccountHistory() { { userId: (user as UserIncluded)?.id }, { enabled: !!user?.id } ); + const { data: resultUserOrders, isLoading: isLoadingUserOrders } = + api.order.getList.useQuery({ + status: "delivered", + }); const { data: userSavings } = resultUserSavings || {}; + const { data: userOrders } = resultUserOrders || {}; - if (!user || isLoadingUserSavings || !userSavings) + if ( + !user || + isLoadingUserSavings || + !userSavings || + isLoadingUserOrders || + !userOrders + ) return (
); + const history: (CouponExtanded | OrderIncluded)[] = [ + ...userSavings, + ...userOrders, + ].sort( + (a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime() + ); + return ( Historique de mes réductions - {userSavings.length > 0 ? ( + {history.length > 0 ? ( - {userSavings.map((userSaving, index) => { + {history.map((userHistoryItem, index) => { const currentDate = new Date(); const currentCouponUsedAt = new Date( - (userSaving.usedAt - ? userSaving.usedAt - : userSaving.offer.validityTo) as string + ("usedAt" in userHistoryItem + ? userHistoryItem.usedAt + : userHistoryItem.createdAt) as string ); - const previousCouponUsedAt = userSavings[index - 1] + const previousCoupon = history[index - 1]; + const previousCouponUsedAt = previousCoupon ? new Date( - (userSavings[index - 1]?.usedAt - ? userSavings[index - 1].usedAt - : userSavings[index - 1]?.offer.validityTo) as string + ("usedAt" in previousCoupon && previousCoupon.usedAt + ? previousCoupon.usedAt + : previousCoupon.createdAt) as string ) : new Date(); @@ -87,7 +108,7 @@ export default function AccountHistory() { }); const darkenPartnerColor = new TinyColor( - userSaving.offer.partner.color + userHistoryItem.offer.partner.color ) .darken(10) .toHexString(); @@ -120,7 +141,7 @@ export default function AccountHistory() { )} {userSaving.offer.partner.icon.alt - {userSaving.offer.partner.name} + {userHistoryItem.offer.partner.name} - {userSaving.offer.title} + {userHistoryItem.offer.title} - {userSaving.used ? ( + {"used" in userHistoryItem && !userHistoryItem.used ? ( + + + + Fin{" "} + {currentCouponUsedAt.toLocaleDateString("fr-FR", { + day: "2-digit", + month: "2-digit", + })} + + + ) : ( Déjà utilisée - ) : ( - - - - Fin{" "} - {currentCouponUsedAt.toLocaleDateString("fr-FR", { - day: "2-digit", - month: "2-digit", - })} - - )} diff --git a/webapp/src/pages/dashboard/wallet/index.tsx b/webapp/src/pages/dashboard/wallet/index.tsx index af267a3c..2eeeea60 100644 --- a/webapp/src/pages/dashboard/wallet/index.tsx +++ b/webapp/src/pages/dashboard/wallet/index.tsx @@ -34,7 +34,7 @@ export default function Wallet() { const { data: resultUserCoupons, isLoading: isLoadingUserCoupons } = api.coupon.getList.useQuery(); const { data: resultUserOrders, isLoading: isLoadingUserOrders } = - api.order.getList.useQuery(); + api.order.getList.useQuery({}); const { data: currentUserCoupons } = resultUserCoupons || { data: [] }; const { data: currentUserOrders } = resultUserOrders || { data: [] }; diff --git a/webapp/src/server/api/routers/order.ts b/webapp/src/server/api/routers/order.ts index 51e24652..2b6d2b38 100644 --- a/webapp/src/server/api/routers/order.ts +++ b/webapp/src/server/api/routers/order.ts @@ -7,6 +7,7 @@ import { createOrderPayload, insertItemPayload } from "~/utils/obiz"; import { payloadWhereOfferIsValid } from "~/utils/tools"; import fs from "fs/promises"; import os from "os"; +import { Where } from "payload/types"; export interface OrderIncluded extends Order { offer: Offer & { partner: Partner & { icon: Media } } & { image: Media }; @@ -266,23 +267,41 @@ export const orderRouter = createTRPCRouter({ } }), - getList: userProtectedProcedure.query(async ({ ctx }) => { - const orders = await ctx.payload.find({ - collection: "orders", - depth: 3, - where: { - and: [ - { user: { equals: ctx.session.id } }, - { status: { not_in: ["awaiting_payment", "archived"] } }, - { - ...payloadWhereOfferIsValid("offer"), - }, - ], - }, - }); + getList: userProtectedProcedure + .input( + z.object({ + status: z.enum(["delivered"]).optional(), + }) + ) + .query(async ({ ctx, input }) => { + const { status } = input; + + let statusQuery: Where = { + status: { not_in: ["awaiting_payment", "archived"] }, + }; - return { data: orders.docs as OrderIncluded[] }; - }), + if (status) { + statusQuery = { + status: { equals: status }, + }; + } + + const orders = await ctx.payload.find({ + collection: "orders", + depth: 3, + where: { + and: [ + { user: { equals: ctx.session.id } }, + { ...statusQuery }, + { + ...payloadWhereOfferIsValid("offer"), + }, + ], + }, + }); + + return { data: orders.docs as OrderIncluded[] }; + }), getById: userProtectedProcedure .input( diff --git a/webapp/src/server/api/routers/saving.ts b/webapp/src/server/api/routers/saving.ts index afaa6dd7..b0d398e3 100644 --- a/webapp/src/server/api/routers/saving.ts +++ b/webapp/src/server/api/routers/saving.ts @@ -26,10 +26,8 @@ export const savingRouter = createTRPCRouter({ user: { equals: userId, }, - ["offer.validityTo"]: { - less_than: new Date(new Date().setHours(23, 59, 59)).toISOString(), - }, }, + limit: 1000, sort: "-usedAt", });