Feat(link-module, core-flows, medusa): Add sales channel location management (#6905)

* add locations with sales channels

* init, not working location based management

* working adding sales channel stock locations

* remote sales channels from location

* remove console log

* rm allowedFields

* redo errorhandler

* make validation take an array

* add changeset

* fix build

* Update packages/core-flows/src/sales-channel/workflows/remove-locations-from-sales-channel.ts

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>

* add default cases to return early

* cleanup

* add sc test and remove associations

---------

Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Philip Korsholm
2024-04-03 08:38:53 +02:00
committed by GitHub
parent 27f4f0d724
commit edafe7db47
19 changed files with 483 additions and 43 deletions

View File

@@ -1,6 +1,9 @@
const { ModuleRegistrationName, Modules } = require("@medusajs/modules-sdk")
const { medusaIntegrationTestRunner } = require("medusa-test-utils")
const { createAdminUser } = require("../../../helpers/create-admin-user")
const {
createAdminUser,
adminHeaders,
} = require("../../../helpers/create-admin-user")
const { breaking } = require("../../../helpers/breaking")
const { ContainerRegistrationKeys } = require("@medusajs/utils")
@@ -345,7 +348,7 @@ medusaIntegrationTestRunner({
})
it("should delete the requested sales channel", async () => {
let toDelete = await breaking(
const toDelete = await breaking(
async () => {
return await dbConnection.manager.findOne(SalesChannel, {
where: { id: salesChannel.id },
@@ -403,6 +406,48 @@ medusaIntegrationTestRunner({
)
})
})
it("should successfully delete channel associations", async () => {
await breaking(null, async () => {
const remoteLink = container.resolve(
ContainerRegistrationKeys.REMOTE_LINK
)
console.warn("testing")
await remoteLink.create([
{
[Modules.SALES_CHANNEL]: {
sales_channel_id: "test-channel",
},
[Modules.STOCK_LOCATION]: {
stock_location_id: "test-location",
},
},
{
[Modules.SALES_CHANNEL]: {
sales_channel_id: "test-channel-default",
},
[Modules.STOCK_LOCATION]: {
stock_location_id: "test-location",
},
},
])
await api
.delete(`/admin/sales-channels/test-channel`, adminReqConfig)
.catch(console.log)
const linkService = remoteLink.getLinkModule(
Modules.SALES_CHANNEL,
"sales_channel_id",
Modules.STOCK_LOCATION,
"stock_location_id"
)
const channelLinks = await linkService.list()
expect(channelLinks).toHaveLength(1)
})
})
})
describe("GET /admin/orders/:id", () => {

View File

@@ -279,5 +279,100 @@ medusaIntegrationTestRunner({
expect(stockLocationLinks).toHaveLength(0)
})
})
describe("Add sales channels", () => {
let salesChannel
let location
beforeEach(async () => {
const salesChannelResponse = await api.post(
"/admin/sales-channels",
{
name: "test name",
description: "test description",
},
adminHeaders
)
salesChannel = salesChannelResponse.data.sales_channel
const locationResponse = await api.post(
"/admin/stock-locations",
{
name: "test location",
},
adminHeaders
)
location = locationResponse.data.stock_location
})
it("should add sales channels to a location", async () => {
const salesChannelResponse = await api.post(
`/admin/stock-locations/${location.id}/sales-channels/batch/add?fields=*sales_channels`,
{ sales_channel_ids: [salesChannel.id] },
adminHeaders
)
expect(
salesChannelResponse.data.stock_location.sales_channels
).toHaveLength(1)
})
})
describe("Remove sales channels", () => {
let salesChannel1
let salesChannel2
let location
beforeEach(async () => {
const salesChannelResponse1 = await api.post(
"/admin/sales-channels",
{
name: "test name",
description: "test description",
},
adminHeaders
)
salesChannel1 = salesChannelResponse1.data.sales_channel
const salesChannelResponse2 = await api.post(
"/admin/sales-channels",
{
name: "test name",
description: "test description",
},
adminHeaders
)
salesChannel2 = salesChannelResponse2.data.sales_channel
const locationResponse = await api.post(
"/admin/stock-locations",
{
name: "test location",
},
adminHeaders
)
location = locationResponse.data.stock_location
await api.post(
`/admin/stock-locations/${location.id}/sales-channels/batch/add?fields=*sales_channels`,
{ sales_channel_ids: [salesChannel1.id, salesChannel2.id] },
adminHeaders
)
})
it("should remove sales channels from a location", async () => {
const salesChannelResponse = await api.post(
`/admin/stock-locations/${location.id}/sales-channels/batch/remove?fields=*sales_channels`,
{ sales_channel_ids: [salesChannel1.id] },
adminHeaders
)
expect(
salesChannelResponse.data.stock_location.sales_channels
).toHaveLength(1)
})
})
},
})