@medusajs/core-flows
Version:
Set of workflow definitions for Medusa
63 lines • 2.23 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPriceListPricesStep = exports.createPriceListPricesStepId = void 0;
const utils_1 = require("@medusajs/framework/utils");
const workflows_sdk_1 = require("@medusajs/framework/workflows-sdk");
exports.createPriceListPricesStepId = "create-price-list-prices";
/**
* This step creates prices for a price list.
*
* @example
* const data = createPriceListPricesStep({
* data: [{
* id: "plist_123",
* prices: [
* {
* currency_code: "USD",
* amount: 1000,
* variant_id: "variant_123",
* }
* ]
* }],
* variant_price_map: {
* "variant_123": "pset_123"
* }
* })
*/
exports.createPriceListPricesStep = (0, workflows_sdk_1.createStep)(exports.createPriceListPricesStepId, async (stepInput, { container }) => {
const { data, variant_price_map: variantPriceSetMap } = stepInput;
const priceListPricesToCreate = [];
const pricingModule = container.resolve(utils_1.Modules.PRICING);
for (const createPriceListPricesData of data) {
const { prices = [], id } = createPriceListPricesData;
const pricesToAdd = [];
for (const price of prices) {
const toPush = {
...price,
price_set_id: variantPriceSetMap[price.variant_id],
};
delete toPush.variant_id;
pricesToAdd.push(toPush);
}
if (pricesToAdd.length) {
priceListPricesToCreate.push({
price_list_id: id,
prices: pricesToAdd,
});
}
}
if (!priceListPricesToCreate.length) {
return new workflows_sdk_1.StepResponse([]);
}
const createdPrices = await pricingModule.addPriceListPrices(priceListPricesToCreate);
return new workflows_sdk_1.StepResponse(createdPrices, createdPrices.map((p) => p.id));
}, async (createdIds, { container }) => {
if (!createdIds?.length) {
return;
}
const pricingModule = container.resolve(utils_1.Modules.PRICING);
if (createdIds.length) {
await pricingModule.removePrices(createdIds);
}
});
//# sourceMappingURL=create-price-list-prices.js.map
;