From 49a132976ded4d094a5129029743f73a020ecf04 Mon Sep 17 00:00:00 2001 From: Zakaria El Asri <33696020+zakariaelas@users.noreply.github.com> Date: Tue, 21 Sep 2021 10:15:33 +0100 Subject: [PATCH] fix: ILIKE operator not supported in sqlite (#393) * fix: replace ILIKE operator with ILike function where possible * remove: unused import * fix: remove alias from test case as it is not needed * fix: product variant query * add: integration tests + fallback to original query for all queries searching on the display_id field * remove: console.log --- .../api/__tests__/admin/customer.js | 100 +-- .../api/__tests__/admin/discount.js | 61 ++ .../api/__tests__/admin/gift-cards.js | 180 ++++- .../api/__tests__/admin/order.js | 687 +++++++++--------- .../api/__tests__/admin/variant.js | 153 ++++ .../medusa/src/services/__tests__/discount.js | 3 - packages/medusa/src/services/customer.js | 18 +- packages/medusa/src/services/discount.js | 8 +- packages/medusa/src/services/draft-order.js | 2 +- packages/medusa/src/services/gift-card.js | 7 +- packages/medusa/src/services/order.js | 7 +- .../medusa/src/services/product-variant.js | 15 +- 12 files changed, 793 insertions(+), 448 deletions(-) create mode 100644 integration-tests/api/__tests__/admin/variant.js diff --git a/integration-tests/api/__tests__/admin/customer.js b/integration-tests/api/__tests__/admin/customer.js index c208e3086f..1458311eff 100644 --- a/integration-tests/api/__tests__/admin/customer.js +++ b/integration-tests/api/__tests__/admin/customer.js @@ -1,50 +1,50 @@ -const { dropDatabase } = require("pg-god"); -const path = require("path"); +const { dropDatabase } = require("pg-god") +const path = require("path") -const setupServer = require("../../../helpers/setup-server"); -const { useApi } = require("../../../helpers/use-api"); -const { useDb, initDb } = require("../../../helpers/use-db"); +const setupServer = require("../../../helpers/setup-server") +const { useApi } = require("../../../helpers/use-api") +const { useDb, initDb } = require("../../../helpers/use-db") -const customerSeeder = require("../../helpers/customer-seeder"); -const adminSeeder = require("../../helpers/admin-seeder"); +const customerSeeder = require("../../helpers/customer-seeder") +const adminSeeder = require("../../helpers/admin-seeder") -jest.setTimeout(30000); +jest.setTimeout(30000) describe("/admin/customers", () => { - let medusaProcess; - let dbConnection; + let medusaProcess + let dbConnection beforeAll(async () => { - const cwd = path.resolve(path.join(__dirname, "..", "..")); - dbConnection = await initDb({ cwd }); - medusaProcess = await setupServer({ cwd }); - }); + const cwd = path.resolve(path.join(__dirname, "..", "..")) + dbConnection = await initDb({ cwd }) + medusaProcess = await setupServer({ cwd }) + }) afterAll(async () => { - const db = useDb(); - await db.shutdown(); + const db = useDb() + await db.shutdown() - medusaProcess.kill(); - }); + medusaProcess.kill() + }) describe("GET /admin/customers", () => { beforeEach(async () => { try { - await adminSeeder(dbConnection); - await customerSeeder(dbConnection); + await adminSeeder(dbConnection) + await customerSeeder(dbConnection) } catch (err) { - console.log(err); - throw err; + console.log(err) + throw err } - }); + }) afterEach(async () => { - const db = useDb(); - await db.teardown(); - }); + const db = useDb() + await db.teardown() + }) it("lists customers and query count", async () => { - const api = useApi(); + const api = useApi() const response = await api .get("/admin/customers", { @@ -53,11 +53,11 @@ describe("/admin/customers", () => { }, }) .catch((err) => { - console.log(err); - }); + console.log(err) + }) - expect(response.status).toEqual(200); - expect(response.data.count).toEqual(3); + expect(response.status).toEqual(200) + expect(response.data.count).toEqual(3) expect(response.data.customers).toEqual( expect.arrayContaining([ expect.objectContaining({ @@ -70,24 +70,24 @@ describe("/admin/customers", () => { id: "test-customer-3", }), ]) - ); - }); + ) + }) it("lists customers with specific query", async () => { - const api = useApi(); + const api = useApi() const response = await api - .get("/admin/customers?q=test2@email.com", { + .get("/admin/customers?q=est2@", { headers: { Authorization: "Bearer test_token", }, }) .catch((err) => { - console.log(err); - }); + console.log(err) + }) - expect(response.status).toEqual(200); - expect(response.data.count).toEqual(1); + expect(response.status).toEqual(200) + expect(response.data.count).toEqual(1) expect(response.data.customers).toEqual( expect.arrayContaining([ expect.objectContaining({ @@ -95,11 +95,11 @@ describe("/admin/customers", () => { email: "test2@email.com", }), ]) - ); - }); + ) + }) it("lists customers with expand query", async () => { - const api = useApi(); + const api = useApi() const response = await api .get("/admin/customers?q=test1@email.com&expand=shipping_addresses", { @@ -108,11 +108,11 @@ describe("/admin/customers", () => { }, }) .catch((err) => { - console.log(err); - }); + console.log(err) + }) - expect(response.status).toEqual(200); - expect(response.data.count).toEqual(1); + expect(response.status).toEqual(200) + expect(response.data.count).toEqual(1) expect(response.data.customers).toEqual( expect.arrayContaining([ expect.objectContaining({ @@ -126,7 +126,7 @@ describe("/admin/customers", () => { ]), }), ]) - ); - }); - }); -}); + ) + }) + }) +}) diff --git a/integration-tests/api/__tests__/admin/discount.js b/integration-tests/api/__tests__/admin/discount.js index a731cec416..cbc124ce53 100644 --- a/integration-tests/api/__tests__/admin/discount.js +++ b/integration-tests/api/__tests__/admin/discount.js @@ -24,6 +24,67 @@ describe("/admin/discounts", () => { medusaProcess.kill() }) + describe("GET /admin/discounts", () => { + beforeEach(async () => { + const manager = dbConnection.manager + try { + await adminSeeder(dbConnection) + await manager.insert(DiscountRule, { + id: "test-discount-rule", + description: "Test discount rule", + type: "percentage", + value: 10, + allocation: "total", + }) + await manager.insert(Discount, { + id: "test-discount", + code: "TESTING", + rule_id: "test-discount-rule", + is_dynamic: false, + is_disabled: false, + }) + await manager.insert(Discount, { + id: "messi-discount", + code: "BARCA100", + rule_id: "test-discount-rule", + is_dynamic: false, + is_disabled: false, + }) + } catch (err) { + throw err + } + }) + + afterEach(async () => { + const db = useDb() + await db.teardown() + }) + + it("should list discounts that match a specific query in a case insensitive manner", async () => { + const api = useApi() + + const response = await api + .get("/admin/discounts?q=barca", { + headers: { + Authorization: "Bearer test_token", + }, + }) + .catch((err) => { + console.log(err) + }) + expect(response.status).toEqual(200) + expect(response.data.count).toEqual(1) + expect(response.data.discounts).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + id: "messi-discount", + code: "BARCA100", + }), + ]) + ) + }) + }) + describe("POST /admin/discounts", () => { beforeEach(async () => { try { diff --git a/integration-tests/api/__tests__/admin/gift-cards.js b/integration-tests/api/__tests__/admin/gift-cards.js index 5b71d5bc5c..219e8b54d1 100644 --- a/integration-tests/api/__tests__/admin/gift-cards.js +++ b/integration-tests/api/__tests__/admin/gift-cards.js @@ -1,54 +1,162 @@ -const path = require("path"); -const { Region } = require("@medusajs/medusa"); +const path = require("path") +const { Region, GiftCard } = require("@medusajs/medusa") -const setupServer = require("../../../helpers/setup-server"); -const { useApi } = require("../../../helpers/use-api"); -const { initDb, useDb } = require("../../../helpers/use-db"); -const adminSeeder = require("../../helpers/admin-seeder"); +const setupServer = require("../../../helpers/setup-server") +const { useApi } = require("../../../helpers/use-api") +const { initDb, useDb } = require("../../../helpers/use-db") +const adminSeeder = require("../../helpers/admin-seeder") -jest.setTimeout(30000); +jest.setTimeout(30000) describe("/admin/gift-cards", () => { - let medusaProcess; - let dbConnection; + let medusaProcess + let dbConnection beforeAll(async () => { - const cwd = path.resolve(path.join(__dirname, "..", "..")); - dbConnection = await initDb({ cwd }); - medusaProcess = await setupServer({ cwd }); - }); + const cwd = path.resolve(path.join(__dirname, "..", "..")) + dbConnection = await initDb({ cwd }) + medusaProcess = await setupServer({ cwd }) + }) afterAll(async () => { - const db = useDb(); - await db.shutdown(); + const db = useDb() + await db.shutdown() - medusaProcess.kill(); - }); + medusaProcess.kill() + }) + + describe("GET /admin/gift-cards", () => { + beforeEach(async () => { + const manager = dbConnection.manager + try { + await adminSeeder(dbConnection) + await manager.insert(Region, { + id: "test-region", + name: "Test Region", + currency_code: "usd", + tax_rate: 0, + }) + await manager.insert(GiftCard, { + id: "gift_test", + code: "GC_TEST", + value: 20000, + balance: 20000, + region_id: "test-region", + }) + await manager.insert(GiftCard, { + id: "another_gift_test", + code: "CARD_TEST", + value: 200000, + balance: 200000, + region_id: "test-region", + }) + } catch (err) { + console.log(err) + throw err + } + }) + + afterEach(async () => { + const db = useDb() + await db.teardown() + }) + + it("lists gift cards and query count", async () => { + const api = useApi() + + const response = await api + .get("/admin/gift-cards", { + headers: { + Authorization: "Bearer test_token", + }, + }) + .catch((err) => { + console.log(err) + }) + + expect(response.status).toEqual(200) + expect(response.data.gift_cards).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + id: "gift_test", + code: "GC_TEST", + }), + expect.objectContaining({ + id: "another_gift_test", + code: "CARD_TEST", + }), + ]) + ) + }) + + it("lists gift cards with specific query", async () => { + const api = useApi() + + const response = await api + .get("/admin/gift-cards?q=gc", { + headers: { + Authorization: "Bearer test_token", + }, + }) + .catch((err) => { + console.log(err) + }) + + expect(response.status).toEqual(200) + expect(response.data.gift_cards.length).toEqual(1) + expect(response.data.gift_cards).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + id: "gift_test", + code: "GC_TEST", + }), + ]) + ) + }) + + it("lists no gift cards on query for non-existing gift card code", async () => { + const api = useApi() + + const response = await api + .get("/admin/gift-cards?q=bla", { + headers: { + Authorization: "Bearer test_token", + }, + }) + .catch((err) => { + console.log(err) + }) + + expect(response.status).toEqual(200) + expect(response.data.gift_cards.length).toEqual(0) + expect(response.data.gift_cards).toEqual([]) + }) + }) describe("POST /admin/gift-cards", () => { beforeEach(async () => { - const manager = dbConnection.manager; + const manager = dbConnection.manager try { - await adminSeeder(dbConnection); + await adminSeeder(dbConnection) await manager.insert(Region, { id: "region", name: "Test Region", currency_code: "usd", tax_rate: 0, - }); + }) } catch (err) { - console.log(err); - throw err; + console.log(err) + throw err } - }); + }) afterEach(async () => { - const db = useDb(); - await db.teardown(); - }); + const db = useDb() + await db.teardown() + }) it("creates a gift card", async () => { - const api = useApi(); + const api = useApi() const response = await api .post( @@ -64,13 +172,13 @@ describe("/admin/gift-cards", () => { } ) .catch((err) => { - console.log(err); - }); + console.log(err) + }) - expect(response.status).toEqual(200); - expect(response.data.gift_card.value).toEqual(1000); - expect(response.data.gift_card.balance).toEqual(1000); - expect(response.data.gift_card.region_id).toEqual("region"); - }); - }); -}); + expect(response.status).toEqual(200) + expect(response.data.gift_card.value).toEqual(1000) + expect(response.data.gift_card.balance).toEqual(1000) + expect(response.data.gift_card.region_id).toEqual("region") + }) + }) +}) diff --git a/integration-tests/api/__tests__/admin/order.js b/integration-tests/api/__tests__/admin/order.js index 863b151551..251578cca0 100644 --- a/integration-tests/api/__tests__/admin/order.js +++ b/integration-tests/api/__tests__/admin/order.js @@ -1,64 +1,64 @@ -const path = require("path"); +const path = require("path") const { ReturnReason, Order, LineItem, ProductVariant, -} = require("@medusajs/medusa"); +} = require("@medusajs/medusa") -const setupServer = require("../../../helpers/setup-server"); -const { useApi } = require("../../../helpers/use-api"); -const { initDb, useDb } = require("../../../helpers/use-db"); +const setupServer = require("../../../helpers/setup-server") +const { useApi } = require("../../../helpers/use-api") +const { initDb, useDb } = require("../../../helpers/use-db") -const orderSeeder = require("../../helpers/order-seeder"); -const swapSeeder = require("../../helpers/swap-seeder"); -const adminSeeder = require("../../helpers/admin-seeder"); -const claimSeeder = require("../../helpers/claim-seeder"); +const orderSeeder = require("../../helpers/order-seeder") +const swapSeeder = require("../../helpers/swap-seeder") +const adminSeeder = require("../../helpers/admin-seeder") +const claimSeeder = require("../../helpers/claim-seeder") const { expectPostCallToReturn, expectAllPostCallsToReturn, callGet, partial, -} = require("../../helpers/call-helpers"); +} = require("../../helpers/call-helpers") -jest.setTimeout(30000); +jest.setTimeout(30000) describe("/admin/orders", () => { - let medusaProcess; - let dbConnection; + let medusaProcess + let dbConnection beforeAll(async () => { - const cwd = path.resolve(path.join(__dirname, "..", "..")); - dbConnection = await initDb({ cwd }); - medusaProcess = await setupServer({ cwd }); - }); + const cwd = path.resolve(path.join(__dirname, "..", "..")) + dbConnection = await initDb({ cwd }) + medusaProcess = await setupServer({ cwd }) + }) afterAll(async () => { - const db = useDb(); - await db.shutdown(); + const db = useDb() + await db.shutdown() - medusaProcess.kill(); - }); + medusaProcess.kill() + }) describe("GET /admin/orders", () => { beforeEach(async () => { try { - await adminSeeder(dbConnection); - await orderSeeder(dbConnection); + await adminSeeder(dbConnection) + await orderSeeder(dbConnection) } catch (err) { - console.log(err); - throw err; + console.log(err) + throw err } - }); + }) afterEach(async () => { - const db = useDb(); - await db.teardown(); - }); + const db = useDb() + await db.teardown() + }) it("gets orders", async () => { - const api = useApi(); + const api = useApi() const response = await api .get("/admin/orders", { @@ -67,25 +67,25 @@ describe("/admin/orders", () => { }, }) .catch((err) => { - console.log(err); - }); - expect(response.status).toEqual(200); - }); - }); + console.log(err) + }) + expect(response.status).toEqual(200) + }) + }) describe("GET /admin/orders", () => { beforeEach(async () => { try { - await adminSeeder(dbConnection); - await orderSeeder(dbConnection); - await swapSeeder(dbConnection); - await claimSeeder(dbConnection); + await adminSeeder(dbConnection) + await orderSeeder(dbConnection) + await swapSeeder(dbConnection) + await claimSeeder(dbConnection) } catch (err) { - console.log(err); - throw err; + console.log(err) + throw err } - const manager = dbConnection.manager; + const manager = dbConnection.manager const order2 = manager.create(Order, { id: "test-order-not-payed", @@ -136,9 +136,9 @@ describe("/admin/orders", () => { }, ], items: [], - }); + }) - await manager.save(order2); + await manager.save(order2) const li2 = manager.create(LineItem, { id: "test-item", @@ -151,23 +151,23 @@ describe("/admin/orders", () => { quantity: 1, variant_id: "test-variant", order_id: "test-order-not-payed", - }); + }) - await manager.save(li2); - }); + await manager.save(li2) + }) afterEach(async () => { - const db = useDb(); - await db.teardown(); - }); + const db = useDb() + await db.teardown() + }) it("cancels an order and increments inventory_quantity", async () => { - const api = useApi(); - const manager = dbConnection.manager; + const api = useApi() + const manager = dbConnection.manager - const initialInventoryRes = await api.get("/store/variants/test-variant"); + const initialInventoryRes = await api.get("/store/variants/test-variant") - expect(initialInventoryRes.data.variant.inventory_quantity).toEqual(1); + expect(initialInventoryRes.data.variant.inventory_quantity).toEqual(1) const response = await api .post( @@ -180,26 +180,26 @@ describe("/admin/orders", () => { } ) .catch((err) => { - console.log(err); - }); - expect(response.status).toEqual(200); + console.log(err) + }) + expect(response.status).toEqual(200) - const secondInventoryRes = await api.get("/store/variants/test-variant"); + const secondInventoryRes = await api.get("/store/variants/test-variant") - expect(secondInventoryRes.data.variant.inventory_quantity).toEqual(2); - }); + expect(secondInventoryRes.data.variant.inventory_quantity).toEqual(2) + }) it("cancels an order but does not increment inventory_quantity of unmanaged variant", async () => { - const api = useApi(); - const manager = dbConnection.manager; + const api = useApi() + const manager = dbConnection.manager await manager.query( `UPDATE "product_variant" SET manage_inventory=false WHERE id = 'test-variant'` - ); + ) - const initialInventoryRes = await api.get("/store/variants/test-variant"); + const initialInventoryRes = await api.get("/store/variants/test-variant") - expect(initialInventoryRes.data.variant.inventory_quantity).toEqual(1); + expect(initialInventoryRes.data.variant.inventory_quantity).toEqual(1) const response = await api .post( @@ -212,35 +212,35 @@ describe("/admin/orders", () => { } ) .catch((err) => { - console.log(err); - }); - expect(response.status).toEqual(200); + console.log(err) + }) + expect(response.status).toEqual(200) - const secondInventoryRes = await api.get("/store/variants/test-variant"); + const secondInventoryRes = await api.get("/store/variants/test-variant") - expect(secondInventoryRes.data.variant.inventory_quantity).toEqual(1); - }); - }); + expect(secondInventoryRes.data.variant.inventory_quantity).toEqual(1) + }) + }) describe("POST /admin/orders/:id/claims", () => { beforeEach(async () => { try { - await adminSeeder(dbConnection); - await orderSeeder(dbConnection); - await claimSeeder(dbConnection); + await adminSeeder(dbConnection) + await orderSeeder(dbConnection) + await claimSeeder(dbConnection) } catch (err) { - console.log(err); - throw err; + console.log(err) + throw err } - }); + }) afterEach(async () => { - const db = useDb(); - await db.teardown(); - }); + const db = useDb() + await db.teardown() + }) it("creates a claim", async () => { - const api = useApi(); + const api = useApi() const response = await api.post( "/admin/orders/test-order/claims", @@ -267,30 +267,30 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); - expect(response.status).toEqual(200); + ) + expect(response.status).toEqual(200) const variant = await api.get("/admin/products", { headers: { authorization: "Bearer test_token", }, - }); + }) // find test variant and verify that its inventory quantity has changed const toTest = variant.data.products[0].variants.find( (v) => v.id === "test-variant" - ); - expect(toTest.inventory_quantity).toEqual(0); + ) + expect(toTest.inventory_quantity).toEqual(0) expect(response.data.order.claims[0].shipping_address_id).toEqual( "test-shipping-address" - ); + ) expect(response.data.order.claims[0].shipping_address).toEqual( expect.objectContaining({ first_name: "lebron", country_code: "us", }) - ); + ) expect(response.data.order.claims[0].claim_items).toEqual( expect.arrayContaining([ @@ -305,7 +305,7 @@ describe("/admin/orders", () => { ]), }), ]) - ); + ) expect(response.data.order.claims[0].additional_items).toEqual( expect.arrayContaining([ @@ -314,11 +314,11 @@ describe("/admin/orders", () => { quantity: 1, }), ]) - ); - }); + ) + }) it("creates a claim with a shipping address", async () => { - const api = useApi(); + const api = useApi() const response = await api.post( "/admin/orders/test-order/claims", @@ -353,8 +353,8 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); - expect(response.status).toEqual(200); + ) + expect(response.status).toEqual(200) expect(response.data.order.claims[0].shipping_address).toEqual( expect.objectContaining({ @@ -365,7 +365,7 @@ describe("/admin/orders", () => { postal_code: "12345", country_code: "us", }) - ); + ) expect(response.data.order.claims[0].claim_items).toEqual( expect.arrayContaining([ @@ -380,7 +380,7 @@ describe("/admin/orders", () => { ]), }), ]) - ); + ) expect(response.data.order.claims[0].additional_items).toEqual( expect.arrayContaining([ @@ -389,11 +389,11 @@ describe("/admin/orders", () => { quantity: 1, }), ]) - ); - }); + ) + }) it("creates a claim with return shipping", async () => { - const api = useApi(); + const api = useApi() const response = await api.post( "/admin/orders/test-order/claims", @@ -421,9 +421,9 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(response.status).toEqual(200); + expect(response.status).toEqual(200) expect(response.data.order.claims[0].claim_items).toEqual( expect.arrayContaining([ @@ -438,7 +438,7 @@ describe("/admin/orders", () => { ]), }), ]) - ); + ) expect(response.data.order.claims[0].additional_items).toEqual( expect.arrayContaining([ @@ -447,7 +447,7 @@ describe("/admin/orders", () => { quantity: 1, }), ]) - ); + ) expect( response.data.order.claims[0].return_order.shipping_method @@ -456,11 +456,11 @@ describe("/admin/orders", () => { price: 0, shipping_option_id: "test-return-option", }) - ); - }); + ) + }) it("updates a claim", async () => { - const api = useApi(); + const api = useApi() const response = await api.post( "/admin/orders/test-order/claims", @@ -487,10 +487,10 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); - expect(response.status).toEqual(200); + ) + expect(response.status).toEqual(200) - const cid = response.data.order.claims[0].id; + const cid = response.data.order.claims[0].id const { status, data: updateData } = await api.post( `/admin/orders/test-order/claims/${cid}`, { @@ -505,18 +505,18 @@ describe("/admin/orders", () => { authorization: "bearer test_token", }, } - ); + ) - expect(status).toEqual(200); + expect(status).toEqual(200) expect(updateData.order.claims[0].shipping_methods).toEqual([ expect.objectContaining({ id: "test-method", }), - ]); - }); + ]) + }) it("updates claim items", async () => { - const api = useApi(); + const api = useApi() const response = await api.post( "/admin/orders/test-order/claims", @@ -543,11 +543,11 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); - expect(response.status).toEqual(200); + ) + expect(response.status).toEqual(200) - let claim = response.data.order.claims[0]; - const cid = claim.id; + let claim = response.data.order.claims[0] + const cid = claim.id const { status, data: updateData } = await api.post( `/admin/orders/test-order/claims/${cid}`, { @@ -570,14 +570,14 @@ describe("/admin/orders", () => { authorization: "bearer test_token", }, } - ); + ) - expect(status).toEqual(200); - expect(updateData.order.claims.length).toEqual(1); + expect(status).toEqual(200) + expect(updateData.order.claims.length).toEqual(1) - claim = updateData.order.claims[0]; + claim = updateData.order.claims[0] - expect(claim.claim_items.length).toEqual(1); + expect(claim.claim_items.length).toEqual(1) expect(claim.claim_items).toEqual( expect.arrayContaining([ expect.objectContaining({ @@ -599,11 +599,11 @@ describe("/admin/orders", () => { // ]), }), ]) - ); - }); + ) + }) it("updates claim items - removes image", async () => { - const api = useApi(); + const api = useApi() const response = await api.post( "/admin/orders/test-order/claims", @@ -630,11 +630,11 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); - expect(response.status).toEqual(200); + ) + expect(response.status).toEqual(200) - let claim = response.data.order.claims[0]; - const cid = claim.id; + let claim = response.data.order.claims[0] + const cid = claim.id const { status, data: updateData } = await api.post( `/admin/orders/test-order/claims/${cid}`, { @@ -654,14 +654,14 @@ describe("/admin/orders", () => { authorization: "bearer test_token", }, } - ); + ) - expect(status).toEqual(200); - expect(updateData.order.claims.length).toEqual(1); + expect(status).toEqual(200) + expect(updateData.order.claims.length).toEqual(1) - claim = updateData.order.claims[0]; + claim = updateData.order.claims[0] - expect(claim.claim_items.length).toEqual(1); + expect(claim.claim_items.length).toEqual(1) expect(claim.claim_items).toEqual([ expect.objectContaining({ id: claim.claim_items[0].id, @@ -674,11 +674,11 @@ describe("/admin/orders", () => { // expect.objectContaining({ value: "tags" }), // ]), }), - ]); - }); + ]) + }) it("fulfills a claim", async () => { - const api = useApi(); + const api = useApi() const response = await api .post( @@ -713,10 +713,10 @@ describe("/admin/orders", () => { } ) .catch((err) => { - console.log(err); - }); + console.log(err) + }) - const cid = response.data.order.claims[0].id; + const cid = response.data.order.claims[0].id const fulRes = await api.post( `/admin/orders/test-order/claims/${cid}/fulfillments`, {}, @@ -725,18 +725,18 @@ describe("/admin/orders", () => { Authorization: "Bearer test_token", }, } - ); - expect(fulRes.status).toEqual(200); + ) + expect(fulRes.status).toEqual(200) expect(fulRes.data.order.claims).toEqual([ expect.objectContaining({ id: cid, order_id: "test-order", fulfillment_status: "fulfilled", }), - ]); + ]) - const fid = fulRes.data.order.claims[0].fulfillments[0].id; - const iid = fulRes.data.order.claims[0].additional_items[0].id; + const fid = fulRes.data.order.claims[0].fulfillments[0].id + const iid = fulRes.data.order.claims[0].additional_items[0].id expect(fulRes.data.order.claims[0].fulfillments).toEqual([ expect.objectContaining({ items: [ @@ -747,63 +747,63 @@ describe("/admin/orders", () => { }, ], }), - ]); - }); + ]) + }) it("Only allow canceling claim after canceling fulfillments", async () => { - const order_id = "order-with-claim"; + const order_id = "order-with-claim" const order = await callGet({ path: `/admin/orders/${order_id}`, get: "order", - }); + }) - const claim = order.claims.filter((s) => s.id === "claim-w-f")[0]; - const claim_id = claim.id; + const claim = order.claims.filter((s) => s.id === "claim-w-f")[0] + const claim_id = claim.id const expectCancelToReturn = partial(expectPostCallToReturn, { path: `/admin/orders/${order_id}/claims/${claim_id}/cancel`, - }); + }) - await expectCancelToReturn({ code: 400 }); + await expectCancelToReturn({ code: 400 }) await expectAllPostCallsToReturn({ code: 200, col: claim.fulfillments, pathf: (f) => `/admin/orders/${order_id}/claims/${claim_id}/fulfillments/${f.id}/cancel`, - }); + }) - await expectCancelToReturn({ code: 200 }); - }); + await expectCancelToReturn({ code: 200 }) + }) it("Only allow canceling claim after canceling returns", async () => { - const order_id = "order-with-claim"; + const order_id = "order-with-claim" const order = await callGet({ path: `/admin/orders/${order_id}`, get: "order", - }); + }) - const claim = order.claims.filter((c) => c.id === "claim-w-r")[0]; - const claim_id = claim.id; + const claim = order.claims.filter((c) => c.id === "claim-w-r")[0] + const claim_id = claim.id const expectCancelToReturn = partial(expectPostCallToReturn, { path: `/admin/orders/${order_id}/claims/${claim_id}/cancel`, - }); + }) - await expectCancelToReturn({ code: 400 }); + await expectCancelToReturn({ code: 400 }) await expectPostCallToReturn({ code: 200, path: `/admin/returns/${claim.return_order.id}/cancel`, - }); + }) - await expectCancelToReturn({ code: 200 }); - }); + await expectCancelToReturn({ code: 200 }) + }) it("fails to creates a claim due to no stock on additional items", async () => { - const api = useApi(); + const api = useApi() try { await api.post( "/admin/orders/test-order/claims", @@ -830,43 +830,43 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) } catch (e) { - expect(e.response.status).toEqual(400); + expect(e.response.status).toEqual(400) expect(e.response.data.message).toEqual( "Variant with id: test-variant does not have the required inventory" - ); + ) } - }); - }); + }) + }) describe("POST /admin/orders/:id/return", () => { - let rrId; + let rrId beforeEach(async () => { try { - await adminSeeder(dbConnection); - await orderSeeder(dbConnection); + await adminSeeder(dbConnection) + await orderSeeder(dbConnection) const created = dbConnection.manager.create(ReturnReason, { value: "too_big", label: "Too Big", - }); - const result = await dbConnection.manager.save(created); + }) + const result = await dbConnection.manager.save(created) - rrId = result.id; + rrId = result.id } catch (err) { - console.log(err); - throw err; + console.log(err) + throw err } - }); + }) afterEach(async () => { - const db = useDb(); - await db.teardown(); - }); + const db = useDb() + await db.teardown() + }) it("creates a return", async () => { - const api = useApi(); + const api = useApi() const response = await api.post( "/admin/orders/test-order/return", @@ -885,10 +885,10 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); - expect(response.status).toEqual(200); + ) + expect(response.status).toEqual(200) - expect(response.data.order.returns[0].refund_amount).toEqual(7200); + expect(response.data.order.returns[0].refund_amount).toEqual(7200) expect(response.data.order.returns[0].items).toEqual([ expect.objectContaining({ item_id: "test-item", @@ -896,11 +896,11 @@ describe("/admin/orders", () => { reason_id: rrId, note: "TOO SMALL", }), - ]); - }); + ]) + }) it("increases inventory_quantity when return is received", async () => { - const api = useApi(); + const api = useApi() const returned = await api.post( "/admin/orders/test-order/return", @@ -918,24 +918,22 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) //Find variant that should have its inventory_quantity updated - const toTest = returned.data.order.items.find( - (i) => i.id === "test-item" - ); + const toTest = returned.data.order.items.find((i) => i.id === "test-item") - expect(returned.status).toEqual(200); - expect(toTest.variant.inventory_quantity).toEqual(2); - }); + expect(returned.status).toEqual(200) + expect(toTest.variant.inventory_quantity).toEqual(2) + }) it("does not increases inventory_quantity when return is received when inventory is not managed", async () => { - const api = useApi(); - const manager = dbConnection.manager; + const api = useApi() + const manager = dbConnection.manager await manager.query( `UPDATE "product_variant" SET manage_inventory=false WHERE id = 'test-variant'` - ); + ) const returned = await api.post( "/admin/orders/test-order/return", @@ -953,48 +951,46 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) //Find variant that should have its inventory_quantity updated - const toTest = returned.data.order.items.find( - (i) => i.id === "test-item" - ); + const toTest = returned.data.order.items.find((i) => i.id === "test-item") - expect(returned.status).toEqual(200); - expect(toTest.variant.inventory_quantity).toEqual(1); - }); - }); + expect(returned.status).toEqual(200) + expect(toTest.variant.inventory_quantity).toEqual(1) + }) + }) describe("GET /admin/orders", () => { beforeEach(async () => { try { - await adminSeeder(dbConnection); + await adminSeeder(dbConnection) // Manually insert date for filtering - const createdAt = new Date("26 January 1997 12:00 UTC"); + const createdAt = new Date("26 January 1997 12:00 UTC") await orderSeeder(dbConnection, { created_at: createdAt.toISOString(), - }); + }) } catch (err) { - console.log(err); - throw err; + console.log(err) + throw err } - }); + }) afterEach(async () => { - const db = useDb(); - await db.teardown(); - }); + const db = useDb() + await db.teardown() + }) it("lists all orders", async () => { - const api = useApi(); + const api = useApi() const response = await api.get("/admin/orders?fields=id", { headers: { authorization: "Bearer test_token", }, - }); + }) - expect(response.status).toEqual(200); + expect(response.status).toEqual(200) expect(response.data.orders).toEqual([ expect.objectContaining({ id: "test-order", @@ -1013,11 +1009,52 @@ describe("/admin/orders", () => { expect.objectContaining({ id: "test-order-w-r", }), - ]); - }); + ]) + }) + + it("list all orders with matching order email", async () => { + const api = useApi() + + const response = await api.get( + "/admin/orders?fields=id,email&q=test@email", + { + headers: { + authorization: "Bearer test_token", + }, + } + ) + + expect(response.status).toEqual(200) + expect(response.data.count).toEqual(1) + expect(response.data.orders).toEqual([ + expect.objectContaining({ + id: "test-order", + email: "test@email.com", + }), + ]) + }) + + it("list all orders with matching shipping_address first name", async () => { + const api = useApi() + + const response = await api.get("/admin/orders?q=lebron", { + headers: { + authorization: "Bearer test_token", + }, + }) + + expect(response.status).toEqual(200) + expect(response.data.count).toEqual(1) + expect(response.data.orders).toEqual([ + expect.objectContaining({ + id: "test-order", + shipping_address: expect.objectContaining({ first_name: "lebron" }), + }), + ]) + }) it("successfully lists orders with greater than", async () => { - const api = useApi(); + const api = useApi() const response = await api.get( "/admin/orders?fields=id&created_at[gt]=01-26-1990", @@ -1026,9 +1063,9 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(response.status).toEqual(200); + expect(response.status).toEqual(200) expect(response.data.orders).toEqual([ expect.objectContaining({ id: "test-order", @@ -1046,11 +1083,11 @@ describe("/admin/orders", () => { expect.objectContaining({ id: "test-order-w-r", }), - ]); - }); + ]) + }) it("successfully lists no orders with greater than", async () => { - const api = useApi(); + const api = useApi() const response = await api.get( "/admin/orders?fields=id&created_at[gt]=01-26-2000", @@ -1059,14 +1096,14 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(response.status).toEqual(200); - expect(response.data.orders).toEqual([]); - }); + expect(response.status).toEqual(200) + expect(response.data.orders).toEqual([]) + }) it("successfully lists orders with less than", async () => { - const api = useApi(); + const api = useApi() const response = await api.get( "/admin/orders?fields=id&created_at[lt]=01-26-2000", @@ -1075,9 +1112,9 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(response.status).toEqual(200); + expect(response.status).toEqual(200) expect(response.data.orders).toEqual([ expect.objectContaining({ id: "test-order", @@ -1095,11 +1132,11 @@ describe("/admin/orders", () => { expect.objectContaining({ id: "test-order-w-r", }), - ]); - }); + ]) + }) it("successfully lists no orders with less than", async () => { - const api = useApi(); + const api = useApi() const response = await api.get( "/admin/orders?fields=id&created_at[lt]=01-26-1990", @@ -1108,14 +1145,14 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(response.status).toEqual(200); - expect(response.data.orders).toEqual([]); - }); + expect(response.status).toEqual(200) + expect(response.data.orders).toEqual([]) + }) it("successfully lists orders using unix (greater than)", async () => { - const api = useApi(); + const api = useApi() const response = await api.get( "/admin/orders?fields=id&created_at[gt]=633351600", @@ -1124,9 +1161,9 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(response.status).toEqual(200); + expect(response.status).toEqual(200) expect(response.data.orders).toEqual([ expect.objectContaining({ id: "test-order", @@ -1144,8 +1181,8 @@ describe("/admin/orders", () => { expect.objectContaining({ id: "test-order-w-r", }), - ]); - }); + ]) + }) it.each([ [ @@ -1175,49 +1212,49 @@ describe("/admin/orders", () => { ])( "Only allows canceling order after canceling %s", async (id, o, of, pf) => { - const order_id = o; + const order_id = o const order = await callGet({ path: `/admin/orders/${order_id}`, get: "order", - }); + }) const expectCanceltoReturn = partial(expectPostCallToReturn, { path: `/admin/orders/${order_id}/cancel`, - }); + }) - await expectCanceltoReturn({ code: 400 }); + await expectCanceltoReturn({ code: 400 }) await expectAllPostCallsToReturn({ code: 200, col: of(order), pathf: pf, - }); + }) - await expectCanceltoReturn({ code: 200 }); + await expectCanceltoReturn({ code: 200 }) } - ); - }); + ) + }) describe("POST /admin/orders/:id/swaps", () => { beforeEach(async () => { try { - await adminSeeder(dbConnection); - await orderSeeder(dbConnection); - await swapSeeder(dbConnection); + await adminSeeder(dbConnection) + await orderSeeder(dbConnection) + await swapSeeder(dbConnection) } catch (err) { - console.log(err); - throw err; + console.log(err) + throw err } - }); + }) afterEach(async () => { - const db = useDb(); - await db.teardown(); - }); + const db = useDb() + await db.teardown() + }) it("creates a swap", async () => { - const api = useApi(); + const api = useApi() const response = await api.post( "/admin/orders/test-order/swaps", @@ -1235,12 +1272,12 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); - expect(response.status).toEqual(200); - }); + ) + expect(response.status).toEqual(200) + }) it("creates a swap and a return", async () => { - const api = useApi(); + const api = useApi() const returnedOrderFirst = await api.post( "/admin/orders/order-with-swap/return", @@ -1258,9 +1295,9 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(returnedOrderFirst.status).toEqual(200); + expect(returnedOrderFirst.status).toEqual(200) const returnedOrderSecond = await api.post( "/admin/orders/order-with-swap/return", @@ -1278,19 +1315,19 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) // find item to test returned quantiy for const toTest = returnedOrderSecond.data.order.items.find( (i) => i.id === "test-item-many" - ); + ) - expect(returnedOrderSecond.status).toEqual(200); - expect(toTest.returned_quantity).toBe(3); - }); + expect(returnedOrderSecond.status).toEqual(200) + expect(toTest.returned_quantity).toBe(3) + }) it("creates a swap and receives the items", async () => { - const api = useApi(); + const api = useApi() const createdSwapOrder = await api.post( "/admin/orders/test-order/swaps", @@ -1308,11 +1345,11 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(createdSwapOrder.status).toEqual(200); + expect(createdSwapOrder.status).toEqual(200) - const swap = createdSwapOrder.data.order.swaps[0]; + const swap = createdSwapOrder.data.order.swaps[0] const receivedSwap = await api.post( `/admin/returns/${swap.return_order.id}/receive`, @@ -1329,14 +1366,14 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(receivedSwap.status).toEqual(200); - expect(receivedSwap.data.return.status).toBe("received"); - }); + expect(receivedSwap.status).toEqual(200) + expect(receivedSwap.data.return.status).toBe("received") + }) it("creates a swap on a swap", async () => { - const api = useApi(); + const api = useApi() const swapOnSwap = await api.post( "/admin/orders/order-with-swap/swaps", @@ -1354,13 +1391,13 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(swapOnSwap.status).toEqual(200); - }); + expect(swapOnSwap.status).toEqual(200) + }) it("receives a swap on swap", async () => { - const api = useApi(); + const api = useApi() const received = await api.post( `/admin/returns/return-on-swap/receive`, @@ -1377,13 +1414,13 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(received.status).toEqual(200); - }); + expect(received.status).toEqual(200) + }) it("creates a return on a swap", async () => { - const api = useApi(); + const api = useApi() const returnOnSwap = await api.post( "/admin/orders/order-with-swap/return", @@ -1400,13 +1437,13 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(returnOnSwap.status).toEqual(200); - }); + expect(returnOnSwap.status).toEqual(200) + }) it("creates a return on an order", async () => { - const api = useApi(); + const api = useApi() const returnOnOrder = await api.post( "/admin/orders/test-order/return", @@ -1423,9 +1460,9 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(returnOnOrder.status).toEqual(200); + expect(returnOnOrder.status).toEqual(200) const captured = await api.post( "/admin/orders/test-order/capture", @@ -1435,9 +1472,9 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - const returnId = returnOnOrder.data.order.returns[0].id; + const returnId = returnOnOrder.data.order.returns[0].id const received = await api.post( `/admin/returns/${returnId}/receive`, @@ -1454,63 +1491,63 @@ describe("/admin/orders", () => { authorization: "Bearer test_token", }, } - ); + ) - expect(received.status).toEqual(200); - }); + expect(received.status).toEqual(200) + }) it("Only allows canceling swap after canceling fulfillments", async () => { try { - const swap_id = "swap-w-f"; + const swap_id = "swap-w-f" const swap = await callGet({ path: `/admin/swaps/${swap_id}`, get: "swap", - }); + }) - const { order_id } = swap; + const { order_id } = swap const expectCancelToReturn = partial(expectPostCallToReturn, { path: `/admin/orders/${order_id}/swaps/${swap_id}/cancel`, - }); + }) - await expectCancelToReturn({ code: 400 }); + await expectCancelToReturn({ code: 400 }) await expectAllPostCallsToReturn({ code: 200, col: swap.fulfillments, pathf: (f) => `/admin/orders/${order_id}/swaps/${swap_id}/fulfillments/${f.id}/cancel`, - }); + }) - await expectCancelToReturn({ code: 200 }); + await expectCancelToReturn({ code: 200 }) } catch (e) { - console.log(e); + console.log(e) } - }); + }) it("Only allows canceling swap after canceling return", async () => { - const swap_id = "swap-w-r"; + const swap_id = "swap-w-r" const swap = await callGet({ path: `/admin/swaps/${swap_id}`, get: "swap", - }); + }) - const { order_id } = swap; + const { order_id } = swap const expectCancelToReturn = partial(expectPostCallToReturn, { path: `/admin/orders/${order_id}/swaps/${swap_id}/cancel`, - }); + }) - await expectCancelToReturn({ code: 400 }); + await expectCancelToReturn({ code: 400 }) await expectPostCallToReturn({ code: 200, path: `/admin/returns/${swap.return_order.id}/cancel`, - }); + }) - await expectCancelToReturn({ code: 200 }); - }); - }); -}); + await expectCancelToReturn({ code: 200 }) + }) + }) +}) diff --git a/integration-tests/api/__tests__/admin/variant.js b/integration-tests/api/__tests__/admin/variant.js new file mode 100644 index 0000000000..a0244141c1 --- /dev/null +++ b/integration-tests/api/__tests__/admin/variant.js @@ -0,0 +1,153 @@ +const path = require("path") + +const setupServer = require("../../../helpers/setup-server") +const { useApi } = require("../../../helpers/use-api") +const { initDb, useDb } = require("../../../helpers/use-db") + +const adminSeeder = require("../../helpers/admin-seeder") +const productSeeder = require("../../helpers/product-seeder") + +jest.setTimeout(30000) + +describe("/admin/products", () => { + let medusaProcess + let dbConnection + + beforeAll(async () => { + const cwd = path.resolve(path.join(__dirname, "..", "..")) + dbConnection = await initDb({ cwd }) + medusaProcess = await setupServer({ cwd }) + }) + + afterAll(async () => { + const db = useDb() + await db.shutdown() + + medusaProcess.kill() + }) + + describe("GET /admin/product-variants", () => { + beforeEach(async () => { + try { + await productSeeder(dbConnection) + await adminSeeder(dbConnection) + } catch (err) { + console.log(err) + throw err + } + }) + + afterEach(async () => { + const db = useDb() + await db.teardown() + }) + + it("lists all product variants", async () => { + const api = useApi() + + const response = await api + .get("/admin/variants/", { + headers: { + Authorization: "Bearer test_token", + }, + }) + .catch((err) => { + console.log(err) + }) + + expect(response.status).toEqual(200) + expect(response.data.variants).toEqual( + expect.arrayContaining([ + expect.objectContaining( + { + id: "test-variant", + }, + { + id: "test-variant_2", + }, + { + id: "test-variant_1", + } + ), + ]) + ) + }) + + it("lists all product variants matching a specific sku", async () => { + const api = useApi() + const response = await api + .get("/admin/variants?q=sku2", { + headers: { + Authorization: "Bearer test_token", + }, + }) + .catch((err) => { + console.log(err) + }) + + expect(response.status).toEqual(200) + expect(response.data.variants.length).toEqual(1) + expect(response.data.variants).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + sku: "test-sku2", + }), + ]) + ) + }) + + it("lists all product variants matching a specific variant title", async () => { + const api = useApi() + const response = await api + .get("/admin/variants?q=rank (1)", { + headers: { + Authorization: "Bearer test_token", + }, + }) + .catch((err) => { + console.log(err) + }) + + expect(response.status).toEqual(200) + expect(response.data.variants.length).toEqual(1) + expect(response.data.variants).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + id: "test-variant_1", + sku: "test-sku1", + }), + ]) + ) + }) + + it("lists all product variants matching a specific product title", async () => { + const api = useApi() + const response = await api + .get("/admin/variants?q=Test product1", { + headers: { + Authorization: "Bearer test_token", + }, + }) + .catch((err) => { + console.log(err) + }) + + expect(response.status).toEqual(200) + expect(response.data.variants.length).toEqual(2) + expect(response.data.variants).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + product_id: "test-product1", + id: "test-variant_3", + sku: "test-sku3", + }), + expect.objectContaining({ + product_id: "test-product1", + id: "test-variant_4", + sku: "test-sku4", + }), + ]) + ) + }) + }) +}) diff --git a/packages/medusa/src/services/__tests__/discount.js b/packages/medusa/src/services/__tests__/discount.js index ff47e6e0d5..8b740fb1a8 100644 --- a/packages/medusa/src/services/__tests__/discount.js +++ b/packages/medusa/src/services/__tests__/discount.js @@ -494,9 +494,6 @@ describe("DiscountService", () => { expect(discountRepository.findAndCount).toHaveBeenCalledTimes(1) expect(discountRepository.findAndCount).toHaveBeenCalledWith({ - join: { - alias: "discount", - }, where: expect.anything(), skip: 0, take: 50, diff --git a/packages/medusa/src/services/customer.js b/packages/medusa/src/services/customer.js index 88ade052b8..850ddb27f7 100644 --- a/packages/medusa/src/services/customer.js +++ b/packages/medusa/src/services/customer.js @@ -3,7 +3,7 @@ import Scrypt from "scrypt-kdf" import _ from "lodash" import { Validator, MedusaError } from "medusa-core-utils" import { BaseService } from "medusa-interfaces" -import { Brackets } from "typeorm" +import { Brackets, ILike } from "typeorm" /** * Provides layer to manipulate customers. @@ -149,9 +149,9 @@ class CustomerService extends BaseService { qb.andWhere( new Brackets(qb => { - qb.where(`email ILIKE :q`, { q: `%${q}%` }) - .orWhere(`first_name ILIKE :q`, { q: `%${q}%` }) - .orWhere(`last_name ILIKE :q`, { q: `%${q}%` }) + qb.where({ email: ILike(`%${q}%`) }) + .orWhere({ first_name: ILike(`%${q}%`) }) + .orWhere({ last_name: ILike(`%${q}%`) }) }) ) } @@ -183,18 +183,14 @@ class CustomerService extends BaseService { delete where.first_name delete where.last_name - query.join = { - alias: "customer", - } - query.where = qb => { qb.where(where) qb.andWhere( new Brackets(qb => { - qb.where(`customer.first_name ILIKE :q`, { q: `%${q}%` }) - .orWhere(`customer.last_name ILIKE :q`, { q: `%${q}%` }) - .orWhere(`customer.email ILIKE :q`, { q: `%${q}%` }) + qb.where({ email: ILike(`%${q}%`) }) + .orWhere({ first_name: ILike(`%${q}%`) }) + .orWhere({ last_name: ILike(`%${q}%`) }) }) ) } diff --git a/packages/medusa/src/services/discount.js b/packages/medusa/src/services/discount.js index 6451eb9c72..a4fabdac65 100644 --- a/packages/medusa/src/services/discount.js +++ b/packages/medusa/src/services/discount.js @@ -2,7 +2,7 @@ import _ from "lodash" import randomize from "randomatic" import { BaseService } from "medusa-interfaces" import { Validator, MedusaError } from "medusa-core-utils" -import { Brackets } from "typeorm" +import { Brackets, ILike } from "typeorm" /** * Provides layer to manipulate discounts. @@ -151,16 +151,12 @@ class DiscountService extends BaseService { delete where.code - query.join = { - alias: "discount", - } - query.where = qb => { qb.where(where) qb.andWhere( new Brackets(qb => { - qb.where(`discount.code ILIKE :q`, { q: `%${q}%` }) + qb.where({ code: ILike(`%${q}%`) }) }) ) } diff --git a/packages/medusa/src/services/draft-order.js b/packages/medusa/src/services/draft-order.js index d16044b43b..fe2ceb51aa 100644 --- a/packages/medusa/src/services/draft-order.js +++ b/packages/medusa/src/services/draft-order.js @@ -1,7 +1,7 @@ import _ from "lodash" import { BaseService } from "medusa-interfaces" import { MedusaError } from "medusa-core-utils" -import { Brackets } from "typeorm" +import { Brackets, ILike } from "typeorm" /** * Handles draft orders diff --git a/packages/medusa/src/services/gift-card.js b/packages/medusa/src/services/gift-card.js index 173a3d1aee..3762ce36f5 100644 --- a/packages/medusa/src/services/gift-card.js +++ b/packages/medusa/src/services/gift-card.js @@ -1,8 +1,7 @@ -import _ from "lodash" -import randomize from "randomatic" -import { BaseService } from "medusa-interfaces" -import { Brackets } from "typeorm" import { MedusaError } from "medusa-core-utils" +import { BaseService } from "medusa-interfaces" +import randomize from "randomatic" +import { Brackets } from "typeorm" /** * Provides layer to manipulate gift cards. diff --git a/packages/medusa/src/services/order.js b/packages/medusa/src/services/order.js index 86d43f4d78..64684ac6d6 100644 --- a/packages/medusa/src/services/order.js +++ b/packages/medusa/src/services/order.js @@ -1,5 +1,4 @@ -import _ from "lodash" -import { Validator, MedusaError } from "medusa-core-utils" +import { MedusaError, Validator } from "medusa-core-utils" import { BaseService } from "medusa-interfaces" import { Brackets } from "typeorm" @@ -237,7 +236,9 @@ class OrderService extends BaseService { qb.andWhere( new Brackets(qb => { - qb.where(`shipping_address.first_name ILIKE :q`, { q: `%${q}%` }) + qb.where(`shipping_address.first_name ILIKE :qfn`, { + qfn: `%${q}%`, + }) .orWhere(`order.email ILIKE :q`, { q: `%${q}%` }) .orWhere(`display_id::varchar(255) ILIKE :dId`, { dId: `${q}` }) }) diff --git a/packages/medusa/src/services/product-variant.js b/packages/medusa/src/services/product-variant.js index 388d8fad24..055efbde95 100644 --- a/packages/medusa/src/services/product-variant.js +++ b/packages/medusa/src/services/product-variant.js @@ -1,6 +1,6 @@ import _ from "lodash" import { BaseService } from "medusa-interfaces" -import { Brackets, Raw, IsNull } from "typeorm" +import { Brackets, Raw, IsNull, ILike } from "typeorm" import { Validator, MedusaError } from "medusa-core-utils" /** @@ -558,14 +558,11 @@ class ProductVariantService extends BaseService { } query.where = qb => { - qb.where(where).andWhere( - new Brackets(qb => { - qb.where([ - { sku: Raw(a => `${a} ILIKE :q`, { q: `%${q}%` }) }, - { title: Raw(a => `${a} ILIKE :q`, { q: `%${q}%` }) }, - ]).orWhere(`product.title ILIKE :q`, { q: `%${q}%` }) - }) - ) + qb.where(where).andWhere([ + { sku: ILike(`%${q}%`) }, + { title: ILike(`%${q}%`) }, + { product: { title: ILike(`%${q}%`) } }, + ]) } }