From 8459c03c1926d80e021b26c7f34e6cd3e5f3a7c7 Mon Sep 17 00:00:00 2001 From: Lianyang1234 Date: Thu, 26 Mar 2026 19:26:55 -0300 Subject: [PATCH] nuevos cambios --- .../commerce/commerces/store.service.js | 39 ++++++++++++++++--- tests/e2e/stores.test.js | 39 +++++++++++++++++++ 2 files changed, 72 insertions(+), 6 deletions(-) diff --git a/src/modules/commerce/commerces/store.service.js b/src/modules/commerce/commerces/store.service.js index 1668091..bb968c3 100644 --- a/src/modules/commerce/commerces/store.service.js +++ b/src/modules/commerce/commerces/store.service.js @@ -826,25 +826,52 @@ export const filterStoreProductsService = async (id, filters, pagination) => { } const resolvedIsOffer = isOffer ?? is_offer; + let normalizedIsOfferFilter; if (resolvedIsOffer !== undefined && resolvedIsOffer !== null) { - whereConditions.is_offer = + normalizedIsOfferFilter = typeof resolvedIsOffer === "boolean" ? resolvedIsOffer : parseBooleanField(resolvedIsOffer, "isOffer"); + whereConditions.is_offer = normalizedIsOfferFilter; } const resolvedMinPrice = minPrice ?? price_min; const resolvedMaxPrice = maxPrice ?? price_max; + const effectivePriceRange = {}; if (resolvedMinPrice !== undefined && resolvedMinPrice !== null) { - whereConditions.price = { gte: Number(resolvedMinPrice) }; + effectivePriceRange.gte = Number(resolvedMinPrice); } if (resolvedMaxPrice !== undefined && resolvedMaxPrice !== null) { - whereConditions.price = { - ...whereConditions.price, - lte: Number(resolvedMaxPrice) - }; + effectivePriceRange.lte = Number(resolvedMaxPrice); + } + + if (Object.keys(effectivePriceRange).length > 0) { + const effectivePriceBranches = []; + + if (normalizedIsOfferFilter !== true) { + effectivePriceBranches.push({ + AND: [ + { is_offer: false }, + { price: effectivePriceRange } + ] + }); + } + + if (normalizedIsOfferFilter !== false) { + effectivePriceBranches.push({ + AND: [ + { is_offer: true }, + { offer_price: effectivePriceRange } + ] + }); + } + + whereConditions.OR = [ + ...(Array.isArray(whereConditions.OR) ? whereConditions.OR : []), + ...effectivePriceBranches + ]; } const [totalProducts, products] = await Promise.all([ diff --git a/tests/e2e/stores.test.js b/tests/e2e/stores.test.js index 8e02527..164db1c 100644 --- a/tests/e2e/stores.test.js +++ b/tests/e2e/stores.test.js @@ -167,6 +167,45 @@ describe("GET /api/commerces/products/filter/:id", () => { expect(Array.isArray(res.body.content)).toBe(true); }); + it("aplica el rango sobre price u offer_price segun el precio efectivo", async () => { + prisma.stores.findUnique.mockResolvedValue({ id_store: 1 }); + prisma.products.count.mockResolvedValue(1); + prisma.products.findMany.mockResolvedValue([ + { + ...mockProducts[0], + price: 20, + offer_price: 12, + is_offer: true + } + ]); + + const res = await request(app).get( + "/api/commerces/products/filter/1?price_min=10&price_max=15" + ); + + expect(res.status).toBe(200); + expect(prisma.products.findMany).toHaveBeenCalledWith( + expect.objectContaining({ + where: expect.objectContaining({ + OR: [ + { + AND: [ + { is_offer: false }, + { price: { gte: 10, lte: 15 } } + ] + }, + { + AND: [ + { is_offer: true }, + { offer_price: { gte: 10, lte: 15 } } + ] + } + ] + }) + }) + ); + }); + it("devuelve 404 cuando no hay productos con los filtros aplicados", async () => { prisma.stores.findUnique.mockResolvedValue({ id_store: 1 }); prisma.products.count.mockResolvedValue(0);