feat(workflows): Data aggregation (#4732)

* apply the aggregator automatically

* add comment

* apply aggregate

* improve pipe aggregation

* improve test cases

* improvements

* clean tests

* renameing to merge

* fix merge apply

* move merge apply

* cleanup cart workflow and end point

* fixes and naming
This commit is contained in:
Adrien de Peretti
2023-08-10 14:01:56 +02:00
committed by GitHub
parent 2363a5324e
commit a268d2cb0b
12 changed files with 324 additions and 214 deletions
@@ -13,10 +13,9 @@ import {
RegionHandlers,
SalesChannelHandlers,
} from "../../handlers"
import { aggregateData, exportWorkflow, pipe } from "../../helper"
import { exportWorkflow, pipe } from "../../helper"
enum CreateCartActions {
setConfig = "setConfig",
setContext = "setContext",
attachLineItems = "attachLineItems",
findRegion = "findRegion",
@@ -26,7 +25,6 @@ enum CreateCartActions {
findOrCreateCustomer = "findOrCreateCustomer",
removeCart = "removeCart",
removeAddresses = "removeAddresses",
retrieveCart = "retrieveCart",
}
const workflowAlias = "cart"
@@ -40,10 +38,6 @@ const getWorkflowInput = (alias = workflowAlias) => ({
const workflowSteps: TransactionStepsDefinition = {
next: [
{
action: CreateCartActions.setConfig,
noCompensation: true,
},
{
action: CreateCartActions.findOrCreateCustomer,
noCompensation: true,
@@ -67,10 +61,6 @@ const workflowSteps: TransactionStepsDefinition = {
next: {
action: CreateCartActions.attachLineItems,
noCompensation: true,
next: {
action: CreateCartActions.retrieveCart,
noCompensation: true,
},
},
},
},
@@ -79,16 +69,6 @@ const workflowSteps: TransactionStepsDefinition = {
}
const handlers = new Map([
[
CreateCartActions.setConfig,
{
invoke: pipe(
getWorkflowInput(CommonHandlers.setConfig.aliases.Config),
aggregateData(),
CommonHandlers.setConfig
),
},
],
[
CreateCartActions.findOrCreateCustomer,
{
@@ -206,26 +186,6 @@ const handlers = new Map([
),
},
],
[
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)
@@ -235,4 +195,4 @@ type CreateCartWorkflowOutput = Record<any, any>
export const createCart = exportWorkflow<
CartWorkflow.CreateCartWorkflowInputDTO,
CreateCartWorkflowOutput
>(Workflows.CreateCart, CreateCartActions.retrieveCart)
>(Workflows.CreateCart, CreateCartActions.createCart)
@@ -3,7 +3,7 @@ import {
TransactionStepsDefinition,
WorkflowManager,
} from "@medusajs/orchestration"
import { aggregateData, exportWorkflow, pipe } from "../../helper"
import { exportWorkflow, pipe } from "../../helper"
import { ProductTypes, WorkflowTypes } from "@medusajs/types"
import {
@@ -57,12 +57,12 @@ const handlers = new Map([
{
invoke: pipe(
{
merge: true,
inputAlias: InputAlias.ProductsInputData,
invoke: {
from: InputAlias.ProductsInputData,
},
},
aggregateData(),
ProductHandlers.createProductsPrepareData
),
},
@@ -72,21 +72,21 @@ const handlers = new Map([
{
invoke: pipe(
{
merge: true,
invoke: {
from: CreateProductsActions.prepare,
},
},
aggregateData(),
ProductHandlers.createProducts
),
compensate: pipe(
{
merge: true,
invoke: {
from: CreateProductsActions.createProducts,
alias: ProductHandlers.removeProducts.aliases.products,
},
},
aggregateData(),
ProductHandlers.removeProducts
),
},
@@ -96,6 +96,7 @@ const handlers = new Map([
{
invoke: pipe(
{
merge: true,
invoke: [
{
from: CreateProductsActions.prepare,
@@ -108,11 +109,11 @@ const handlers = new Map([
},
],
},
aggregateData(),
ProductHandlers.attachShippingProfileToProducts
),
compensate: pipe(
{
merge: true,
invoke: [
{
from: CreateProductsActions.prepare,
@@ -125,7 +126,6 @@ const handlers = new Map([
},
],
},
aggregateData(),
ProductHandlers.detachShippingProfileFromProducts
),
},
@@ -135,6 +135,7 @@ const handlers = new Map([
{
invoke: pipe(
{
merge: true,
invoke: [
{
from: CreateProductsActions.prepare,
@@ -146,11 +147,11 @@ const handlers = new Map([
},
],
},
aggregateData(),
ProductHandlers.attachSalesChannelToProducts
),
compensate: pipe(
{
merge: true,
invoke: [
{
from: CreateProductsActions.prepare,
@@ -162,7 +163,6 @@ const handlers = new Map([
},
],
},
aggregateData(),
ProductHandlers.detachSalesChannelFromProducts
),
},
@@ -172,23 +172,23 @@ const handlers = new Map([
{
invoke: pipe(
{
merge: true,
invoke: {
from: CreateProductsActions.createProducts,
alias: InventoryHandlers.createInventoryItems.aliases.products,
},
},
aggregateData(),
InventoryHandlers.createInventoryItems
),
compensate: pipe(
{
merge: true,
invoke: {
from: CreateProductsActions.createInventoryItems,
alias:
InventoryHandlers.removeInventoryItems.aliases.inventoryItems,
},
},
aggregateData(),
InventoryHandlers.removeInventoryItems
),
},
@@ -198,24 +198,24 @@ const handlers = new Map([
{
invoke: pipe(
{
merge: true,
invoke: {
from: CreateProductsActions.createInventoryItems,
alias:
InventoryHandlers.attachInventoryItems.aliases.inventoryItems,
},
},
aggregateData(),
InventoryHandlers.attachInventoryItems
),
compensate: pipe(
{
merge: true,
invoke: {
from: CreateProductsActions.createInventoryItems,
alias:
InventoryHandlers.detachInventoryItems.aliases.inventoryItems,
},
},
aggregateData(),
InventoryHandlers.detachInventoryItems
),
},
@@ -225,6 +225,7 @@ const handlers = new Map([
{
invoke: pipe(
{
merge: true,
invoke: [
{
from: CreateProductsActions.prepare,
@@ -236,14 +237,18 @@ const handlers = new Map([
},
],
},
aggregateData(),
ProductHandlers.updateProductsVariantsPrices
),
compensate: pipe(
{
merge: true,
invoke: [
{
from: CreateProductsActions.prepare,
alias:
MiddlewaresHandlers
.createProductsPrepareCreatePricesCompensation.aliases
.preparedData,
},
{
from: CreateProductsActions.createProducts,
@@ -252,7 +257,6 @@ const handlers = new Map([
},
],
},
aggregateData(),
MiddlewaresHandlers.createProductsPrepareCreatePricesCompensation,
ProductHandlers.updateProductsVariantsPrices
),