diff --git a/prisma/initial_data.ts b/prisma/initial_data.ts index 0520e9e..e8aee38 100644 --- a/prisma/initial_data.ts +++ b/prisma/initial_data.ts @@ -29,11 +29,16 @@ export const categories = [ }, ]; +export const variantAttributes = [ + { name: "no aplica" }, + { name: "talla" }, + { name: "dimensiones" }, +] + export const products = [ { title: "Polo React", imgSrc: `${imagesBaseUrl}/polos/polo-react.png`, - price: 20.0, description: "Viste tu pasión por React con estilo y comodidad en cada línea de código.", categoryId: 1, @@ -48,7 +53,6 @@ export const products = [ { title: "Polo JavaScript", imgSrc: `${imagesBaseUrl}/polos/polo-js.png`, - price: 20.0, description: "Deja que tu amor por JavaScript hable a través de cada hilo de este polo.", categoryId: 1, @@ -63,7 +67,6 @@ export const products = [ { title: "Polo Node.js", imgSrc: `${imagesBaseUrl}/polos/polo-node.png`, - price: 20.0, description: "Conéctate al estilo con este polo de Node.js, tan robusto como tu código.", categoryId: 1, @@ -78,7 +81,6 @@ export const products = [ { title: "Polo TypeScript", imgSrc: `${imagesBaseUrl}/polos/polo-ts.png`, - price: 20.0, description: "Tipa tu estilo con precisión: lleva tu pasión por TypeScript en cada hilo.", categoryId: 1, @@ -93,7 +95,6 @@ export const products = [ { title: "Polo Backend Developer", imgSrc: `${imagesBaseUrl}/polos/polo-backend.png`, - price: 25.0, description: "Domina el servidor con estilo: viste con orgullo tu título de Backend Developer.", categoryId: 1, @@ -108,7 +109,6 @@ export const products = [ { title: "Polo Frontend Developer", imgSrc: `${imagesBaseUrl}/polos/polo-frontend.png`, - price: 25.0, description: "Construye experiencias con estilo: luce con orgullo tu polo de Frontend Developer.", categoryId: 1, @@ -123,7 +123,6 @@ export const products = [ { title: "Polo Full-Stack Developer", imgSrc: `${imagesBaseUrl}/polos/polo-fullstack.png`, - price: 25.0, description: "Domina ambos mundos con estilo: lleva tu título de FullStack Developer en cada línea de tu look.", categoryId: 1, @@ -138,7 +137,6 @@ export const products = [ { title: "Polo It's A Feature", imgSrc: `${imagesBaseUrl}/polos/polo-feature.png`, - price: 15.0, description: "Cuando el bug se convierte en arte: lleva con orgullo tu polo 'It's a feature'.", categoryId: 1, @@ -153,7 +151,6 @@ export const products = [ { title: "Polo It Works On My Machine", imgSrc: `${imagesBaseUrl}/polos/polo-works.png`, - price: 15.0, description: "El clásico del desarrollador: presume tu confianza con 'It works on my machine'.", categoryId: 1, @@ -168,7 +165,6 @@ export const products = [ { title: "Sticker JavaScript", imgSrc: `${imagesBaseUrl}/stickers/sticker-js.png`, - price: 2.99, description: "Muestra tu amor por JavaScript con este elegante sticker clásico.", categoryId: 3, @@ -183,7 +179,6 @@ export const products = [ { title: "Sticker React", imgSrc: `${imagesBaseUrl}/stickers/sticker-react.png`, - price: 2.49, description: "Decora tus dispositivos con el icónico átomo giratorio de React.", categoryId: 3, @@ -198,7 +193,6 @@ export const products = [ { title: "Sticker Git", imgSrc: `${imagesBaseUrl}/stickers/sticker-git.png`, - price: 3.99, description: "Visualiza el poder del control de versiones con este sticker de Git.", categoryId: 3, @@ -213,7 +207,6 @@ export const products = [ { title: "Sticker Docker", imgSrc: `${imagesBaseUrl}/stickers/sticker-docker.png`, - price: 2.99, description: "La adorable ballena de Docker llevando contenedores en un sticker único.", categoryId: 3, @@ -228,7 +221,6 @@ export const products = [ { title: "Sticker Linux", imgSrc: `${imagesBaseUrl}/stickers/sticker-linux.png`, - price: 2.49, description: "El querido pingüino Tux, mascota oficial de Linux, en formato sticker.", categoryId: 3, @@ -243,7 +235,6 @@ export const products = [ { title: "Sticker VS Code", imgSrc: `${imagesBaseUrl}/stickers/sticker-vscode.png`, - price: 2.49, description: "El elegante logo del editor favorito de los desarrolladores.", categoryId: 3, isOnSale: false, @@ -257,7 +248,6 @@ export const products = [ { title: "Sticker GitHub", imgSrc: `${imagesBaseUrl}/stickers/sticker-github.png`, - price: 2.99, description: "El alojamiento de repositorios más popular en un sticker de alta calidad.", categoryId: 3, @@ -272,7 +262,6 @@ export const products = [ { title: "Sticker HTML", imgSrc: `${imagesBaseUrl}/stickers/sticker-html.png`, - price: 2.99, description: "El escudo naranja de HTML5, el lenguaje que estructura la web.", categoryId: 3, @@ -287,7 +276,6 @@ export const products = [ { title: "Taza JavaScript", imgSrc: `${imagesBaseUrl}/tazas/taza-js.png`, - price: 14.99, description: "Disfruta tu café mientras programas con el logo de JavaScript.", categoryId: 2, @@ -302,7 +290,6 @@ export const products = [ { title: "Taza React", imgSrc: `${imagesBaseUrl}/tazas/taza-react.png`, - price: 13.99, description: "Una taza que hace render de tu bebida favorita con estilo React.", categoryId: 2, @@ -317,7 +304,6 @@ export const products = [ { title: "Taza Git", imgSrc: `${imagesBaseUrl}/tazas/taza-git.png`, - price: 12.99, description: "Commit a tu rutina diaria de café con esta taza de Git.", categoryId: 2, isOnSale: false, @@ -331,7 +317,6 @@ export const products = [ { title: "Taza SQL", imgSrc: `${imagesBaseUrl}/tazas/taza-sql.png`, - price: 15.99, description: "Tu amor por los lenguajes estructurados en una taza de SQL.", categoryId: 2, isOnSale: false, @@ -345,7 +330,6 @@ export const products = [ { title: "Taza Linux", imgSrc: `${imagesBaseUrl}/tazas/taza-linux.png`, - price: 13.99, description: "Toma tu café con la libertad que solo Linux puede ofrecer.", categoryId: 2, isOnSale: false, @@ -359,7 +343,6 @@ export const products = [ { title: "Taza GitHub", imgSrc: `${imagesBaseUrl}/tazas/taza-github.png`, - price: 14.99, description: "Colabora con tu café en esta taza con el logo de GitHub.", categoryId: 2, isOnSale: false, @@ -371,3 +354,83 @@ export const products = [ ], }, ]; + +export const variantAttributeValues = [ + // --- POLOS (talla: S, M, L) --- + { attributeId: 1, productId: 1, value: "S", price: 20.0 }, + { attributeId: 1, productId: 1, value: "M", price: 20.0 }, + { attributeId: 1, productId: 1, value: "L", price: 20.0 }, + + { attributeId: 1, productId: 2, value: "S", price: 20.0 }, + { attributeId: 1, productId: 2, value: "M", price: 20.0 }, + { attributeId: 1, productId: 2, value: "L", price: 20.0 }, + + { attributeId: 1, productId: 3, value: "S", price: 20.0 }, + { attributeId: 1, productId: 3, value: "M", price: 20.0 }, + { attributeId: 1, productId: 3, value: "L", price: 20.0 }, + + { attributeId: 1, productId: 4, value: "S", price: 20.0 }, + { attributeId: 1, productId: 4, value: "M", price: 20.0 }, + { attributeId: 1, productId: 4, value: "L", price: 20.0 }, + + { attributeId: 1, productId: 5, value: "S", price: 25.0 }, + { attributeId: 1, productId: 5, value: "M", price: 25.0 }, + { attributeId: 1, productId: 5, value: "L", price: 25.0 }, + + { attributeId: 1, productId: 6, value: "S", price: 25.0 }, + { attributeId: 1, productId: 6, value: "M", price: 25.0 }, + { attributeId: 1, productId: 6, value: "L", price: 25.0 }, + + { attributeId: 1, productId: 7, value: "S", price: 25.0 }, + { attributeId: 1, productId: 7, value: "M", price: 25.0 }, + { attributeId: 1, productId: 7, value: "L", price: 25.0 }, + + { attributeId: 1, productId: 8, value: "S", price: 15.0 }, + { attributeId: 1, productId: 8, value: "M", price: 15.0 }, + { attributeId: 1, productId: 8, value: "L", price: 15.0 }, + + { attributeId: 1, productId: 9, value: "S", price: 15.0 }, + { attributeId: 1, productId: 9, value: "M", price: 15.0 }, + { attributeId: 1, productId: 9, value: "L", price: 15.0 }, + + // --- STICKERS (dimensiones: 3x3, 6x6, 9x9) --- + { attributeId: 2, productId: 10, value: "3x3", price: 2.99 }, + { attributeId: 2, productId: 10, value: "6x6", price: 3.99 }, + { attributeId: 2, productId: 10, value: "9x9", price: 4.99 }, + + { attributeId: 2, productId: 11, value: "3x3", price: 2.49 }, + { attributeId: 2, productId: 11, value: "6x6", price: 3.49 }, + { attributeId: 2, productId: 11, value: "9x9", price: 4.49 }, + + { attributeId: 2, productId: 12, value: "3x3", price: 3.99 }, + { attributeId: 2, productId: 12, value: "6x6", price: 4.99 }, + { attributeId: 2, productId: 12, value: "9x9", price: 5.99 }, + + { attributeId: 2, productId: 13, value: "3x3", price: 2.99 }, + { attributeId: 2, productId: 13, value: "6x6", price: 3.99 }, + { attributeId: 2, productId: 13, value: "9x9", price: 4.99 }, + + { attributeId: 2, productId: 14, value: "3x3", price: 2.49 }, + { attributeId: 2, productId: 14, value: "6x6", price: 3.49 }, + { attributeId: 2, productId: 14, value: "9x9", price: 4.49 }, + + { attributeId: 2, productId: 15, value: "3x3", price: 2.49 }, + { attributeId: 2, productId: 15, value: "6x6", price: 3.49 }, + { attributeId: 2, productId: 15, value: "9x9", price: 4.49 }, + + { attributeId: 2, productId: 16, value: "3x3", price: 2.99 }, + { attributeId: 2, productId: 16, value: "6x6", price: 3.99 }, + { attributeId: 2, productId: 16, value: "9x9", price: 4.99 }, + + { attributeId: 2, productId: 17, value: "3x3", price: 2.99 }, + { attributeId: 2, productId: 17, value: "6x6", price: 3.99 }, + { attributeId: 2, productId: 17, value: "9x9", price: .99 }, + + // --- TAZAS (no aplica: Único) --- + { attributeId: 3, productId: 18, value: "Único", price: 14.99 }, + { attributeId: 3, productId: 19, value: "Único", price: 13.99 }, + { attributeId: 3, productId: 20, value: "Único", price: 12.99 }, + { attributeId: 3, productId: 21, value: "Único", price: 15.99 }, + { attributeId: 3, productId: 22, value: "Único", price: 13.99 }, + { attributeId: 3, productId: 23, value: "Único", price: 14.99 }, +]; \ No newline at end of file diff --git a/prisma/migrations/20250820183901_add_tables_variants_products/migration.sql b/prisma/migrations/20250820183901_add_tables_variants_products/migration.sql new file mode 100644 index 0000000..f64a02c --- /dev/null +++ b/prisma/migrations/20250820183901_add_tables_variants_products/migration.sql @@ -0,0 +1,43 @@ +/* + Warnings: + + - You are about to drop the column `price` on the `products` table. All the data in the column will be lost. + +*/ +-- AlterTable +ALTER TABLE "products" DROP COLUMN "price"; + +-- CreateTable +CREATE TABLE "variants_attributes" ( + "id" SERIAL NOT NULL, + "name" TEXT NOT NULL, + "created_at" TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "variants_attributes_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "variants_attributes_values" ( + "id" SERIAL NOT NULL, + "attribute_id" INTEGER NOT NULL, + "product_id" INTEGER NOT NULL, + "value" TEXT NOT NULL, + "price" DECIMAL(10,2) NOT NULL, + "created_at" TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "variants_attributes_values_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "variants_attributes_name_key" ON "variants_attributes"("name"); + +-- CreateIndex +CREATE UNIQUE INDEX "variants_attributes_values_attribute_id_product_id_value_key" ON "variants_attributes_values"("attribute_id", "product_id", "value"); + +-- AddForeignKey +ALTER TABLE "variants_attributes_values" ADD CONSTRAINT "variants_attributes_values_attribute_id_fkey" FOREIGN KEY ("attribute_id") REFERENCES "variants_attributes"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "variants_attributes_values" ADD CONSTRAINT "variants_attributes_values_product_id_fkey" FOREIGN KEY ("product_id") REFERENCES "products"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/migrations/20250822015032_update_table_cart_with_attribute_id/migration.sql b/prisma/migrations/20250822015032_update_table_cart_with_attribute_id/migration.sql new file mode 100644 index 0000000..aab9f1b --- /dev/null +++ b/prisma/migrations/20250822015032_update_table_cart_with_attribute_id/migration.sql @@ -0,0 +1,23 @@ +/* + Warnings: + + - You are about to drop the column `product_id` on the `cart_items` table. All the data in the column will be lost. + - A unique constraint covering the columns `[cart_id,attribute_value_id]` on the table `cart_items` will be added. If there are existing duplicate values, this will fail. + - Added the required column `attribute_value_id` to the `cart_items` table without a default value. This is not possible if the table is not empty. + +*/ +-- DropForeignKey +ALTER TABLE "cart_items" DROP CONSTRAINT "cart_items_product_id_fkey"; + +-- DropIndex +DROP INDEX "cart_items_cart_id_product_id_key"; + +-- AlterTable +ALTER TABLE "cart_items" DROP COLUMN "product_id", +ADD COLUMN "attribute_value_id" INTEGER NOT NULL; + +-- CreateIndex +CREATE UNIQUE INDEX "cart_items_cart_id_attribute_value_id_key" ON "cart_items"("cart_id", "attribute_value_id"); + +-- AddForeignKey +ALTER TABLE "cart_items" ADD CONSTRAINT "cart_items_attribute_value_id_fkey" FOREIGN KEY ("attribute_value_id") REFERENCES "variants_attributes_values"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index e0f992b..b8df94a 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -55,7 +55,6 @@ model Product { title String imgSrc String @map("img_src") alt String? - price Decimal @db.Decimal(10, 2) description String? categoryId Int? @map("category_id") isOnSale Boolean @default(false) @map("is_on_sale") @@ -63,13 +62,43 @@ model Product { createdAt DateTime @default(now()) @map("created_at") @db.Timestamp(0) updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamp(0) - category Category? @relation(fields: [categoryId], references: [id], onDelete: SetNull) - cartItems CartItem[] - orderItems OrderItem[] + category Category? @relation(fields: [categoryId], references: [id], onDelete: SetNull) + + orderItems OrderItem[] + variantAttributeValues VariantAttributeValue[] @@map("products") } +model VariantAttribute { + id Int @id @default(autoincrement()) + name String @unique + createdAt DateTime @default(now()) @map("created_at") @db.Timestamp(0) + updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamp(0) + + variantsAttributeValue VariantAttributeValue[] + + @@map("variants_attributes") +} + +model VariantAttributeValue { + id Int @id @default(autoincrement()) + attributeId Int @map("attribute_id") + productId Int @map("product_id") + value String + price Decimal @db.Decimal(10, 2) + createdAt DateTime @default(now()) @map("created_at") @db.Timestamp(0) + updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamp(0) + + variantAttribute VariantAttribute @relation(fields: [attributeId], references: [id]) + product Product @relation(fields: [productId], references: [id]) + + CartItem CartItem[] + + @@unique([attributeId, productId, value], name: "unique_attribute_product_value") + @@map("variants_attributes_values") +} + model Cart { id Int @id @default(autoincrement()) sessionCartId String @unique @default(dbgenerated("gen_random_uuid()")) @map("session_cart_id") @db.Uuid @@ -86,15 +115,15 @@ model Cart { model CartItem { id Int @id @default(autoincrement()) cartId Int @map("cart_id") - productId Int @map("product_id") + attributeValueId Int @map("attribute_value_id") quantity Int createdAt DateTime @default(now()) @map("created_at") @db.Timestamp(0) updatedAt DateTime @default(now()) @map("updated_at") @db.Timestamp(0) cart Cart @relation(fields: [cartId], references: [id], onDelete: Cascade) - product Product @relation(fields: [productId], references: [id], onDelete: Cascade) + variantAttributeValue VariantAttributeValue @relation(fields: [attributeValueId], references: [id], onDelete: Cascade) - @@unique([cartId, productId], name: "unique_cart_item") + @@unique([cartId, attributeValueId], name: "unique_cart_item") @@map("cart_items") } diff --git a/prisma/seed.ts b/prisma/seed.ts index 106da46..4a2b3e5 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -1,4 +1,4 @@ -import { categories, products } from "./initial_data"; +import { categories, products, variantAttributes, variantAttributeValues } from "./initial_data"; import { PrismaClient } from "../generated/prisma/client"; const prisma = new PrismaClient(); @@ -9,10 +9,23 @@ async function seedDb() { }); console.log("1. Categories successfully inserted"); + await prisma.variantAttribute.createMany({ + data: variantAttributes, + }) + console.log("2. Variant Attributes successfully inserted"); + await prisma.product.createMany({ data: products, + }); - console.log("2. Products successfully inserted"); + console.log("3. Products successfully inserted"); + + await prisma.variantAttributeValue.createMany({ + data: variantAttributeValues, + }) + + console.log("4. Variant Attribute Values successfully inserted"); + } seedDb() diff --git a/src/models/cart.model.ts b/src/models/cart.model.ts index ad4206a..8550190 100644 --- a/src/models/cart.model.ts +++ b/src/models/cart.model.ts @@ -33,6 +33,7 @@ export type CartProductInfo = Pick< export type CartItemWithProduct = { product: CartProductInfo; quantity: number; + attributeId: number; }; // Tipo para el carrito con items y productos incluidos diff --git a/src/services/cart.service.ts b/src/services/cart.service.ts index f742706..d3a96e6 100644 --- a/src/services/cart.service.ts +++ b/src/services/cart.service.ts @@ -20,18 +20,22 @@ async function getCart( if (!whereCondition) return null; const data = await prisma.cart.findFirst({ + // se modifico esta funcion where: whereCondition, include: { items: { include: { - product: { - select: { - id: true, - title: true, - imgSrc: true, - alt: true, - price: true, - isOnSale: true, + variantAttributeValue: { + include: { + product: { + select: { + id: true, + title: true, + imgSrc: true, + alt: true, + isOnSale: true, + }, + }, }, }, }, @@ -49,9 +53,10 @@ async function getCart( items: data.items.map((item) => ({ ...item, product: { - ...item.product, - price: item.product.price.toNumber(), + ...item.variantAttributeValue.product, + price: item.variantAttributeValue.price.toNumber(), }, + variantAttributeValue: item.variantAttributeValue, })), }; } @@ -82,14 +87,17 @@ export async function getOrCreateCart( include: { items: { include: { - product: { - select: { - id: true, - title: true, - imgSrc: true, - alt: true, - price: true, - isOnSale: true, + variantAttributeValue: { + include: { + product: { + select: { + id: true, + title: true, + imgSrc: true, + alt: true, + isOnSale: true, + }, + }, }, }, }, @@ -104,9 +112,10 @@ export async function getOrCreateCart( items: newCart.items.map((item) => ({ ...item, product: { - ...item.product, - price: item.product.price.toNumber(), + ...item.variantAttributeValue.product, + price: item.variantAttributeValue.price.toNumber(), }, + variantAttributeValue: item.variantAttributeValue, })), }; } @@ -130,7 +139,7 @@ export async function createRemoteItems( await prisma.cartItem.createMany({ data: items.map((item) => ({ cartId: cart.id, - productId: item.product.id, + attributeValueId: item.attributeId, // modificar quantity: item.quantity, })), }); @@ -146,12 +155,14 @@ export async function createRemoteItems( export async function alterQuantityCartItem( userId: User["id"] | undefined, sessionCartId: string | undefined, - productId: number, + attributeId: number, quantity: number = 1 ): Promise { const cart = await getOrCreateCart(userId, sessionCartId); - const existingItem = cart.items.find((item) => item.product.id === productId); + const existingItem = cart.items.find( + (item) => item.attributeValueId === attributeId + ); if (existingItem) { const newQuantity = existingItem.quantity + quantity; @@ -170,7 +181,7 @@ export async function alterQuantityCartItem( await prisma.cartItem.create({ data: { cartId: cart.id, - productId, + attributeValueId: attributeId, quantity, }, }); @@ -236,14 +247,17 @@ export async function linkCartToUser( include: { items: { include: { - product: { - select: { - id: true, - title: true, - imgSrc: true, - alt: true, - price: true, - isOnSale: true, + variantAttributeValue: { + include: { + product: { + select: { + id: true, + title: true, + imgSrc: true, + alt: true, + isOnSale: true, + }, + }, }, }, }, @@ -258,9 +272,10 @@ export async function linkCartToUser( items: updatedCart.items.map((item) => ({ ...item, product: { - ...item.product, - price: item.product.price.toNumber(), + ...item.variantAttributeValue.product, + price: item.variantAttributeValue.price.toNumber(), }, + variantAttributeValue: item.variantAttributeValue, })), }; } @@ -285,41 +300,46 @@ export async function mergeGuestCartWithUserCart( include: { items: { include: { - product: { - select: { - id: true, - title: true, - imgSrc: true, - alt: true, - price: true, - isOnSale: true, + variantAttributeValue: { + include: { + product: { + select: { + id: true, + title: true, + imgSrc: true, + alt: true, + isOnSale: true, + }, + }, }, }, }, }, }, }); + return { ...updatedCart, items: updatedCart.items.map((item) => ({ ...item, product: { - ...item.product, - price: item.product.price.toNumber(), + ...item.variantAttributeValue.product, + price: item.variantAttributeValue.price.toNumber(), }, + variantAttributeValue: item.variantAttributeValue, })), }; } // Obtener productos duplicados para eliminarlos del carrito del usuario - const guestProductIds = guestCart.items.map((item) => item.productId); + const guestAttributeValueIds = guestCart.items.map((item) => item.attributeValueId); // Eliminar productos del carrito usuario que también existan en el carrito invitado await prisma.cartItem.deleteMany({ where: { cartId: userCart.id, - productId: { - in: guestProductIds, + attributeValueId: { + in: guestAttributeValueIds, }, }, }); @@ -328,7 +348,7 @@ export async function mergeGuestCartWithUserCart( await prisma.cartItem.createMany({ data: guestCart.items.map((item) => ({ cartId: userCart.id, - productId: item.productId, + attributeValueId: item.attributeValueId, quantity: item.quantity, })), }); @@ -341,3 +361,4 @@ export async function mergeGuestCartWithUserCart( // Devolver el carrito actualizado del usuario return await getCart(userId); } +