diff --git a/prisma/initial_data.ts b/prisma/initial_data.ts index e8aee38..3ab5380 100644 --- a/prisma/initial_data.ts +++ b/prisma/initial_data.ts @@ -395,36 +395,36 @@ export const variantAttributeValues = [ // --- STICKERS (dimensiones: 3x3, 6x6, 9x9) --- { attributeId: 2, productId: 10, value: "3x3", price: 2.99 }, - { attributeId: 2, productId: 10, value: "6x6", price: 3.99 }, - { attributeId: 2, productId: 10, value: "9x9", price: 4.99 }, + { attributeId: 2, productId: 10, value: "5x5", price: 3.99 }, + { attributeId: 2, productId: 10, value: "10x10", price: 4.99 }, { attributeId: 2, productId: 11, value: "3x3", price: 2.49 }, - { attributeId: 2, productId: 11, value: "6x6", price: 3.49 }, - { attributeId: 2, productId: 11, value: "9x9", price: 4.49 }, + { attributeId: 2, productId: 11, value: "5x5", price: 3.49 }, + { attributeId: 2, productId: 11, value: "10x10", price: 4.49 }, { attributeId: 2, productId: 12, value: "3x3", price: 3.99 }, - { attributeId: 2, productId: 12, value: "6x6", price: 4.99 }, - { attributeId: 2, productId: 12, value: "9x9", price: 5.99 }, + { attributeId: 2, productId: 12, value: "5x5", price: 4.99 }, + { attributeId: 2, productId: 12, value: "10x10", price: 5.99 }, { attributeId: 2, productId: 13, value: "3x3", price: 2.99 }, - { attributeId: 2, productId: 13, value: "6x6", price: 3.99 }, - { attributeId: 2, productId: 13, value: "9x9", price: 4.99 }, + { attributeId: 2, productId: 13, value: "5x5", price: 3.99 }, + { attributeId: 2, productId: 13, value: "10x10", price: 4.99 }, { attributeId: 2, productId: 14, value: "3x3", price: 2.49 }, - { attributeId: 2, productId: 14, value: "6x6", price: 3.49 }, - { attributeId: 2, productId: 14, value: "9x9", price: 4.49 }, + { attributeId: 2, productId: 14, value: "5x5", price: 3.49 }, + { attributeId: 2, productId: 14, value: "10x10", price: 4.49 }, { attributeId: 2, productId: 15, value: "3x3", price: 2.49 }, - { attributeId: 2, productId: 15, value: "6x6", price: 3.49 }, - { attributeId: 2, productId: 15, value: "9x9", price: 4.49 }, + { attributeId: 2, productId: 15, value: "5x5", price: 3.49 }, + { attributeId: 2, productId: 15, value: "10x10", price: 4.49 }, { attributeId: 2, productId: 16, value: "3x3", price: 2.99 }, - { attributeId: 2, productId: 16, value: "6x6", price: 3.99 }, - { attributeId: 2, productId: 16, value: "9x9", price: 4.99 }, + { attributeId: 2, productId: 16, value: "5x5", price: 3.99 }, + { attributeId: 2, productId: 16, value: "10x10", price: 4.99 }, { attributeId: 2, productId: 17, value: "3x3", price: 2.99 }, - { attributeId: 2, productId: 17, value: "6x6", price: 3.99 }, - { attributeId: 2, productId: 17, value: "9x9", price: .99 }, + { attributeId: 2, productId: 17, value: "5x5", price: 3.99 }, + { attributeId: 2, productId: 17, value: "10x10", price: .99 }, // --- TAZAS (no aplica: Único) --- { attributeId: 3, productId: 18, value: "Único", price: 14.99 }, diff --git a/src/models/product.model.ts b/src/models/product.model.ts index 96ba043..489125f 100644 --- a/src/models/product.model.ts +++ b/src/models/product.model.ts @@ -1,5 +1,12 @@ +import type { VariantAttributeValue } from "./variant-attribute.model"; import type { Product as PrismaProduct } from "@/../generated/prisma/client"; -export type Product = Omit & { - price: number; +export type Product = PrismaProduct & { + price?: number | null; + minPrice?: number | null; + maxPrice?: number | null; }; + +export type ProductVariantValue = PrismaProduct & { + variantAttributeValues: VariantAttributeValue[]; +} \ No newline at end of file diff --git a/src/models/variant-attribute.model.ts b/src/models/variant-attribute.model.ts new file mode 100644 index 0000000..2c91027 --- /dev/null +++ b/src/models/variant-attribute.model.ts @@ -0,0 +1,2 @@ +import type { VariantAttributeValue as PrismaVariantAttributeValue } from "@/../generated/prisma/client"; +export type VariantAttributeValue= PrismaVariantAttributeValue \ No newline at end of file diff --git a/src/routes/category/components/product-card/index.tsx b/src/routes/category/components/product-card/index.tsx index a6abe33..7805793 100644 --- a/src/routes/category/components/product-card/index.tsx +++ b/src/routes/category/components/product-card/index.tsx @@ -8,6 +8,7 @@ interface ProductCardProps { export function ProductCard({ product }: ProductCardProps) { return ( + <>

{product.title}

{product.description}

+ { + product?.price &&

S/{product.price}

+ } + { + product?.minPrice && +

Entre S/{product.minPrice} - {product.maxPrice}

+ } {product.isOnSale && ( @@ -34,5 +42,6 @@ export function ProductCard({ product }: ProductCardProps) { )} + ); } diff --git a/src/routes/category/index.tsx b/src/routes/category/index.tsx index 7c0aef5..e4f83a0 100644 --- a/src/routes/category/index.tsx +++ b/src/routes/category/index.tsx @@ -36,8 +36,24 @@ export async function loader({ params, request }: Route.LoaderArgs) { const min = minPrice ? parseFloat(minPrice) : 0; const max = maxPrice ? parseFloat(maxPrice) : Infinity; return products.filter( - (product) => product.price >= min && product.price <= max - ); + (product) => { + const minProductPrice = product.minPrice||0 + const maxProductPrice = product.maxPrice ||0 + const productPrice = product.price || 0 + + if (min && max) { + return ((productPrice||minProductPrice) >= min) && ((productPrice||maxProductPrice) <= max) + } + + if (min) { + return (productPrice||minProductPrice) >= min + } + if (max) { + return (productPrice||maxProductPrice) <= max + + } + return true + }); }; const filteredProducts = filterProductsByPrice( diff --git a/src/routes/root/index.tsx b/src/routes/root/index.tsx index f3197d1..b76631d 100644 --- a/src/routes/root/index.tsx +++ b/src/routes/root/index.tsx @@ -68,7 +68,7 @@ export async function loader({ request }: Route.LoaderArgs) { } const totalItems = - cart?.items.reduce((total, item) => total + item.quantity, 0) || 0; + cart?.items?.reduce((total, item) => total + item.quantity, 0) || 0; // Preparar datos de respuesta según estado de autenticación const responseData = user ? { user, totalItems } : { totalItems }; diff --git a/src/services/cart.service.ts b/src/services/cart.service.ts index f742706..74cbdfc 100644 --- a/src/services/cart.service.ts +++ b/src/services/cart.service.ts @@ -18,42 +18,52 @@ async function getCart( : undefined; if (!whereCondition) return null; + try { - const data = await prisma.cart.findFirst({ - where: whereCondition, - include: { - items: { - include: { - product: { - select: { - id: true, - title: true, - imgSrc: true, - alt: true, - price: true, - isOnSale: true, + const data = await prisma.cart.findFirst({ + where: whereCondition, + include: { + items: { + include: { + product: { + select: { + id: true, + title: true, + imgSrc: true, + alt: true, + price: true, + isOnSale: true, + }, }, }, - }, - orderBy: { - id: "asc", + orderBy: { + id: "asc", + }, }, }, - }, - }); - - if (!data) return null; + }); + + if (!data) return null; + + return { + ...data, + items: data.items.map((item) => ({ + ...item, + product: { + ...item.product, + price: item.product.price.toNumber(), + }, + })), + }; + }catch(e) { + console.log(e) + return { + error: true, + status: 500, + message: "Error al obtener el carrito. Verifica el modelo Product.", + }; + } - return { - ...data, - items: data.items.map((item) => ({ - ...item, - product: { - ...item.product, - price: item.product.price.toNumber(), - }, - })), - }; } export async function getRemoteCart( diff --git a/src/services/product.service.ts b/src/services/product.service.ts index 3406570..add02b7 100644 --- a/src/services/product.service.ts +++ b/src/services/product.service.ts @@ -1,38 +1,66 @@ import { prisma } from "@/db/prisma"; import type { Category } from "@/models/category.model"; -import type { Product } from "@/models/product.model"; +import type { Product, ProductVariantValue } from "@/models/product.model"; +import type { VariantAttributeValue } from "@/models/variant-attribute.model"; import { getCategoryBySlug } from "./category.service"; +const formattedProduct = (product: ProductVariantValue) => { + const {variantAttributeValues, ...rest} = product + const prices = variantAttributeValues.map((v: VariantAttributeValue) => Number(v.price)) + const minPrice = Math.min(...prices) + const maxPrice = Math.max(...prices) + if (minPrice === maxPrice) { + return { + ...rest, + price: minPrice + } + } + return { + ...rest, + minPrice, + maxPrice + } +} + export async function getProductsByCategorySlug( categorySlug: Category["slug"] ): Promise { const category = await getCategoryBySlug(categorySlug); const products = await prisma.product.findMany({ where: { categoryId: category.id }, + include: { + variantAttributeValues: true + } }); - return products.map((product) => ({ - ...product, - price: product.price.toNumber(), - })); + return products.map(formattedProduct) } export async function getProductById(id: number): Promise { const product = await prisma.product.findUnique({ where: { id }, + include: { + variantAttributeValues: true + } }); if (!product) { throw new Error("Product not found"); } + const variants = product.variantAttributeValues.map((variant)=> ({ + ...variant, + price: Number(variant.price) + })) - return { ...product, price: product.price.toNumber() }; +return {...product, variantAttributeValues: variants } as Product } export async function getAllProducts(): Promise { - return (await prisma.product.findMany()).map((p) => ({ - ...p, - price: p.price.toNumber(), - })); + const products = await prisma.product.findMany({ + include: { + variantAttributeValues: true + } + }); + return products.map(formattedProduct) }