feat(medusa, types, utils, workflow): Migrate medusa workflow to the workflow package (#4682)

This commit is contained in:
Adrien de Peretti
2023-08-05 16:03:45 +02:00
committed by GitHub
parent 433914ae01
commit dc46927bc6
68 changed files with 1224 additions and 1064 deletions
@@ -3,46 +3,284 @@ import {
TransactionStepsDefinition,
WorkflowManager,
} from "@medusajs/orchestration"
import {
createProducts as createProductsHandler,
removeProducts,
} from "../handlers"
import { exportWorkflow, pipe } from "../helper"
import { ProductTypes } from "@medusajs/types"
import { ProductTypes, WorkflowTypes } from "@medusajs/types"
import {
InventoryHandlers,
MiddlewaresHandlers,
ProductHandlers,
} from "../handlers"
import { aggregateData } from "../helper/aggregate"
enum Actions {
export enum Actions {
prepare = "prepare",
createProducts = "createProducts",
attachToSalesChannel = "attachToSalesChannel",
attachShippingProfile = "attachShippingProfile",
createPrices = "createPrices",
createInventoryItems = "createInventoryItems",
attachInventoryItems = "attachInventoryItems",
result = "result",
}
const workflowSteps: TransactionStepsDefinition = {
export const workflowSteps: TransactionStepsDefinition = {
next: {
action: Actions.createProducts,
action: Actions.prepare,
noCompensation: true,
next: {
action: Actions.createProducts,
next: [
{
action: Actions.attachShippingProfile,
saveResponse: false,
},
{
action: Actions.attachToSalesChannel,
saveResponse: false,
},
{
action: Actions.createPrices,
next: {
action: Actions.createInventoryItems,
next: {
action: Actions.attachInventoryItems,
next: {
action: Actions.result,
noCompensation: true,
},
},
},
},
],
},
},
}
const handlers = new Map([
[
Actions.prepare,
{
invoke: pipe(
{
inputAlias: InputAlias.ProductsInputData,
invoke: {
from: InputAlias.ProductsInputData,
},
},
aggregateData(),
MiddlewaresHandlers.createProductsPrepareData
),
},
],
[
Actions.createProducts,
{
invoke: pipe(
{
inputAlias: InputAlias.Products,
invoke: {
from: InputAlias.Products,
alias: InputAlias.Products,
from: Actions.prepare,
},
},
createProductsHandler
aggregateData(),
ProductHandlers.createProducts
),
compensate: pipe(
{
invoke: {
from: Actions.createProducts,
alias: InputAlias.Products,
alias: ProductHandlers.removeProducts.aliases.products,
},
},
removeProducts
aggregateData(),
ProductHandlers.removeProducts
),
},
],
[
Actions.attachShippingProfile,
{
invoke: pipe(
{
invoke: [
{
from: Actions.prepare,
},
{
from: Actions.createProducts,
alias:
ProductHandlers.attachShippingProfileToProducts.aliases
.products,
},
],
},
aggregateData(),
ProductHandlers.attachShippingProfileToProducts
),
compensate: pipe(
{
invoke: [
{
from: Actions.prepare,
},
{
from: Actions.createProducts,
alias:
ProductHandlers.detachShippingProfileFromProducts.aliases
.products,
},
],
},
aggregateData(),
ProductHandlers.detachShippingProfileFromProducts
),
},
],
[
Actions.attachToSalesChannel,
{
invoke: pipe(
{
invoke: [
{
from: Actions.prepare,
},
{
from: Actions.createProducts,
alias:
ProductHandlers.attachSalesChannelToProducts.aliases.products,
},
],
},
aggregateData(),
ProductHandlers.attachSalesChannelToProducts
),
compensate: pipe(
{
invoke: [
{
from: Actions.prepare,
},
{
from: Actions.createProducts,
alias:
ProductHandlers.detachSalesChannelFromProducts.aliases.products,
},
],
},
aggregateData(),
ProductHandlers.detachSalesChannelFromProducts
),
},
],
[
Actions.createInventoryItems,
{
invoke: pipe(
{
invoke: {
from: Actions.createProducts,
alias: InventoryHandlers.createInventoryItems.aliases.products,
},
},
aggregateData(),
InventoryHandlers.createInventoryItems
),
compensate: pipe(
{
invoke: {
from: Actions.createInventoryItems,
alias:
InventoryHandlers.removeInventoryItems.aliases.inventoryItems,
},
},
aggregateData(),
InventoryHandlers.removeInventoryItems
),
},
],
[
Actions.attachInventoryItems,
{
invoke: pipe(
{
invoke: {
from: Actions.createInventoryItems,
alias:
InventoryHandlers.attachInventoryItems.aliases.inventoryItems,
},
},
aggregateData(),
InventoryHandlers.attachInventoryItems
),
compensate: pipe(
{
invoke: {
from: Actions.createInventoryItems,
alias:
InventoryHandlers.detachInventoryItems.aliases.inventoryItems,
},
},
aggregateData(),
InventoryHandlers.detachInventoryItems
),
},
],
[
Actions.createPrices,
{
invoke: pipe(
{
invoke: [
{
from: Actions.prepare,
},
{
from: Actions.createProducts,
alias:
ProductHandlers.updateProductsVariantsPrices.aliases.products,
},
],
},
aggregateData(),
ProductHandlers.updateProductsVariantsPrices
),
compensate: pipe(
{
invoke: [
{
from: Actions.prepare,
},
{
from: Actions.createProducts,
alias:
ProductHandlers.updateProductsVariantsPrices.aliases.products,
},
],
},
aggregateData(),
MiddlewaresHandlers.createProductsPrepareCreatePricesCompensation,
ProductHandlers.updateProductsVariantsPrices
),
},
],
[
Actions.result,
{
invoke: pipe(
{
invoke: [
{
from: Actions.prepare,
},
{
from: Actions.createProducts,
alias: ProductHandlers.listProducts.aliases.products,
},
],
},
aggregateData(),
ProductHandlers.listProducts
),
},
],
@@ -51,6 +289,6 @@ const handlers = new Map([
WorkflowManager.register(Workflows.CreateProducts, workflowSteps, handlers)
export const createProducts = exportWorkflow<
ProductTypes.CreateProductDTO[],
WorkflowTypes.ProductWorkflow.CreateProductsWorkflowInputDTO,
ProductTypes.ProductDTO[]
>(Workflows.CreateProducts, Actions.createProducts)
>(Workflows.CreateProducts, Actions.result)