@medusajs/core-flows 0.0.9 → 0.0.10-next-20240505114610

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. package/dist/common/steps/use-remote-query.d.ts +2 -0
  2. package/dist/common/steps/use-remote-query.js +7 -1
  3. package/dist/definition/cart/steps/create-order-from-cart.d.ts +7 -0
  4. package/dist/definition/cart/steps/create-order-from-cart.js +45 -0
  5. package/dist/definition/cart/steps/find-sales-channel.d.ts +1 -1
  6. package/dist/definition/cart/steps/index.d.ts +1 -1
  7. package/dist/definition/cart/steps/index.js +1 -1
  8. package/dist/definition/cart/steps/link-cart-payment-collection.js +3 -2
  9. package/dist/definition/cart/utils/fields.d.ts +1 -0
  10. package/dist/definition/cart/utils/fields.js +40 -1
  11. package/dist/definition/cart/utils/prepare-line-item-data.d.ts +17 -1
  12. package/dist/definition/cart/utils/prepare-line-item-data.js +28 -2
  13. package/dist/definition/cart/workflows/add-to-cart.js +77 -48
  14. package/dist/definition/cart/workflows/complete-cart.d.ts +3 -0
  15. package/dist/definition/cart/workflows/complete-cart.js +36 -0
  16. package/dist/definition/cart/workflows/create-carts.js +74 -45
  17. package/dist/definition/cart/workflows/index.d.ts +1 -0
  18. package/dist/definition/cart/workflows/index.js +1 -0
  19. package/dist/definition/cart/workflows/list-shipping-options-for-cart.d.ts +1 -21
  20. package/dist/definition/cart/workflows/list-shipping-options-for-cart.js +51 -42
  21. package/dist/definition/cart/workflows/refresh-payment-collection.js +6 -7
  22. package/dist/definition/cart/workflows/update-line-item-in-cart.js +79 -35
  23. package/dist/order/workflows/create-orders.js +16 -8
  24. package/dist/stock-location/steps/associate-locations-with-fulfillment-sets.js +3 -3
  25. package/dist/stock-location/steps/delete-stock-locations.d.ts +2 -1
  26. package/dist/stock-location/steps/delete-stock-locations.js +5 -3
  27. package/dist/stock-location/workflows/delete-stock-locations.js +3 -6
  28. package/package.json +6 -6
  29. package/dist/definition/cart/steps/get-shipping-option-price-sets.d.ts +0 -7
  30. package/dist/definition/cart/steps/get-shipping-option-price-sets.js +0 -36
@@ -2,6 +2,8 @@ interface StepInput {
2
2
  entry_point: string;
3
3
  fields: string[];
4
4
  variables?: Record<string, any>;
5
+ throw_if_key_not_found?: boolean;
6
+ throw_if_relation_not_found?: boolean | string[];
5
7
  list?: boolean;
6
8
  }
7
9
  export declare const useRemoteQueryStepId = "use-remote-query";
@@ -12,7 +12,13 @@ exports.useRemoteQueryStep = (0, workflows_sdk_1.createStep)(exports.useRemoteQu
12
12
  fields,
13
13
  variables,
14
14
  });
15
- const entities = await query(queryObject);
15
+ const config = {
16
+ throwIfKeyNotFound: !!data.throw_if_key_not_found,
17
+ throwIfRelationNotFound: data.throw_if_key_not_found
18
+ ? data.throw_if_relation_not_found
19
+ : undefined,
20
+ };
21
+ const entities = await query(queryObject, undefined, config);
16
22
  const result = list ? entities : entities[0];
17
23
  return new workflows_sdk_1.StepResponse(result);
18
24
  });
@@ -0,0 +1,7 @@
1
+ import { CartWorkflowDTO } from "@medusajs/types";
2
+ interface StepInput {
3
+ cart: CartWorkflowDTO;
4
+ }
5
+ export declare const createOrderFromCartStepId = "create-order-from-cart";
6
+ export declare const createOrderFromCartStep: import("@medusajs/workflows-sdk").StepFunction<StepInput, import("@medusajs/types").OrderDTO>;
7
+ export {};
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createOrderFromCartStep = exports.createOrderFromCartStepId = void 0;
4
+ const utils_1 = require("@medusajs/utils");
5
+ const workflows_sdk_1 = require("@medusajs/workflows-sdk");
6
+ const create_orders_1 = require("../../../order/workflows/create-orders");
7
+ exports.createOrderFromCartStepId = "create-order-from-cart";
8
+ exports.createOrderFromCartStep = (0, workflows_sdk_1.createStep)(exports.createOrderFromCartStepId, async (input, { container }) => {
9
+ const { cart } = input;
10
+ const itemAdjustments = (cart.items || [])
11
+ ?.map((item) => item.adjustments || [])
12
+ .flat(1);
13
+ const shippingAdjustments = (cart.shipping_methods || [])
14
+ ?.map((sm) => sm.adjustments || [])
15
+ .flat(1);
16
+ const promoCodes = [...itemAdjustments, ...shippingAdjustments]
17
+ .map((adjustment) => adjustment.code)
18
+ .filter((code) => Boolean);
19
+ const { transaction, result } = await (0, create_orders_1.createOrdersWorkflow)(container).run({
20
+ input: {
21
+ region_id: cart.region?.id,
22
+ customer_id: cart.customer?.id,
23
+ sales_channel_id: cart.sales_channel_id,
24
+ status: utils_1.OrderStatus.PENDING,
25
+ email: cart.email,
26
+ currency_code: cart.currency_code,
27
+ shipping_address: cart.shipping_address,
28
+ billing_address: cart.billing_address,
29
+ // TODO: This should be handle correctly
30
+ no_notification: false,
31
+ items: cart.items,
32
+ shipping_methods: cart.shipping_methods,
33
+ metadata: cart.metadata,
34
+ promo_codes: promoCodes,
35
+ },
36
+ });
37
+ return new workflows_sdk_1.StepResponse(result, { transaction });
38
+ }, async (flow, { container }) => {
39
+ if (!flow) {
40
+ return;
41
+ }
42
+ await (0, create_orders_1.createOrdersWorkflow)(container).cancel({
43
+ transaction: flow.transaction,
44
+ });
45
+ });
@@ -3,5 +3,5 @@ interface StepInput {
3
3
  salesChannelId?: string | null;
4
4
  }
5
5
  export declare const findSalesChannelStepId = "find-sales-channel";
6
- export declare const findSalesChannelStep: import("@medusajs/workflows-sdk").StepFunction<StepInput, SalesChannelDTO | null | undefined>;
6
+ export declare const findSalesChannelStep: import("@medusajs/workflows-sdk").StepFunction<StepInput, SalesChannelDTO | null>;
7
7
  export {};
@@ -3,13 +3,13 @@ export * from "./add-to-cart";
3
3
  export * from "./confirm-inventory";
4
4
  export * from "./create-carts";
5
5
  export * from "./create-line-item-adjustments";
6
+ export * from "./create-order-from-cart";
6
7
  export * from "./create-shipping-method-adjustments";
7
8
  export * from "./find-one-or-any-region";
8
9
  export * from "./find-or-create-customer";
9
10
  export * from "./find-sales-channel";
10
11
  export * from "./get-actions-to-compute-from-promotions";
11
12
  export * from "./get-item-tax-lines";
12
- export * from "./get-shipping-option-price-sets";
13
13
  export * from "./get-variant-price-sets";
14
14
  export * from "./get-variants";
15
15
  export * from "./prepare-adjustments-from-promotion-actions";
