feat(medusa): Attach or update cart sales channel (#1873)

What

Allow to create a cart with a sales channel, otherwise the default one is attached.
Also allow to update the sales channel on an existing cart and in that case the line items that does not belongs to the new sales channel attached are removed

How

Updating existing end points and service method to integrate the new requirements

Tests

Add new integration tests

Fixes CORE-270
Fixes CORE-272

Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Adrien de Peretti
2022-07-27 16:54:05 +00:00
committed by GitHub
co-authored by Oliver Windall Juhl
parent 8dd85e5f03
commit df66378535
17 changed files with 595 additions and 169 deletions
@@ -34,7 +34,7 @@ export type CartFactoryData = {
export const simpleCartFactory = async (
connection: Connection,
data: CartFactoryData = {},
seed: number
seed?: number
): Promise<Cart> => {
if (typeof seed !== "undefined") {
faker.seed(seed)
@@ -8,6 +8,7 @@ export type SalesChannelFactoryData = {
is_disabled?: boolean
id?: string
products?: Product[],
is_default?: boolean
}
export const simpleSalesChannelFactory = async (
@@ -36,12 +37,19 @@ export const simpleSalesChannelFactory = async (
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}');
INSERT INTO product_sales_channel (product_id, sales_channel_id)
VALUES ('${product.id}', '${salesChannel.id}');
`)
)
}
await Promise.all(promises)
}
if (data.is_default) {
await manager.query(
`UPDATE store SET default_sales_channel_id = '${salesChannel.id}'`
)
}
return salesChannel
}