Skip to content

Commit 937f49a

Browse files
authored
Merge branch 'grupo-3-main' into feat/update-product-variant-ui
2 parents 819617e + f796181 commit 937f49a

File tree

12 files changed

+200
-145
lines changed

12 files changed

+200
-145
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Warnings:
3+
4+
- You are about to drop the column `product_id` on the `cart_items` table. All the data in the column will be lost.
5+
- A unique constraint covering the columns `[cart_id,attribute_value_id]` on the table `cart_items` will be added. If there are existing duplicate values, this will fail.
6+
- Added the required column `attribute_value_id` to the `cart_items` table without a default value. This is not possible if the table is not empty.
7+
8+
*/
9+
-- DropForeignKey
10+
ALTER TABLE "cart_items" DROP CONSTRAINT "cart_items_product_id_fkey";
11+
12+
-- DropIndex
13+
DROP INDEX "cart_items_cart_id_product_id_key";
14+
15+
-- AlterTable
16+
ALTER TABLE "cart_items" DROP COLUMN "product_id",
17+
ADD COLUMN "attribute_value_id" INTEGER NOT NULL;
18+
19+
-- CreateIndex
20+
CREATE UNIQUE INDEX "cart_items_cart_id_attribute_value_id_key" ON "cart_items"("cart_id", "attribute_value_id");
21+
22+
-- AddForeignKey
23+
ALTER TABLE "cart_items" ADD CONSTRAINT "cart_items_attribute_value_id_fkey" FOREIGN KEY ("attribute_value_id") REFERENCES "variants_attributes_values"("id") ON DELETE CASCADE ON UPDATE CASCADE;

