Skip to content

Commit

Permalink
fix(medusa): Upsert addresses on Orders (#3153)
Browse files Browse the repository at this point in the history
  • Loading branch information
olivermrbl committed Feb 1, 2023
1 parent f776ed2 commit b242e22
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-rivers-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---

fix(medusa): Upsert addresses on Orders
78 changes: 78 additions & 0 deletions integration-tests/api/__tests__/admin/order/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,84 @@ describe("/admin/orders", () => {
updated_at: expect.any(String),
})
})

it("creates a billing address", async () => {
const api = useApi()

await dbConnection.manager.query(
`update "order" set billing_address_id = null where id = 'test-order'`
)

const response = await api
.post(
"/admin/orders/test-order",
{
billing_address: {
first_name: "asafas",
last_name: "safas",
address_1: "asfsa",
city: "safas",
country_code: "us",
postal_code: "3243",
},
},
adminReqConfig
)
.catch((err) => {
console.log(err.response.data)
})

expect(response.status).toEqual(200)
expect(response.data.order.billing_address).toEqual(
expect.objectContaining({
first_name: "asafas",
last_name: "safas",
address_1: "asfsa",
city: "safas",
country_code: "us",
postal_code: "3243",
})
)
})

it("creates a shipping address", async () => {
const api = useApi()

await dbConnection.manager.query(
`update "order" set shipping_address_id = null where id = 'test-order'`
)

const response = await api
.post(
"/admin/orders/test-order",
{
shipping_address: {
first_name: "asafas",
last_name: "safas",
address_1: "asfsa",
city: "safas",
country_code: "us",
postal_code: "3243",
},
},
adminReqConfig
)
.catch((err) => {
console.log(err.response.data)
})

expect(response.status).toEqual(200)
expect(response.data.order.shipping_address).toEqual(
expect.objectContaining({
first_name: "asafas",
last_name: "safas",
address_1: "asfsa",
city: "safas",
country_code: "us",
postal_code: "3243",
})
)
})
})

describe("GET /admin/orders", () => {
Expand Down
12 changes: 5 additions & 7 deletions packages/medusa/src/services/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import {
PaymentStatus,
Return,
Swap,
TrackingLink,
TrackingLink
} from "../models"
import { AddressRepository } from "../repositories/address"
import { OrderRepository } from "../repositories/order"
import { FindConfig, QuerySelector, Selector } from "../types/common"
import {
CreateFulfillmentOrder,
FulFillmentItemType,
FulFillmentItemType
} from "../types/fulfillment"
import { UpdateOrderInput } from "../types/orders"
import { CreateShippingMethodDto } from "../types/shipping-options"
Expand All @@ -48,7 +48,7 @@ import {
ShippingOptionService,
ShippingProfileService,
TaxProviderService,
TotalsService,
TotalsService
} from "."

export const ORDER_CART_ALREADY_EXISTS_ERROR = "Order from cart already exists"
Expand Down Expand Up @@ -903,8 +903,7 @@ class OrderService extends TransactionBaseService {

await addrRepo.save({ ...addr, ...address })
} else {
const created = addrRepo.create({ ...address })
await addrRepo.save(created)
order.billing_address = addrRepo.create({ ...address })
}
}

Expand Down Expand Up @@ -941,8 +940,7 @@ class OrderService extends TransactionBaseService {

await addrRepo.save({ ...addr, ...address })
} else {
const created = addrRepo.create({ ...address })
await addrRepo.save(created)
order.shipping_address = addrRepo.create({ ...address })
}
}

Expand Down

0 comments on commit b242e22

Please sign in to comment.