Skip to content

Commit 40f130c

Browse files
committed
refactor: update product model and related services to use single variant attribute and improve type definitions
1 parent e475948 commit 40f130c

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

src/lib/utils.tests.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export const createTestVariantAttributeValue = (
125125
price: 100, // ya es number
126126
createdAt: new Date(),
127127
updatedAt: new Date(),
128-
variantAttribute: [{ id: 1, name: "Talla", createdAt: new Date(), updatedAt: new Date() }],
128+
variantAttribute: { id: 1, name: "Talla", createdAt: new Date(), updatedAt: new Date() },
129129
...overrides,
130130
});
131131

@@ -151,13 +151,22 @@ export const createTestOrderItem = (
151151
({
152152
id: 1,
153153
orderId: 1,
154-
productId: 1,
154+
attributeValueId: 1,
155155
quantity: 1,
156156
title: "Test Product",
157157
price: 100,
158158
imgSrc: "test-image.jpg",
159159
createdAt: new Date(),
160160
updatedAt: new Date(),
161+
variantAttributeValue: {
162+
id: 1,
163+
attributeId: 1,
164+
productId: 1,
165+
value: "Default",
166+
price: new Decimal(100),
167+
createdAt: new Date(),
168+
updatedAt: new Date(),
169+
},
161170
...overrides,
162171
} satisfies OrderItem);
163172

@@ -167,7 +176,7 @@ export const createTestDBOrderItem = (
167176
({
168177
id: 1,
169178
orderId: 1,
170-
productId: 1,
179+
attributeValueId: 1,
171180
quantity: 1,
172181
title: "Test Product",
173182
price: new Decimal(100),

src/models/product.model.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ export type Product = PrismaProduct & {
88
variantAttributeValues?: VariantAttributeValueWithNumber[];
99
};
1010

11-
12-
1311
export type VariantAttributeValueWithNumber = Omit<PrismaVariantAttributeValue, "price"> & {
1412
price: number
15-
variantAttribute: VariantAttribute[]
16-
}
13+
variantAttribute: VariantAttribute
14+
}
15+
16+
export type ProductDTO = PrismaProduct & {
17+
variantAttributeValues: PrismaVariantAttributeValue[];
18+
};

src/services/chat-system-prompt.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { CartWithItems } from "@/models/cart.model";
22
import type { Category } from "@/models/category.model";
3-
import type { Product } from "@/models/product.model";
3+
import type { Product, VariantAttributeValueWithNumber } from "@/models/product.model";
44

55
interface SystemPromptConfig {
66
categories: Category[];
@@ -31,24 +31,25 @@ export function generateSystemPrompt({
3131
if (product.variantAttributeValues && product.variantAttributeValues.length > 0) {
3232
const variantType = product.variantAttributeValues[0]?.variantAttribute?.name;
3333

34-
if (variantType && variantType !== 'único') {
35-
switch (variantType) {
36-
case 'talla':
37-
const sizes = product.variantAttributeValues.map((v: any) => v.value).join(", ");
34+
switch (variantType) {
35+
case 'talla': {
36+
const sizes = product.variantAttributeValues.map((v: VariantAttributeValueWithNumber) => v.value).join(", ");
3837
variantDisplay = `\n- 👕 Tallas disponibles: ${sizes}`;
3938
break;
40-
case 'dimensión':
39+
}
40+
case 'dimensión': {
4141
const dimensions = product.variantAttributeValues
42-
.map((v: any) => `${v.value} (S/${v.price})`)
42+
.map((v: VariantAttributeValueWithNumber) => `${v.value} (S/${v.price})`)
4343
.join(", ");
4444
variantDisplay = `\n- 📐 Dimensiones: ${dimensions}`;
4545
break;
46-
default:
46+
}
47+
default: {
4748
const options = product.variantAttributeValues
48-
.map((v: any) => `${v.value} (S/${v.price})`)
49+
.map((v: VariantAttributeValueWithNumber) => `${v.value} (S/${v.price})`)
4950
.join(", ");
5051
variantDisplay = `\n- ⚙️ Opciones: ${options}`;
51-
}
52+
}
5253
}
5354
}
5455

src/services/product.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { prisma } from "@/db/prisma";
22
import type { Category } from "@/models/category.model";
3-
import type { Product } from "@/models/product.model";
3+
import type { Product, ProductDTO } from "@/models/product.model";
44
import type { VariantAttributeValue } from "@/models/variant-attribute.model";
55

66
import { getCategoryBySlug } from "./category.service";
77

8-
export const formattedProduct = (product: any): Product => {
8+
export const formattedProduct = (product: ProductDTO): Product => {
99
const { variantAttributeValues, ...rest } = product;
1010
const prices = variantAttributeValues.map((v: VariantAttributeValue) =>
1111
v.price.toNumber()

0 commit comments

Comments
 (0)