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 16:54:20 +00:00
committed by GitHub
parent 0e5f0d8cd6
commit 110c995a6a
11 changed files with 168 additions and 96 deletions
@@ -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)
@@ -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
}