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:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/workflows": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat(workflows): list product should be reusable in different context
|
||||||
@@ -275,10 +275,16 @@ const handlers = new Map([
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
from: Actions.createProducts,
|
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(),
|
aggregateData(),
|
||||||
ProductHandlers.listProducts
|
ProductHandlers.listProducts
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -1,17 +1,20 @@
|
|||||||
import { ProductTypes } from "@medusajs/types"
|
|
||||||
import { WorkflowArguments } from "../../helper"
|
import { WorkflowArguments } from "../../helper"
|
||||||
|
|
||||||
type ProductHandle = string
|
type ProductHandle = string
|
||||||
type SalesChannelId = string
|
type SalesChannelId = string
|
||||||
|
|
||||||
|
type PartialProduct = { handle: string; id: string }
|
||||||
|
|
||||||
|
type HandlerInput = {
|
||||||
|
productsHandleSalesChannelsMap: Map<ProductHandle, SalesChannelId[]>
|
||||||
|
products: PartialProduct[]
|
||||||
|
}
|
||||||
|
|
||||||
export async function attachSalesChannelToProducts({
|
export async function attachSalesChannelToProducts({
|
||||||
container,
|
container,
|
||||||
context,
|
context,
|
||||||
data,
|
data,
|
||||||
}: WorkflowArguments<{
|
}: WorkflowArguments<HandlerInput>): Promise<void> {
|
||||||
productsHandleSalesChannelsMap: Map<ProductHandle, SalesChannelId[]>
|
|
||||||
products: ProductTypes.ProductDTO[]
|
|
||||||
}>): Promise<void> {
|
|
||||||
const { manager } = context
|
const { manager } = context
|
||||||
const productsHandleSalesChannelsMap = data.productsHandleSalesChannelsMap
|
const productsHandleSalesChannelsMap = data.productsHandleSalesChannelsMap
|
||||||
const products = data.products
|
const products = data.products
|
||||||
|
|||||||
@@ -1,17 +1,18 @@
|
|||||||
import { ProductTypes } from "@medusajs/types"
|
|
||||||
import { WorkflowArguments } from "../../helper"
|
import { WorkflowArguments } from "../../helper"
|
||||||
|
|
||||||
type ProductHandle = string
|
type ProductHandle = string
|
||||||
type ShippingProfileId = string
|
type ShippingProfileId = string
|
||||||
|
type PartialProduct = { handle: string; id: string }
|
||||||
|
type handlerInput = {
|
||||||
|
productsHandleShippingProfileIdMap: Map<ProductHandle, ShippingProfileId>
|
||||||
|
products: PartialProduct[]
|
||||||
|
}
|
||||||
|
|
||||||
export async function attachShippingProfileToProducts({
|
export async function attachShippingProfileToProducts({
|
||||||
container,
|
container,
|
||||||
context,
|
context,
|
||||||
data,
|
data,
|
||||||
}: WorkflowArguments<{
|
}: WorkflowArguments<handlerInput>): Promise<void> {
|
||||||
productsHandleShippingProfileIdMap: Map<ProductHandle, ShippingProfileId>
|
|
||||||
products: ProductTypes.ProductDTO[]
|
|
||||||
}>): Promise<void> {
|
|
||||||
const { manager } = context
|
const { manager } = context
|
||||||
|
|
||||||
const productsHandleShippingProfileIdMap =
|
const productsHandleShippingProfileIdMap =
|
||||||
|
|||||||
@@ -2,12 +2,14 @@ import { ProductTypes } from "@medusajs/types"
|
|||||||
import { WorkflowArguments } from "../../helper"
|
import { WorkflowArguments } from "../../helper"
|
||||||
import { Modules, ModulesDefinition } from "@medusajs/modules-sdk"
|
import { Modules, ModulesDefinition } from "@medusajs/modules-sdk"
|
||||||
|
|
||||||
|
type HandlerInput = {
|
||||||
|
products: ProductTypes.CreateProductDTO[]
|
||||||
|
}
|
||||||
|
|
||||||
export async function createProducts({
|
export async function createProducts({
|
||||||
container,
|
container,
|
||||||
data,
|
data,
|
||||||
}: WorkflowArguments<{
|
}: WorkflowArguments<HandlerInput>): Promise<ProductTypes.ProductDTO[]> {
|
||||||
products: ProductTypes.CreateProductDTO[]
|
|
||||||
}>): Promise<ProductTypes.ProductDTO[]> {
|
|
||||||
const data_ = data.products
|
const data_ = data.products
|
||||||
|
|
||||||
const productModuleService: ProductTypes.IProductModuleService =
|
const productModuleService: ProductTypes.IProductModuleService =
|
||||||
|
|||||||
@@ -1,17 +1,18 @@
|
|||||||
import { ProductTypes } from "@medusajs/types"
|
|
||||||
import { WorkflowArguments } from "../../helper"
|
import { WorkflowArguments } from "../../helper"
|
||||||
|
|
||||||
type ProductHandle = string
|
type ProductHandle = string
|
||||||
type SalesChannelId = string
|
type SalesChannelId = string
|
||||||
|
type PartialProduct = { handle: string; id: string }
|
||||||
|
type HandlerInput = {
|
||||||
|
productsHandleSalesChannelsMap: Map<ProductHandle, SalesChannelId[]>
|
||||||
|
products: PartialProduct[]
|
||||||
|
}
|
||||||
|
|
||||||
export async function detachSalesChannelFromProducts({
|
export async function detachSalesChannelFromProducts({
|
||||||
container,
|
container,
|
||||||
context,
|
context,
|
||||||
data,
|
data,
|
||||||
}: WorkflowArguments<{
|
}: WorkflowArguments<HandlerInput>): Promise<void> {
|
||||||
productsHandleSalesChannelsMap: Map<ProductHandle, SalesChannelId[]>
|
|
||||||
products: ProductTypes.ProductDTO[]
|
|
||||||
}>): Promise<void> {
|
|
||||||
const { manager } = context
|
const { manager } = context
|
||||||
const productsHandleSalesChannelsMap = data.productsHandleSalesChannelsMap
|
const productsHandleSalesChannelsMap = data.productsHandleSalesChannelsMap
|
||||||
const products = data.products
|
const products = data.products
|
||||||
|
|||||||
@@ -1,17 +1,18 @@
|
|||||||
import { ProductTypes } from "@medusajs/types"
|
|
||||||
import { WorkflowArguments } from "../../helper"
|
import { WorkflowArguments } from "../../helper"
|
||||||
|
|
||||||
type ProductHandle = string
|
type ProductHandle = string
|
||||||
type ShippingProfileId = string
|
type ShippingProfileId = string
|
||||||
|
type PartialProduct = { handle: string; id: string }
|
||||||
|
type HandlerInput = {
|
||||||
|
productsHandleShippingProfileIdMap: Map<ProductHandle, ShippingProfileId>
|
||||||
|
products: PartialProduct[]
|
||||||
|
}
|
||||||
|
|
||||||
export async function detachShippingProfileFromProducts({
|
export async function detachShippingProfileFromProducts({
|
||||||
container,
|
container,
|
||||||
context,
|
context,
|
||||||
data,
|
data,
|
||||||
}: WorkflowArguments<{
|
}: WorkflowArguments<HandlerInput>): Promise<void> {
|
||||||
productsHandleShippingProfileIdMap: Map<ProductHandle, ShippingProfileId>
|
|
||||||
products: ProductTypes.ProductDTO[]
|
|
||||||
}>): Promise<void> {
|
|
||||||
const { manager } = context
|
const { manager } = context
|
||||||
const productsHandleShippingProfileIdMap =
|
const productsHandleShippingProfileIdMap =
|
||||||
data.productsHandleShippingProfileIdMap
|
data.productsHandleShippingProfileIdMap
|
||||||
|
|||||||
@@ -1,17 +1,20 @@
|
|||||||
import { ProductTypes, WorkflowTypes } from "@medusajs/types"
|
import { ProductTypes, WorkflowTypes } from "@medusajs/types"
|
||||||
import { WorkflowArguments } from "../../helper"
|
import { WorkflowArguments } from "../../helper"
|
||||||
|
|
||||||
|
type HandlerInput = {
|
||||||
|
ids: string[]
|
||||||
|
config?: WorkflowTypes.CommonWorkflow.WorkflowInputConfig
|
||||||
|
}
|
||||||
|
|
||||||
export async function listProducts({
|
export async function listProducts({
|
||||||
container,
|
container,
|
||||||
context,
|
context,
|
||||||
data,
|
data,
|
||||||
}: WorkflowArguments<{
|
}: // TODO: should return product DTO or priced product but needs to be created in the types package
|
||||||
products: ProductTypes.ProductDTO[]
|
WorkflowArguments<HandlerInput>): Promise<ProductTypes.ProductDTO[]> {
|
||||||
config?: WorkflowTypes.CommonWorkflow.WorkflowInputConfig
|
|
||||||
}>): Promise<ProductTypes.ProductDTO[]> {
|
|
||||||
const { manager } = context
|
const { manager } = context
|
||||||
|
|
||||||
const products = data.products
|
const productIds = data.ids
|
||||||
const listConfig = data.config?.listConfig ?? {}
|
const listConfig = data.config?.listConfig ?? {}
|
||||||
|
|
||||||
const productService = container.resolve("productService")
|
const productService = container.resolve("productService")
|
||||||
@@ -30,15 +33,15 @@ export async function listProducts({
|
|||||||
Object.assign(config, { relations: listConfig.relations })
|
Object.assign(config, { relations: listConfig.relations })
|
||||||
}
|
}
|
||||||
|
|
||||||
const rawProduct = await productService
|
const rawProducts = await productService
|
||||||
.withTransaction(manager as any)
|
.withTransaction(manager as any)
|
||||||
.retrieve(products[0].id, shouldUseConfig ? config : undefined)
|
.list({ id: productIds }, shouldUseConfig ? config : undefined)
|
||||||
|
|
||||||
return await pricingService
|
return await pricingService
|
||||||
.withTransaction(manager as any)
|
.withTransaction(manager as any)
|
||||||
.setProductPrices([rawProduct])
|
.setProductPrices(rawProducts)
|
||||||
}
|
}
|
||||||
|
|
||||||
listProducts.aliases = {
|
listProducts.aliases = {
|
||||||
products: "products",
|
ids: "ids",
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,12 @@ import { ProductTypes } from "@medusajs/types"
|
|||||||
import { WorkflowArguments } from "../../helper"
|
import { WorkflowArguments } from "../../helper"
|
||||||
import { Modules, ModulesDefinition } from "@medusajs/modules-sdk"
|
import { Modules, ModulesDefinition } from "@medusajs/modules-sdk"
|
||||||
|
|
||||||
|
type HandlerInput = { products: { id: string }[] }
|
||||||
|
|
||||||
export async function removeProducts({
|
export async function removeProducts({
|
||||||
container,
|
container,
|
||||||
data,
|
data,
|
||||||
}: WorkflowArguments<{ products: ProductTypes.ProductDTO[] }>): Promise<void> {
|
}: WorkflowArguments<HandlerInput>): Promise<void> {
|
||||||
if (!data.products.length) {
|
if (!data.products.length) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,18 +7,19 @@ type VariantIndexAndPrices = {
|
|||||||
index: number
|
index: number
|
||||||
prices: WorkflowTypes.ProductWorkflow.CreateProductVariantPricesInputDTO[]
|
prices: WorkflowTypes.ProductWorkflow.CreateProductVariantPricesInputDTO[]
|
||||||
}
|
}
|
||||||
|
type HandlerInput = {
|
||||||
export async function updateProductsVariantsPrices({
|
|
||||||
container,
|
|
||||||
context,
|
|
||||||
data,
|
|
||||||
}: WorkflowArguments<{
|
|
||||||
productsHandleVariantsIndexPricesMap: Map<
|
productsHandleVariantsIndexPricesMap: Map<
|
||||||
ProductHandle,
|
ProductHandle,
|
||||||
VariantIndexAndPrices[]
|
VariantIndexAndPrices[]
|
||||||
>
|
>
|
||||||
products: ProductTypes.ProductDTO[]
|
products: ProductTypes.ProductDTO[]
|
||||||
}>) {
|
}
|
||||||
|
|
||||||
|
export async function updateProductsVariantsPrices({
|
||||||
|
container,
|
||||||
|
context,
|
||||||
|
data,
|
||||||
|
}: WorkflowArguments<HandlerInput>) {
|
||||||
const { manager } = context
|
const { manager } = context
|
||||||
const products = data.products
|
const products = data.products
|
||||||
const productsHandleVariantsIndexPricesMap =
|
const productsHandleVariantsIndexPricesMap =
|
||||||
|
|||||||
Reference in New Issue
Block a user