feat(core-flows,medusa,order,types): update orders (#10373)

**What**
- add order update endpoint
- add workflows and steps for updating orders
- add `registerChanges` method to Order module + workflow step

---

CLOSES CMRC-633
This commit is contained in:
Frane Polić
2024-12-03 18:15:26 +01:00
committed by GitHub
parent 56ae076fd2
commit ab948b7c65
21 changed files with 809 additions and 22 deletions

View File

@@ -199,6 +199,14 @@ export async function createOrderSeeder({
province: "ny",
postal_code: "94016",
},
billing_address: {
address_1: "test billing address 1",
address_2: "test billing address 2",
city: "ny",
country_code: "us",
province: "ny",
postal_code: "94016",
},
sales_channel_id: salesChannel.id,
items: [
{ quantity: 1, variant_id: product.variants[0].id },

View File

@@ -20,6 +20,282 @@ medusaIntegrationTestRunner({
await createAdminUser(dbConnection, adminHeaders, container)
})
describe("POST /orders/:id", () => {
beforeEach(async () => {
seeder = await createOrderSeeder({
api,
container: getContainer(),
})
order = seeder.order
order = (
await api.get(`/admin/orders/${order.id}?fields=+email`, adminHeaders)
).data.order
})
it("should update shipping address on an order (by creating a new Address record)", async () => {
const addressBefore = order.shipping_address
const response = await api.post(
`/admin/orders/${order.id}`,
{
shipping_address: {
city: "New New York",
address_1: "New Main street 123",
},
},
adminHeaders
)
expect(response.data.order.shipping_address.id).not.toEqual(
addressBefore.id
) // new addres created
expect(response.data.order.shipping_address).toEqual(
expect.objectContaining({
customer_id: addressBefore.customer_id,
company: addressBefore.company,
first_name: addressBefore.first_name,
last_name: addressBefore.last_name,
address_1: "New Main street 123",
address_2: addressBefore.address_2,
city: "New New York",
country_code: addressBefore.country_code,
province: addressBefore.province,
postal_code: addressBefore.postal_code,
phone: addressBefore.phone,
})
)
const orderChangesResult = (
await api.get(`/admin/orders/${order.id}/changes`, adminHeaders)
).data.order_changes
expect(orderChangesResult.length).toEqual(1)
expect(orderChangesResult[0]).toEqual(
expect.objectContaining({
version: 1,
change_type: "update_order",
status: "confirmed",
confirmed_at: expect.any(String),
actions: expect.arrayContaining([
expect.objectContaining({
version: 1,
applied: true,
reference_id: addressBefore.id,
reference: "shipping_address",
action: "UPDATE_ORDER_PROPERTIES",
details: {
city: "New New York",
address_1: "New Main street 123",
},
}),
]),
})
)
})
it("should fail to update shipping address if country code has been changed", async () => {
const response = await api
.post(
`/admin/orders/${order.id}`,
{
shipping_address: {
country_code: "HR",
},
},
adminHeaders
)
.catch((e) => e)
expect(response.response.status).toBe(400)
expect(response.response.data.message).toBe(
"Country code cannot be changed"
)
const orderChangesResult = (
await api.get(`/admin/orders/${order.id}/changes`, adminHeaders)
).data.order_changes
expect(orderChangesResult.length).toEqual(0)
})
it("should update billing address on an order (by creating a new Address record)", async () => {
const addressBefore = order.billing_address
const response = await api.post(
`/admin/orders/${order.id}`,
{
billing_address: {
city: "New New York",
address_1: "New Main street 123",
},
},
adminHeaders
)
expect(response.data.order.billing_address.id).not.toEqual(
addressBefore.id
) // new addres created
expect(response.data.order.billing_address).toEqual(
expect.objectContaining({
customer_id: addressBefore.customer_id,
company: addressBefore.company,
first_name: addressBefore.first_name,
last_name: addressBefore.last_name,
address_1: "New Main street 123",
address_2: addressBefore.address_2,
city: "New New York",
country_code: addressBefore.country_code,
province: addressBefore.province,
postal_code: addressBefore.postal_code,
phone: addressBefore.phone,
})
)
const orderChangesResult = (
await api.get(`/admin/orders/${order.id}/changes`, adminHeaders)
).data.order_changes
expect(orderChangesResult.length).toEqual(1)
expect(orderChangesResult[0]).toEqual(
expect.objectContaining({
version: 1,
change_type: "update_order",
status: "confirmed",
confirmed_at: expect.any(String),
actions: expect.arrayContaining([
expect.objectContaining({
version: 1,
applied: true,
reference_id: addressBefore.id,
reference: "billing_address",
action: "UPDATE_ORDER_PROPERTIES",
details: {
city: "New New York",
address_1: "New Main street 123",
},
}),
]),
})
)
})
it("should fail to update billing address if country code has been changed", async () => {
const response = await api
.post(
`/admin/orders/${order.id}`,
{
billing_address: {
country_code: "HR",
},
},
adminHeaders
)
.catch((e) => e)
expect(response.response.status).toBe(400)
expect(response.response.data.message).toBe(
"Country code cannot be changed"
)
const orderChangesResult = (
await api.get(`/admin/orders/${order.id}/changes`, adminHeaders)
).data.order_changes
expect(orderChangesResult.length).toEqual(0)
})
it("should update orders email and shipping address and create 2 change records", async () => {
const response = await api.post(
`/admin/orders/${order.id}?fields=+email,*shipping_address`,
{
email: "new-email@example.com",
shipping_address: {
address_1: "New Main street 123",
},
},
adminHeaders
)
expect(response.data.order.email).toBe("new-email@example.com")
expect(response.data.order.shipping_address.id).not.toEqual(
order.shipping_address.id
)
expect(response.data.order.shipping_address).toEqual(
expect.objectContaining({
address_1: "New Main street 123",
})
)
const orderChangesResult = (
await api.get(`/admin/orders/${order.id}/changes`, adminHeaders)
).data.order_changes
expect(orderChangesResult.length).toEqual(2)
expect(orderChangesResult).toEqual(
expect.arrayContaining([
expect.objectContaining({
version: 1,
change_type: "update_order",
status: "confirmed",
confirmed_at: expect.any(String),
actions: expect.arrayContaining([
expect.objectContaining({
version: 1,
applied: true,
reference_id: order.shipping_address.id,
reference: "shipping_address",
action: "UPDATE_ORDER_PROPERTIES",
details: {
address_1: "New Main street 123",
},
}),
]),
}),
expect.objectContaining({
version: 1,
change_type: "update_order",
status: "confirmed",
confirmed_at: expect.any(String),
actions: expect.arrayContaining([
expect.objectContaining({
version: 1,
applied: true,
reference_id: order.email,
reference: "email",
action: "UPDATE_ORDER_PROPERTIES",
details: {
email: "new-email@example.com",
},
}),
]),
}),
])
)
})
it("should fail to update email if it is invalid", async () => {
const response = await api
.post(
`/admin/orders/${order.id}`,
{
email: "invalid-email",
},
adminHeaders
)
.catch((e) => e)
expect(response.response.status).toBe(400)
expect(response.response.data.message).toBe("The email is not valid")
const orderChangesResult = (
await api.get(`/admin/orders/${order.id}/changes`, adminHeaders)
).data.order_changes
expect(orderChangesResult.length).toEqual(0)
})
})
describe("POST /orders/:id/fulfillments", () => {
beforeEach(async () => {
const stockChannelOverride = (