chore(types, api): support shipping option type api endpoints (#13191)

* chore(types, api): support shipping option type api endpoints

* core flows

* api

* typos

* compiler errors

* integration tests

* remove metadata

* changeset

* modify test

* upsert

* change remote query

* minor to patch

* description optional

* description optional

* woops my bad

* my bad again

---------

Co-authored-by: william bouchard <williambouchard@williams-MacBook-Pro.local>
This commit is contained in:
William Bouchardandwilliam bouchard authored and GitHub committed 2025-08-14 10:15:51 -04:00
1 parent aa27240b7f
commit 34c3c14e0a
29 files changed
+1240 -105

No files matched your search

@@ -1 +1,2 @@
export * from "./steps"
export * from "./workflows"
@@ -0,0 +1,29 @@
import { FulfillmentTypes, IFulfillmentModuleService, } from "@medusajs/framework/types"
import { Modules } from "@medusajs/framework/utils"
import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
export const createShippingOptionTypesStepId = "create-shipping-option-types"
/**
* This step creates one or more shipping option types.
*/
export const createShippingOptionTypesStep = createStep(
createShippingOptionTypesStepId,
async (data: FulfillmentTypes.CreateShippingOptionTypeDTO[], { container }) => {
const service = container.resolve<IFulfillmentModuleService>(Modules.FULFILLMENT)
const created = await service.createShippingOptionTypes(data)
return new StepResponse(
created,
created.map((shippingOptionType) => shippingOptionType.id)
)
},
async (createdIds, { container }) => {
if (!createdIds?.length) {
return
}
const service = container.resolve<IFulfillmentModuleService>(Modules.FULFILLMENT)
await service.deleteShippingOptionTypes(createdIds)
}
)
@@ -0,0 +1,31 @@
import { IFulfillmentModuleService, } from "@medusajs/framework/types"
import { Modules } from "@medusajs/framework/utils"
import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
/**
* The IDs of the shipping option types to delete.
*/
export type DeleteShippingOptionTypesStepInput = string[]
export const deleteShippingOptionTypesStepId = "delete-shipping-option-types"
/**
* This step deletes one or more shipping option types.
*/
export const deleteShippingOptionTypesStep = createStep(
deleteShippingOptionTypesStepId,
async (ids: DeleteShippingOptionTypesStepInput, { container }) => {
const service = container.resolve<IFulfillmentModuleService>(Modules.FULFILLMENT)
await service.softDeleteShippingOptionTypes(ids)
return new StepResponse(void 0, ids)
},
async (prevIds, { container }) => {
if (!prevIds?.length) {
return
}
const service = container.resolve<IFulfillmentModuleService>(Modules.FULFILLMENT)
await service.restoreShippingOptionTypes(prevIds)
}
)
@@ -1 +1,4 @@
export * from "./list-shipping-options-for-context"
export * from "./create-shipping-option-types"
export * from "./delete-shipping-option-types"
export * from "./update-shipping-option-types"
@@ -0,0 +1,52 @@
import { FulfillmentTypes, IFulfillmentModuleService, } from "@medusajs/framework/types"
import { getSelectsAndRelationsFromObjectArray, Modules, } from "@medusajs/framework/utils"
import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk"
/**
* The data to identify and update the shipping option types.
*/
export type UpdateShippingOptionTypesStepInput = {
/**
* The filters to select the shipping option types to update.
*/
selector: FulfillmentTypes.FilterableShippingOptionTypeProps
/**
* The data to update the shipping option types with.
*/
update: FulfillmentTypes.UpdateShippingOptionTypeDTO
}
export const updateShippingOptionTypesStepId = "update-shipping-option-types"
/**
* This step updates shipping option types matching the specified filters.
*/
export const updateShippingOptionTypesStep = createStep(
updateShippingOptionTypesStepId,
async (data: UpdateShippingOptionTypesStepInput, { container }) => {
const service = container.resolve<IFulfillmentModuleService>(Modules.FULFILLMENT)
const { selects, relations } = getSelectsAndRelationsFromObjectArray([
data.update,
])
const prevData = await service.listShippingOptionTypes(data.selector, {
select: selects,
relations,
})
const shippingOptionTypes = await service.updateShippingOptionTypes(
data.selector,
data.update
)
return new StepResponse(shippingOptionTypes, prevData)
},
async (prevData, { container }) => {
if (!prevData?.length) {
return
}
const service = container.resolve<IFulfillmentModuleService>(Modules.FULFILLMENT)
await service.upsertShippingOptionTypes(prevData)
}
)
@@ -0,0 +1,80 @@
import { AdditionalData, FulfillmentTypes, } from "@medusajs/framework/types"
import { ShippingOptionTypeWorkflowEvents } from "@medusajs/framework/utils"
import {
createHook,
createWorkflow,
transform,
WorkflowData,
WorkflowResponse,
} from "@medusajs/framework/workflows-sdk"
import { emitEventStep } from "../../common"
import { createShippingOptionTypesStep } from "../steps"
/**
* The data to create one or more shipping option types, along with custom data that's passed to the workflow's hooks.
*/
export type CreateShippingOptionTypesWorkflowInput = {
/**
* The shipping option types to create.
*/
shipping_option_types: FulfillmentTypes.CreateShippingOptionTypeDTO[]
} & AdditionalData
export const createShippingOptionTypesWorkflowId = "create-shipping-option-types"
/**
* This workflow creates one or more shipping option types. It's used by the
* [Create Shipping Option Type Admin API Route](TODO HERE).
*
* This workflow has a hook that allows you to perform custom actions on the created shipping option types. For example, you can pass under `additional_data` custom data that
* allows you to create custom data models linked to the shipping option types.
*
* You can also use this workflow within your own custom workflows, allowing you to wrap custom logic around shipping option type creation.
*
* @example
* const { result } = await createShippingOptionTypesWorkflow(container)
* .run({
* input: {
* shipping_option_types: [
* {
* label: "Standard ",
* code: "standard",
* description: "Ship in 2-3 days."
* }
* ],
* additional_data: {
* erp_id: "123"
* }
* }
* })
*
* @summary
*
* Create one or more shipping option types.
*
* @property hooks.shippingOptionTypesCreated - This hook is executed after the shipping option types are created. You can consume this hook to perform custom actions on the created shipping option types.
*/
export const createShippingOptionTypesWorkflow = createWorkflow(
createShippingOptionTypesWorkflowId,
(input: WorkflowData<CreateShippingOptionTypesWorkflowInput>) => {
const shippingOptionTypes = createShippingOptionTypesStep(input.shipping_option_types)
const shippingOptionTypesCreated = createHook("shippingOptionTypesCreated", {
shipping_option_types: shippingOptionTypes,
additional_data: input.additional_data,
})
const typeIdEvents = transform({ shippingOptionTypes }, ({ shippingOptionTypes }) => {
return shippingOptionTypes.map((v) => {
return { id: v.id }
})
})
emitEventStep({
eventName: ShippingOptionTypeWorkflowEvents.CREATED,
data: typeIdEvents,
})
return new WorkflowResponse(shippingOptionTypes, {
hooks: [shippingOptionTypesCreated],
})
}
)
@@ -0,0 +1,76 @@
import { Modules, ShippingOptionTypeWorkflowEvents } from "@medusajs/framework/utils"
import {
createHook,
createWorkflow,
parallelize,
transform,
WorkflowData,
WorkflowResponse,
} from "@medusajs/framework/workflows-sdk"
import { emitEventStep } from "../../common/steps/emit-event"
import { removeRemoteLinkStep } from "../../common/steps/remove-remote-links"
import { deleteShippingOptionTypesStep } from "../steps"
/**
* The data to delete one or more shipping option types.
*/
export type DeleteShippingOptionTypesWorkflowInput = {
/**
* The IDs of the types to delete.
*/
ids: string[]
}
export const deleteShippingOptionTypesWorkflowId = "delete-shipping-option-types"
/**
* This workflow deletes one or more shipping-option types. It's used by the
* [Delete Shipping Option Types Admin API Route](TODO HERE).
*
* This workflow has a hook that allows you to perform custom actions after the shipping-option types are deleted. For example,
* you can delete custom records linked to the shipping-option types.
*
* You can also use this workflow within your own custom workflows, allowing you to wrap custom logic around shipping option type deletion.
*
* @example
* const { result } = await deleteShippingOptionTypesWorkflow(container)
* .run({
* input: {
* ids: ["sotype_123"],
* }
* })
*
* @summary
*
* Delete one or more shippingOption types.
*
* @property hooks.shippingOptionTypesDeleted - This hook is executed after the types are deleted. You can consume this hook to perform custom actions on the deleted types.
*/
export const deleteShippingOptionTypesWorkflow = createWorkflow(
deleteShippingOptionTypesWorkflowId,
(input: WorkflowData<DeleteShippingOptionTypesWorkflowInput>) => {
const deletedShippingOptionTypes = deleteShippingOptionTypesStep(input.ids)
const shippingOptionTypesDeleted = createHook("shippingOptionTypesDeleted", {
ids: input.ids,
})
const typeIdEvents = transform({ input }, ({ input }) => {
return input.ids?.map((id) => {
return { id }
})
})
parallelize(
removeRemoteLinkStep({
[Modules.FULFILLMENT]: { shipping_option_type_id: input.ids },
}),
emitEventStep({
eventName: ShippingOptionTypeWorkflowEvents.DELETED,
data: typeIdEvents,
})
)
return new WorkflowResponse(deletedShippingOptionTypes, {
hooks: [shippingOptionTypesDeleted],
})
}
)
@@ -0,0 +1,3 @@
export * from "./create-shipping-option-types"
export * from "./delete-shipping-option-types"
export * from "./update-shipping-option-types"
@@ -0,0 +1,90 @@
import { AdditionalData, FulfillmentTypes } from "@medusajs/framework/types"
import { ShippingOptionTypeWorkflowEvents } from "@medusajs/framework/utils"
import {
createHook,
createWorkflow,
transform,
WorkflowData,
WorkflowResponse,
} from "@medusajs/framework/workflows-sdk"
import { emitEventStep } from "../../common/steps/emit-event"
import { updateShippingOptionTypesStep } from "../steps"
/**
* The data to update one or more shipping option types, along with custom data that's passed to the workflow's hooks.
*/
type UpdateShippingOptionTypesWorkflowInput = {
/**
* The filters to select the shipping option types to update.
*/
selector: FulfillmentTypes.FilterableShippingOptionTypeProps
/**
* The data to update in the shipping option types.
*/
update: FulfillmentTypes.UpdateShippingOptionTypeDTO
} & AdditionalData
export const updateShippingOptionTypesWorkflowId = "update-shipping-option-types"
/**
* This workflow updates one or more shipping option types. It's used by the
* [Update Shipping Option Type Admin API Route](TODO HERE).
*
* This workflow has a hook that allows you to perform custom actions on the updated shipping option types. For example, you can pass under `additional_data` custom data that
* allows you to update custom data models linked to the shipping option types.
*
* You can also use this workflow within your own custom workflows, allowing you to wrap custom logic around shipping option type updates.
*
* @example
* const { result } = await updateShippingOptionTypesWorkflow(container)
* .run({
* input: {
* selector: {
* id: "ptyp_123"
* },
* update: {
* value: "clothing"
* },
* additional_data: {
* erp_id: "123"
* }
* }
* })
*
* @summary
*
* Update one or more shipping option types.
*
* @property hooks.shippingOptionTypesUpdated - This hook is executed after the shipping option types are updated. You can consume this hook to perform custom actions on the updated shipping option types.
*/
export const updateShippingOptionTypesWorkflow = createWorkflow(
updateShippingOptionTypesWorkflowId,
(input: WorkflowData<UpdateShippingOptionTypesWorkflowInput>) => {
const updatedShippingOptionTypes = updateShippingOptionTypesStep(input)
const shippingOptionTypesUpdated = createHook("shippingOptionTypesUpdated", {
shipping_option_types: updatedShippingOptionTypes,
additional_data: input.additional_data,
})
const typeIdEvents = transform(
{ updatedShippingOptionTypes },
({ updatedShippingOptionTypes }) => {
const arr = Array.isArray(updatedShippingOptionTypes)
? updatedShippingOptionTypes
: [updatedShippingOptionTypes]
return arr?.map((v) => {
return { id: v.id }
})
}
)
emitEventStep({
eventName: ShippingOptionTypeWorkflowEvents.UPDATED,
data: typeIdEvents,
})
return new WorkflowResponse(updatedShippingOptionTypes, {
hooks: [shippingOptionTypesUpdated],
})
}
)