Skip to content

Commit 6de7ffd

Browse files
committed
refactor: mejoramos los type para la carpeta modelo usando buenas practicas para tener una aplicación mas mantenible
1 parent 0c385ee commit 6de7ffd

File tree

4 files changed

+23
-26
lines changed

4 files changed

+23
-26
lines changed

src/models/cart.model.ts

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,44 @@
1-
import { type Product } from "./product.model";
1+
import { type Product } from "./product.model";
22

33
import type { CategoryVariant } from "./category.model";
44
import type { Cart as PrismaCart } from "@/../generated/prisma/client";
5+
import type { Nullable } from "./utils.model";
56

67
export type Cart = PrismaCart;
78

9+
type productInfo = Pick<
10+
Product,
11+
"id" | "title" | "imgSrc" | "alt" | "price" | "isOnSale"
12+
>;
13+
814
export type CartItem = {
915
id: number;
1016
cartId: number;
1117
productId: number;
12-
categoryVariantId: number | null;
18+
categoryVariantId: Nullable<number>;
1319
quantity: number;
14-
finalPrice: number; // ← number, no Decimal
20+
finalPrice: number;
1521
createdAt: Date;
1622
updatedAt: Date;
17-
// Campos adicionales transformados
18-
product: Pick<
19-
Product,
20-
"id" | "title" | "imgSrc" | "alt" | "price" | "isOnSale"
21-
>;
22-
categoryVariant?: CategoryVariant | null;
23+
product: productInfo;
24+
categoryVariant?: Nullable<CategoryVariant>;
2325
};
2426

2527
export interface CartItemInput {
2628
productId: Product["id"];
2729
quantity: number;
28-
categoryVariantId: number | null;
29-
variantInfo: string | null;
30+
categoryVariantId: Nullable<number>;
31+
variantInfo: Nullable<string>;
3032
title: Product["title"];
3133
price: Product["price"];
3234
imgSrc: Product["imgSrc"];
3335
}
3436

35-
// Tipo para representar un producto simplificado en el carrito
36-
37-
export type CartProductInfo = Pick<
38-
Product,
39-
"id" | "title" | "imgSrc" | "alt" | "price" | "isOnSale"
40-
>;
41-
4237
// Tipo para representar un item de carrito con su producto
4338
export type CartItemWithProduct = {
44-
product: CartProductInfo;
39+
product: productInfo;
4540
quantity: number;
46-
categoryVariantId: number | null;
41+
categoryVariantId: Nullable<number>;
4742
finalPrice: number;
4843
};
4944

src/models/category.model.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import type { Category as PrismaCategory } from "@/../generated/prisma/client";
2+
import type { CategoryVariant as PrismaCategoryVariant } from "@/../generated/prisma/client";
23

34
export const VALID_SLUGS = ["polos", "stickers", "tazas"] as const;
45

56
export type Category = PrismaCategory;
67

7-
export type CategoryVariant = {
8-
id: number;
9-
label: string;
10-
value: string;
8+
export type CategoryVariant = Omit<PrismaCategoryVariant, "priceModifier"> & {
119
priceModifier: number;
1210
};
1311

src/models/order.model.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type {
22
Order as PrismaOrder,
33
OrderItem as PrismaOrderItem,
44
} from "@/../generated/prisma/client";
5+
import type { Nullable } from "./utils.model";
56

67
export type OrderDetails = Pick<
78
PrismaOrder,
@@ -29,10 +30,10 @@ export type Order = Omit<PrismaOrder, "totalAmount"> & {
2930

3031
export interface OrderItemInput {
3132
productId: number;
32-
categoryVariantId?: number | null;
33+
categoryVariantId?: Nullable<number>;
3334
quantity: number;
3435
title: string;
35-
variantInfo?: string | null; // ← NUEVO
36+
variantInfo?: Nullable<string>;
3637
price: number;
3738
imgSrc: string;
3839
}

src/models/utils.model.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type Nullable<T> = T | null;
2+
export type Optional<T> = T | undefined;
3+
export type Maybe<T> = T | null | undefined;

0 commit comments

Comments
 (0)