chore: add batch update prices for price lists (#6999)
This commit is contained in:
@@ -5,6 +5,5 @@ 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"
|
||||
export * from "./validate-variant-price-links"
|
||||
|
||||
@@ -1,123 +0,0 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import {
|
||||
AddPriceListPricesDTO,
|
||||
CreatePriceListPriceDTO,
|
||||
CreatePriceListPriceWorkflowDTO,
|
||||
IPricingModuleService,
|
||||
PriceDTO,
|
||||
UpdatePriceListPriceDTO,
|
||||
UpdatePriceListPriceWorkflowDTO,
|
||||
UpdatePriceListPricesDTO,
|
||||
UpsertPriceListPricesWorkflowStepDTO,
|
||||
} from "@medusajs/types"
|
||||
import { buildPriceSetPricesForModule, promiseAll } from "@medusajs/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
export const upsertPriceListPricesStepId = "upsert-price-list-prices"
|
||||
export const upsertPriceListPricesStep = createStep(
|
||||
upsertPriceListPricesStepId,
|
||||
async (stepInput: UpsertPriceListPricesWorkflowStepDTO, { container }) => {
|
||||
const { data, variant_price_map: variantPriceSetMap } = stepInput
|
||||
|
||||
const priceListPricesToUpdate: UpdatePriceListPricesDTO[] = []
|
||||
const priceListPricesToAdd: AddPriceListPricesDTO[] = []
|
||||
const pricingModule = container.resolve<IPricingModuleService>(
|
||||
ModuleRegistrationName.PRICING
|
||||
)
|
||||
|
||||
for (const upsertPriceListPricesData of data) {
|
||||
const { prices = [], id } = upsertPriceListPricesData
|
||||
|
||||
const pricesToAdd: CreatePriceListPriceDTO[] = []
|
||||
const pricesToUpdate: UpdatePriceListPriceDTO[] = []
|
||||
|
||||
for (const price of prices) {
|
||||
const priceSetId = variantPriceSetMap[price.variant_id!]
|
||||
|
||||
if (isPriceUpdate(price)) {
|
||||
pricesToUpdate.push({ ...price, price_set_id: priceSetId })
|
||||
} else {
|
||||
pricesToAdd.push({ ...price, price_set_id: priceSetId })
|
||||
}
|
||||
}
|
||||
|
||||
if (pricesToUpdate.length) {
|
||||
priceListPricesToUpdate.push({
|
||||
price_list_id: id,
|
||||
prices: pricesToUpdate,
|
||||
})
|
||||
}
|
||||
|
||||
if (pricesToAdd.length) {
|
||||
priceListPricesToAdd.push({
|
||||
price_list_id: id,
|
||||
prices: pricesToAdd,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const updatedPrices = await pricingModule.listPrices(
|
||||
{
|
||||
id: priceListPricesToUpdate
|
||||
.map((priceListData) => priceListData.prices.map((price) => price.id))
|
||||
.filter(Boolean)
|
||||
.flat(1),
|
||||
},
|
||||
{ relations: ["price_list"] }
|
||||
)
|
||||
|
||||
const priceListPricesMap = new Map<string, PriceDTO[]>()
|
||||
const dataBeforePriceUpdate: UpdatePriceListPricesDTO[] = []
|
||||
|
||||
for (const updatedPrice of updatedPrices) {
|
||||
const priceListId = updatedPrice.price_list!.id
|
||||
const prices = priceListPricesMap.get(priceListId) || []
|
||||
|
||||
priceListPricesMap.set(priceListId, prices)
|
||||
}
|
||||
|
||||
for (const [priceListId, prices] of Object.entries(priceListPricesMap)) {
|
||||
dataBeforePriceUpdate.push({
|
||||
price_list_id: priceListId,
|
||||
prices: buildPriceSetPricesForModule(prices),
|
||||
})
|
||||
}
|
||||
|
||||
// TODO: `addPriceListPrices` will return a list of price lists
|
||||
// This should be reworked to return prices instead, as we need to
|
||||
// do a revert incase this step fails
|
||||
const [createdPriceListPrices, _] = await promiseAll([
|
||||
pricingModule.addPriceListPrices(priceListPricesToAdd),
|
||||
pricingModule.updatePriceListPrices(priceListPricesToUpdate),
|
||||
])
|
||||
|
||||
return new StepResponse(null, {
|
||||
createdPriceListPrices,
|
||||
updatedPriceListPrices: dataBeforePriceUpdate,
|
||||
})
|
||||
},
|
||||
async (data, { container }) => {
|
||||
if (!data) {
|
||||
return
|
||||
}
|
||||
|
||||
const { createdPriceListPrices = [], updatedPriceListPrices = [] } = data
|
||||
const pricingModule = container.resolve<IPricingModuleService>(
|
||||
ModuleRegistrationName.PRICING
|
||||
)
|
||||
|
||||
if (createdPriceListPrices.length) {
|
||||
await pricingModule.removePrices(createdPriceListPrices.map((p) => p.id))
|
||||
}
|
||||
|
||||
if (updatedPriceListPrices.length) {
|
||||
await pricingModule.updatePriceListPrices(updatedPriceListPrices)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
function isPriceUpdate(
|
||||
data: UpdatePriceListPriceWorkflowDTO | CreatePriceListPriceWorkflowDTO
|
||||
): data is UpdatePriceListPriceWorkflowDTO {
|
||||
return "id" in data
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
import { UpdatePriceListWorkflowInputDTO } from "@medusajs/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
MedusaError,
|
||||
@@ -10,7 +9,11 @@ export const validateVariantPriceLinksStepId = "validate-variant-price-links"
|
||||
export const validateVariantPriceLinksStep = createStep(
|
||||
validateVariantPriceLinksStepId,
|
||||
async (
|
||||
data: Pick<UpdatePriceListWorkflowInputDTO, "prices">[],
|
||||
data: {
|
||||
prices: {
|
||||
variant_id: string
|
||||
}[]
|
||||
}[],
|
||||
{ container }
|
||||
) => {
|
||||
const remoteQuery = container.resolve(
|
||||
@@ -18,7 +21,7 @@ export const validateVariantPriceLinksStep = createStep(
|
||||
)
|
||||
|
||||
const variantIds: string[] = data
|
||||
.map((pl) => pl?.prices?.map((price) => price.variant_id!) || [])
|
||||
.map((pl) => pl?.prices?.map((price) => price.variant_id) || [])
|
||||
.filter(Boolean)
|
||||
.flat(1)
|
||||
|
||||
|
||||
@@ -2,5 +2,5 @@ export * from "./create-price-list-prices"
|
||||
export * from "./create-price-lists"
|
||||
export * from "./delete-price-lists"
|
||||
export * from "./remove-price-list-prices"
|
||||
export * from "./update-price-list-prices"
|
||||
export * from "./update-price-lists"
|
||||
export * from "./upsert-price-list-prices"
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { UpdatePriceListPricesWorkflowDTO } from "@medusajs/types"
|
||||
import {
|
||||
WorkflowData,
|
||||
createWorkflow,
|
||||
parallelize,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import {
|
||||
updatePriceListPricesStep,
|
||||
validatePriceListsStep,
|
||||
validateVariantPriceLinksStep,
|
||||
} from "../steps"
|
||||
|
||||
export const updatePriceListPricesWorkflowId = "update-price-list-prices"
|
||||
export const updatePriceListPricesWorkflow = createWorkflow(
|
||||
updatePriceListPricesWorkflowId,
|
||||
(
|
||||
input: WorkflowData<{
|
||||
data: UpdatePriceListPricesWorkflowDTO[]
|
||||
}>
|
||||
): WorkflowData<void> => {
|
||||
const [_, variantPriceMap] = parallelize(
|
||||
validatePriceListsStep(input.data),
|
||||
validateVariantPriceLinksStep(input.data)
|
||||
)
|
||||
|
||||
updatePriceListPricesStep({
|
||||
data: input.data,
|
||||
variant_price_map: variantPriceMap,
|
||||
})
|
||||
}
|
||||
)
|
||||
@@ -1,18 +1,6 @@
|
||||
import { UpdatePriceListWorkflowInputDTO } from "@medusajs/types"
|
||||
import {
|
||||
WorkflowData,
|
||||
createWorkflow,
|
||||
parallelize,
|
||||
transform,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import {
|
||||
createPriceListPricesStep,
|
||||
getExistingPriceListsPriceIdsStep,
|
||||
removePriceListPricesStep,
|
||||
updatePriceListsStep,
|
||||
validatePriceListsStep,
|
||||
validateVariantPriceLinksStep,
|
||||
} from "../steps"
|
||||
import { WorkflowData, createWorkflow } from "@medusajs/workflows-sdk"
|
||||
import { updatePriceListsStep, validatePriceListsStep } from "../steps"
|
||||
|
||||
export const updatePriceListsWorkflowId = "update-price-lists"
|
||||
export const updatePriceListsWorkflow = createWorkflow(
|
||||
@@ -20,41 +8,8 @@ export const updatePriceListsWorkflow = createWorkflow(
|
||||
(
|
||||
input: WorkflowData<{ price_lists_data: UpdatePriceListWorkflowInputDTO[] }>
|
||||
): WorkflowData<void> => {
|
||||
const [priceListsMap, variantPriceMap] = parallelize(
|
||||
validatePriceListsStep(input.price_lists_data),
|
||||
validateVariantPriceLinksStep(input.price_lists_data)
|
||||
)
|
||||
validatePriceListsStep(input.price_lists_data)
|
||||
|
||||
const getPriceListPricesInput = transform({ priceListsMap }, (data) => ({
|
||||
price_list_ids: Object.keys(data.priceListsMap),
|
||||
}))
|
||||
|
||||
const priceListPriceIdMap = getExistingPriceListsPriceIdsStep(
|
||||
getPriceListPricesInput
|
||||
)
|
||||
|
||||
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) => {
|
||||
delete priceListData.prices
|
||||
|
||||
return priceListData
|
||||
})
|
||||
})
|
||||
|
||||
updatePriceListsStep(updatePriceListInput)
|
||||
updatePriceListsStep(input.price_lists_data)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
import { UpdatePriceListWorkflowInputDTO } from "@medusajs/types"
|
||||
import {
|
||||
WorkflowData,
|
||||
createWorkflow,
|
||||
parallelize,
|
||||
} from "@medusajs/workflows-sdk"
|
||||
import {
|
||||
upsertPriceListPricesStep,
|
||||
validatePriceListsStep,
|
||||
validateVariantPriceLinksStep,
|
||||
} from "../steps"
|
||||
|
||||
export const upsertPriceListPricesWorkflowId = "upsert-price-list-prices"
|
||||
export const upsertPriceListPricesWorkflow = createWorkflow(
|
||||
upsertPriceListPricesWorkflowId,
|
||||
(
|
||||
input: WorkflowData<{
|
||||
price_lists_data: Pick<UpdatePriceListWorkflowInputDTO, "id" | "prices">[]
|
||||
}>
|
||||
): WorkflowData<void> => {
|
||||
const [_, variantPriceMap] = parallelize(
|
||||
validatePriceListsStep(input.price_lists_data),
|
||||
validateVariantPriceLinksStep(input.price_lists_data)
|
||||
)
|
||||
|
||||
upsertPriceListPricesStep({
|
||||
data: input.price_lists_data,
|
||||
variant_price_map: variantPriceMap,
|
||||
})
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user