Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core-flows): copy line items attribute #8471

Merged
merged 3 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2032,6 +2032,7 @@ medusaIntegrationTestRunner({
original_shipping_total: 0,
items: [
expect.objectContaining({
id: expect.stringContaining("ordli_"),
product_id: product.id,
unit_price: 100,
quantity: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export const completeCartFields = [
"payment_collection.*",
"payment_collection.payment_sessions.*",
"items.variant.id",
"items.variant.product.id",
"items.variant.manage_inventory",
"items.variant.allow_backorder",
"items.variant.inventory_items.inventory_item_id",
Expand All @@ -95,6 +96,9 @@ export const productVariantsFields = [
"sku",
"manage_inventory",
"allow_backorder",
"requires_shipping",
"is_discountable",
"variant_option_values",
"barcode",
"product.id",
"product.title",
Expand All @@ -104,6 +108,7 @@ export const productVariantsFields = [
"product.type",
"product.collection",
"product.handle",
"product.discountable",
"calculated_price.calculated_amount",
"calculated_price.is_calculated_price_tax_inclusive",
"inventory_items.inventory_item_id",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {
BigNumberInput,
CartLineItemDTO,
CreateOrderAdjustmentDTO,
CreateOrderLineItemTaxLineDTO,
ProductVariantDTO,
} from "@medusajs/types"

interface Input {
item?: CartLineItemDTO
quantity: BigNumberInput
metadata?: Record<string, any>
unitPrice: BigNumberInput
Expand All @@ -18,6 +20,7 @@ interface Input {

export function prepareLineItemData(data: Input) {
const {
item,
variant,
unitPrice,
isTaxInclusive,
Expand All @@ -34,23 +37,31 @@ export function prepareLineItemData(data: Input) {

const lineItem: any = {
quantity,
title: variant.title,
title: variant.title ?? item?.title,
subtitle: variant.product.title ?? item?.subtitle,
thumbnail: variant.product.thumbnail ?? item?.thumbnail,

subtitle: variant.product.title,
thumbnail: variant.product.thumbnail,

product_id: variant.product.id,
product_title: variant.product.title,
product_description: variant.product.description,
product_subtitle: variant.product.subtitle,
product_type: variant.product.type?.[0]?.value ?? null,
product_collection: variant.product.collection?.[0]?.value ?? null,
product_handle: variant.product.handle,
product_id: variant.product.id ?? item?.product_id,
product_title: variant.product.title ?? item?.product_title,
product_description:
variant.product.description ?? item?.product_description,
product_subtitle: variant.product.subtitle ?? item?.product_subtitle,
product_type:
variant.product.type?.[0]?.value ?? item?.product_type ?? null,
product_collection:
variant.product.collection?.[0]?.value ??
item?.product_collection ??
null,
product_handle: variant.product.handle ?? item?.product_handle,

variant_id: variant.id,
variant_sku: variant.sku,
variant_barcode: variant.barcode,
variant_title: variant.title,
variant_sku: variant.sku ?? item?.variant_sku,
variant_barcode: variant.barcode ?? item?.variant_barcode,
variant_title: variant.title ?? item?.variant_title,
variant_option_values: item?.variant_option_values,

is_discountable: variant.product.discountable ?? item?.is_discountable,
requires_shipping: variant.requires_shipping ?? item?.requires_shipping,

unit_price: unitPrice,
is_tax_inclusive: !!isTaxInclusive,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { authorizePaymentSessionStep } from "../../../payment/steps/authorize-pa
import { validateCartPaymentsStep } from "../steps"
import { reserveInventoryStep } from "../steps/reserve-inventory"
import { completeCartFields } from "../utils/fields"
import { prepareLineItemData } from "../utils/prepare-line-item-data"
import { confirmVariantInventoryWorkflow } from "./confirm-variant-inventory"

export const completeCartWorkflowId = "complete-cart"
Expand Down Expand Up @@ -80,11 +81,32 @@ export const completeCartWorkflow = createWorkflow(
)

const cartToOrder = transform({ input, cart }, ({ cart }) => {
const itemAdjustments = (cart.items || [])
?.map((item) => item.adjustments || [])
const allItems = (cart.items ?? []).map((item) => {
return prepareLineItemData({
item,
variant: item.variant,
unitPrice: item.raw_unit_price ?? item.unit_price,
isTaxInclusive: item.is_tax_inclusive,
quantity: item.raw_quantity ?? item.quantity,
metadata: item?.metadata ?? {},
taxLines: item.tax_lines || [],
adjustments: item.adjustments || [],
})
})

const shippinMethods = JSON.parse(
JSON.stringify(cart.shipping_methods ?? []),
(_, value) => {
delete value.id
return value
}
)
carlos-r-l-rodrigues marked this conversation as resolved.
Show resolved Hide resolved

const itemAdjustments = allItems
.map((item) => item.adjustments || [])
.flat(1)
const shippingAdjustments = (cart.shipping_methods || [])
?.map((sm) => sm.adjustments || [])
const shippingAdjustments = shippinMethods
.map((sm) => sm.adjustments || [])
.flat(1)

const promoCodes = [...itemAdjustments, ...shippingAdjustments]
Expand All @@ -101,8 +123,8 @@ export const completeCartWorkflow = createWorkflow(
shipping_address: cart.shipping_address,
billing_address: cart.billing_address,
no_notification: false,
items: cart.items,
shipping_methods: cart.shipping_methods,
items: allItems,
shipping_methods: shippinMethods,
metadata: cart.metadata,
promo_codes: promoCodes,
}
Expand Down
4 changes: 4 additions & 0 deletions packages/core/types/src/product/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ export interface ProductVariantDTO {
* Whether the product variant's inventory should be managed by the core system.
*/
manage_inventory: boolean
/**
* Whether the product variant's requires shipping.
*/
requires_shipping: boolean
/**
* The HS Code of the product variant.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/medusa/src/api/admin/orders/query-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const defaultAdminRetrieveOrderFields = [
"total",
"subtotal",
"tax_total",
"order_change",
"discount_total",
"discount_tax_total",
"original_total",
Expand Down
Loading