@@ -19,13 +19,13 @@ __exportStar(require("./add-to-cart"), exports);
19
19
  __exportStar(require("./confirm-inventory"), exports);
20
20
  __exportStar(require("./create-carts"), exports);
21
21
  __exportStar(require("./create-line-item-adjustments"), exports);
22
+ __exportStar(require("./create-order-from-cart"), exports);
22
23
  __exportStar(require("./create-shipping-method-adjustments"), exports);
23
24
  __exportStar(require("./find-one-or-any-region"), exports);
24
25
  __exportStar(require("./find-or-create-customer"), exports);
25
26
  __exportStar(require("./find-sales-channel"), exports);
26
27
  __exportStar(require("./get-actions-to-compute-from-promotions"), exports);
27
28
  __exportStar(require("./get-item-tax-lines"), exports);
28
- __exportStar(require("./get-shipping-option-price-sets"), exports);
29
29
  __exportStar(require("./get-variant-price-sets"), exports);
30
30
  __exportStar(require("./get-variants"), exports);
31
31
  __exportStar(require("./prepare-adjustments-from-promotion-actions"), exports);
@@ -2,10 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.linkCartAndPaymentCollectionsStep = exports.linkCartAndPaymentCollectionsStepId = void 0;
4
4
  const modules_sdk_1 = require("@medusajs/modules-sdk");
5
+ const utils_1 = require("@medusajs/utils");
5
6
  const workflows_sdk_1 = require("@medusajs/workflows-sdk");
6
7
  exports.linkCartAndPaymentCollectionsStepId = "link-cart-payment-collection";
