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
This commit is contained in:
Adrien de Peretti
2023-08-07 18:22:22 +02:00
committed by GitHub
parent b396168dfd
commit 8ae31aff4b
10 changed files with 66 additions and 41 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/workflows": patch
---
feat(workflows): list product should be reusable in different context

View File

@@ -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
),

View File

@@ -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<ProductHandle, SalesChannelId[]>
products: PartialProduct[]
}
export async function attachSalesChannelToProducts({
container,
context,
data,
}: WorkflowArguments<{
productsHandleSalesChannelsMap: Map<ProductHandle, SalesChannelId[]>
products: ProductTypes.ProductDTO[]
}>): Promise<void> {
}: WorkflowArguments<HandlerInput>): Promise<void> {
const { manager } = context
const productsHandleSalesChannelsMap = data.productsHandleSalesChannelsMap
const products = data.products

View File

@@ -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<ProductHandle, ShippingProfileId>
products: PartialProduct[]
}
export async function attachShippingProfileToProducts({
container,
context,
data,
}: WorkflowArguments<{
productsHandleShippingProfileIdMap: Map<ProductHandle, ShippingProfileId>
products: ProductTypes.ProductDTO[]
}>): Promise<void> {
}: WorkflowArguments<handlerInput>): Promise<void> {
const { manager } = context
const productsHandleShippingProfileIdMap =

View File

@@ -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<ProductTypes.ProductDTO[]> {
}: WorkflowArguments<HandlerInput>): Promise<ProductTypes.ProductDTO[]> {
const data_ = data.products
const productModuleService: ProductTypes.IProductModuleService =

View File

@@ -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<ProductHandle, SalesChannelId[]>
products: PartialProduct[]
}
export async function detachSalesChannelFromProducts({
container,
context,
data,
}: WorkflowArguments<{
productsHandleSalesChannelsMap: Map<ProductHandle, SalesChannelId[]>
products: ProductTypes.ProductDTO[]
}>): Promise<void> {
}: WorkflowArguments<HandlerInput>): Promise<void> {
const { manager } = context
const productsHandleSalesChannelsMap = data.productsHandleSalesChannelsMap
const products = data.products

View File

@@ -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<ProductHandle, ShippingProfileId>
products: PartialProduct[]
}
export async function detachShippingProfileFromProducts({
container,
context,
data,
}: WorkflowArguments<{
productsHandleShippingProfileIdMap: Map<ProductHandle, ShippingProfileId>
products: ProductTypes.ProductDTO[]
}>): Promise<void> {
}: WorkflowArguments<HandlerInput>): Promise<void> {
const { manager } = context
const productsHandleShippingProfileIdMap =
data.productsHandleShippingProfileIdMap

View File

@@ -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<ProductTypes.ProductDTO[]> {
}: // TODO: should return product DTO or priced product but needs to be created in the types package
WorkflowArguments<HandlerInput>): Promise<ProductTypes.ProductDTO[]> {
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",
}

View File

@@ -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<void> {
}: WorkflowArguments<HandlerInput>): Promise<void> {
if (!data.products.length) {
return
}

View File

@@ -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<HandlerInput>) {
const { manager } = context
const products = data.products
const productsHandleVariantsIndexPricesMap =