From 8ae31aff4b23c980a2ecc8300a75798c263f0298 Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Mon, 7 Aug 2023 18:22:22 +0200 Subject: [PATCH] feat(workflows):product handlers should be reusable in different context (#4703) **What** The listProducts handler should not be specific to the workflow. Same for the product removal, shouldn't expect an entire DTO but just a collection of object that at least contains the id. Same principle applied to other product handlers --- .changeset/curly-ways-confess.md | 5 +++++ .../src/definition/create-products.ts | 8 ++++++- .../attach-sales-channel-to-products.ts | 13 +++++++----- .../attach-shipping-profile-to-products.ts | 11 +++++----- .../src/handlers/product/create-products.ts | 8 ++++--- .../detach-sales-channel-from-products.ts | 11 +++++----- .../detach-shipping-profile-from-products.ts | 11 +++++----- .../src/handlers/product/list-products.ts | 21 +++++++++++-------- .../src/handlers/product/remove-products.ts | 4 +++- .../update-products-variants-prices.ts | 15 ++++++------- 10 files changed, 66 insertions(+), 41 deletions(-) create mode 100644 .changeset/curly-ways-confess.md diff --git a/.changeset/curly-ways-confess.md b/.changeset/curly-ways-confess.md new file mode 100644 index 0000000000..6f6aaf316e --- /dev/null +++ b/.changeset/curly-ways-confess.md @@ -0,0 +1,5 @@ +--- +"@medusajs/workflows": patch +--- + +feat(workflows): list product should be reusable in different context diff --git a/packages/workflows/src/definition/create-products.ts b/packages/workflows/src/definition/create-products.ts index 714b4a1555..eb86dd4e13 100644 --- a/packages/workflows/src/definition/create-products.ts +++ b/packages/workflows/src/definition/create-products.ts @@ -275,10 +275,16 @@ const handlers = new Map([ }, { from: Actions.createProducts, - alias: ProductHandlers.listProducts.aliases.products, + alias: "products", }, ], }, + async ({ data }) => { + return { + alias: ProductHandlers.listProducts.aliases.ids, + value: data.products.map((product) => product.id), + } + }, aggregateData(), ProductHandlers.listProducts ), diff --git a/packages/workflows/src/handlers/product/attach-sales-channel-to-products.ts b/packages/workflows/src/handlers/product/attach-sales-channel-to-products.ts index 805fbaabf5..b028ca76bf 100644 --- a/packages/workflows/src/handlers/product/attach-sales-channel-to-products.ts +++ b/packages/workflows/src/handlers/product/attach-sales-channel-to-products.ts @@ -1,17 +1,20 @@ -import { ProductTypes } from "@medusajs/types" import { WorkflowArguments } from "../../helper" type ProductHandle = string type SalesChannelId = string +type PartialProduct = { handle: string; id: string } + +type HandlerInput = { + productsHandleSalesChannelsMap: Map + products: PartialProduct[] +} + export async function attachSalesChannelToProducts({ container, context, data, -}: WorkflowArguments<{ - productsHandleSalesChannelsMap: Map - products: ProductTypes.ProductDTO[] -}>): Promise { +}: WorkflowArguments): Promise { const { manager } = context const productsHandleSalesChannelsMap = data.productsHandleSalesChannelsMap const products = data.products diff --git a/packages/workflows/src/handlers/product/attach-shipping-profile-to-products.ts b/packages/workflows/src/handlers/product/attach-shipping-profile-to-products.ts index df634412d6..6f527767aa 100644 --- a/packages/workflows/src/handlers/product/attach-shipping-profile-to-products.ts +++ b/packages/workflows/src/handlers/product/attach-shipping-profile-to-products.ts @@ -1,17 +1,18 @@ -import { ProductTypes } from "@medusajs/types" import { WorkflowArguments } from "../../helper" type ProductHandle = string type ShippingProfileId = string +type PartialProduct = { handle: string; id: string } +type handlerInput = { + productsHandleShippingProfileIdMap: Map + products: PartialProduct[] +} export async function attachShippingProfileToProducts({ container, context, data, -}: WorkflowArguments<{ - productsHandleShippingProfileIdMap: Map - products: ProductTypes.ProductDTO[] -}>): Promise { +}: WorkflowArguments): Promise { const { manager } = context const productsHandleShippingProfileIdMap = diff --git a/packages/workflows/src/handlers/product/create-products.ts b/packages/workflows/src/handlers/product/create-products.ts index 7c5cfb60fe..61c305f319 100644 --- a/packages/workflows/src/handlers/product/create-products.ts +++ b/packages/workflows/src/handlers/product/create-products.ts @@ -2,12 +2,14 @@ import { ProductTypes } from "@medusajs/types" import { WorkflowArguments } from "../../helper" import { Modules, ModulesDefinition } from "@medusajs/modules-sdk" +type HandlerInput = { + products: ProductTypes.CreateProductDTO[] +} + export async function createProducts({ container, data, -}: WorkflowArguments<{ - products: ProductTypes.CreateProductDTO[] -}>): Promise { +}: WorkflowArguments): Promise { const data_ = data.products const productModuleService: ProductTypes.IProductModuleService = diff --git a/packages/workflows/src/handlers/product/detach-sales-channel-from-products.ts b/packages/workflows/src/handlers/product/detach-sales-channel-from-products.ts index 526e32f617..42e26ae4de 100644 --- a/packages/workflows/src/handlers/product/detach-sales-channel-from-products.ts +++ b/packages/workflows/src/handlers/product/detach-sales-channel-from-products.ts @@ -1,17 +1,18 @@ -import { ProductTypes } from "@medusajs/types" import { WorkflowArguments } from "../../helper" type ProductHandle = string type SalesChannelId = string +type PartialProduct = { handle: string; id: string } +type HandlerInput = { + productsHandleSalesChannelsMap: Map + products: PartialProduct[] +} export async function detachSalesChannelFromProducts({ container, context, data, -}: WorkflowArguments<{ - productsHandleSalesChannelsMap: Map - products: ProductTypes.ProductDTO[] -}>): Promise { +}: WorkflowArguments): Promise { const { manager } = context const productsHandleSalesChannelsMap = data.productsHandleSalesChannelsMap const products = data.products diff --git a/packages/workflows/src/handlers/product/detach-shipping-profile-from-products.ts b/packages/workflows/src/handlers/product/detach-shipping-profile-from-products.ts index 300b1bafb9..bc9d34721d 100644 --- a/packages/workflows/src/handlers/product/detach-shipping-profile-from-products.ts +++ b/packages/workflows/src/handlers/product/detach-shipping-profile-from-products.ts @@ -1,17 +1,18 @@ -import { ProductTypes } from "@medusajs/types" import { WorkflowArguments } from "../../helper" type ProductHandle = string type ShippingProfileId = string +type PartialProduct = { handle: string; id: string } +type HandlerInput = { + productsHandleShippingProfileIdMap: Map + products: PartialProduct[] +} export async function detachShippingProfileFromProducts({ container, context, data, -}: WorkflowArguments<{ - productsHandleShippingProfileIdMap: Map - products: ProductTypes.ProductDTO[] -}>): Promise { +}: WorkflowArguments): Promise { const { manager } = context const productsHandleShippingProfileIdMap = data.productsHandleShippingProfileIdMap diff --git a/packages/workflows/src/handlers/product/list-products.ts b/packages/workflows/src/handlers/product/list-products.ts index 9d0d47be76..fe2b783b8e 100644 --- a/packages/workflows/src/handlers/product/list-products.ts +++ b/packages/workflows/src/handlers/product/list-products.ts @@ -1,17 +1,20 @@ import { ProductTypes, WorkflowTypes } from "@medusajs/types" import { WorkflowArguments } from "../../helper" +type HandlerInput = { + ids: string[] + config?: WorkflowTypes.CommonWorkflow.WorkflowInputConfig +} + export async function listProducts({ container, context, data, -}: WorkflowArguments<{ - products: ProductTypes.ProductDTO[] - config?: WorkflowTypes.CommonWorkflow.WorkflowInputConfig -}>): Promise { +}: // TODO: should return product DTO or priced product but needs to be created in the types package +WorkflowArguments): Promise { const { manager } = context - const products = data.products + const productIds = data.ids const listConfig = data.config?.listConfig ?? {} const productService = container.resolve("productService") @@ -30,15 +33,15 @@ export async function listProducts({ Object.assign(config, { relations: listConfig.relations }) } - const rawProduct = await productService + const rawProducts = await productService .withTransaction(manager as any) - .retrieve(products[0].id, shouldUseConfig ? config : undefined) + .list({ id: productIds }, shouldUseConfig ? config : undefined) return await pricingService .withTransaction(manager as any) - .setProductPrices([rawProduct]) + .setProductPrices(rawProducts) } listProducts.aliases = { - products: "products", + ids: "ids", } diff --git a/packages/workflows/src/handlers/product/remove-products.ts b/packages/workflows/src/handlers/product/remove-products.ts index 337dfa92b4..ddfe486b53 100644 --- a/packages/workflows/src/handlers/product/remove-products.ts +++ b/packages/workflows/src/handlers/product/remove-products.ts @@ -2,10 +2,12 @@ import { ProductTypes } from "@medusajs/types" import { WorkflowArguments } from "../../helper" import { Modules, ModulesDefinition } from "@medusajs/modules-sdk" +type HandlerInput = { products: { id: string }[] } + export async function removeProducts({ container, data, -}: WorkflowArguments<{ products: ProductTypes.ProductDTO[] }>): Promise { +}: WorkflowArguments): Promise { if (!data.products.length) { return } diff --git a/packages/workflows/src/handlers/product/update-products-variants-prices.ts b/packages/workflows/src/handlers/product/update-products-variants-prices.ts index e02248d6b1..a7dc754184 100644 --- a/packages/workflows/src/handlers/product/update-products-variants-prices.ts +++ b/packages/workflows/src/handlers/product/update-products-variants-prices.ts @@ -7,18 +7,19 @@ type VariantIndexAndPrices = { index: number prices: WorkflowTypes.ProductWorkflow.CreateProductVariantPricesInputDTO[] } - -export async function updateProductsVariantsPrices({ - container, - context, - data, -}: WorkflowArguments<{ +type HandlerInput = { productsHandleVariantsIndexPricesMap: Map< ProductHandle, VariantIndexAndPrices[] > products: ProductTypes.ProductDTO[] -}>) { +} + +export async function updateProductsVariantsPrices({ + container, + context, + data, +}: WorkflowArguments) { const { manager } = context const products = data.products const productsHandleVariantsIndexPricesMap =