Skip to content

Commit cd32444

Browse files
Merge pull request #263 from codeableorg/grupo-3-mike-2
fix: correct sticker category check and improve price filter logic
2 parents ef1925a + ca8e64f commit cd32444

File tree

6 files changed

+125
-86
lines changed

6 files changed

+125
-86
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/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+
attributeId: number;
3637
};
3738

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

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/services/cart.service.ts

Lines changed: 89 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -18,52 +18,46 @@ async function getCart(
1818
: undefined;
1919

2020
if (!whereCondition) return null;
21-
try {
2221

23-
const data = await prisma.cart.findFirst({
24-
where: whereCondition,
25-
include: {
26-
items: {
27-
include: {
28-
product: {
29-
select: {
30-
id: true,
31-
title: true,
32-
imgSrc: true,
33-
alt: true,
34-
price: true,
35-
isOnSale: true,
22+
const data = await prisma.cart.findFirst({
23+
where: whereCondition,
24+
include: {
25+
items: {
26+
include: {
27+
variantAttributeValue: {
28+
include: {
29+
product: {
30+
select: {
31+
id: true,
32+
title: true,
33+
imgSrc: true,
34+
alt: true,
35+
isOnSale: true,
36+
},
3637
},
3738
},
3839
},
39-
orderBy: {
40-
id: "asc",
41-
},
4240
},
43-
},
44-
});
45-
46-
if (!data) return null;
47-
48-
return {
49-
...data,
50-
items: data.items.map((item) => ({
51-
...item,
52-
product: {
53-
...item.product,
54-
price: item.product.price.toNumber(),
41+
orderBy: {
42+
id: "asc",
5543
},
56-
})),
57-
};
58-
}catch(e) {
59-
console.log(e)
60-
return {
61-
error: true,
62-
status: 500,
63-
message: "Error al obtener el carrito. Verifica el modelo Product.",
64-
};
65-
}
44+
},
45+
},
46+
});
6647

48+
if (!data) return null;
49+
50+
return {
51+
...data,
52+
items: data.items.map((item) => ({
53+
...item,
54+
product: {
55+
...item.variantAttributeValue.product,
56+
price: item.variantAttributeValue.price.toNumber(),
57+
},
58+
variantAttributeValue: item.variantAttributeValue,
59+
})),
60+
};
6761
}
6862

6963
export async function getRemoteCart(
@@ -92,14 +86,17 @@ export async function getOrCreateCart(
9286
include: {
9387
items: {
9488
include: {
95-
product: {
96-
select: {
97-
id: true,
98-
title: true,
99-
imgSrc: true,
100-
alt: true,
101-
price: true,
102-
isOnSale: true,
89+
variantAttributeValue: {
90+
include: {
91+
product: {
92+
select: {
93+
id: true,
94+
title: true,
95+
imgSrc: true,
96+
alt: true,
97+
isOnSale: true,
98+
},
99+
},
103100
},
104101
},
105102
},
@@ -114,9 +111,10 @@ export async function getOrCreateCart(
114111
items: newCart.items.map((item) => ({
115112
...item,
116113
product: {
117-
...item.product,
118-
price: item.product.price.toNumber(),
114+
...item.variantAttributeValue.product,
115+
price: item.variantAttributeValue.price.toNumber(),
119116
},
117+
variantAttributeValue: item.variantAttributeValue,
120118
})),
121119
};
122120
}
@@ -140,7 +138,7 @@ export async function createRemoteItems(
140138
await prisma.cartItem.createMany({
141139
data: items.map((item) => ({
142140
cartId: cart.id,
143-
productId: item.product.id,
141+
attributeValueId: item.attributeId, // modificar
144142
quantity: item.quantity,
145143
})),
146144
});
@@ -156,12 +154,14 @@ export async function createRemoteItems(
156154
export async function alterQuantityCartItem(
157155
userId: User["id"] | undefined,
158156
sessionCartId: string | undefined,
159-
productId: number,
157+
attributeId: number,
160158
quantity: number = 1
161159
): Promise<CartWithItems> {
162160
const cart = await getOrCreateCart(userId, sessionCartId);
163161

164-
const existingItem = cart.items.find((item) => item.product.id === productId);
162+
const existingItem = cart.items.find(
163+
(item) => item.attributeValueId === attributeId
164+
);
165165

166166
if (existingItem) {
167167
const newQuantity = existingItem.quantity + quantity;
@@ -180,7 +180,7 @@ export async function alterQuantityCartItem(
180180
await prisma.cartItem.create({
181181
data: {
182182
cartId: cart.id,
183-
productId,
183+
attributeValueId: attributeId,
184184
quantity,
185185
},
186186
});
@@ -246,14 +246,17 @@ export async function linkCartToUser(
246246
include: {
247247
items: {
248248
include: {
249-
product: {
250-
select: {
251-
id: true,
252-
title: true,
253-
imgSrc: true,
254-
alt: true,
255-
price: true,
256-
isOnSale: true,
249+
variantAttributeValue: {
250+
include: {
251+
product: {
252+
select: {
253+
id: true,
254+
title: true,
255+
imgSrc: true,
256+
alt: true,
257+
isOnSale: true,
258+
},
259+
},
257260
},
258261
},
259262
},
@@ -268,9 +271,10 @@ export async function linkCartToUser(
268271
items: updatedCart.items.map((item) => ({
269272
...item,
270273
product: {
271-
...item.product,
272-
price: item.product.price.toNumber(),
274+
...item.variantAttributeValue.product,
275+
price: item.variantAttributeValue.price.toNumber(),
273276
},
277+
variantAttributeValue: item.variantAttributeValue,
274278
})),
275279
};
276280
}
@@ -295,41 +299,48 @@ export async function mergeGuestCartWithUserCart(
295299
include: {
296300
items: {
297301
include: {
298-
product: {
299-
select: {
300-
id: true,
301-
title: true,
302-
imgSrc: true,
303-
alt: true,
304-
price: true,
305-
isOnSale: true,
302+
variantAttributeValue: {
303+
include: {
304+
product: {
305+
select: {
306+
id: true,
307+
title: true,
308+
imgSrc: true,
309+
alt: true,
310+
isOnSale: true,
311+
},
312+
},
306313
},
307314
},
308315
},
309316
},
310317
},
311318
});
319+
312320
return {
313321
...updatedCart,
314322
items: updatedCart.items.map((item) => ({
315323
...item,
316324
product: {
317-
...item.product,
318-
price: item.product.price.toNumber(),
325+
...item.variantAttributeValue.product,
326+
price: item.variantAttributeValue.price.toNumber(),
319327
},
328+
variantAttributeValue: item.variantAttributeValue,
320329
})),
321330
};
322331
}
323332

324333
// Obtener productos duplicados para eliminarlos del carrito del usuario
325-
const guestProductIds = guestCart.items.map((item) => item.productId);
334+
const guestAttributeValueIds = guestCart.items.map(
335+
(item) => item.attributeValueId
336+
);
326337

327338
// Eliminar productos del carrito usuario que también existan en el carrito invitado
328339
await prisma.cartItem.deleteMany({
329340
where: {
330341
cartId: userCart.id,
331-
productId: {
332-
in: guestProductIds,
342+
attributeValueId: {
343+
in: guestAttributeValueIds,
333344
},
334345
},
335346
});
@@ -338,7 +349,7 @@ export async function mergeGuestCartWithUserCart(
338349
await prisma.cartItem.createMany({
339350
data: guestCart.items.map((item) => ({
340351
cartId: userCart.id,
341-
productId: item.productId,
352+
attributeValueId: item.attributeValueId,
342353
quantity: item.quantity,
343354
})),
344355
});
@@ -351,3 +362,4 @@ export async function mergeGuestCartWithUserCart(
351362
// Devolver el carrito actualizado del usuario
352363
return await getCart(userId);
353364
}
365+

0 commit comments

Comments
 (0)