fix(): Deleted default sales channel should be prevented (#10193)

FIXES CMRC-722

**What**
- It should not be allowed to delete a default sales channel
- The admin does not allow to delete a sales channel use as the default for the store
This commit is contained in:
Adrien de Peretti
2024-12-05 17:19:45 +01:00
committed by GitHub
parent 2b455b15a6
commit 559fc6587a
9 changed files with 510 additions and 1563 deletions

View File

@@ -0,0 +1,36 @@
import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
import { MedusaError, Modules } from "@medusajs/framework/utils"
export const canDeleteSalesChannelsOrThrowStepId =
"can-delete-sales-channels-or-throw-step"
export const canDeleteSalesChannelsOrThrowStep = createStep(
canDeleteSalesChannelsOrThrowStepId,
async ({ ids }: { ids: string | string[] }, { container }) => {
const salesChannelIdsToDelete = Array.isArray(ids) ? ids : [ids]
const storeModule = await container.resolve(Modules.STORE)
const stores = await storeModule.listStores(
{
default_sales_channel_id: salesChannelIdsToDelete,
},
{
select: ["default_sales_channel_id"],
}
)
const defaultSalesChannelIds = stores.map((s) => s.default_sales_channel_id)
if (defaultSalesChannelIds.length) {
throw new MedusaError(
MedusaError.Types.INVALID_DATA,
`Cannot delete default sales channels: ${defaultSalesChannelIds.join(
", "
)}`
)
}
return new StepResponse(true)
}
)

View File

@@ -6,3 +6,4 @@ export * from "./detach-products-from-sales-channels"
export * from "./update-sales-channels"
export * from "./associate-locations-with-channels"
export * from "./detach-locations-from-channels"
export * from "./can-delete-sales-channels"

View File

@@ -7,6 +7,7 @@ import {
import { emitEventStep } from "../../common"
import { removeRemoteLinkStep } from "../../common/steps/remove-remote-links"
import { deleteSalesChannelsStep } from "../steps/delete-sales-channels"
import { canDeleteSalesChannelsOrThrowStep } from "../steps"
export type DeleteSalesChannelsWorkflowInput = { ids: string[] }
@@ -19,6 +20,7 @@ export const deleteSalesChannelsWorkflow = createWorkflow(
(
input: WorkflowData<DeleteSalesChannelsWorkflowInput>
): WorkflowData<void> => {
canDeleteSalesChannelsOrThrowStep({ ids: input.ids })
deleteSalesChannelsStep(input.ids)
removeRemoteLinkStep({