Skip to content

Commit 6d35a94

Browse files
authored
Merge pull request #211 from codeableorg/orm_cart
refactor: migrate cart service to use Prisma client
2 parents 5f2821c + 2055d96 commit 6d35a94

File tree

8 files changed

+282
-331
lines changed

8 files changed

+282
-331
lines changed

src/models/cart.model.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
import { type Product } from "./product.model";
2-
import { type User } from "./user.model";
32

4-
export interface CartItem {
5-
id: number;
3+
import type {
4+
Cart as PrismaCart,
5+
CartItem as PrismaCartItem,
6+
} from "generated/prisma/client";
7+
8+
export type CartItem = PrismaCartItem & {
69
product: Pick<
710
Product,
811
"id" | "title" | "imgSrc" | "alt" | "price" | "isOnSale"
912
>;
10-
quantity: number;
11-
createdAt: string;
12-
updatedAt: string;
13-
}
13+
};
1414

15-
export interface Cart {
16-
id: number;
17-
userId: User["id"] | null;
18-
sessionCartId: string;
19-
items: CartItem[];
20-
createdAt: string;
21-
updatedAt: string;
22-
}
15+
export type Cart = PrismaCart;
2316

2417
export interface CartItemInput {
2518
productId: Product["id"];
@@ -28,3 +21,21 @@ export interface CartItemInput {
2821
price: Product["price"];
2922
imgSrc: Product["imgSrc"];
3023
}
24+
25+
// Tipo para representar un producto simplificado en el carrito
26+
27+
export type CartProductInfo = Pick<
28+
Product,
29+
"id" | "title" | "imgSrc" | "alt" | "price" | "isOnSale"
30+
>;
31+
32+
// Tipo para representar un item de carrito con su producto
33+
export type CartItemWithProduct = {
34+
product: CartProductInfo;
35+
quantity: number;
36+
};
37+
38+
// Tipo para el carrito con items y productos incluidos
39+
export type CartWithItems = Cart & {
40+
items: CartItem[];
41+
};

src/models/product.model.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,5 @@
1-
// import { type Category } from "./category.model";
2-
31
import type { Product as PrismaProduct } from "generated/prisma/client";
42

5-
// export interface Product {
6-
// id: number;
7-
// title: string;
8-
// imgSrc: string;
9-
// alt: string | null;
10-
// price: number;
11-
// description: string | null;
12-
// categoryId: number;
13-
// // categorySlug: Category["slug"];
14-
// isOnSale: boolean;
15-
// features: string[];
16-
// createdAt: string;
17-
// updatedAt: string;
18-
// }
19-
203
export type Product = Omit<PrismaProduct, "price"> & {
214
price: number;
225
};

src/repositories/cart.repository.ts

Lines changed: 0 additions & 224 deletions
This file was deleted.

src/routes/root/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
Separator,
1818
} from "@/components/ui";
1919
import { getCart } from "@/lib/cart";
20-
import type { Cart } from "@/models/cart.model";
20+
import type { CartWithItems } from "@/models/cart.model";
2121
import { getCurrentUser } from "@/services/auth.service";
2222
import { createRemoteItems } from "@/services/cart.service";
2323
import { commitSession, getSession } from "@/session.server";
@@ -45,7 +45,7 @@ export async function action({ request }: Route.ActionArgs) {
4545
export async function loader({ request }: Route.LoaderArgs) {
4646
const session = await getSession(request.headers.get("Cookie"));
4747
const sessionCartId = session.get("sessionCartId");
48-
let cart: Cart | null = null;
48+
let cart: CartWithItems | null = null;
4949

5050
// Obtenemos el usuario actual (autenticado o no)
5151
const user = await getCurrentUser(request);

0 commit comments

Comments
 (0)