@@ -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
6963export 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(
156154export 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