Repository navigation
Expand file tree
/
Copy pathcart.ts
More file actions
136 lines (121 loc) · 2.99 KB
/
Copy pathcart.ts
File metadata and controls
136 lines (121 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { prisma } from './db';
import type { CartWithItemsAndProducts } from '@/types';
import { getUserFromDb } from './user';
// export const addToCart = async (productId: string) => {
// const user = await getUserFromDb();
// try {
// const existingCart = await prisma.cart.findFirst({
// where: {
// userId: user?.id,
// },
// });
// if (existingCart) {
// const existingCartItem = await prisma.cartItem.findFirst({
// where: {
// cartId: existingCart?.id,
// productId,
// },
// });
// if (existingCartItem) {
// await prisma.cartItem.update({
// where: {
// id: existingCartItem.id,
// },
// data: {
// quantity: existingCartItem.quantity + 1,
// },
// });
// } else {
// await prisma.cartItem.create({
// data: {
// cartId: existingCart.id,
// productId,
// quantity: 1,
// },
// });
// }
// } else {
// const newCart = await prisma.cart.create({
// data: {
// userId: user?.id,
// cartItems: {
// create: {
// productId,
// quantity: 1,
// },
// },
// },
// });
// }
// } catch (error) {
// console.error(error);
// return null;
// }
// };
export const getCurrentCart =
async (): Promise<CartWithItemsAndProducts | null> => {
const user = await getUserFromDb();
try {
const carts = await prisma.cart.findMany({
where: {
userId: user?.id,
},
include: {
cartItems: {
include: {
product: true,
},
},
},
});
return carts[0];
} catch (error) {
console.error(error);
return null;
}
};
export const getCartTotal = (cart: CartWithItemsAndProducts): number => {
let total = 0;
for (const cartItem of cart.cartItems) {
total += cartItem.product.price * cartItem.quantity;
}
return total;
};
export const getCartData = async () => {
const cart = await getCurrentCart();
const cartTotal = cart ? getCartTotal(cart) : 0;
return {
data: {
cart,
cartTotal,
},
};
};
export const deleteCarts = async (userId: string | undefined) => {
const currentCart = await getCurrentCart();
try {
const deletedCartItems = await prisma.cartItem.deleteMany({
where: { cartId: currentCart?.id },
});
const deletedCarts = await prisma.cart.deleteMany({
where: { userId },
});
return {
deletedCarts,
deletedCartItems,
};
} catch (error) {
console.error(error);
return null;
}
};
export const getCartItemId = async (productId: string) => {
const currentCart = await getCurrentCart();
const cartItem = await prisma.cartItem.findFirst({
where: {
cartId: currentCart?.id,
productId,
},
});
return cartItem?.id;
};