UNPKG

@medusajs/core-flows

Version:

Set of workflow definitions for Medusa

135 lines 5.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createShippingOptionsWorkflow = exports.createShippingOptionsWorkflowId = void 0; const workflows_sdk_1 = require("@medusajs/framework/workflows-sdk"); const steps_1 = require("../steps"); const set_shipping_options_price_sets_1 = require("../steps/set-shipping-options-price-sets"); const validate_fulfillment_providers_1 = require("../steps/validate-fulfillment-providers"); const validate_shipping_option_prices_1 = require("../steps/validate-shipping-option-prices"); exports.createShippingOptionsWorkflowId = "create-shipping-options-workflow"; /** * This workflow creates one or more shipping options. It's used by the * [Create Shipping Option Admin API Route](https://docs.medusajs.com/api/admin#shipping-options_postshippingoptions). * * You can use this workflow within your own customizations or custom workflows, allowing you to * create shipping options within your custom flows. * * @example * To calculate a shipping option with flat rate prices: * * :::note * * Learn more about adding rules to the shipping option's prices in the Pricing Module's * [Price Rules](https://docs.medusajs.com/resources/commerce-modules/pricing/price-rules) documentation. * * ::: * * ```ts * const { result } = await createShippingOptionsWorkflow(container) * .run({ * input: [ * { * name: "Standard Shipping", * service_zone_id: "serzo_123", * shipping_profile_id: "sp_123", * provider_id: "prov_123", * type: { * label: "Standard", * description: "Standard shipping", * code: "standard" * }, * price_type: "flat", * prices: [ * { * amount: 500, * currency_code: "usd" * } * ] * } * ] * }) * ``` * * To calculate shipping option with calculated prices, set `price_type` to `calculated` and don't pass a `prices` array: * * :::note * * You can calculate the shipping option's price for a cart using the [calculateShippingOptionsPricesWorkflow](https://docs.medusajs.com/resources/references/medusa-workflows/calculateShippingOptionsPricesWorkflow). * * ::: * * ```ts * const { result } = await createShippingOptionsWorkflow(container) * .run({ * input: [ * { * name: "Standard Shipping", * service_zone_id: "serzo_123", * shipping_profile_id: "sp_123", * provider_id: "prov_123", * type: { * label: "Standard", * description: "Standard shipping", * code: "standard" * }, * price_type: "calculated", * } * ] * }) * ``` * * @summary * * Create shipping options. */ exports.createShippingOptionsWorkflow = (0, workflows_sdk_1.createWorkflow)(exports.createShippingOptionsWorkflowId, (input) => { (0, workflows_sdk_1.parallelize)((0, validate_fulfillment_providers_1.validateFulfillmentProvidersStep)(input), (0, validate_shipping_option_prices_1.validateShippingOptionPricesStep)(input)); const data = (0, workflows_sdk_1.transform)(input, (data) => { const shippingOptionsIndexToPrices = data.map((option, index) => { /** * Flat rate ShippingOptions always needs to provide a price array. * * For calculated pricing we create an "empty" price set * so we can have simpler update flow for both cases and allow updating price_type. */ const prices = option.prices ?? []; return { shipping_option_index: index, prices, }; }); return { shippingOptions: data, shippingOptionsIndexToPrices, }; }); const createdShippingOptions = (0, steps_1.upsertShippingOptionsStep)(data.shippingOptions); const normalizedShippingOptionsPrices = (0, workflows_sdk_1.transform)({ shippingOptions: createdShippingOptions, shippingOptionsIndexToPrices: data.shippingOptionsIndexToPrices, }, (data) => { const shippingOptionsPrices = data.shippingOptionsIndexToPrices.map(({ shipping_option_index, prices }) => { return { id: data.shippingOptions[shipping_option_index].id, prices, }; }); return { shippingOptionsPrices, }; }); const shippingOptionsPriceSetsLinkData = (0, steps_1.createShippingOptionsPriceSetsStep)(normalizedShippingOptionsPrices.shippingOptionsPrices); const normalizedLinkData = (0, workflows_sdk_1.transform)({ shippingOptionsPriceSetsLinkData, }, (data) => { return data.shippingOptionsPriceSetsLinkData.map((item) => { return { id: item.id, price_sets: [item.priceSetId], }; }); }); (0, set_shipping_options_price_sets_1.setShippingOptionsPriceSetsStep)(normalizedLinkData); return new workflows_sdk_1.WorkflowResponse(createdShippingOptions); }); //# sourceMappingURL=create-shipping-options.js.map