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
}),
])
)
})
})
})
})

View File

@@ -46,7 +46,7 @@ export type OrderFactoryData = {
export const simpleOrderFactory = async (
connection: Connection,
data: OrderFactoryData = {},
seed: number
seed?: number
): Promise<Order> => {
if (typeof seed !== "undefined") {
faker.seed(seed)

View File

@@ -1,4 +1,4 @@
import { SalesChannel } from "@medusajs/medusa"
import { Product, SalesChannel } from "@medusajs/medusa"
import faker from "faker"
import { Connection } from "typeorm"
@@ -7,6 +7,7 @@ export type SalesChannelFactoryData = {
description?: string
is_disabled?: boolean
id?: string
products?: Product[],
}
export const simpleSalesChannelFactory = async (
@@ -20,7 +21,7 @@ export const simpleSalesChannelFactory = async (
const manager = connection.manager
const salesChannel = manager.create(SalesChannel, {
let salesChannel = manager.create(SalesChannel, {
id: data.id ?? `simple-id-${Math.random() * 1000}`,
name: data.name || faker.name.firstName(),
description: data.description || faker.name.lastName(),
@@ -28,5 +29,19 @@ export const simpleSalesChannelFactory = async (
typeof data.is_disabled !== undefined ? data.is_disabled : false,
})
return await manager.save(salesChannel)
salesChannel = await manager.save(salesChannel)
if (data.products) {
const promises = []
for (const product of data.products) {
promises.push(
manager.query(`
INSERT INTO product_sales_channel (product_id, sales_channel_id) VALUES ('${product.id}', '${salesChannel.id}');
`)
)
}
await Promise.all(promises)
}
return salesChannel
}