feat(core-flows,medusa,types): fulfillment API: create (#7101)

what:

- adds fulfillment create API

RESOLVES CORE-1962
This commit is contained in:
Riqwan Thamir authored and GitHub committed 2024-04-23 08:35:44 +00:00
1 parent 14a7378375
commit 18f3aacee6
26 files changed
+1037 -14

No files matched your search

@@ -0,0 +1,28 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { FulfillmentTypes, IFulfillmentModuleService } from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
export const createFulfillmentStepId = "create-fulfillment"
export const createFulfillmentStep = createStep(
createFulfillmentStepId,
async (data: FulfillmentTypes.CreateFulfillmentDTO, { container }) => {
const service = container.resolve<IFulfillmentModuleService>(
ModuleRegistrationName.FULFILLMENT
)
const fulfillment = await service.createFulfillment(data)
return new StepResponse(fulfillment, fulfillment.id)
},
async (id, { container }) => {
if (!id) {
return
}
const service = container.resolve<IFulfillmentModuleService>(
ModuleRegistrationName.FULFILLMENT
)
await service.cancelFulfillment(id)
}
)
@@ -1,6 +1,7 @@
export * from "./add-rules-to-fulfillment-shipping-option"
export * from "./add-shipping-options-prices"
export * from "./cancel-fulfillment"
export * from "./create-fulfillment"
export * from "./create-fulfillment-set"
export * from "./create-service-zones"
export * from "./create-shipping-profiles"
@@ -9,4 +10,6 @@ export * from "./delete-service-zones"
export * from "./delete-shipping-options"
export * from "./remove-rules-from-fulfillment-shipping-option"
export * from "./set-shipping-options-prices"
export * from "./update-fulfillment"
export * from "./upsert-shipping-options"
export * from "./validate-shipment"
@@ -0,0 +1,35 @@
import { FulfillmentWorkflow } from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { updateFulfillmentWorkflow } from "../workflows/update-fulfillment"
export const updateFulfillmentWorkflowStepId = "update-fulfillment-workflow"
export const updateFulfillmentWorkflowStep = createStep(
updateFulfillmentWorkflowStepId,
async (
data: FulfillmentWorkflow.UpdateFulfillmentWorkflowInput,
{ container }
) => {
const {
transaction,
result: updated,
errors,
} = await updateFulfillmentWorkflow(container).run({
input: data,
throwOnError: false,
})
if (Array.isArray(errors) && errors[0]) {
throw errors[0].error
}
return new StepResponse(updated, transaction)
},
async (transaction, { container }) => {
if (!transaction) {
return
}
await updateFulfillmentWorkflow(container).cancel({ transaction })
}
)
@@ -0,0 +1,42 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { FulfillmentWorkflow, IFulfillmentModuleService } from "@medusajs/types"
import { getSelectsAndRelationsFromObjectArray } from "@medusajs/utils"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
export const updateFulfillmentStepId = "update-fulfillment"
export const updateFulfillmentStep = createStep(
updateFulfillmentStepId,
async (
input: FulfillmentWorkflow.UpdateFulfillmentWorkflowInput,
{ container }
) => {
const { id, ...data } = input
const service = container.resolve<IFulfillmentModuleService>(
ModuleRegistrationName.FULFILLMENT
)
const { selects, relations } = getSelectsAndRelationsFromObjectArray([data])
const fulfillment = await service.retrieveFulfillment(id, {
select: selects,
relations,
})
await service.updateFulfillment(id, data)
return new StepResponse(void 0, fulfillment)
},
async (fulfillment, { container }) => {
if (!fulfillment) {
return
}
const service = container.resolve<IFulfillmentModuleService>(
ModuleRegistrationName.FULFILLMENT
)
const { id, ...data } = fulfillment
// TODO: this does not revert the relationships that are created in invoke step
// There should be a consistent way to handle across workflows
await service.updateFulfillment(id, data)
}
)
@@ -0,0 +1,40 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { IFulfillmentModuleService } from "@medusajs/types"
import { MedusaError } from "@medusajs/utils"
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
export const validateShipmentStepId = "validate-shipment"
export const validateShipmentStep = createStep(
validateShipmentStepId,
async (id: string, { container }) => {
const service = container.resolve<IFulfillmentModuleService>(
ModuleRegistrationName.FULFILLMENT
)
const fulfillment = await service.retrieveFulfillment(id, {
select: ["shipped_at", "canceled_at", "shipping_option_id"],
})
if (fulfillment.shipped_at) {
throw new MedusaError(
MedusaError.Types.NOT_ALLOWED,
"Shipment has already been created"
)
}
if (fulfillment.canceled_at) {
throw new MedusaError(
MedusaError.Types.NOT_ALLOWED,
"Cannot create shipment for a canceled fulfillment"
)
}
if (!fulfillment.shipping_option_id) {
throw new MedusaError(
MedusaError.Types.NOT_ALLOWED,
"Cannot create shipment without a Shipping Option"
)
}
return new StepResponse(void 0)
}
)
@@ -0,0 +1,13 @@
import { FulfillmentDTO, FulfillmentWorkflow } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { createFulfillmentStep } from "../steps"
export const createFulfillmentWorkflowId = "create-fulfillment-workflow"
export const createFulfillmentWorkflow = createWorkflow(
createFulfillmentWorkflowId,
(
input: WorkflowData<FulfillmentWorkflow.CreateFulfillmentWorkflowInput>
): WorkflowData<FulfillmentDTO> => {
return createFulfillmentStep(input)
}
)
@@ -0,0 +1,25 @@
import { FulfillmentWorkflow } from "@medusajs/types"
import {
WorkflowData,
createWorkflow,
transform,
} from "@medusajs/workflows-sdk"
import { validateShipmentStep } from "../steps"
import { updateFulfillmentWorkflowStep } from "../steps/update-fulfillment-workflow"
export const createShipmentWorkflowId = "create-shipment-workflow"
export const createShipmentWorkflow = createWorkflow(
createShipmentWorkflowId,
(
input: WorkflowData<FulfillmentWorkflow.CreateShipmentWorkflowInput>
): WorkflowData<void> => {
validateShipmentStep(input.id)
const update = transform({ input }, (data) => ({
...data.input,
shipped_at: new Date(),
}))
updateFulfillmentWorkflowStep(update)
}
)
@@ -1,11 +1,14 @@
export * from "./add-rules-to-fulfillment-shipping-option"
export * from "./cancel-fulfillment"
export * from "./create-fulfillment"
export * from "./create-service-zones"
export * from "./create-shipment"
export * from "./create-shipping-options"
export * from "./create-shipping-profiles"
export * from "./delete-fulfillment-sets"
export * from "./delete-service-zones"
export * from "./delete-shipping-options"
export * from "./remove-rules-from-fulfillment-shipping-option"
export * from "./update-fulfillment"
export * from "./update-service-zones"
export * from "./update-shipping-options"
@@ -0,0 +1,13 @@
import { FulfillmentWorkflow } from "@medusajs/types"
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
import { updateFulfillmentStep } from "../steps"
export const updateFulfillmentWorkflowId = "update-fulfillment-workflow"
export const updateFulfillmentWorkflow = createWorkflow(
updateFulfillmentWorkflowId,
(
input: WorkflowData<FulfillmentWorkflow.UpdateFulfillmentWorkflowInput>
): WorkflowData<void> => {
updateFulfillmentStep(input)
}
)