feat(medusa, medusa-js, medusa-react): Start implementing remove batch products on a sales channel (#1842)

What
Support sales channel remove product batch in medusa, medusa-js and medusa-react

How
By implementing a new endpoint and the associated service method as well as the repository methods.

Medusa-js new removeProductd method in the resource

Medusa-react new hook in the mutations

Tests

Endpoint test
Service test
Integration test
Hook tests

Fixes CORE-292
This commit is contained in:
Adrien de Peretti
2022-07-13 21:40:23 +02:00
committed by GitHub
parent 7162972318
commit cdd91974f9
19 changed files with 396 additions and 218 deletions

View File

@@ -1,6 +1,6 @@
const path = require("path")
const { SalesChannel } = require("@medusajs/medusa")
const { SalesChannel, Product } = require("@medusajs/medusa")
const { useApi } = require("../../../helpers/use-api")
const { useDb } = require("../../../helpers/use-db")
@@ -657,4 +657,91 @@ describe("sales channels", () => {
})
})
})
describe("DELETE /admin/sales-channels/:id/products/batch", () => {
let salesChannel
let product
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}')
`)
} catch (e) {
console.error(e)
}
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should remove products from a sales channel", async() => {
const api = useApi()
let attachedProduct = await dbConnection.manager.findOne(Product, {
where: { id: product.id },
relations: ["sales_channels"]
})
expect(attachedProduct.sales_channels.length).toBe(1)
expect(attachedProduct.sales_channels).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: expect.any(String),
name: "test name",
description: "test description",
is_disabled: false,
})
])
)
const payload = {
product_ids: [{ id: product.id }]
}
await api.delete(
`/admin/sales-channels/${salesChannel.id}/products/batch`,
{
...adminReqConfig,
data: payload,
},
)
// Validate idempotency
const response = await api.delete(
`/admin/sales-channels/${salesChannel.id}/products/batch`,
{
...adminReqConfig,
data: payload,
},
)
expect(response.status).toEqual(200)
expect(response.data.sales_channel).toEqual(
expect.objectContaining({
id: expect.any(String),
name: "test name",
description: "test description",
is_disabled: false,
})
)
attachedProduct = await dbConnection.manager.findOne(Product, {
where: { id: product.id },
relations: ["sales_channels"]
})
expect(attachedProduct.sales_channels.length).toBe(0)
})
})
})