feat(medusa): Allow to query order/product by SC (#1867)

**What**

Allow to query the products and orders by sales channels

**How**

Updating the existing end points and repositories  (if necessary) to take a new query params that is sales_channel_id as an array of string

**Tests**

Add new integration tests

Fixes CORE-295
Fixes CORE-288
This commit is contained in:
Adrien de Peretti
2022-07-19 18:54:20 +02:00
committed by GitHub
parent 0e5f0d8cd6
commit 110c995a6a
11 changed files with 168 additions and 96 deletions

View File

@@ -12,6 +12,8 @@ const {
simpleCartFactory,
} = require("../../factories")
const { simpleOrderFactory } = require("../../factories")
const orderSeeder = require("../../helpers/order-seeder");
const productSeeder = require("../../helpers/product-seeder");
const startServerWithEnvironment =
require("../../../helpers/start-server-with-environment").default
@@ -664,17 +666,15 @@ describe("sales channels", () => {
beforeEach(async() => {
try {
await adminSeeder(dbConnection)
salesChannel = await simpleSalesChannelFactory(dbConnection, {
name: "test name",
description: "test description",
})
product = await simpleProductFactory(dbConnection, {
id: "product_1",
title: "test title",
})
await dbConnection.manager.query(`
INSERT INTO product_sales_channel VALUES ('${product.id}', '${salesChannel.id}')
`)
salesChannel = await simpleSalesChannelFactory(dbConnection, {
name: "test name",
description: "test description",
products: [product]
})
} catch (e) {
console.error(e)
}
@@ -811,4 +811,108 @@ describe("sales channels", () => {
)
})
})
describe("/admin/orders using sales channels", () => {
describe("GET /admin/orders", () => {
let order
beforeEach(async() => {
try {
await adminSeeder(dbConnection)
order = await simpleOrderFactory(dbConnection, {
sales_channel: {
name: "test name",
description: "test description",
},
})
await orderSeeder(dbConnection)
} catch (err) {
console.log(err)
throw err
}
})
afterEach(async() => {
const db = useDb()
await db.teardown()
})
it("should successfully lists orders that belongs to the requested sales channels", async() => {
const api = useApi()
const response = await api.get(
`/admin/orders?sales_channel_id[]=${order.sales_channel_id}`,
{
headers: {
authorization: "Bearer test_token",
},
}
)
expect(response.status).toEqual(200)
expect(response.data.orders.length).toEqual(1)
expect(response.data.orders).toEqual([
expect.objectContaining({
id: order.id,
}),
])
})
})
})
describe("/admin/products using sales channels", () => {
describe("GET /admin/products", () => {
const productData = {
id: "product-sales-channel-1",
title: "test description",
}
let salesChannel
beforeEach(async () => {
try {
await productSeeder(dbConnection)
await adminSeeder(dbConnection)
const product = await simpleProductFactory(dbConnection, productData)
salesChannel = await simpleSalesChannelFactory(dbConnection, {
name: "test name",
description: "test description",
products: [product]
})
} catch (err) {
console.log(err)
throw err
}
})
afterEach(async() => {
const db = useDb()
await db.teardown()
})
it("should returns a list of products that belongs to the requested sales channels", async() => {
const api = useApi()
const response = await api
.get(`/admin/products?sales_channel_id[]=${salesChannel.id}`, {
headers: {
Authorization: "Bearer test_token",
},
})
.catch((err) => {
console.log(err)
})
expect(response.status).toEqual(200)
expect(response.data.products.length).toEqual(1)
expect(response.data.products).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: productData.id,
title: productData.title
}),
])
)
})
})
})
})