7
8
  exports.linkCartAndPaymentCollectionsStep = (0, workflows_sdk_1.createStep)(exports.linkCartAndPaymentCollectionsStepId, async (data, { container }) => {
8
- const remoteLink = container.resolve("remoteLink");
9
+ const remoteLink = container.resolve(utils_1.ContainerRegistrationKeys.REMOTE_LINK);
9
10
  const links = data.links.map((d) => ({
10
11
  [modules_sdk_1.Modules.CART]: { cart_id: d.cart_id },
11
12
  [modules_sdk_1.Modules.PAYMENT]: { payment_collection_id: d.payment_collection_id },
@@ -16,7 +17,7 @@ exports.linkCartAndPaymentCollectionsStep = (0, workflows_sdk_1.createStep)(expo
16
17
  if (!data) {
17
18
  return;
18
19
  }
19
- const remoteLink = container.resolve("remoteLink");
20
+ const remoteLink = container.resolve(utils_1.ContainerRegistrationKeys.REMOTE_LINK);
20
21
  const links = data.links.map((d) => ({
21
22
  [modules_sdk_1.Modules.CART]: { cart_id: d.cart_id },
22
23
  [modules_sdk_1.Modules.PAYMENT]: { payment_collection_id: d.payment_collection_id },
@@ -1 +1,2 @@
1
1
  export declare const cartFieldsForRefreshSteps: string[];
2
+ export declare const completeCartFields: string[];
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.cartFieldsForRefreshSteps = void 0;
3
+ exports.completeCartFields = exports.cartFieldsForRefreshSteps = void 0;
4
4
  exports.cartFieldsForRefreshSteps = [
5
5
  "region_id",
6
6
  "currency_code",
@@ -13,3 +13,42 @@ exports.cartFieldsForRefreshSteps = [
13
13
  "customer.*",
14
14
  "customer.groups.*",
15
15
  ];
16
+ exports.completeCartFields = [
17
+ "id",
18
+ "currency_code",
19
+ "email",
20
+ "created_at",
21
+ "updated_at",
22
+ "total",
23
+ "subtotal",
24
+ "tax_total",
25
+ "discount_total",
26
+ "discount_tax_total",
27
+ "original_total",
28
+ "original_tax_total",
29
+ "item_total",
30
+ "item_subtotal",
31
+ "item_tax_total",
32
+ "sales_channel_id",
33
+ "original_item_total",
34
+ "original_item_subtotal",
35
+ "original_item_tax_total",
36
+ "shipping_total",
37
+ "shipping_subtotal",
38
+ "shipping_tax_total",
39
+ "original_shipping_tax_total",
40
+ "original_shipping_tax_subtotal",
41
+ "original_shipping_total",
42
+ "items.*",
43
+ "items.tax_lines.*",
44
+ "items.adjustments.*",
45
+ "customer.*",
46
+ "shipping_methods.*",
47
+ "shipping_methods.tax_lines.*",
48
+ "shipping_methods.adjustments.*",
49
+ "shipping_address.*",
50
+ "billing_address.*",
51
+ "region.*",
52
+ "payment_collection.*",
53
+ "payment_collection.payment_sessions.*",
54
+ ];
@@ -1,10 +1,26 @@
1
- import { BigNumberInput, ProductVariantDTO } from "@medusajs/types";
1
+ import { BigNumberInput, CreateOrderAdjustmentDTO, CreateOrderLineItemTaxLineDTO, ProductVariantDTO } from "@medusajs/types";
2
2
  interface Input {
3
3
  quantity: BigNumberInput;
4
4
  metadata?: Record<string, any>;
5
5
  unitPrice: BigNumberInput;
6
6
  variant: ProductVariantDTO;
7
+ taxLines?: CreateOrderLineItemTaxLineDTO[];
8
+ adjustments?: CreateOrderAdjustmentDTO[];
7
9
  cartId?: string;
8
10
  }
9
11
  export declare function prepareLineItemData(data: Input): any;
12
+ export declare function prepareTaxLinesData(data: CreateOrderLineItemTaxLineDTO[]): {
13
+ description: string | undefined;
14
+ tax_rate_id: string | undefined;
15
+ code: string;
16
+ rate: BigNumberInput;
17
+ provider_id: string | undefined;
18
+ }[];
19
+ export declare function prepareAdjustmentsData(data: CreateOrderAdjustmentDTO[]): {
20
+ code: string | undefined;
21
+ amount: BigNumberInput;
22
+ description: string | undefined;
23
+ promotion_id: string | undefined;
24
+ provider_id: string | undefined;
25
+ }[];
10
26
  export {};
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.prepareLineItemData = void 0;
3
+ exports.prepareAdjustmentsData = exports.prepareTaxLinesData = exports.prepareLineItemData = void 0;
4
4
  function prepareLineItemData(data) {
5
- const { variant, unitPrice, quantity, metadata, cartId } = data;
5
+ const { variant, unitPrice, quantity, metadata, cartId, taxLines, adjustments, } = data;
6
6
  if (!variant.product) {
7
7
  throw new Error("Variant does not have a product");
8
8
  }
@@ -25,9 +25,35 @@ function prepareLineItemData(data) {
25
25
  unit_price: unitPrice,
26
26
  metadata,
27
27
  };
28
+ if (taxLines) {
29
+ lineItem.tax_lines = prepareTaxLinesData(taxLines);
30
+ }
31
+ if (adjustments) {
32
+ lineItem.adjustments = prepareAdjustmentsData(adjustments);
33
+ }
28
34
  if (cartId) {
29
35
  lineItem.cart_id = cartId;
30
36
  }
31
37
  return lineItem;
32
38
  }
33
39
  exports.prepareLineItemData = prepareLineItemData;
40
+ function prepareTaxLinesData(data) {
41
+ return data.map((d) => ({
42
+ description: d.description,
43
+ tax_rate_id: d.tax_rate_id,
44
+ code: d.code,
45
+ rate: d.rate,
46
+ provider_id: d.provider_id,
47
+ }));
48
+ }
49
+ exports.prepareTaxLinesData = prepareTaxLinesData;
50
+ function prepareAdjustmentsData(data) {
51
+ return data.map((d) => ({
52
+ code: d.code,
53
+ amount: d.amount,
54
+ description: d.description,
55
+ promotion_id: d.promotion_id,
56
+ provider_id: d.promotion_id,
57
+ }));
58
+ }
59
+ exports.prepareAdjustmentsData = prepareAdjustmentsData;
@@ -18,44 +18,85 @@ exports.addToCartWorkflow = (0, workflows_sdk_1.createWorkflow)(exports.addToCar
18
18
  const variantIds = (0, workflows_sdk_1.transform)({ input }, (data) => {
19
19
  return (data.input.items ?? []).map((i) => i.variant_id);
20
20
  });
21
- (0, steps_1.validateVariantsExistStep)({ variantIds });
22
- const variants = (0, steps_1.getVariantsStep)({
23
- filter: { id: variantIds },
24
- config: {
25
- select: [
26
- "id",
27
- "title",
28
- "sku",
29
- "barcode",
30
- "product.id",
31
- "product.title",
32
- "product.description",
33
- "product.subtitle",
34
- "product.thumbnail",
35
- "product.type",
36
- "product.collection",
37
- "product.handle",
38
- ],
39
- relations: ["product"],
40
- },
21
+ // TODO: This is on par with the context used in v1.*, but we can be more flexible.
22
+ const pricingContext = (0, workflows_sdk_1.transform)({ cart: input.cart }, (data) => {
23
+ return {
24
+ currency_code: data.cart.currency_code,
25
+ region_id: data.cart.region_id,
26
+ customer_id: data.cart.customer_id,
27
+ };
41
28
  });
42
- const salesChannelLocations = (0, use_remote_query_1.useRemoteQueryStep)({
43
- entry_point: "sales_channels",
44
- fields: ["id", "name", "stock_locations.id", "stock_locations.name"],
45
- variables: { id: input.cart.sales_channel_id },
29
+ const variants = (0, use_remote_query_1.useRemoteQueryStep)({
30
+ entry_point: "variants",
31
+ fields: [
32
+ "id",
33
+ "title",
34
+ "sku",
35
+ "barcode",
36
+ "manage_inventory",
37
+ "product.id",
38
+ "product.title",
39
+ "product.description",
40
+ "product.subtitle",
41
+ "product.thumbnail",
42
+ "product.type",
43
+ "product.collection",
44
+ "product.handle",
45
+ "calculated_price.calculated_amount",
46
+ "inventory_items.inventory_item_id",
47
+ "inventory_items.required_quantity",
48
+ "inventory_items.inventory.location_levels.stock_locations.id",
49
+ "inventory_items.inventory.location_levels.stock_locations.name",
50
+ "inventory_items.inventory.location_levels.stock_locations.sales_channels.id",
51
+ "inventory_items.inventory.location_levels.stock_locations.sales_channels.name",
52
+ ],
53
+ variables: {
54
+ id: variantIds,
55
+ calculated_price: {
56
+ context: pricingContext,
57
+ },
58
+ },
59
+ throw_if_key_not_found: true,
46
60
  });
47
- const productVariantInventoryItems = (0, use_remote_query_1.useRemoteQueryStep)({
48
- entry_point: "product_variant_inventory_items",
49
- fields: ["variant_id", "inventory_item_id", "required_quantity"],
50
- variables: { variant_id: variantIds },
51
- }).config({ name: "inventory-items" });
52
- const confirmInventoryInput = (0, workflows_sdk_1.transform)({ productVariantInventoryItems, salesChannelLocations, input, variants }, (data) => {
53
- if (!data.salesChannelLocations.length) {
54
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, `Sales channel ${data.input.cart.sales_channel_id} is not associated with any stock locations.`);
61
+ const confirmInventoryInput = (0, workflows_sdk_1.transform)({ input, variants }, (data) => {
62
+ const managedVariants = data.variants.filter((v) => v.manage_inventory);
63
+ if (!managedVariants.length) {
64
+ return { items: [] };
65
+ }
66
+ const productVariantInventoryItems = [];
67
+ const stockLocations = data.variants
68
+ .map((v) => v.inventory_items)
69
+ .flat()
70
+ .map((ii) => {
71
+ productVariantInventoryItems.push({
72
+ variant_id: ii.variant_id,
73
+ inventory_item_id: ii.inventory_item_id,
74
+ required_quantity: ii.required_quantity,
75
+ });
76
+ return ii.inventory.location_levels;
77
+ })
78
+ .flat()
79
+ .map((ll) => ll.stock_locations)
80
+ .flat();
81
+ const salesChannelId = data.input.cart.sales_channel_id;
82
+ if (salesChannelId) {
83
+ const salesChannels = stockLocations
84
+ .map((sl) => sl.sales_channels)
85
+ .flat()
86
+ .filter((sc) => sc.id === salesChannelId);
87
+ if (!salesChannels.length) {
88
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, `Sales channel ${salesChannelId} is not associated with any stock locations.`);
89
+ }
90
+ }
91
+ const priceNotFound = data.variants
92
+ .filter((v) => !v.calculated_price)
93
+ .map((v) => v.id);
94
+ if (priceNotFound.length) {
95
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, `Variants with IDs ${priceNotFound.join(", ")} do not have a price`);
55
96
  }
56
97
  const items = (0, prepare_confirm_inventory_input_1.prepareConfirmInventoryInput)({
57
- product_variant_inventory_items: data.productVariantInventoryItems,
58
- location_ids: data.salesChannelLocations[0].stock_locations.map((l) => l.id),
98
+ product_variant_inventory_items: productVariantInventoryItems,
99
+ location_ids: stockLocations.map((l) => l.id),
59
100
  items: data.input.items,
60
101
  variants: data.variants.map((v) => ({
61
102
  id: v.id,
@@ -65,24 +106,12 @@ exports.addToCartWorkflow = (0, workflows_sdk_1.createWorkflow)(exports.addToCar
65
106
  return { items };
66
107
  });
67
108
  (0, steps_1.confirmInventoryStep)(confirmInventoryInput);
68
- // TODO: This is on par with the context used in v1.*, but we can be more flexible.
69
- const pricingContext = (0, workflows_sdk_1.transform)({ cart: input.cart }, (data) => {
70
- return {
71
- currency_code: data.cart.currency_code,
72
- region_id: data.cart.region_id,
73
- customer_id: data.cart.customer_id,
74
- };
75
- });
76
- const priceSets = (0, steps_1.getVariantPriceSetsStep)({
77
- variantIds,
78
- context: pricingContext,
79
- });
80
- const lineItems = (0, workflows_sdk_1.transform)({ priceSets, input, variants }, (data) => {
109
+ const lineItems = (0, workflows_sdk_1.transform)({ input, variants }, (data) => {
81
110
  const items = (data.input.items ?? []).map((item) => {
82
111
  const variant = data.variants.find((v) => v.id === item.variant_id);
83
112
  return (0, prepare_line_item_data_1.prepareLineItemData)({
84
113
  variant: variant,
85
- unitPrice: data.priceSets[item.variant_id].calculated_amount,
114
+ unitPrice: variant.calculated_price.calculated_amount,
86
115
  quantity: item.quantity,
87
116
  metadata: item?.metadata ?? {},
88
117
  cartId: data.input.cart.id,
@@ -0,0 +1,3 @@
1
+ import { CompleteCartWorkflowInputDTO, OrderDTO } from "@medusajs/types";
2
+ export declare const completeCartWorkflowId = "complete-cart";
3
+ export declare const completeCartWorkflow: import("@medusajs/workflows-sdk").ReturnWorkflow<CompleteCartWorkflowInputDTO, OrderDTO, Record<string, Function>>;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.completeCartWorkflow = exports.completeCartWorkflowId = void 0;
4
+ const workflows_sdk_1 = require("@medusajs/workflows-sdk");
5
+ const common_1 = require("../../../common");
6
+ const steps_1 = require("../steps");
7
+ const update_tax_lines_1 = require("../steps/update-tax-lines");
8
+ const fields_1 = require("../utils/fields");
9
+ /*
10
+ - [] Create Tax Lines
11
+ - [] Authorize Payment
12
+ - fail:
13
+ - [] Delete Tax lines
14
+ - [] Reserve Item from inventory (if enabled)
15
+ - fail:
16
+ - [] Delete reservations
17
+ - [] Cancel Payment
18
+ - [] Create order
19
+ */
20
+ exports.completeCartWorkflowId = "complete-cart";
21
+ exports.completeCartWorkflow = (0, workflows_sdk_1.createWorkflow)(exports.completeCartWorkflowId, (input) => {
22
+ const cart = (0, common_1.useRemoteQueryStep)({
23
+ entry_point: "cart",
24
+ fields: fields_1.completeCartFields,
25
+ variables: { id: input.id },
26
+ list: false,
27
+ });
28
+ (0, update_tax_lines_1.updateTaxLinesStep)({ cart_or_cart_id: cart, force_tax_calculation: true });
29
+ const finalCart = (0, common_1.useRemoteQueryStep)({
30
+ entry_point: "cart",
31
+ fields: fields_1.completeCartFields,
32
+ variables: { id: input.id },
33
+ list: false,
34
+ }).config({ name: "final-cart" });
35
+ return (0, steps_1.createOrderFromCartStep)({ cart: finalCart });
36
+ });
@@ -24,49 +24,86 @@ exports.createCartWorkflow = (0, workflows_sdk_1.createWorkflow)(exports.createC
24
24
  }), (0, steps_1.findOrCreateCustomerStep)({
25
25
  customerId: input.customer_id,
26
26
  email: input.email,
27
- }), (0, steps_1.validateVariantsExistStep)({ variantIds }));
28
- const variants = (0, steps_1.getVariantsStep)({
29
- filter: { id: variantIds },
30
- config: {
31
- select: [
32
- "id",
33
- "title",
34
- "sku",
35
- "manage_inventory",
36
- "barcode",
37
- "product.id",
38
- "product.title",
39
- "product.description",
40
- "product.subtitle",
41
- "product.thumbnail",
42
- "product.type",
43
- "product.collection",
44
- "product.handle",
45
- ],
46
- relations: ["product"],
47
- },
27
+ }));
28
+ // TODO: This is on par with the context used in v1.*, but we can be more flexible.
29
+ const pricingContext = (0, workflows_sdk_1.transform)({ input, region, customerData }, (data) => {
30
+ return {
31
+ currency_code: data.input.currency_code ?? data.region.currency_code,
32
+ region_id: data.region.id,
33
+ customer_id: data.customerData.customer?.id,
34
+ };
48
35
  });
49
- const salesChannelLocations = (0, use_remote_query_1.useRemoteQueryStep)({
50
- entry_point: "sales_channels",
51
- fields: ["id", "name", "stock_locations.id", "stock_locations.name"],
52
- variables: { id: salesChannel.id },
36
+ const variants = (0, use_remote_query_1.useRemoteQueryStep)({
37
+ entry_point: "variants",
38
+ fields: [
39
+ "id",
40
+ "title",
41
+ "sku",
42
+ "manage_inventory",
43
+ "barcode",
44
+ "product.id",
45
+ "product.title",
46
+ "product.description",
47
+ "product.subtitle",
48
+ "product.thumbnail",
49
+ "product.type",
50
+ "product.collection",
51
+ "product.handle",
52
+ "calculated_price.calculated_amount",
53
+ "inventory_items.inventory_item_id",
54
+ "inventory_items.required_quantity",
55
+ "inventory_items.inventory.location_levels.stock_locations.id",
56
+ "inventory_items.inventory.location_levels.stock_locations.name",
57
+ "inventory_items.inventory.location_levels.stock_locations.sales_channels.id",
58
+ "inventory_items.inventory.location_levels.stock_locations.sales_channels.name",
59
+ ],
60
+ variables: {
61
+ id: variantIds,
62
+ calculated_price: {
63
+ context: pricingContext,
64
+ },
65
+ },
66
+ throw_if_key_not_found: true,
53
67
  });
54
- const productVariantInventoryItems = (0, use_remote_query_1.useRemoteQueryStep)({
55
- entry_point: "product_variant_inventory_items",
56
- fields: ["variant_id", "inventory_item_id", "required_quantity"],
57
- variables: { variant_id: variantIds },
58
- }).config({ name: "inventory-items" });
59
- const confirmInventoryInput = (0, workflows_sdk_1.transform)({ productVariantInventoryItems, salesChannelLocations, input, variants }, (data) => {
60
- // We don't want to confirm inventory if there are no items in the cart.
61
- if (!data.input.items) {
68
+ const confirmInventoryInput = (0, workflows_sdk_1.transform)({ input, salesChannel, variants }, (data) => {
69
+ const managedVariants = data.variants.filter((v) => v.manage_inventory);
70
+ if (!managedVariants.length) {
62
71
  return { items: [] };
63
72
  }
64
- if (!data.salesChannelLocations.length) {
65
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, `Sales channel ${data.input.sales_channel_id} is not associated with any stock locations.`);
73
+ const productVariantInventoryItems = [];
74
+ const stockLocations = managedVariants
75
+ .map((v) => v.inventory_items)
76
+ .flat()
77
+ .map((ii) => {
78
+ productVariantInventoryItems.push({
79
+ variant_id: ii.variant_id,
80
+ inventory_item_id: ii.inventory_item_id,
81
+ required_quantity: ii.required_quantity,
82
+ });
83
+ return ii.inventory.location_levels;
84
+ })
85
+ .flat()
86
+ .map((ll) => ll.stock_locations)
87
+ .flat();
88
+ const salesChannelId = data.salesChannel?.id;
89
+ if (salesChannelId) {
90
+ const salesChannels = stockLocations
91
+ .map((sl) => sl.sales_channels)
92
+ .flat()
93
+ .filter((sc) => sc.id === salesChannelId);
94
+ if (!salesChannels.length) {
95
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, `Sales channel ${salesChannelId} is not associated with any stock locations.`);
96
+ }
97
+ }
98
+ const priceNotFound = data.variants
99
+ .filter((v) => !v.calculated_price)
100
+ .map((v) => v.id);
101
+ if (priceNotFound.length) {
102
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, `Variants with IDs ${priceNotFound.join(", ")} do not have a price`);
66
103
  }
67
104
  const items = (0, prepare_confirm_inventory_input_1.prepareConfirmInventoryInput)({
68
- product_variant_inventory_items: data.productVariantInventoryItems,
69
- location_ids: data.salesChannelLocations[0].stock_locations.map((l) => l.id),
105
+ product_variant_inventory_items: productVariantInventoryItems,
106
+ location_ids: stockLocations.map((l) => l.id),
70
107
  items: data.input.items,
71
108
  variants: data.variants.map((v) => ({
72
109
  id: v.id,
@@ -76,14 +113,6 @@ exports.createCartWorkflow = (0, workflows_sdk_1.createWorkflow)(exports.createC
76
113
  return { items };
77
114
  });
78
115
  (0, steps_1.confirmInventoryStep)(confirmInventoryInput);
79
- // TODO: This is on par with the context used in v1.*, but we can be more flexible.
80
- const pricingContext = (0, workflows_sdk_1.transform)({ input, region, customerData }, (data) => {
81
- return {
82
- currency_code: data.input.currency_code ?? data.region.currency_code,
83
- region_id: data.region.id,
84
- customer_id: data.customerData.customer?.id,
85
- };
86
- });
87
116
  const priceSets = (0, steps_1.getVariantPriceSetsStep)({
88
117
  variantIds,
89
118
  context: pricingContext,
@@ -1,5 +1,6 @@
1
1
  export * from "./add-shipping-method-to-cart";
2
2
  export * from "./add-to-cart";
3
+ export * from "./complete-cart";
3
4
  export * from "./create-carts";
4
5
  export * from "./create-payment-collection-for-cart";
5
6
  export * from "./list-shipping-options-for-cart";
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./add-shipping-method-to-cart"), exports);
18
18
  __exportStar(require("./add-to-cart"), exports);
19
+ __exportStar(require("./complete-cart"), exports);
19
20
  __exportStar(require("./create-carts"), exports);
20
21
  __exportStar(require("./create-payment-collection-for-cart"), exports);
21
22
  __exportStar(require("./list-shipping-options-for-cart"), exports);
@@ -1,23 +1,3 @@
1
1
  import { ListShippingOptionsForCartWorkflowInputDTO } from "@medusajs/types";
2
2
  export declare const listShippingOptionsForCartWorkflowId = "list-shipping-options-for-cart";
3
- export declare const listShippingOptionsForCartWorkflow: import("@medusajs/workflows-sdk").ReturnWorkflow<ListShippingOptionsForCartWorkflowInputDTO, {
4
- amount: any;
5
- id: string;
6
- name: string;
7
- price_type: import("@medusajs/types").ShippingOptionPriceType;
8
- service_zone_id: string;
9
- shipping_profile_id: string;
10
- provider_id: string;
11
- shipping_option_type_id: string | null;
12
- data: Record<string, unknown> | null;
13
- metadata: Record<string, unknown> | null;
14
- service_zone: import("@medusajs/types").ServiceZoneDTO;
15
- shipping_profile: import("@medusajs/types").ShippingProfileDTO;
16
- fulfillment_provider: import("@medusajs/types").FulfillmentProviderDTO;
17
- type: import("@medusajs/types").ShippingOptionTypeDTO;
18
- rules: import("@medusajs/types").ShippingOptionRuleDTO[];
19
- fulfillments: import("@medusajs/types").FulfillmentDTO[];
20
- created_at: Date;
21
- updated_at: Date;
22
- deleted_at: Date | null;
23
- }[], Record<string, Function>>;
3
+ export declare const listShippingOptionsForCartWorkflow: import("@medusajs/workflows-sdk").ReturnWorkflow<ListShippingOptionsForCartWorkflowInputDTO, any[], Record<string, Function>>;
@@ -1,62 +1,71 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.listShippingOptionsForCartWorkflow = exports.listShippingOptionsForCartWorkflowId = void 0;
4
+ const utils_1 = require("@medusajs/utils");
4
5
  const workflows_sdk_1 = require("@medusajs/workflows-sdk");
5
6
  const use_remote_query_1 = require("../../../common/steps/use-remote-query");
6
- const shipping_options_1 = require("../../../shipping-options");
7
- const steps_1 = require("../steps");
8
7
  exports.listShippingOptionsForCartWorkflowId = "list-shipping-options-for-cart";
9
8
  exports.listShippingOptionsForCartWorkflow = (0, workflows_sdk_1.createWorkflow)(exports.listShippingOptionsForCartWorkflowId, (input) => {
10
9
  const scLocationFulfillmentSets = (0, use_remote_query_1.useRemoteQueryStep)({
11
10
  entry_point: "sales_channels",
12
- fields: ["stock_locations.fulfillment_sets.id"],
13
- variables: { id: input.sales_channel_id },
14
- });
15
- const listOptionsInput = (0, workflows_sdk_1.transform)({ scLocationFulfillmentSets, input }, (data) => {
16
- const fulfillmentSetIds = data.scLocationFulfillmentSets
17
- .map((sc) => sc.stock_locations.map((loc) => loc.fulfillment_sets.map(({ id }) => id)))
18
- .flat(2);
19
- return {
20
- context: {
21
- fulfillment_set_id: fulfillmentSetIds,
22
- address: {
23
- city: data.input.shipping_address?.city,
24
- country_code: data.input.shipping_address?.country_code,
25
- province_code: data.input.shipping_address?.province,
11
+ fields: [
12
+ "stock_locations.fulfillment_sets.id",
13
+ "stock_locations.fulfillment_sets.name",
14
+ "stock_locations.fulfillment_sets.price_type",
15
+ "stock_locations.fulfillment_sets.service_zone_id",
16
+ "stock_locations.fulfillment_sets.shipping_profile_id",
17
+ "stock_locations.fulfillment_sets.provider_id",
18
+ "stock_locations.fulfillment_sets.data",
19
+ "stock_locations.fulfillment_sets.amount",
20
+ "stock_locations.fulfillment_sets.service_zones.shipping_options.id",
21
+ "stock_locations.fulfillment_sets.service_zones.shipping_options.name",
22
+ "stock_locations.fulfillment_sets.service_zones.shipping_options.price_type",
23
+ "stock_locations.fulfillment_sets.service_zones.shipping_options.service_zone_id",
24
+ "stock_locations.fulfillment_sets.service_zones.shipping_options.shipping_profile_id",
25
+ "stock_locations.fulfillment_sets.service_zones.shipping_options.provider_id",
26
+ "stock_locations.fulfillment_sets.service_zones.shipping_options.data",
27
+ "stock_locations.fulfillment_sets.service_zones.shipping_options.amount",
28
+ "stock_locations.fulfillment_sets.service_zones.shipping_options.type.id",
29
+ "stock_locations.fulfillment_sets.service_zones.shipping_options.type.label",
30
+ "stock_locations.fulfillment_sets.service_zones.shipping_options.type.description",
31
+ "stock_locations.fulfillment_sets.service_zones.shipping_options.type.code",
32
+ "stock_locations.fulfillment_sets.service_zones.shipping_options.provider.id",
33
+ "stock_locations.fulfillment_sets.service_zones.shipping_options.provider.is_enabled",
34
+ "stock_locations.fulfillment_sets.service_zones.shipping_options.calculated_price.calculated_amount",
35
+ ],
36
+ variables: {
37
+ id: input.sales_channel_id,
38
+ "stock_locations.fulfillment_sets.service_zones.shipping_options": {
39
+ filters: {
40
+ address: {
41
+ city: input.shipping_address?.city,
42
+ country_code: input.shipping_address?.country_code,
43
+ province_code: input.shipping_address?.province,
44
+ },
26
45
  },
27
46
  },
28
- config: {
29
- select: [
30
- "id",
31
- "name",
32
- "price_type",
33
- "service_zone_id",
34
- "shipping_profile_id",
35
- "provider_id",
36
- "data",
37
- "amount",
38
- ],
39
- relations: ["type", "provider"],
47
+ "stock_locations.fulfillment_sets.service_zones.shipping_options.calculated_price": {
48
+ context: {
49
+ currency_code: input.currency_code,
50
+ },
40
51
  },
41
- };
42
- });
43
- const options = (0, shipping_options_1.listShippingOptionsForContextStep)(listOptionsInput);
44
- const optionIds = (0, workflows_sdk_1.transform)({ options }, (data) => data.options.map((option) => option.id));
45
- // TODO: Separate shipping options based on price_type, flat_rate vs calculated
46
- const priceSets = (0, steps_1.getShippingOptionPriceSetsStep)({
47
- optionIds,
48
- context: {
49
- currency_code: input.currency_code,
50
52
  },
51
53
  });
52
- const shippingOptionsWithPrice = (0, workflows_sdk_1.transform)({ priceSets, options }, (data) => {
53
- const options = data.options.map((option) => {
54
- const price = data.priceSets?.[option.id].calculated_amount;
54
+ const shippingOptionsWithPrice = (0, workflows_sdk_1.transform)({ options: scLocationFulfillmentSets }, (data) => {
55
+ const optionsMissingPrices = [];
56
+ const options = (0, utils_1.deepFlatMap)(data.options, "stock_locations.fulfillment_sets.service_zones.shipping_options.calculated_price", ({ shipping_options }) => {
57
+ const { calculated_price, ...options } = shipping_options ?? {};
58
+ if (!calculated_price) {
59
+ optionsMissingPrices.push(options.id);
60
+ }
55
61
  return {
56
- ...option,
57
- amount: price,
62
+ ...options,
63
+ amount: calculated_price?.calculated_amount,
58
64
  };
59
65
  });
66
+ if (optionsMissingPrices.length) {
67
+ throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Shipping options with IDs ${optionsMissingPrices.join(", ")} do not have a price`);
68
+ }
60
69
  return options;
61
70
  });
62
71
  return shippingOptionsWithPrice;
@@ -26,18 +26,17 @@ exports.refreshPaymentCollectionForCartWorkflow = (0, workflows_sdk_1.createWork
26
26
  "payment_collection.payment_sessions.id",
27
27
  ],
28
28
  variables: { id: input.cart_id },
29
+ throw_if_key_not_found: true,
29
30
  });
31
+ const cart = (0, workflows_sdk_1.transform)({ carts }, (data) => data.carts[0]);
30
32
  (0, payment_collection_1.deletePaymentSessionStep)({
31
- payment_session_id: carts[0].payment_collection.payment_sessions?.[0].id,
33
+ payment_session_id: cart.payment_collection.payment_sessions?.[0].id,
32
34
  });
33
- // TODO: Temporary fixed cart total, so we can test the workflow.
34
- // This will be removed when the totals utilities are built.
35
- const cartTotal = 4242;
36
35
  (0, payment_collection_1.updatePaymentCollectionStep)({
37
- selector: { id: carts[0].payment_collection.id },
36
+ selector: { id: cart.payment_collection.id },
38
37
  update: {
39
- amount: cartTotal,
40
- currency_code: carts[0].currency_code,
38
+ amount: cart.total,
39
+ currency_code: cart.currency_code,
41
40
  },
42
41
  });
43
42
  });
@@ -14,38 +14,88 @@ const refresh_payment_collection_1 = require("./refresh-payment-collection");
14
14
  // - Validate shipping methods for new items (fulfillment module)
15
15
  exports.updateLineItemInCartWorkflowId = "update-line-item-in-cart";
16
16
  exports.updateLineItemInCartWorkflow = (0, workflows_sdk_1.createWorkflow)(exports.updateLineItemInCartWorkflowId, (input) => {
17
- const item = (0, workflows_sdk_1.transform)({ input }, (data) => data.input.item);
18
- const pricingContext = (0, workflows_sdk_1.transform)({ cart: input.cart, item }, (data) => {
17
+ const variantIds = (0, workflows_sdk_1.transform)({ input }, (data) => {
18
+ return [data.input.item.variant_id];
19
+ });
20
+ // TODO: This is on par with the context used in v1.*, but we can be more flexible.
21
+ const pricingContext = (0, workflows_sdk_1.transform)({ cart: input.cart }, (data) => {
19
22
  return {
20
23
  currency_code: data.cart.currency_code,
21
24
  region_id: data.cart.region_id,
22
25
  customer_id: data.cart.customer_id,
23
26
  };
24
27
  });
25
- const variantIds = (0, workflows_sdk_1.transform)({ input }, (data) => [
26
- data.input.item.variant_id,
27
- ]);
28
- const salesChannelLocations = (0, use_remote_query_1.useRemoteQueryStep)({
29
- entry_point: "sales_channels",
30
- fields: ["id", "name", "stock_locations.id", "stock_locations.name"],
31
- variables: { id: input.cart.sales_channel_id },
32
- });
33
- const productVariantInventoryItems = (0, use_remote_query_1.useRemoteQueryStep)({
34
- entry_point: "product_variant_inventory_items",
35
- fields: ["variant_id", "inventory_item_id", "required_quantity"],
36
- variables: { variant_id: variantIds },
37
- }).config({ name: "inventory-items" });
38
- const variants = (0, steps_2.getVariantsStep)({
39
- filter: { id: variantIds },
40
- config: { select: ["id", "manage_inventory"] },
28
+ const variants = (0, use_remote_query_1.useRemoteQueryStep)({
29
+ entry_point: "variants",
30
+ fields: [
31
+ "id",
32
+ "title",
33
+ "sku",
34
+ "barcode",
35
+ "manage_inventory",
36
+ "product.id",
37
+ "product.title",
38
+ "product.description",
39
+ "product.subtitle",
40
+ "product.thumbnail",
41
+ "product.type",
42
+ "product.collection",
43
+ "product.handle",
44
+ "calculated_price.calculated_amount",
45
+ "inventory_items.inventory_item_id",
46
+ "inventory_items.required_quantity",
47
+ "inventory_items.inventory.location_levels.stock_locations.id",
48
+ "inventory_items.inventory.location_levels.stock_locations.name",
49
+ "inventory_items.inventory.location_levels.stock_locations.sales_channels.id",
50
+ "inventory_items.inventory.location_levels.stock_locations.sales_channels.name",
51
+ ],
52
+ variables: {
53
+ id: variantIds,
54
+ calculated_price: {
55
+ context: pricingContext,
56
+ },
57
+ },
58
+ throw_if_key_not_found: true,
41
59
  });
42
- const confirmInventoryInput = (0, workflows_sdk_1.transform)({ productVariantInventoryItems, salesChannelLocations, input, variants }, (data) => {
43
- if (!data.salesChannelLocations.length) {
44
- throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, `Sales channel ${data.input.cart.sales_channel_id} is not associated with any stock locations.`);
60
+ const confirmInventoryInput = (0, workflows_sdk_1.transform)({ input, variants }, (data) => {
61
+ const managedVariants = data.variants.filter((v) => v.manage_inventory);
62
+ if (!managedVariants.length) {
63
+ return { items: [] };
64
+ }
65
+ const productVariantInventoryItems = [];
66
+ const stockLocations = data.variants
67
+ .map((v) => v.inventory_items)
68
+ .flat()
69
+ .map((ii) => {
70
+ productVariantInventoryItems.push({
71
+ variant_id: ii.variant_id,
72
+ inventory_item_id: ii.inventory_item_id,
73
+ required_quantity: ii.required_quantity,
74
+ });
75
+ return ii.inventory.location_levels;
76
+ })
77
+ .flat()
78
+ .map((ll) => ll.stock_locations)
79
+ .flat();
80
+ const salesChannelId = data.input.cart.sales_channel_id;
81
+ if (salesChannelId) {
82
+ const salesChannels = stockLocations
83
+ .map((sl) => sl.sales_channels)
84
+ .flat()
85
+ .filter((sc) => sc.id === salesChannelId);
86
+ if (!salesChannels.length) {
87
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, `Sales channel ${salesChannelId} is not associated with any stock locations.`);
88
+ }
89
+ }
90
+ const priceNotFound = data.variants
91
+ .filter((v) => !v.calculated_price)
92
+ .map((v) => v.id);
93
+ if (priceNotFound.length) {
94
+ throw new medusa_core_utils_1.MedusaError(medusa_core_utils_1.MedusaError.Types.INVALID_DATA, `Variants with IDs ${priceNotFound.join(", ")} do not have a price`);
45
95
  }
46
96
  const items = (0, prepare_confirm_inventory_input_1.prepareConfirmInventoryInput)({
47
- product_variant_inventory_items: data.productVariantInventoryItems,
48
- location_ids: data.salesChannelLocations[0].stock_locations.map((l) => l.id),
97
+ product_variant_inventory_items: productVariantInventoryItems,
98
+ location_ids: stockLocations.map((l) => l.id),
49
99
  items: [data.input.item],
50
100
  variants: data.variants.map((v) => ({
51
101
  id: v.id,
@@ -55,26 +105,20 @@ exports.updateLineItemInCartWorkflow = (0, workflows_sdk_1.createWorkflow)(expor
55
105
  return { items };
56
106
  });
57
107
  (0, steps_2.confirmInventoryStep)(confirmInventoryInput);
58
- const priceSets = (0, steps_2.getVariantPriceSetsStep)({
59
- variantIds,
60
- context: pricingContext,
61
- });
62
- const lineItemUpdate = (0, workflows_sdk_1.transform)({ input, priceSets, item }, (data) => {
63
- const price = data.priceSets[data.item.variant_id].calculated_amount;
108
+ const lineItemUpdate = (0, workflows_sdk_1.transform)({ input, variants }, (data) => {
109
+ const variant = data.variants[0];
110
+ const item = data.input.item;
64
111
  return {
65
112
  data: {
66
113
  ...data.input.update,
67
- unit_price: price,
114
+ unit_price: variant.calculated_price.calculated_amount,
68
115
  },
69
116
  selector: {
70
- id: data.input.item.id,
117
+ id: item.id,
71
118
  },
72
119
  };
73
120
  });
74
- const result = (0, steps_1.updateLineItemsStep)({
75
- data: lineItemUpdate.data,
76
- selector: lineItemUpdate.selector,
77
- });
121
+ const result = (0, steps_1.updateLineItemsStep)(lineItemUpdate);
78
122
  const cart = (0, use_remote_query_1.useRemoteQueryStep)({
79
123
  entry_point: "cart",
80
124
  fields: fields_1.cartFieldsForRefreshSteps,
@@ -4,7 +4,13 @@ exports.createOrdersWorkflow = exports.createOrdersWorkflowId = void 0;
4
4
  const utils_1 = require("@medusajs/utils");
5
5
  const workflows_sdk_1 = require("@medusajs/workflows-sdk");
6
6
  const common_1 = require("../../common");
7
- const cart_1 = require("../../definition/cart");
7
+ const confirm_inventory_1 = require("../../definition/cart/steps/confirm-inventory");
8
+ const find_one_or_any_region_1 = require("../../definition/cart/steps/find-one-or-any-region");
9
+ const find_or_create_customer_1 = require("../../definition/cart/steps/find-or-create-customer");
10
+ const find_sales_channel_1 = require("../../definition/cart/steps/find-sales-channel");
11
+ const get_variant_price_sets_1 = require("../../definition/cart/steps/get-variant-price-sets");
12
+ const get_variants_1 = require("../../definition/cart/steps/get-variants");
13
+ const validate_variants_existence_1 = require("../../definition/cart/steps/validate-variants-existence");
8
14
  const prepare_confirm_inventory_input_1 = require("../../definition/cart/utils/prepare-confirm-inventory-input");
9
15
  const prepare_line_item_data_1 = require("../../definition/cart/utils/prepare-line-item-data");
10
16
  const steps_1 = require("../steps");
@@ -16,15 +22,15 @@ exports.createOrdersWorkflow = (0, workflows_sdk_1.createWorkflow)(exports.creat
16
22
  .map((item) => item.variant_id)
17
23
  .filter(Boolean);
18
24
  });
19
- const [salesChannel, region, customerData] = (0, workflows_sdk_1.parallelize)((0, cart_1.findSalesChannelStep)({
25
+ const [salesChannel, region, customerData] = (0, workflows_sdk_1.parallelize)((0, find_sales_channel_1.findSalesChannelStep)({
20
26
  salesChannelId: input.sales_channel_id,
21
- }), (0, cart_1.findOneOrAnyRegionStep)({
27
+ }), (0, find_one_or_any_region_1.findOneOrAnyRegionStep)({
22
28
  regionId: input.region_id,
23
- }), (0, cart_1.findOrCreateCustomerStep)({
29
+ }), (0, find_or_create_customer_1.findOrCreateCustomerStep)({
24
30
  customerId: input.customer_id,
25
31
  email: input.email,
26
- }), (0, cart_1.validateVariantsExistStep)({ variantIds }));
27
- const variants = (0, cart_1.getVariantsStep)({
32
+ }), (0, validate_variants_existence_1.validateVariantsExistStep)({ variantIds }));
33
+ const variants = (0, get_variants_1.getVariantsStep)({
28
34
  filter: { id: variantIds },
29
35
  config: {
30
36
  select: [
@@ -73,7 +79,7 @@ exports.createOrdersWorkflow = (0, workflows_sdk_1.createWorkflow)(exports.creat
73
79
  });
74
80
  return { items };
75
81
  });
76
- (0, cart_1.confirmInventoryStep)(confirmInventoryInput);
82
+ (0, confirm_inventory_1.confirmInventoryStep)(confirmInventoryInput);
77
83
  // TODO: This is on par with the context used in v1.*, but we can be more flexible.
78
84
  const pricingContext = (0, workflows_sdk_1.transform)({ input, region, customerData }, (data) => {
79
85
  return {
@@ -82,7 +88,7 @@ exports.createOrdersWorkflow = (0, workflows_sdk_1.createWorkflow)(exports.creat
82
88
  customer_id: data.customerData.customer?.id,
83
89
  };
84
90
  });
85
- const priceSets = (0, cart_1.getVariantPriceSetsStep)({
91
+ const priceSets = (0, get_variant_price_sets_1.getVariantPriceSetsStep)({
86
92
  variantIds,
87
93
  context: pricingContext,
88
94
  });
@@ -120,6 +126,8 @@ exports.createOrdersWorkflow = (0, workflows_sdk_1.createWorkflow)(exports.creat
120
126
  data.priceSets[item.variant_id]?.calculated_amount),
121
127
  quantity: item.quantity,
122
128
  metadata: item?.metadata ?? {},
129
+ taxLines: item.tax_lines || [],
130
+ adjustments: item.adjustments || [],
123
131
  });
124
132
  });
125
133
  return items;
@@ -14,12 +14,12 @@ exports.associateFulfillmentSetsWithLocationStep = (0, workflows_sdk_1.createSte
14
14
  .map((link) => {
15
15
  return link.fulfillment_set_ids.map((id) => {
16
16
  return {
17
- [modules_sdk_1.Modules.FULFILLMENT]: {
18
- fulfillment_set_id: id,
19
- },
20
17
  [modules_sdk_1.Modules.STOCK_LOCATION]: {
21
18
  stock_location_id: link.location_id,
22
19
  },
20
+ [modules_sdk_1.Modules.FULFILLMENT]: {
21
+ fulfillment_set_id: id,
22
+ },
23
23
  };
24
24
  });
25
25
  })
@@ -1,2 +1,3 @@
1
+ import { DeleteEntityInput } from "@medusajs/modules-sdk";
1
2
  export declare const deleteStockLocationsStepId = "delete-stock-locations-step";
2
- export declare const deleteStockLocationsStep: import("@medusajs/workflows-sdk").StepFunction<string[], undefined>;
3
+ export declare const deleteStockLocationsStep: import("@medusajs/workflows-sdk").StepFunction<string[], DeleteEntityInput>;
@@ -1,13 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.deleteStockLocationsStep = exports.deleteStockLocationsStepId = void 0;
4
- const workflows_sdk_1 = require("@medusajs/workflows-sdk");
5
4
  const modules_sdk_1 = require("@medusajs/modules-sdk");
5
+ const workflows_sdk_1 = require("@medusajs/workflows-sdk");
6
6
  exports.deleteStockLocationsStepId = "delete-stock-locations-step";
7
7
  exports.deleteStockLocationsStep = (0, workflows_sdk_1.createStep)(exports.deleteStockLocationsStepId, async (input, { container }) => {
8
8
  const service = container.resolve(modules_sdk_1.ModuleRegistrationName.STOCK_LOCATION);
9
- await service.softDelete(input);
10
- return new workflows_sdk_1.StepResponse(void 0, input);
9
+ const softDeletedEntities = await service.softDelete(input);
10
+ return new workflows_sdk_1.StepResponse({
11
+ [modules_sdk_1.Modules.STOCK_LOCATION]: softDeletedEntities,
12
+ }, input);
11
13
  }, async (deletedLocationIds, { container }) => {
12
14
  if (!deletedLocationIds?.length) {
13
15
  return;
@@ -2,13 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.deleteStockLocationsWorkflow = exports.deleteStockLocationsWorkflowId = void 0;
4
4
  const workflows_sdk_1 = require("@medusajs/workflows-sdk");
5
- const modules_sdk_1 = require("@medusajs/modules-sdk");
6
- const steps_1 = require("../steps");
7
5
  const remove_remote_links_1 = require("../../common/steps/remove-remote-links");
6
+ const steps_1 = require("../steps");
8
7
  exports.deleteStockLocationsWorkflowId = "delete-stock-locations-workflow";
9
8
  exports.deleteStockLocationsWorkflow = (0, workflows_sdk_1.createWorkflow)(exports.deleteStockLocationsWorkflowId, (input) => {
10
- (0, steps_1.deleteStockLocationsStep)(input.ids);
11
- (0, remove_remote_links_1.removeRemoteLinkStep)({
12
- [modules_sdk_1.Modules.STOCK_LOCATION]: { stock_location_id: input.ids },
13
- });
9
+ const softDeletedEntities = (0, steps_1.deleteStockLocationsStep)(input.ids);
10
+ (0, remove_remote_links_1.removeRemoteLinkStep)(softDeletedEntities);
14
11
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@medusajs/core-flows",
3
- "version": "0.0.9",
3
+ "version": "0.0.10-next-20240505114610",
4
4
  "description": "Set of workflow definitions for Medusa",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -21,7 +21,7 @@
21
21
  "author": "Medusa",
22
22
  "license": "MIT",
23
23
  "devDependencies": {
24
- "@medusajs/types": "^1.11.16",
24
+ "@medusajs/types": "1.11.17-next-20240505114610",
25
25
  "cross-env": "^5.2.1",
26
26
  "jest": "^29.6.3",
27
27
  "rimraf": "^5.0.1",
@@ -29,10 +29,10 @@
29
29
  "typescript": "^5.1.6"
30
30
  },
31
31
  "dependencies": {
32
- "@medusajs/modules-sdk": "^1.12.11",
33
- "@medusajs/orchestration": "^0.5.7",
34
- "@medusajs/utils": "^1.11.9",
35
- "@medusajs/workflows-sdk": "^0.1.6",
32
+ "@medusajs/modules-sdk": "1.12.12-next-20240505114610",
33
+ "@medusajs/orchestration": "0.5.8-next-20240505114610",
34
+ "@medusajs/utils": "1.11.10-next-20240505114610",
35
+ "@medusajs/workflows-sdk": "0.1.7-next-20240505114610",
36
36
  "awilix": "^8.0.1",
37
37
  "ulid": "^2.3.0"
38
38
  },
@@ -1,7 +0,0 @@
1
- interface StepInput {
2
- optionIds: string[];
3
- context?: Record<string, unknown>;
4
- }
5
- export declare const getShippingOptionPriceSetsStepId = "get-shipping-option-price-sets";
6
- export declare const getShippingOptionPriceSetsStep: import("@medusajs/workflows-sdk").StepFunction<StepInput, any>;
7
- export {};
@@ -1,36 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getShippingOptionPriceSetsStep = exports.getShippingOptionPriceSetsStepId = void 0;
4
- const modules_sdk_1 = require("@medusajs/modules-sdk");
5
- const utils_1 = require("@medusajs/utils");
6
- const workflows_sdk_1 = require("@medusajs/workflows-sdk");
7
- exports.getShippingOptionPriceSetsStepId = "get-shipping-option-price-sets";
8
- exports.getShippingOptionPriceSetsStep = (0, workflows_sdk_1.createStep)(exports.getShippingOptionPriceSetsStepId, async (data, { container }) => {
9
- if (!data.optionIds.length) {
10
- return new workflows_sdk_1.StepResponse({});
11
- }
12
- const pricingModuleService = container.resolve(modules_sdk_1.ModuleRegistrationName.PRICING);
13
- const remoteQuery = container.resolve(utils_1.ContainerRegistrationKeys.REMOTE_QUERY);
14
- const query = (0, utils_1.remoteQueryObjectFromString)({
15
- entryPoint: "shipping_option_price_set",
16
- fields: ["id", "shipping_option_id", "price_set_id"],
17
- variables: {
18
- shipping_option_id: data.optionIds,
19
- },
20
- });
21
- const optionPriceSets = await remoteQuery(query);
22
- const optionsMissingPrices = (0, utils_1.arrayDifference)(data.optionIds, optionPriceSets.map((v) => v.shipping_option_id));
23
- if (optionsMissingPrices.length) {
24
- throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, `Shipping options with IDs ${optionsMissingPrices.join(", ")} do not have a price`);
25
- }
26
- const calculatedPriceSets = await pricingModuleService.calculatePrices({ id: optionPriceSets.map((v) => v.price_set_id) }, { context: data.context });
27
- const idToPriceSet = new Map(calculatedPriceSets.map((p) => [p.id, p]));
28
- const optionToCalculatedPriceSets = optionPriceSets.reduce((acc, { shipping_option_id, price_set_id }) => {
29
- const calculatedPriceSet = idToPriceSet.get(price_set_id);
30
- if (calculatedPriceSet) {
31
- acc[shipping_option_id] = calculatedPriceSet;
32
- }
33
- return acc;
34
- }, {});
35
- return new workflows_sdk_1.StepResponse(optionToCalculatedPriceSets);
36
- });