feat(medusa,workflows) Create cart workflow (#4685)

* chore: add baseline test for create cart

* chore: add basic paths into handlers + make first tests pass

* chore: move input alias to cart specific workflow

* chore: move data around into buckets

* chore: normalize handlers and introduce types

* chore: move aliases to handlers concern

* chore: add compensation step for create cart

* chore: merge with latest develop

* chore: handle error manually + type inputs

* chore: handle error manually

* chore: added types for each handler

* chore: remove addresses

* chore: added changset

* chore: undo package changes

* chore: added config settings to retreieve, cleanup of types

* chore: capitalize cart handlers

* chore: rename todo

* chore: add feature flag for workflow

* chore: reorder handlers

* chore: add logger to route handler

* chore: removed weird vscode moving around things

* chore: refactor handlers

* chore: refactor compensate step

* chore: changed poistion

* chore: aggregate config data

* chore: moved handlers to their own domain + pr review addressing

* chore: address pr reviews

* chore: move types to type package

* chore: update type to include config

* chore: remove error scoping
This commit is contained in:
Riqwan Thamir
2023-08-08 12:10:27 +02:00
committed by GitHub
parent a42c41e8ab
commit 281b0746cf
35 changed files with 1140 additions and 13 deletions

View File

@@ -0,0 +1,238 @@
import {
TransactionStepsDefinition,
WorkflowManager,
} from "@medusajs/orchestration"
import { CartWorkflow } from "@medusajs/types"
import { Workflows } from "../../definitions"
import {
AddressHandlers,
CartHandlers,
CommonHandlers,
CustomerHandlers,
RegionHandlers,
SalesChannelHandlers,
} from "../../handlers"
import { aggregateData, exportWorkflow, pipe } from "../../helper"
enum CreateCartActions {
setConfig = "setConfig",
setContext = "setContext",
attachLineItems = "attachLineItems",
findRegion = "findRegion",
findSalesChannel = "findSalesChannel",
createCart = "createCart",
findOrCreateAddresses = "findOrCreateAddresses",
findOrCreateCustomer = "findOrCreateCustomer",
removeCart = "removeCart",
removeAddresses = "removeAddresses",
retrieveCart = "retrieveCart",
}
const workflowAlias = "cart"
const getWorkflowInput = (alias = workflowAlias) => ({
inputAlias: workflowAlias,
invoke: {
from: workflowAlias,
alias,
},
})
const workflowSteps: TransactionStepsDefinition = {
next: [
{
action: CreateCartActions.setConfig,
noCompensation: true,
},
{
action: CreateCartActions.findOrCreateCustomer,
noCompensation: true,
},
{
action: CreateCartActions.findSalesChannel,
noCompensation: true,
},
{
action: CreateCartActions.setContext,
noCompensation: true,
},
{
action: CreateCartActions.findRegion,
noCompensation: true,
next: {
action: CreateCartActions.findOrCreateAddresses,
noCompensation: true,
next: {
action: CreateCartActions.createCart,
next: {
action: CreateCartActions.attachLineItems,
noCompensation: true,
next: {
action: CreateCartActions.retrieveCart,
noCompensation: true,
},
},
},
},
},
],
}
const handlers = new Map([
[
CreateCartActions.setConfig,
{
invoke: pipe(
getWorkflowInput(CommonHandlers.setConfig.aliases.Config),
aggregateData(),
CommonHandlers.setConfig
),
},
],
[
CreateCartActions.findOrCreateCustomer,
{
invoke: pipe(
getWorkflowInput(
CustomerHandlers.findOrCreateCustomer.aliases.Customer
),
CustomerHandlers.findOrCreateCustomer
),
},
],
[
CreateCartActions.findSalesChannel,
{
invoke: pipe(
getWorkflowInput(
SalesChannelHandlers.findSalesChannel.aliases.SalesChannel
),
SalesChannelHandlers.findSalesChannel
),
},
],
[
CreateCartActions.setContext,
{
invoke: pipe(
getWorkflowInput(CommonHandlers.setContext.aliases.Context),
CommonHandlers.setContext
),
},
],
[
CreateCartActions.findRegion,
{
invoke: pipe(
getWorkflowInput(RegionHandlers.findRegion.aliases.Region),
RegionHandlers.findRegion
),
},
],
[
CreateCartActions.findOrCreateAddresses,
{
invoke: pipe(
{
invoke: [
getWorkflowInput(
AddressHandlers.findOrCreateAddresses.aliases.Addresses
).invoke,
{
from: CreateCartActions.findRegion,
alias: AddressHandlers.findOrCreateAddresses.aliases.Region,
},
],
},
AddressHandlers.findOrCreateAddresses
),
},
],
[
CreateCartActions.createCart,
{
invoke: pipe(
{
invoke: [
{
from: CreateCartActions.findRegion,
alias: CartHandlers.createCart.aliases.Region,
},
{
from: CreateCartActions.setContext,
alias: CartHandlers.createCart.aliases.Context,
},
{
from: CreateCartActions.findOrCreateCustomer,
alias: CartHandlers.createCart.aliases.Customer,
},
{
from: CreateCartActions.findOrCreateAddresses,
alias: CartHandlers.createCart.aliases.Addresses,
},
],
},
CartHandlers.createCart
),
compensate: pipe(
{
invoke: [
{
from: CreateCartActions.createCart,
alias: CartHandlers.removeCart.aliases.Cart,
},
],
},
CartHandlers.removeCart
),
},
],
[
CreateCartActions.attachLineItems,
{
invoke: pipe(
{
invoke: [
getWorkflowInput(
CartHandlers.attachLineItemsToCart.aliases.LineItems
).invoke,
{
from: CreateCartActions.createCart,
alias: CartHandlers.attachLineItemsToCart.aliases.Cart,
},
],
},
CartHandlers.attachLineItemsToCart
),
},
],
[
CreateCartActions.retrieveCart,
{
invoke: pipe(
{
invoke: [
{
from: CreateCartActions.setConfig,
alias: CommonHandlers.setConfig.aliases.Config,
},
{
from: CreateCartActions.createCart,
alias: CartHandlers.retrieveCart.aliases.Cart,
},
],
},
CartHandlers.retrieveCart
),
},
],
])
WorkflowManager.register(Workflows.CreateCart, workflowSteps, handlers)
type CreateCartWorkflowOutput = Record<any, any>
export const createCart = exportWorkflow<
CartWorkflow.CreateCartWorkflowInputDTO,
CreateCartWorkflowOutput
>(Workflows.CreateCart, CreateCartActions.retrieveCart)

View File

@@ -0,0 +1 @@
export * from "./create-cart"

View File

@@ -1 +1,2 @@
export * from "./create-products"
export * from "./cart"