@@ -1,7 +0,0 @@
|
|||||||
---
|
|
||||||
"@medusajs/test-utils": patch
|
|
||||||
"@medusajs/core-flows": patch
|
|
||||||
"@medusajs/framework": patch
|
|
||||||
---
|
|
||||||
|
|
||||||
feat(core-flows): Refresh adjustments when editing orders
|
|
||||||
@@ -1,11 +1,8 @@
|
|||||||
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
|
||||||
import { IOrderModuleService, IPromotionModuleService } from "@medusajs/types"
|
|
||||||
import {
|
import {
|
||||||
ContainerRegistrationKeys,
|
ContainerRegistrationKeys,
|
||||||
Modules,
|
Modules,
|
||||||
OrderChangeStatus,
|
OrderChangeStatus,
|
||||||
PromotionStatus,
|
|
||||||
PromotionType,
|
|
||||||
RuleOperator,
|
RuleOperator,
|
||||||
} from "@medusajs/utils"
|
} from "@medusajs/utils"
|
||||||
import {
|
import {
|
||||||
@@ -29,7 +26,6 @@ medusaIntegrationTestRunner({
|
|||||||
let inventoryItemExtra
|
let inventoryItemExtra
|
||||||
let location
|
let location
|
||||||
let locationTwo
|
let locationTwo
|
||||||
let buyRuleProduct
|
|
||||||
let productExtra
|
let productExtra
|
||||||
let container
|
let container
|
||||||
let region
|
let region
|
||||||
@@ -158,31 +154,6 @@ medusaIntegrationTestRunner({
|
|||||||
)
|
)
|
||||||
).data.product
|
).data.product
|
||||||
|
|
||||||
buyRuleProduct = (
|
|
||||||
await api.post(
|
|
||||||
"/admin/products",
|
|
||||||
{
|
|
||||||
title: "Buy rule product",
|
|
||||||
options: [{ title: "size", values: ["large", "small"] }],
|
|
||||||
shipping_profile_id: shippingProfile.id,
|
|
||||||
variants: [
|
|
||||||
{
|
|
||||||
title: "buy rule variant",
|
|
||||||
sku: "buy-rule-variant-sku",
|
|
||||||
options: { size: "large" },
|
|
||||||
prices: [
|
|
||||||
{
|
|
||||||
currency_code: "usd",
|
|
||||||
amount: 10,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
adminHeaders
|
|
||||||
)
|
|
||||||
).data.product
|
|
||||||
|
|
||||||
const orderModule = container.resolve(Modules.ORDER)
|
const orderModule = container.resolve(Modules.ORDER)
|
||||||
|
|
||||||
order = await orderModule.createOrders({
|
order = await orderModule.createOrders({
|
||||||
@@ -1177,493 +1148,5 @@ medusaIntegrationTestRunner({
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("Order Edits promotions", () => {
|
|
||||||
let appliedPromotion
|
|
||||||
let promotionModule: IPromotionModuleService
|
|
||||||
let orderModule: IOrderModuleService
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
promotionModule = container.resolve(Modules.PROMOTION)
|
|
||||||
|
|
||||||
appliedPromotion = await promotionModule.createPromotions({
|
|
||||||
code: "PROMOTION_APPLIED",
|
|
||||||
type: PromotionType.STANDARD,
|
|
||||||
status: PromotionStatus.ACTIVE,
|
|
||||||
application_method: {
|
|
||||||
type: "percentage",
|
|
||||||
target_type: "order",
|
|
||||||
allocation: "across",
|
|
||||||
value: 10,
|
|
||||||
currency_code: "usd",
|
|
||||||
target_rules: [],
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
orderModule = container.resolve(Modules.ORDER)
|
|
||||||
|
|
||||||
order = await orderModule.createOrders({
|
|
||||||
email: "foo@bar.com",
|
|
||||||
region_id: region.id,
|
|
||||||
sales_channel_id: salesChannel.id,
|
|
||||||
items: [
|
|
||||||
{
|
|
||||||
// @ts-ignore
|
|
||||||
id: "item-1",
|
|
||||||
title: "Custom Item",
|
|
||||||
quantity: 1,
|
|
||||||
unit_price: 10,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
shipping_address: {
|
|
||||||
first_name: "Test",
|
|
||||||
last_name: "Test",
|
|
||||||
address_1: "Test",
|
|
||||||
city: "Test",
|
|
||||||
country_code: "US",
|
|
||||||
postal_code: "12345",
|
|
||||||
phone: "12345",
|
|
||||||
},
|
|
||||||
billing_address: {
|
|
||||||
first_name: "Test",
|
|
||||||
last_name: "Test",
|
|
||||||
address_1: "Test",
|
|
||||||
city: "Test",
|
|
||||||
country_code: "US",
|
|
||||||
postal_code: "12345",
|
|
||||||
},
|
|
||||||
currency_code: "usd",
|
|
||||||
})
|
|
||||||
|
|
||||||
await orderModule.createOrderLineItemAdjustments([
|
|
||||||
{
|
|
||||||
code: appliedPromotion.code!,
|
|
||||||
amount: 1,
|
|
||||||
item_id: "item-1",
|
|
||||||
promotion_id: appliedPromotion.id,
|
|
||||||
},
|
|
||||||
])
|
|
||||||
|
|
||||||
const remoteLink = container.resolve(ContainerRegistrationKeys.LINK)
|
|
||||||
|
|
||||||
await remoteLink.create({
|
|
||||||
[Modules.ORDER]: { order_id: order.id },
|
|
||||||
[Modules.PROMOTION]: { promotion_id: appliedPromotion.id },
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should update adjustments when adding a new item", async () => {
|
|
||||||
let result = await api.post(
|
|
||||||
"/admin/order-edits",
|
|
||||||
{
|
|
||||||
order_id: order.id,
|
|
||||||
description: "Test",
|
|
||||||
},
|
|
||||||
adminHeaders
|
|
||||||
)
|
|
||||||
|
|
||||||
const orderId = result.data.order_change.order_id
|
|
||||||
|
|
||||||
result = (await api.get(`/admin/orders/${orderId}`, adminHeaders)).data
|
|
||||||
.order
|
|
||||||
|
|
||||||
expect(result.original_total).toEqual(10)
|
|
||||||
expect(result.total).toEqual(9)
|
|
||||||
|
|
||||||
// Add item with price $12 + $1.2 in taxes
|
|
||||||
result = (
|
|
||||||
await api.post(
|
|
||||||
`/admin/order-edits/${orderId}/items`,
|
|
||||||
{
|
|
||||||
items: [
|
|
||||||
{
|
|
||||||
variant_id: productExtra.variants[0].id,
|
|
||||||
quantity: 1,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
adminHeaders
|
|
||||||
)
|
|
||||||
).data.order_preview
|
|
||||||
|
|
||||||
// 10% discount on two items of $12 and $10 = $2.2
|
|
||||||
// Aside from this there is a tax rate of 10%, which adds ($1.2 - $0.12 (discount tax)) of taxes on the item of $12.
|
|
||||||
expect(result.total).toEqual(20.88)
|
|
||||||
expect(result.original_total).toEqual(23.2)
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should update adjustments when updating an item", async () => {
|
|
||||||
let result = await api.post(
|
|
||||||
"/admin/order-edits",
|
|
||||||
{
|
|
||||||
order_id: order.id,
|
|
||||||
description: "Test",
|
|
||||||
},
|
|
||||||
adminHeaders
|
|
||||||
)
|
|
||||||
|
|
||||||
const orderId = result.data.order_change.order_id
|
|
||||||
|
|
||||||
const item = order.items[0]
|
|
||||||
|
|
||||||
result = (await api.get(`/admin/orders/${orderId}`, adminHeaders)).data
|
|
||||||
.order
|
|
||||||
|
|
||||||
expect(result.original_total).toEqual(10)
|
|
||||||
expect(result.total).toEqual(9)
|
|
||||||
|
|
||||||
let adjustments = result.items[0].adjustments
|
|
||||||
|
|
||||||
expect(adjustments).toEqual([
|
|
||||||
expect.objectContaining({
|
|
||||||
amount: 1,
|
|
||||||
item_id: item.id,
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
|
|
||||||
// Update item quantity
|
|
||||||
result = (
|
|
||||||
await api.post(
|
|
||||||
`/admin/order-edits/${orderId}/items/item/${item.id}`,
|
|
||||||
{
|
|
||||||
quantity: 2,
|
|
||||||
},
|
|
||||||
adminHeaders
|
|
||||||
)
|
|
||||||
).data.order_preview
|
|
||||||
|
|
||||||
expect(result.total).toEqual(18)
|
|
||||||
expect(result.original_total).toEqual(20)
|
|
||||||
|
|
||||||
adjustments = result.items[0].adjustments
|
|
||||||
|
|
||||||
expect(adjustments).toEqual([
|
|
||||||
expect.objectContaining({
|
|
||||||
amount: 2,
|
|
||||||
item_id: item.id,
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should update adjustments when removing an item", async () => {
|
|
||||||
let result = await api.post(
|
|
||||||
"/admin/order-edits",
|
|
||||||
{
|
|
||||||
order_id: order.id,
|
|
||||||
description: "Test",
|
|
||||||
},
|
|
||||||
adminHeaders
|
|
||||||
)
|
|
||||||
|
|
||||||
const orderId = result.data.order_change.order_id
|
|
||||||
|
|
||||||
const item = order.items[0]
|
|
||||||
|
|
||||||
result = (await api.get(`/admin/orders/${orderId}`, adminHeaders)).data
|
|
||||||
.order
|
|
||||||
|
|
||||||
expect(result.original_total).toEqual(10)
|
|
||||||
expect(result.total).toEqual(9)
|
|
||||||
|
|
||||||
let adjustments = result.items[0].adjustments
|
|
||||||
|
|
||||||
expect(adjustments).toEqual([
|
|
||||||
expect.objectContaining({
|
|
||||||
amount: 1,
|
|
||||||
item_id: item.id,
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
|
|
||||||
result = (
|
|
||||||
await api.post(
|
|
||||||
`/admin/order-edits/${orderId}/items`,
|
|
||||||
{
|
|
||||||
items: [
|
|
||||||
{
|
|
||||||
variant_id: productExtra.variants[0].id,
|
|
||||||
quantity: 1,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
adminHeaders
|
|
||||||
)
|
|
||||||
).data.order_preview
|
|
||||||
|
|
||||||
const orderItems = result.items
|
|
||||||
|
|
||||||
expect(orderItems).toEqual([
|
|
||||||
expect.objectContaining({
|
|
||||||
adjustments: [
|
|
||||||
expect.objectContaining({
|
|
||||||
amount: 1,
|
|
||||||
item_id: item.id,
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
}),
|
|
||||||
expect.objectContaining({
|
|
||||||
adjustments: [
|
|
||||||
expect.objectContaining({
|
|
||||||
amount: 1.2,
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
|
|
||||||
const newItem = result.items.find(
|
|
||||||
(item) => item.variant_id === productExtra.variants[0].id
|
|
||||||
)
|
|
||||||
|
|
||||||
const actionId = newItem.actions[0].id
|
|
||||||
|
|
||||||
result = (
|
|
||||||
await api.delete(
|
|
||||||
`/admin/order-edits/${orderId}/items/${actionId}`,
|
|
||||||
adminHeaders
|
|
||||||
)
|
|
||||||
).data.order_preview
|
|
||||||
|
|
||||||
adjustments = result.items[0].adjustments
|
|
||||||
|
|
||||||
expect(adjustments).toEqual([
|
|
||||||
expect.objectContaining({
|
|
||||||
amount: 1,
|
|
||||||
item_id: item.id,
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should not create adjustments when adding a new item if promotion is disabled", async () => {
|
|
||||||
let result = await api.post(
|
|
||||||
"/admin/order-edits",
|
|
||||||
{
|
|
||||||
order_id: order.id,
|
|
||||||
description: "Test",
|
|
||||||
},
|
|
||||||
adminHeaders
|
|
||||||
)
|
|
||||||
|
|
||||||
const orderId = result.data.order_change.order_id
|
|
||||||
|
|
||||||
result = (await api.get(`/admin/orders/${orderId}`, adminHeaders)).data
|
|
||||||
.order
|
|
||||||
|
|
||||||
expect(result.original_total).toEqual(10)
|
|
||||||
expect(result.total).toEqual(9)
|
|
||||||
|
|
||||||
await api.post(
|
|
||||||
`/admin/promotions/${appliedPromotion.id}`,
|
|
||||||
{
|
|
||||||
status: "draft",
|
|
||||||
},
|
|
||||||
adminHeaders
|
|
||||||
)
|
|
||||||
|
|
||||||
// Add item with price $12 + $1.2 in taxes
|
|
||||||
result = (
|
|
||||||
await api.post(
|
|
||||||
`/admin/order-edits/${orderId}/items`,
|
|
||||||
{
|
|
||||||
items: [
|
|
||||||
{
|
|
||||||
variant_id: productExtra.variants[0].id,
|
|
||||||
quantity: 1,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
adminHeaders
|
|
||||||
)
|
|
||||||
).data.order_preview
|
|
||||||
|
|
||||||
expect(result.total).toEqual(23.2)
|
|
||||||
expect(result.original_total).toEqual(23.2)
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should not change adjustments if order edit is canceled", async () => {
|
|
||||||
let result = await api.post(
|
|
||||||
"/admin/order-edits",
|
|
||||||
{
|
|
||||||
order_id: order.id,
|
|
||||||
description: "Test",
|
|
||||||
},
|
|
||||||
adminHeaders
|
|
||||||
)
|
|
||||||
|
|
||||||
const orderId = result.data.order_change.order_id
|
|
||||||
|
|
||||||
result = (await api.get(`/admin/orders/${orderId}`, adminHeaders)).data
|
|
||||||
.order
|
|
||||||
|
|
||||||
expect(result.original_total).toEqual(10)
|
|
||||||
expect(result.total).toEqual(9)
|
|
||||||
|
|
||||||
// Add item with price $12 + $1.2 in taxes
|
|
||||||
result = (
|
|
||||||
await api.post(
|
|
||||||
`/admin/order-edits/${orderId}/items`,
|
|
||||||
{
|
|
||||||
items: [
|
|
||||||
{
|
|
||||||
variant_id: productExtra.variants[0].id,
|
|
||||||
quantity: 1,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
adminHeaders
|
|
||||||
)
|
|
||||||
).data.order_preview
|
|
||||||
|
|
||||||
expect(result.total).toEqual(20.88)
|
|
||||||
expect(result.original_total).toEqual(23.2)
|
|
||||||
|
|
||||||
await api.delete(`/admin/order-edits/${orderId}`, adminHeaders)
|
|
||||||
|
|
||||||
result = (await api.get(`/admin/orders/${orderId}`, adminHeaders)).data
|
|
||||||
.order
|
|
||||||
|
|
||||||
expect(result.original_total).toEqual(10)
|
|
||||||
expect(result.total).toEqual(9)
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should add, remove, and add buy-get adjustment depending on the quantity of the buy rule product", async () => {
|
|
||||||
promotionModule = container.resolve(Modules.PROMOTION)
|
|
||||||
|
|
||||||
appliedPromotion = await promotionModule.createPromotions({
|
|
||||||
code: "BUY_GET_PROMO",
|
|
||||||
type: "buyget",
|
|
||||||
status: "active",
|
|
||||||
application_method: {
|
|
||||||
allocation: "each",
|
|
||||||
value: 100,
|
|
||||||
max_quantity: 1,
|
|
||||||
type: "percentage",
|
|
||||||
target_type: "items",
|
|
||||||
apply_to_quantity: 1,
|
|
||||||
buy_rules_min_quantity: 2,
|
|
||||||
target_rules: [
|
|
||||||
{
|
|
||||||
operator: "eq",
|
|
||||||
attribute: "items.product.id",
|
|
||||||
values: [productExtra.id],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
buy_rules: [
|
|
||||||
{
|
|
||||||
operator: "eq",
|
|
||||||
attribute: "items.product.id",
|
|
||||||
values: [buyRuleProduct.id],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
is_tax_inclusive: false,
|
|
||||||
is_automatic: true,
|
|
||||||
})
|
|
||||||
|
|
||||||
const orderModule: IOrderModuleService = container.resolve(
|
|
||||||
Modules.ORDER
|
|
||||||
)
|
|
||||||
|
|
||||||
order = await orderModule.createOrders({
|
|
||||||
email: "foo@bar.com",
|
|
||||||
region_id: region.id,
|
|
||||||
sales_channel_id: salesChannel.id,
|
|
||||||
items: [
|
|
||||||
{
|
|
||||||
variant_id: buyRuleProduct.variants[0].id,
|
|
||||||
quantity: 2,
|
|
||||||
title: "Buy rule product",
|
|
||||||
unit_price: 10,
|
|
||||||
product_id: buyRuleProduct.id,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
variant_id: productExtra.variants[0].id,
|
|
||||||
quantity: 1,
|
|
||||||
title: "Extra product",
|
|
||||||
unit_price: 10,
|
|
||||||
product_id: productExtra.id,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
shipping_address: {
|
|
||||||
first_name: "Test",
|
|
||||||
last_name: "Test",
|
|
||||||
address_1: "Test",
|
|
||||||
city: "Test",
|
|
||||||
country_code: "US",
|
|
||||||
postal_code: "12345",
|
|
||||||
phone: "12345",
|
|
||||||
},
|
|
||||||
billing_address: {
|
|
||||||
first_name: "Test",
|
|
||||||
last_name: "Test",
|
|
||||||
address_1: "Test",
|
|
||||||
city: "Test",
|
|
||||||
country_code: "US",
|
|
||||||
postal_code: "12345",
|
|
||||||
},
|
|
||||||
currency_code: "usd",
|
|
||||||
})
|
|
||||||
|
|
||||||
await orderModule.createOrderLineItemAdjustments([
|
|
||||||
{
|
|
||||||
code: appliedPromotion.code!,
|
|
||||||
amount: 1,
|
|
||||||
item_id: "item-1",
|
|
||||||
promotion_id: appliedPromotion.id,
|
|
||||||
},
|
|
||||||
])
|
|
||||||
|
|
||||||
const remoteLink = container.resolve(ContainerRegistrationKeys.LINK)
|
|
||||||
|
|
||||||
await remoteLink.create({
|
|
||||||
[Modules.ORDER]: { order_id: order.id },
|
|
||||||
[Modules.PROMOTION]: { promotion_id: appliedPromotion.id },
|
|
||||||
})
|
|
||||||
|
|
||||||
// Initially, the buy-get adjustment should be added to the order
|
|
||||||
let result = await api.post(
|
|
||||||
"/admin/order-edits",
|
|
||||||
{
|
|
||||||
order_id: order.id,
|
|
||||||
description: "Test",
|
|
||||||
},
|
|
||||||
adminHeaders
|
|
||||||
)
|
|
||||||
|
|
||||||
const orderId = result.data.order_change.order_id
|
|
||||||
|
|
||||||
result = (await api.get(`/admin/orders/${orderId}`, adminHeaders)).data
|
|
||||||
.order
|
|
||||||
|
|
||||||
expect(result.original_total).toEqual(30)
|
|
||||||
expect(result.total).toEqual(20)
|
|
||||||
|
|
||||||
const buyRuleItem = result.items.find(
|
|
||||||
(item) => item.product_id === buyRuleProduct.id
|
|
||||||
)
|
|
||||||
|
|
||||||
// Update buy rule product quantity to 1
|
|
||||||
// This should remove the buy-get adjustment, as it is no longer valid
|
|
||||||
result = (
|
|
||||||
await api.post(
|
|
||||||
`/admin/order-edits/${orderId}/items/item/${buyRuleItem.id}`,
|
|
||||||
{
|
|
||||||
quantity: 1,
|
|
||||||
},
|
|
||||||
adminHeaders
|
|
||||||
)
|
|
||||||
).data.order_preview
|
|
||||||
|
|
||||||
expect(result.total).toEqual(20)
|
|
||||||
expect(result.original_total).toEqual(20)
|
|
||||||
|
|
||||||
// Canceling the order edit should bring back the buy-get adjustment
|
|
||||||
await api.delete(`/admin/order-edits/${orderId}`, adminHeaders)
|
|
||||||
|
|
||||||
result = (await api.get(`/admin/orders/${orderId}`, adminHeaders)).data
|
|
||||||
.order
|
|
||||||
|
|
||||||
expect(result.original_total).toEqual(30)
|
|
||||||
expect(result.total).toEqual(20)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import {
|
|||||||
import { useQueryGraphStep } from "../../../common"
|
import { useQueryGraphStep } from "../../../common"
|
||||||
import { createOrderChangeStep } from "../../steps/create-order-change"
|
import { createOrderChangeStep } from "../../steps/create-order-change"
|
||||||
import { throwIfOrderIsCancelled } from "../../utils/order-validation"
|
import { throwIfOrderIsCancelled } from "../../utils/order-validation"
|
||||||
import { refreshOrderEditAdjustmentsWorkflow } from "./refresh-order-edit-adjustments"
|
|
||||||
import { fieldsToRefreshOrderEdit } from "./utils/fields"
|
import { fieldsToRefreshOrderEdit } from "./utils/fields"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -105,12 +104,6 @@ export const beginOrderEditOrderWorkflow = createWorkflow(
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
refreshOrderEditAdjustmentsWorkflow.runAsStep({
|
|
||||||
input: {
|
|
||||||
order: order,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
return new WorkflowResponse(createOrderChangeStep(orderChangeInput))
|
return new WorkflowResponse(createOrderChangeStep(orderChangeInput))
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ import {
|
|||||||
throwIfIsCancelled,
|
throwIfIsCancelled,
|
||||||
throwIfOrderChangeIsNotActive,
|
throwIfOrderChangeIsNotActive,
|
||||||
} from "../../utils/order-validation"
|
} from "../../utils/order-validation"
|
||||||
import { refreshOrderEditAdjustmentsWorkflow } from "./refresh-order-edit-adjustments"
|
|
||||||
import { fieldsToRefreshOrderEdit } from "./utils/fields"
|
import { fieldsToRefreshOrderEdit } from "./utils/fields"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -161,11 +160,5 @@ export const cancelBeginOrderEditWorkflow = createWorkflow(
|
|||||||
data: eventData,
|
data: eventData,
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
refreshOrderEditAdjustmentsWorkflow.runAsStep({
|
|
||||||
input: {
|
|
||||||
order: order,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ import {
|
|||||||
import { addOrderLineItemsWorkflow } from "../add-line-items"
|
import { addOrderLineItemsWorkflow } from "../add-line-items"
|
||||||
import { createOrderChangeActionsWorkflow } from "../create-order-change-actions"
|
import { createOrderChangeActionsWorkflow } from "../create-order-change-actions"
|
||||||
import { updateOrderTaxLinesWorkflow } from "../update-tax-lines"
|
import { updateOrderTaxLinesWorkflow } from "../update-tax-lines"
|
||||||
import { refreshOrderEditAdjustmentsWorkflow } from "./refresh-order-edit-adjustments"
|
|
||||||
import { fieldsToRefreshOrderEdit } from "./utils/fields"
|
import { fieldsToRefreshOrderEdit } from "./utils/fields"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -184,12 +183,6 @@ export const orderEditAddNewItemWorkflow = createWorkflow(
|
|||||||
input: orderChangeActionInput,
|
input: orderChangeActionInput,
|
||||||
})
|
})
|
||||||
|
|
||||||
refreshOrderEditAdjustmentsWorkflow.runAsStep({
|
|
||||||
input: {
|
|
||||||
order: order,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
return new WorkflowResponse(previewOrderChangeStep(input.order_id))
|
return new WorkflowResponse(previewOrderChangeStep(input.order_id))
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
-7
@@ -24,7 +24,6 @@ import {
|
|||||||
throwIfOrderChangeIsNotActive,
|
throwIfOrderChangeIsNotActive,
|
||||||
} from "../../utils/order-validation"
|
} from "../../utils/order-validation"
|
||||||
import { createOrderChangeActionsWorkflow } from "../create-order-change-actions"
|
import { createOrderChangeActionsWorkflow } from "../create-order-change-actions"
|
||||||
import { refreshOrderEditAdjustmentsWorkflow } from "./refresh-order-edit-adjustments"
|
|
||||||
import { fieldsToRefreshOrderEdit } from "./utils/fields"
|
import { fieldsToRefreshOrderEdit } from "./utils/fields"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -177,12 +176,6 @@ export const orderEditUpdateItemQuantityWorkflow = createWorkflow(
|
|||||||
input: orderChangeActionInput,
|
input: orderChangeActionInput,
|
||||||
})
|
})
|
||||||
|
|
||||||
refreshOrderEditAdjustmentsWorkflow.runAsStep({
|
|
||||||
input: {
|
|
||||||
order: order,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
return new WorkflowResponse(previewOrderChangeStep(input.order_id))
|
return new WorkflowResponse(previewOrderChangeStep(input.order_id))
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
-136
@@ -1,136 +0,0 @@
|
|||||||
import { PromotionActions } from "@medusajs/framework/utils"
|
|
||||||
import {
|
|
||||||
createWorkflow,
|
|
||||||
transform,
|
|
||||||
WorkflowData,
|
|
||||||
WorkflowResponse,
|
|
||||||
} from "@medusajs/framework/workflows-sdk"
|
|
||||||
import {
|
|
||||||
ComputeActionItemLine,
|
|
||||||
OrderStatus,
|
|
||||||
PromotionDTO,
|
|
||||||
} from "@medusajs/types"
|
|
||||||
import {
|
|
||||||
refreshDraftOrderAdjustmentsWorkflow
|
|
||||||
} from "../../../draft-order/workflows/refresh-draft-order-adjustments"
|
|
||||||
import { previewOrderChangeStep } from "../../steps"
|
|
||||||
|
|
||||||
export const refreshOrderEditAdjustmentsWorkflowId =
|
|
||||||
"refresh-order-edit-adjustments"
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The details of the order to refresh the adjustments for.
|
|
||||||
*/
|
|
||||||
export interface RefreshOrderEditAdjustmentsWorkflowInput {
|
|
||||||
/**
|
|
||||||
* The order edit to refresh the adjustments for.
|
|
||||||
*/
|
|
||||||
order: {
|
|
||||||
/**
|
|
||||||
* The ID of the order.
|
|
||||||
*/
|
|
||||||
id: string
|
|
||||||
/**
|
|
||||||
* The status of the order.
|
|
||||||
*/
|
|
||||||
status: OrderStatus
|
|
||||||
/**
|
|
||||||
* The 2 character ISO code for the currency.
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* "usd"
|
|
||||||
*/
|
|
||||||
currency_code: string
|
|
||||||
/**
|
|
||||||
* The date the order was canceled at.
|
|
||||||
*/
|
|
||||||
canceled_at?: string | Date
|
|
||||||
/**
|
|
||||||
* The items in the order.
|
|
||||||
*/
|
|
||||||
items: ComputeActionItemLine[]
|
|
||||||
/**
|
|
||||||
* The promotions applied to the order.
|
|
||||||
*/
|
|
||||||
promotions: PromotionDTO[]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This workflow refreshes the adjustments for an order edit. It's used by other workflows, such as
|
|
||||||
* {@link beginOrderEditOrderWorkflow}.
|
|
||||||
*
|
|
||||||
* You can use this workflow within your own customizations or custom workflows, allowing you to wrap custom logic around refreshing adjustments for order edits
|
|
||||||
* in your custom flows.
|
|
||||||
*
|
|
||||||
* @since 2.10.0
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* const { result } = await refreshOrderEditAdjustmentsWorkflow(container)
|
|
||||||
* .run({
|
|
||||||
* input: {
|
|
||||||
* order: {
|
|
||||||
* id: "order_123",
|
|
||||||
* // Imported from @medusajs/framework/types
|
|
||||||
* status: OrderStatus.PENDING,
|
|
||||||
* currency_code: "usd",
|
|
||||||
* items: [
|
|
||||||
* {
|
|
||||||
* id: "item_1",
|
|
||||||
* quantity: 1,
|
|
||||||
* subtotal: 10,
|
|
||||||
* original_total: 10,
|
|
||||||
* is_discountable: true
|
|
||||||
* }
|
|
||||||
* ],
|
|
||||||
* promotions: [],
|
|
||||||
* },
|
|
||||||
* },
|
|
||||||
* })
|
|
||||||
*
|
|
||||||
* @summary
|
|
||||||
*
|
|
||||||
* Refreshes adjustments for an order edit.
|
|
||||||
*/
|
|
||||||
export const refreshOrderEditAdjustmentsWorkflow = createWorkflow(
|
|
||||||
refreshOrderEditAdjustmentsWorkflowId,
|
|
||||||
function (input: WorkflowData<RefreshOrderEditAdjustmentsWorkflowInput>) {
|
|
||||||
const orderEditPromoCodes: string[] = transform({ input }, ({ input }) => {
|
|
||||||
return input.order.promotions
|
|
||||||
.map((p) => p?.code)
|
|
||||||
.filter(Boolean) as string[]
|
|
||||||
})
|
|
||||||
|
|
||||||
// we want the previewed order to contain updated promotions,
|
|
||||||
// so we fetch it to use it for refreshing adjustments
|
|
||||||
const orderPreview = previewOrderChangeStep(input.order.id).config({
|
|
||||||
name: "order-preview",
|
|
||||||
})
|
|
||||||
|
|
||||||
const orderToRefresh = transform(
|
|
||||||
{ input, orderPreview },
|
|
||||||
({ input, orderPreview }) => {
|
|
||||||
return {
|
|
||||||
...orderPreview,
|
|
||||||
items: orderPreview.items.map((item) => ({
|
|
||||||
...item,
|
|
||||||
// Buy-Get promotions rely on the product ID, so we need to manually set it before refreshing adjustments
|
|
||||||
product: { id: item.product_id },
|
|
||||||
})),
|
|
||||||
currency_code: input.order.currency_code,
|
|
||||||
promotions: input.order.promotions,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
refreshDraftOrderAdjustmentsWorkflow.runAsStep({
|
|
||||||
input: {
|
|
||||||
order: orderToRefresh,
|
|
||||||
promo_codes: orderEditPromoCodes,
|
|
||||||
action: PromotionActions.REPLACE,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
return new WorkflowResponse(void 0)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
-7
@@ -25,7 +25,6 @@ import {
|
|||||||
throwIfIsCancelled,
|
throwIfIsCancelled,
|
||||||
throwIfOrderChangeIsNotActive,
|
throwIfOrderChangeIsNotActive,
|
||||||
} from "../../utils/order-validation"
|
} from "../../utils/order-validation"
|
||||||
import { refreshOrderEditAdjustmentsWorkflow } from "./refresh-order-edit-adjustments"
|
|
||||||
import { fieldsToRefreshOrderEdit } from "./utils/fields"
|
import { fieldsToRefreshOrderEdit } from "./utils/fields"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -168,12 +167,6 @@ export const removeItemOrderEditActionWorkflow = createWorkflow(
|
|||||||
|
|
||||||
deleteOrderChangeActionsStep({ ids: [input.action_id] })
|
deleteOrderChangeActionsStep({ ids: [input.action_id] })
|
||||||
|
|
||||||
refreshOrderEditAdjustmentsWorkflow.runAsStep({
|
|
||||||
input: {
|
|
||||||
order: order,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
return new WorkflowResponse(previewOrderChangeStep(order.id))
|
return new WorkflowResponse(previewOrderChangeStep(order.id))
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ import {
|
|||||||
throwIfIsCancelled,
|
throwIfIsCancelled,
|
||||||
throwIfOrderChangeIsNotActive,
|
throwIfOrderChangeIsNotActive,
|
||||||
} from "../../utils/order-validation"
|
} from "../../utils/order-validation"
|
||||||
import { refreshOrderEditAdjustmentsWorkflow } from "./refresh-order-edit-adjustments"
|
|
||||||
import { fieldsToRefreshOrderEdit } from "./utils/fields"
|
import { fieldsToRefreshOrderEdit } from "./utils/fields"
|
||||||
|
|
||||||
function getOrderChangesData({
|
function getOrderChangesData({
|
||||||
@@ -176,12 +175,6 @@ export const requestOrderEditRequestWorkflow = createWorkflow(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
refreshOrderEditAdjustmentsWorkflow.runAsStep({
|
|
||||||
input: {
|
|
||||||
order: order,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
emitEventStep({
|
emitEventStep({
|
||||||
eventName: OrderEditWorkflowEvents.REQUESTED,
|
eventName: OrderEditWorkflowEvents.REQUESTED,
|
||||||
data: eventData,
|
data: eventData,
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ import {
|
|||||||
throwIfIsCancelled,
|
throwIfIsCancelled,
|
||||||
throwIfOrderChangeIsNotActive,
|
throwIfOrderChangeIsNotActive,
|
||||||
} from "../../utils/order-validation"
|
} from "../../utils/order-validation"
|
||||||
import { refreshOrderEditAdjustmentsWorkflow } from "./refresh-order-edit-adjustments"
|
|
||||||
import { fieldsToRefreshOrderEdit } from "./utils/fields"
|
import { fieldsToRefreshOrderEdit } from "./utils/fields"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -186,12 +185,6 @@ export const updateOrderEditAddItemWorkflow = createWorkflow(
|
|||||||
|
|
||||||
updateOrderChangeActionsStep([updateData])
|
updateOrderChangeActionsStep([updateData])
|
||||||
|
|
||||||
refreshOrderEditAdjustmentsWorkflow.runAsStep({
|
|
||||||
input: {
|
|
||||||
order: order,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
return new WorkflowResponse(previewOrderChangeStep(order.id))
|
return new WorkflowResponse(previewOrderChangeStep(order.id))
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
-7
@@ -22,7 +22,6 @@ import {
|
|||||||
throwIfIsCancelled,
|
throwIfIsCancelled,
|
||||||
throwIfOrderChangeIsNotActive,
|
throwIfOrderChangeIsNotActive,
|
||||||
} from "../../utils/order-validation"
|
} from "../../utils/order-validation"
|
||||||
import { refreshOrderEditAdjustmentsWorkflow } from "./refresh-order-edit-adjustments"
|
|
||||||
import { fieldsToRefreshOrderEdit } from "./utils/fields"
|
import { fieldsToRefreshOrderEdit } from "./utils/fields"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -186,12 +185,6 @@ export const updateOrderEditItemQuantityWorkflow = createWorkflow(
|
|||||||
|
|
||||||
updateOrderChangeActionsStep([updateData])
|
updateOrderChangeActionsStep([updateData])
|
||||||
|
|
||||||
refreshOrderEditAdjustmentsWorkflow.runAsStep({
|
|
||||||
input: {
|
|
||||||
order: order,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
return new WorkflowResponse(previewOrderChangeStep(order.id))
|
return new WorkflowResponse(previewOrderChangeStep(order.id))
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user