prisma/schema.prisma

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ model Product {
6363
updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamp(0)
6464
6565
category Category? @relation(fields: [categoryId], references: [id], onDelete: SetNull)
66-
cartItems CartItem[]
66+
6767
orderItems OrderItem[]
6868
variantAttributeValues VariantAttributeValue[]
6969
@@ -92,6 +92,8 @@ model VariantAttributeValue {
9292
9393
variantAttribute VariantAttribute @relation(fields: [attributeId], references: [id])
9494
product Product @relation(fields: [productId], references: [id])
95+
96+
CartItem CartItem[]
9597
9698
@@unique([attributeId, productId, value], name: "unique_attribute_product_value")
9799
@@map("variants_attributes_values")
@@ -113,15 +115,15 @@ model Cart {
113115
model CartItem {
114116
id Int @id @default(autoincrement())
115117
cartId Int @map("cart_id")
116-
productId Int @map("product_id")
118+
attributeValueId Int @map("attribute_value_id")
117119
quantity Int
118120
createdAt DateTime @default(now()) @map("created_at") @db.Timestamp(0)
119121
updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamp(0)
120122
121123
cart Cart @relation(fields: [cartId], references: [id], onDelete: Cascade)
122-
product Product @relation(fields: [productId], references: [id], onDelete: Cascade)
124+
variantAttributeValue VariantAttributeValue @relation(fields: [attributeValueId], references: [id], onDelete: Cascade)
123125
124-
@@unique([cartId, productId], name: "unique_cart_item")
126+
@@unique([cartId, attributeValueId], name: "unique_cart_item")
125127
@@map("cart_items")
126128
}
127129

src/lib/cart.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { CartItem, CartItemInput } from "@/models/cart.model";
2-
import { type Product } from "@/models/product.model";
2+
// import { type Product } from "@/models/product.model";
3+
import { type VariantAttributeValue } from "@/models/variant-attribute.model";
34
import {
45
alterQuantityCartItem,
56
deleteRemoteCartItem,
@@ -18,14 +19,14 @@ export async function getCart(userId?: number, sessionCartId?: string) {
1819
export async function addToCart(
1920
userId: number | undefined,
2021
sessionCartId: string | undefined,
21-
productId: Product["id"],
22+
attributeValueId: VariantAttributeValue["id"],
2223
quantity: number = 1
2324
) {
2425
try {
2526
const updatedCart = await alterQuantityCartItem(
2627
userId,
2728
sessionCartId,
28-
productId,
29+
attributeValueId,
2930
quantity
3031
);
3132
return updatedCart;
@@ -62,10 +63,10 @@ export function calculateTotal(items: CartItem[] | CartItemInput[]): number {
6263
// Type guard to determine which type we're working with
6364
if ("product" in item) {
6465
// CartItem - has a product property
65-
return total + item.product.price * item.quantity;
66+
return total + Number(item.product.price) * item.quantity;
6667
} else {
6768
// CartItemInput - has price directly
68-
return total + item.price * item.quantity;
69+
return total + Number(item.price) * item.quantity;
6970
}
7071
}, 0);
7172
}

src/models/cart.model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export type CartProductInfo = Pick<
3333
export type CartItemWithProduct = {
3434
product: CartProductInfo;
3535
quantity: number;
36+
attributeValueId: number;
3637
};
3738

3839
// Tipo para el carrito con items y productos incluidos

src/models/product.model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type Product = PrismaProduct & {
55
price?: number | null;
66
minPrice?: number | null;
77
maxPrice?: number | null;
8+
variantAttributeValues?: VariantAttributeValue[];
89
};
910

1011
export type ProductVariantValue = PrismaProduct & {

src/routes/cart/add-item/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import type { Route } from "../+types";
77

88
export async function action({ request }: Route.ActionArgs) {
99
const formData = await request.formData();
10-
const productId = Number(formData.get("productId"));
10+
const attributeValueId = Number(formData.get("attributeValueId"));
1111
const quantity = Number(formData.get("quantity")) || 1;
1212
const redirectTo = formData.get("redirectTo") as string | null;
1313
const session = await getSession(request.headers.get("Cookie"));
1414
const sessionCartId = session.get("sessionCartId");
1515
const userId = session.get("userId");
1616

17-
await addToCart(userId, sessionCartId, productId, quantity);
17+
await addToCart(userId, sessionCartId, attributeValueId, quantity);
1818

1919
return redirect(redirectTo || "/cart");
2020
}

src/routes/cart/index.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default function Cart({ loaderData }: Route.ComponentProps) {
3030
Carrito de compras
3131
</h1>
3232
<div className="border-solid border rounded-xl flex flex-col">
33-
{cart?.items?.map(({ product, quantity, id }) => (
33+
{cart?.items?.map(({ product, quantity, id, attributeValueId }) => (
3434
<div key={product.id} className="flex gap-7 p-6 border-b">
3535
<div className="w-20 rounded-xl bg-muted">
3636
<img
@@ -55,14 +55,14 @@ export default function Cart({ loaderData }: Route.ComponentProps) {
5555
</div>
5656
<div className="flex flex-col gap-2 md:flex-row md:justify-between md:items-center">
5757
<p className="text-sm font-medium">
58-
${product.price.toFixed(2)}
58+
${product.price!.toFixed(2)}
5959
</p>
6060
<div className="flex gap-4 items-center">
6161
<Form method="post" action="/cart/add-item">
6262
<input type="hidden" name="quantity" value="-1" />
6363
<Button
64-
name="productId"
65-
value={product.id}
64+
name="attributeValueId"
65+
value={attributeValueId}
6666
variant="outline"
6767
size="sm-icon"
6868
disabled={quantity === 1}
@@ -77,8 +77,8 @@ export default function Cart({ loaderData }: Route.ComponentProps) {
7777
<Button
7878
variant="outline"
7979
size="sm-icon"
80-
name="productId"
81-
value={product.id}
80+
name="attributeValueId"
81+
value={attributeValueId}
8282
>
8383
<Plus />
8484
</Button>

src/routes/category/components/product-card/index.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { Link } from "react-router";
2+
23
import type { Product } from "@/models/product.model";
34

45
interface ProductCardProps {
56
product: Product;
67
}
78

8-
const stickerCategoryId = 3;
9+
const stickerCategoryId = 3; // ID de la categoría "Stickers"
910

1011
export function ProductCard({ product }: ProductCardProps) {
11-
12-
const isSticker = stickerCategoryId;
13-
12+
13+
const isSticker = product.categoryId === stickerCategoryId;
14+
1415
return (
1516
<>
1617
<Link

src/routes/category/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ export async function loader({ params, request }: Route.LoaderArgs) {
4848
if (min) {
4949
return (productPrice||minProductPrice) >= min
5050
}
51+
5152
if (max) {
5253
return (productPrice||maxProductPrice) <= max
53-
5454
}
5555
return true
5656
});

src/routes/checkout/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ export default function Checkout({
266266
<div className="flex text-sm font-medium gap-4 items-center self-end">
267267
<p>{quantity}</p>
268268
<X className="w-4 h-4" />
269-
<p>S/{product.price.toFixed(2)}</p>
269+
<p>S/{product.price!.toFixed(2)}</p>
270270
</div>
271271
</div>
272272
</div>

0 commit comments

Comments
 (0)