feat(core-flows,medusa,types): create/update workflows to create and update PriceList prices (#6711)
what: - create and update workflows for price list prices - update price list endpoint does a "set"
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import {
|
||||
AddPriceListPricesDTO,
|
||||
CreatePriceListPriceDTO,
|
||||
CreatePriceListPricesWorkflowStepDTO,
|
||||
IPricingModuleService,
|
||||
} from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
export const createPriceListPricesStepId = "create-price-list-prices"
|
||||
export const createPriceListPricesStep = createStep(
|
||||
createPriceListPricesStepId,
|
||||
async (stepInput: CreatePriceListPricesWorkflowStepDTO, { container }) => {
|
||||
const { data, variant_price_map: variantPriceSetMap } = stepInput
|
||||
const priceListPricesToCreate: AddPriceListPricesDTO[] = []
|
||||
const pricingModule = container.resolve<IPricingModuleService>(
|
||||
ModuleRegistrationName.PRICING
|
||||
)
|
||||
|
||||
for (const createPriceListPricesData of data) {
|
||||
const { prices = [], id } = createPriceListPricesData
|
||||
const pricesToAdd: CreatePriceListPriceDTO[] = []
|
||||
|
||||
for (const price of prices) {
|
||||
pricesToAdd.push({
|
||||
...price,
|
||||
price_set_id: variantPriceSetMap[price.variant_id!],
|
||||
})
|
||||
}
|
||||
|
||||
if (pricesToAdd.length) {
|
||||
priceListPricesToCreate.push({
|
||||
price_list_id: id,
|
||||
prices: pricesToAdd,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const createdPrices = await pricingModule.addPriceListPrices(
|
||||
priceListPricesToCreate
|
||||
)
|
||||
|
||||
return new StepResponse(
|
||||
null,
|
||||
createdPrices.map((p) => p.id)
|
||||
)
|
||||
},
|
||||
async (createdIds, { container }) => {
|
||||
if (!createdIds?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const pricingModule = container.resolve<IPricingModuleService>(
|
||||
ModuleRegistrationName.PRICING
|
||||
)
|
||||
|
||||
if (createdIds.length) {
|
||||
await pricingModule.removePrices(createdIds)
|
||||
}
|
||||
}
|
||||
)
|
||||
@@ -1,20 +1,15 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import {
|
||||
CreatePriceListDTO,
|
||||
CreatePriceListWorkflowInputDTO,
|
||||
CreatePriceListsWorkflowStepDTO,
|
||||
IPricingModuleService,
|
||||
} from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
type WorkflowStepInput = {
|
||||
data: CreatePriceListWorkflowInputDTO[]
|
||||
variant_price_map: Record<string, string>
|
||||
}
|
||||
|
||||
export const createPriceListsStepId = "create-price-lists"
|
||||
export const createPriceListsStep = createStep(
|
||||
createPriceListsStepId,
|
||||
async (stepInput: WorkflowStepInput, { container }) => {
|
||||
async (stepInput: CreatePriceListsWorkflowStepDTO, { container }) => {
|
||||
const { data, variant_price_map: variantPriceMap } = stepInput
|
||||
|
||||
const pricingModule = container.resolve<IPricingModuleService>(
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IPricingModuleService } from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
export const getExistingPriceListsPriceIdsStepId =
|
||||
"get-existing-price-lists-prices"
|
||||
export const getExistingPriceListsPriceIdsStep = createStep(
|
||||
getExistingPriceListsPriceIdsStepId,
|
||||
async (data: { price_list_ids: string[] }, { container }) => {
|
||||
const { price_list_ids: priceListIds = [] } = data
|
||||
const priceListPriceIdsMap: Record<string, string[]> = {}
|
||||
const pricingModule = container.resolve<IPricingModuleService>(
|
||||
ModuleRegistrationName.PRICING
|
||||
)
|
||||
|
||||
const existingPrices = priceListIds.length
|
||||
? await pricingModule.listPriceSetMoneyAmounts(
|
||||
{ price_list_id: priceListIds },
|
||||
{ relations: ["price_list"] }
|
||||
)
|
||||
: []
|
||||
|
||||
for (const price of existingPrices) {
|
||||
const priceListId = price.price_list!.id
|
||||
const prices = priceListPriceIdsMap[priceListId] || []
|
||||
|
||||
priceListPriceIdsMap[priceListId] = prices.concat(price.id)
|
||||
}
|
||||
|
||||
return new StepResponse(priceListPriceIdsMap)
|
||||
}
|
||||
)
|
||||
@@ -1,6 +1,9 @@
|
||||
export * from "./create-price-list-prices"
|
||||
export * from "./create-price-lists"
|
||||
export * from "./delete-price-lists"
|
||||
export * from "./get-existing-price-lists-price-ids"
|
||||
export * from "./remove-price-list-prices"
|
||||
export * from "./update-price-list-prices"
|
||||
export * from "./update-price-lists"
|
||||
export * from "./upsert-price-list-prices"
|
||||
export * from "./validate-price-lists"
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import {
|
||||
IPricingModuleService,
|
||||
PriceSetMoneyAmountDTO,
|
||||
UpdatePriceListPriceDTO,
|
||||
UpdatePriceListPricesDTO,
|
||||
UpdatePriceListPriceWorkflowStepDTO,
|
||||
} from "@medusajs/types"
|
||||
import { buildPriceSetPricesForModule } from "@medusajs/utils"
|
||||
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
|
||||
|
||||
export const updatePriceListPricesStepId = "update-price-list-prices"
|
||||
export const updatePriceListPricesStep = createStep(
|
||||
updatePriceListPricesStepId,
|
||||
async (stepInput: UpdatePriceListPriceWorkflowStepDTO, { container }) => {
|
||||
const { data = [], variant_price_map: variantPriceSetMap } = stepInput
|
||||
const priceListPricesToUpdate: UpdatePriceListPricesDTO[] = []
|
||||
const priceIds: string[] = []
|
||||
const pricingModule = container.resolve<IPricingModuleService>(
|
||||
ModuleRegistrationName.PRICING
|
||||
)
|
||||
|
||||
for (const priceListData of data) {
|
||||
const pricesToUpdate: UpdatePriceListPriceDTO[] = []
|
||||
const { prices = [], id } = priceListData
|
||||
|
||||
for (const price of prices) {
|
||||
pricesToUpdate.push({
|
||||
...price,
|
||||
price_set_id: variantPriceSetMap[price.variant_id!],
|
||||
})
|
||||
|
||||
if (price.id) {
|
||||
priceIds.push(price.id)
|
||||
}
|
||||
}
|
||||
|
||||
priceListPricesToUpdate.push({
|
||||
price_list_id: id,
|
||||
prices: pricesToUpdate,
|
||||
})
|
||||
}
|
||||
|
||||
const existingPrices = priceIds.length
|
||||
? await pricingModule.listPriceSetMoneyAmounts(
|
||||
{ id: priceIds },
|
||||
{ relations: ["price_list"] }
|
||||
)
|
||||
: []
|
||||
|
||||
const priceListPsmaMap = new Map<string, PriceSetMoneyAmountDTO[]>()
|
||||
const dataBeforePriceUpdate: UpdatePriceListPricesDTO[] = []
|
||||
|
||||
for (const price of existingPrices) {
|
||||
const priceListId = price.price_list!.id
|
||||
const psmas = priceListPsmaMap.get(priceListId) || []
|
||||
|
||||
priceListPsmaMap.set(priceListId, psmas)
|
||||
}
|
||||
|
||||
for (const [priceListId, psmas] of Object.entries(priceListPsmaMap)) {
|
||||
dataBeforePriceUpdate.push({
|
||||
price_list_id: priceListId,
|
||||
prices: buildPriceSetPricesForModule(psmas),
|
||||
})
|
||||
}
|
||||
|
||||
await pricingModule.updatePriceListPrices(priceListPricesToUpdate)
|
||||
|
||||
return new StepResponse(null, dataBeforePriceUpdate)
|
||||
},
|
||||
async (dataBeforePriceUpdate, { container }) => {
|
||||
if (!dataBeforePriceUpdate?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const pricingModule = container.resolve<IPricingModuleService>(
|
||||
ModuleRegistrationName.PRICING
|
||||
)
|
||||
|
||||
if (dataBeforePriceUpdate.length) {
|
||||
await pricingModule.updatePriceListPrices(dataBeforePriceUpdate)
|
||||
}
|
||||
}
|
||||
)
|
||||
@@ -8,20 +8,15 @@ import {
|
||||
UpdatePriceListPriceDTO,
|
||||
UpdatePriceListPriceWorkflowDTO,
|
||||
UpdatePriceListPricesDTO,
|
||||
UpdatePriceListWorkflowInputDTO,
|
||||
UpsertPriceListPricesWorkflowStepDTO,
|
||||
} from "@medusajs/types"
|
||||
import { buildPriceSetPricesForModule, promiseAll } from "@medusajs/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
type WorkflowStepInput = {
|
||||
data: Pick<UpdatePriceListWorkflowInputDTO, "id" | "prices">[]
|
||||
variant_price_map: Record<string, string>
|
||||
}
|
||||
|
||||
export const upsertPriceListPricesStepId = "upsert-price-list-prices"
|
||||
export const upsertPriceListPricesStep = createStep(
|
||||
upsertPriceListPricesStepId,
|
||||
async (stepInput: WorkflowStepInput, { container }) => {
|
||||
async (stepInput: UpsertPriceListPricesWorkflowStepDTO, { container }) => {
|
||||
const { data, variant_price_map: variantPriceSetMap } = stepInput
|
||||
|
||||
const priceListPricesToUpdate: UpdatePriceListPricesDTO[] = []
|
||||
|
||||
@@ -6,12 +6,13 @@ import {
|
||||
} from "@medusajs/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
type WorkflowStepInput = Pick<UpdatePriceListWorkflowInputDTO, "prices">[]
|
||||
|
||||
export const validateVariantPriceLinksStepId = "validate-variant-price-links"
|
||||
export const validateVariantPriceLinksStep = createStep(
|
||||
validateVariantPriceLinksStepId,
|
||||
async (data: WorkflowStepInput, { container }) => {
|
||||
async (
|
||||
data: Pick<UpdatePriceListWorkflowInputDTO, "prices">[],
|
||||
{ container }
|
||||
) => {
|
||||
const remoteQuery = container.resolve(
|
||||
ContainerRegistrationKeys.REMOTE_QUERY
|
||||
)
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { CreatePriceListPricesWorkflowDTO } from "@medusajs/types"
|
||||
import {
|
||||
WorkflowData,
|
||||
createWorkflow,
|
||||
parallelize,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import {
|
||||
createPriceListPricesStep,
|
||||
validatePriceListsStep,
|
||||
validateVariantPriceLinksStep,
|
||||
} from "../steps"
|
||||
|
||||
export const createPriceListPricesWorkflowId = "create-price-list-prices"
|
||||
export const createPriceListPricesWorkflow = createWorkflow(
|
||||
createPriceListPricesWorkflowId,
|
||||
(
|
||||
input: WorkflowData<{
|
||||
data: CreatePriceListPricesWorkflowDTO[]
|
||||
}>
|
||||
): WorkflowData<void> => {
|
||||
const [_, variantPriceMap] = parallelize(
|
||||
validatePriceListsStep(input.data),
|
||||
validateVariantPriceLinksStep(input.data)
|
||||
)
|
||||
|
||||
createPriceListPricesStep({
|
||||
data: input.data,
|
||||
variant_price_map: variantPriceMap,
|
||||
})
|
||||
}
|
||||
)
|
||||
@@ -1,12 +1,10 @@
|
||||
import { createWorkflow, WorkflowData } from "@medusajs/workflows-sdk"
|
||||
import { deletePriceListsStep } from "../steps"
|
||||
|
||||
type WorkflowInput = { ids: string[] }
|
||||
|
||||
export const deletePriceListsWorkflowId = "delete-price-lists"
|
||||
export const deletePriceListsWorkflow = createWorkflow(
|
||||
deletePriceListsWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
|
||||
(input: WorkflowData<{ ids: string[] }>): WorkflowData<void> => {
|
||||
return deletePriceListsStep(input.ids)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from "./create-price-list-prices"
|
||||
export * from "./create-price-lists"
|
||||
export * from "./delete-price-lists"
|
||||
export * from "./remove-price-list-prices"
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { removePriceListPricesStep } from "../steps"
|
||||
|
||||
type WorkflowInput = { ids: string[] }
|
||||
|
||||
export const removePriceListPricesWorkflowId = "remove-price-list-prices"
|
||||
export const removePriceListPricesWorkflow = createWorkflow(
|
||||
removePriceListPricesWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
|
||||
(input: WorkflowData<{ ids: string[] }>): WorkflowData<void> => {
|
||||
removePriceListPricesStep(input.ids)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -6,33 +6,46 @@ import {
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import {
|
||||
createPriceListPricesStep,
|
||||
getExistingPriceListsPriceIdsStep,
|
||||
removePriceListPricesStep,
|
||||
updatePriceListsStep,
|
||||
upsertPriceListPricesStep,
|
||||
validatePriceListsStep,
|
||||
validateVariantPriceLinksStep,
|
||||
} from "../steps"
|
||||
|
||||
type WorkflowInput = { price_lists_data: UpdatePriceListWorkflowInputDTO[] }
|
||||
|
||||
export const updatePriceListsWorkflowId = "update-price-lists"
|
||||
export const updatePriceListsWorkflow = createWorkflow(
|
||||
updatePriceListsWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
|
||||
(
|
||||
input: WorkflowData<{ price_lists_data: UpdatePriceListWorkflowInputDTO[] }>
|
||||
): WorkflowData<void> => {
|
||||
const [priceListsMap, variantPriceMap] = parallelize(
|
||||
validatePriceListsStep(input.price_lists_data),
|
||||
validateVariantPriceLinksStep(input.price_lists_data)
|
||||
)
|
||||
|
||||
const updatePricesInput = transform(
|
||||
{ priceListsMap, variantPriceMap, input },
|
||||
(data) => ({
|
||||
data: data.input.price_lists_data,
|
||||
price_lists_map: data.priceListsMap,
|
||||
variant_price_map: data.variantPriceMap,
|
||||
})
|
||||
const getPriceListPricesInput = transform({ priceListsMap }, (data) => ({
|
||||
price_list_ids: Object.keys(data.priceListsMap),
|
||||
}))
|
||||
|
||||
const priceListPriceIdMap = getExistingPriceListsPriceIdsStep(
|
||||
getPriceListPricesInput
|
||||
)
|
||||
|
||||
upsertPriceListPricesStep(updatePricesInput)
|
||||
const removePriceListPricesInput = transform(
|
||||
{ priceListPriceIdMap },
|
||||
(data) => Object.values(data.priceListPriceIdMap).flat(1)
|
||||
)
|
||||
|
||||
removePriceListPricesStep(removePriceListPricesInput)
|
||||
|
||||
const updatePricesInput = transform({ variantPriceMap, input }, (data) => ({
|
||||
data: data.input.price_lists_data,
|
||||
variant_price_map: data.variantPriceMap,
|
||||
}))
|
||||
|
||||
createPriceListPricesStep(updatePricesInput)
|
||||
|
||||
const updatePriceListInput = transform({ input }, (data) => {
|
||||
return data.input.price_lists_data.map((priceListData) => {
|
||||
|
||||
@@ -10,14 +10,14 @@ import {
|
||||
validateVariantPriceLinksStep,
|
||||
} from "../steps"
|
||||
|
||||
type WorkflowInput = {
|
||||
price_lists_data: Pick<UpdatePriceListWorkflowInputDTO, "id" | "prices">[]
|
||||
}
|
||||
|
||||
export const upsertPriceListPricesWorkflowId = "upsert-price-list-prices"
|
||||
export const upsertPriceListPricesWorkflow = createWorkflow(
|
||||
upsertPriceListPricesWorkflowId,
|
||||
(input: WorkflowData<WorkflowInput>): WorkflowData<void> => {
|
||||
(
|
||||
input: WorkflowData<{
|
||||
price_lists_data: Pick<UpdatePriceListWorkflowInputDTO, "id" | "prices">[]
|
||||
}>
|
||||
): WorkflowData<void> => {
|
||||
const [_, variantPriceMap] = parallelize(
|
||||
validatePriceListsStep(input.price_lists_data),
|
||||
validateVariantPriceLinksStep(input.price_lists_data)
|
||||
|
||||
Reference in New Issue
Block a user