feat(medusa, core-flows, types): Allow to update the rules from a shipping options (#7175)

**What**
Add support for the following operations
- update rules from the update shipping options end point
- update rules from the batch update end point

Also added some improvements, that can be revisited later
- Add a rule value normalizer, jsonb will transform the input value to a [primitive](https://www.postgresql.org/docs/current/datatype-json.html#JSON-TYPE-MAPPING-TABLE) when possible meaning that passing `"true"` will result in storing `true` and not the string. The normalizer takes care of that
This commit is contained in:
Adrien de Peretti
2024-04-30 18:45:17 +02:00
committed by GitHub
parent 3affcc2525
commit e26cda4b6a
11 changed files with 470 additions and 41 deletions

View File

@@ -3,7 +3,7 @@ import {
AddFulfillmentShippingOptionRulesWorkflowDTO,
IFulfillmentModuleService,
} from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
export const createShippingOptionRulesStepId = "create-shipping-option-rules"
export const createShippingOptionRulesStep = createStep(
@@ -18,12 +18,12 @@ export const createShippingOptionRulesStep = createStep(
ModuleRegistrationName.FULFILLMENT
)
const createdPromotionRules =
const createdShippingOptionRules =
await fulfillmentModule.createShippingOptionRules(data)
return new StepResponse(
createdPromotionRules,
createdPromotionRules.map((pr) => pr.id)
createdShippingOptionRules,
createdShippingOptionRules.map((pr) => pr.id)
)
},
async (ruleIds, { container }) => {

View File

@@ -4,7 +4,7 @@ import {
RemoveFulfillmentShippingOptionRulesWorkflowDTO,
RuleOperatorType,
} from "@medusajs/types"
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
export const deleteShippingOptionRulesStepId = "delete-shipping-option-rules"
export const deleteShippingOptionRulesStep = createStep(
@@ -13,6 +13,10 @@ export const deleteShippingOptionRulesStep = createStep(
input: RemoveFulfillmentShippingOptionRulesWorkflowDTO,
{ container }
) => {
if (!input.ids?.length) {
return
}
const { ids } = input
const fulfillmentModule = container.resolve<IFulfillmentModuleService>(
@@ -39,6 +43,7 @@ export const deleteShippingOptionRulesStep = createStep(
await fulfillmentModule.createShippingOptionRules(
shippingOptionRules.map((rule) => ({
id: rule.id,
attribute: rule.attribute,
operator: rule.operator as RuleOperatorType,
value: rule.value as unknown as string | string[],

View File

@@ -0,0 +1,51 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import {
IFulfillmentModuleService,
UpdateFulfillmentShippingOptionRulesWorkflowDTO,
UpdateShippingOptionRuleDTO,
} from "@medusajs/types"
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
export const updateShippingOptionRulesStepId = "update-shipping-option-rules"
export const updateShippingOptionRulesStep = createStep(
updateShippingOptionRulesStepId,
async (
input: UpdateFulfillmentShippingOptionRulesWorkflowDTO,
{ container }
) => {
if (!input.data?.length) {
return
}
const { data } = input
const fulfillmentModule = container.resolve<IFulfillmentModuleService>(
ModuleRegistrationName.FULFILLMENT
)
const ids = data.map((d) => d.id)
const shippingOptionRules = await fulfillmentModule.listShippingOptionRules(
{ id: ids },
{ select: ["id", "attribute", "operator", "value", "shipping_option_id"] }
)
const updatedPromotionRules =
await fulfillmentModule.updateShippingOptionRules(data)
return new StepResponse(
updatedPromotionRules,
shippingOptionRules as unknown as UpdateShippingOptionRuleDTO[]
)
},
async (previousRulesData, { container }) => {
if (!previousRulesData?.length) {
return
}
const fulfillmentModule = container.resolve<IFulfillmentModuleService>(
ModuleRegistrationName.FULFILLMENT
)
await fulfillmentModule.updateShippingOptionRules(previousRulesData)
}
)

View File

@@ -6,15 +6,16 @@ import {
UpdateShippingOptionRuleDTO,
} from "@medusajs/types"
import {
WorkflowData,
createWorkflow,
parallelize,
transform,
WorkflowData,
} from "@medusajs/workflows-sdk"
import {
createShippingOptionRulesStep,
deleteShippingOptionRulesStep,
} from "../steps"
import { updateShippingOptionRulesStep } from "../steps/update-shipping-option-rules"
export const batchShippingOptionRulesWorkflowId = "batch-shipping-option-rules"
export const batchShippingOptionRulesWorkflow = createWorkflow(
@@ -27,25 +28,21 @@ export const batchShippingOptionRulesWorkflow = createWorkflow(
>
>
): WorkflowData<BatchWorkflowOutput<ShippingOptionRuleDTO>> => {
const createInput = transform({ input }, (data) => ({
data: data.input.create ?? [],
}))
const actionInputs = transform({ input }, (data) => {
const { create, update, delete: del } = data.input
return {
createInput: { data: create ?? [] },
updateInput: { data: update ?? [] },
deleteInput: { ids: del ?? [] },
}
})
const updateInput = transform({ input }, (data) => ({
data: data.input.update ?? [],
}))
const deleteInput = transform({ input }, (data) => ({
ids: data.input.delete ?? [],
}))
// TODO: Currently we don't support edits, add support for this.
// We just call the steps directly here since there are no independent workflows, switch to CRUD workflows if they get added.
const [created, deleted] = parallelize(
createShippingOptionRulesStep(createInput),
deleteShippingOptionRulesStep(deleteInput)
const [created, updated, deleted] = parallelize(
createShippingOptionRulesStep(actionInputs.createInput),
updateShippingOptionRulesStep(actionInputs.updateInput),
deleteShippingOptionRulesStep(actionInputs.deleteInput)
)
return transform({ created, deleted }, (data) => ({ ...data, updated: [] }))
return transform({ created, deleted, updated }, (data) => data)
}
)