feat: Add product and pricing link on create and delete operations (#6740)
Things that remain to be done: 1. Handle product and variant updates 2. Add tests for the workflows independently 3. Align the endpoints to the new code conventions we defined 4. Finish up the update/upsert endpoints for variants All of those can be done in a separate PR, as this is quite large already.
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { CreatePriceSetDTO, IPricingModuleService } from "@medusajs/types"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
export const createPriceSetsStepId = "create-price-sets"
|
||||
export const createPriceSetsStep = createStep(
|
||||
createPriceSetsStepId,
|
||||
async (data: CreatePriceSetDTO[], { container }) => {
|
||||
const pricingModule = container.resolve<IPricingModuleService>(
|
||||
ModuleRegistrationName.PRICING
|
||||
)
|
||||
|
||||
const priceSets = await pricingModule.create(data)
|
||||
|
||||
return new StepResponse(
|
||||
priceSets,
|
||||
priceSets.map((priceSet) => priceSet.id)
|
||||
)
|
||||
},
|
||||
async (priceSets, { container }) => {
|
||||
if (!priceSets?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
const pricingModule = container.resolve<IPricingModuleService>(
|
||||
ModuleRegistrationName.PRICING
|
||||
)
|
||||
|
||||
await pricingModule.delete(priceSets)
|
||||
}
|
||||
)
|
||||
@@ -1,3 +1,5 @@
|
||||
export * from "./create-price-sets"
|
||||
export * from "./update-price-sets"
|
||||
export * from "./create-pricing-rule-types"
|
||||
export * from "./delete-pricing-rule-types"
|
||||
export * from "./update-pricing-rule-types"
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IPricingModuleService, UpdatePriceSetDTO } from "@medusajs/types"
|
||||
import {
|
||||
convertItemResponseToUpdateRequest,
|
||||
getSelectsAndRelationsFromObjectArray,
|
||||
} from "@medusajs/utils"
|
||||
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
|
||||
|
||||
export const updatePriceSetsStepId = "update-price-sets"
|
||||
export const updatePriceSetsStep = createStep(
|
||||
updatePriceSetsStepId,
|
||||
async (data: UpdatePriceSetDTO[], { container }) => {
|
||||
const pricingModule = container.resolve<IPricingModuleService>(
|
||||
ModuleRegistrationName.PRICING
|
||||
)
|
||||
|
||||
const { selects, relations } = getSelectsAndRelationsFromObjectArray(data)
|
||||
const dataBeforeUpdate = await pricingModule.list(
|
||||
{ id: data.map((d) => d.id) },
|
||||
{ relations, select: selects }
|
||||
)
|
||||
|
||||
const updatedPriceSets = await pricingModule.update(data)
|
||||
|
||||
return new StepResponse(updatedPriceSets, {
|
||||
dataBeforeUpdate,
|
||||
selects,
|
||||
relations,
|
||||
})
|
||||
},
|
||||
async (revertInput, { container }) => {
|
||||
if (!revertInput) {
|
||||
return
|
||||
}
|
||||
|
||||
const { dataBeforeUpdate = [], selects, relations } = revertInput
|
||||
|
||||
const pricingModule = container.resolve<IPricingModuleService>(
|
||||
ModuleRegistrationName.PRICING
|
||||
)
|
||||
|
||||
await pricingModule.update(
|
||||
dataBeforeUpdate.map((data) =>
|
||||
convertItemResponseToUpdateRequest(data, selects, relations)
|
||||
)
|
||||
)
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user