1- import { Modules , ShippingOptionTypeWorkflowEvents } from "@medusajs/framework/utils"
1+ import {
2+ MedusaError ,
3+ Modules ,
4+ ShippingOptionTypeWorkflowEvents ,
5+ } from "@medusajs/framework/utils"
26import {
37 createHook ,
48 createWorkflow ,
@@ -7,9 +11,25 @@ import {
711 WorkflowData ,
812 WorkflowResponse ,
913} from "@medusajs/framework/workflows-sdk"
14+ import { createStep } from "@medusajs/framework/workflows-sdk"
1015import { emitEventStep } from "../../common/steps/emit-event"
1116import { removeRemoteLinkStep } from "../../common/steps/remove-remote-links"
1217import { deleteShippingOptionTypesStep } from "../steps"
18+ import { useQueryGraphStep } from "../../common"
19+
20+ const validateDeleteShippingOptionTypesStep = createStep (
21+ "validate-delete-shipping-option-types" ,
22+ ( input : { shippingOptions : { id : string } [ ] } ) => {
23+ const shippingOptions = input . shippingOptions
24+
25+ if ( shippingOptions . length > 0 ) {
26+ throw new MedusaError (
27+ MedusaError . Types . INVALID_DATA ,
28+ "Cannot delete shipping option type because some shipping options are using it."
29+ )
30+ }
31+ }
32+ )
1333
1434/**
1535 * The data to delete one or more shipping option types.
@@ -21,7 +41,8 @@ export type DeleteShippingOptionTypesWorkflowInput = {
2141 ids : string [ ]
2242}
2343
24- export const deleteShippingOptionTypesWorkflowId = "delete-shipping-option-types"
44+ export const deleteShippingOptionTypesWorkflowId =
45+ "delete-shipping-option-types"
2546/**
2647 * This workflow deletes one or more shipping-option types. It's used by the
2748 * [Delete Shipping Option Types Admin API Route](TODO HERE).
@@ -48,11 +69,31 @@ export const deleteShippingOptionTypesWorkflowId = "delete-shipping-option-types
4869export const deleteShippingOptionTypesWorkflow = createWorkflow (
4970 deleteShippingOptionTypesWorkflowId ,
5071 ( input : WorkflowData < DeleteShippingOptionTypesWorkflowInput > ) => {
51- const deletedShippingOptionTypes = deleteShippingOptionTypesStep ( input . ids )
52- const shippingOptionTypesDeleted = createHook ( "shippingOptionTypesDeleted" , {
53- ids : input . ids ,
72+ const shippingOptionsQuery = useQueryGraphStep ( {
73+ entity : "shipping_option" ,
74+ filters : { shipping_option_type_id : input . ids } ,
75+ pagination : { take : 1 } ,
76+ fields : [ "id" ] ,
77+ } ) . config ( { name : "get-shipping-options" } )
78+
79+ const shippingOptions = transform (
80+ { shippingOptionsQuery } ,
81+ ( { shippingOptionsQuery } ) =>
82+ shippingOptionsQuery . data as { id : string } [ ]
83+ )
84+
85+ validateDeleteShippingOptionTypesStep ( {
86+ shippingOptions,
5487 } )
5588
89+ const deletedShippingOptionTypes = deleteShippingOptionTypesStep ( input . ids )
90+ const shippingOptionTypesDeleted = createHook (
91+ "shippingOptionTypesDeleted" ,
92+ {
93+ ids : input . ids ,
94+ }
95+ )
96+
5697 const typeIdEvents = transform ( { input } , ( { input } ) => {
5798 return input . ids ?. map ( ( id ) => {
5899 return { id }
0 commit comments