fix: make it possible to update order shipping address (#668)

This commit is contained in:
Sebastian Rindom
2021-11-05 16:48:29 +01:00
committed by GitHub
parent 45b344fbe1
commit e0fa06fe96
3 changed files with 93 additions and 7 deletions

View File

@@ -0,0 +1,22 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`/admin/orders POST /admin/orders/:id updates a shipping adress 1`] = `
Object {
"address_1": "Some Street",
"address_2": "",
"city": "losangeles",
"company": null,
"country_code": "us",
"created_at": Any<String>,
"customer_id": null,
"deleted_at": null,
"first_name": "lebron",
"id": Any<String>,
"last_name": null,
"metadata": null,
"phone": null,
"postal_code": "1235",
"province": "",
"updated_at": Any<String>,
}
`;

View File

@@ -74,6 +74,63 @@ describe("/admin/orders", () => {
})
})
describe("POST /admin/orders/:id", () => {
beforeEach(async () => {
try {
await adminSeeder(dbConnection)
await orderSeeder(dbConnection)
} catch (err) {
throw err
}
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("updates a shipping adress", async () => {
const api = useApi()
const response = await api
.post(
"/admin/orders/test-order",
{
email: "test@test.com",
shipping_address: {
address_1: "Some Street",
address_2: "",
province: "",
postal_code: "1235",
city: "losangeles",
country_code: "us",
},
},
{
headers: {
authorization: "Bearer test_token",
},
}
)
.catch((err) => {
console.log(err.response.data)
})
expect(response.status).toEqual(200)
expect(response.data.order.shipping_address).toMatchSnapshot({
id: expect.any(String),
address_1: "Some Street",
address_2: "",
province: "",
postal_code: "1235",
city: "losangeles",
country_code: "us",
created_at: expect.any(String),
updated_at: expect.any(String),
})
})
})
describe("GET /admin/orders", () => {
beforeEach(async () => {
try {

View File

@@ -784,7 +784,7 @@ class OrderService extends BaseService {
await addrRepo.save({ ...addr, ...address })
} else {
const created = await addrRepo.create({ ...address })
const created = addrRepo.create({ ...address })
await addrRepo.save(created)
}
}
@@ -877,26 +877,33 @@ class OrderService extends BaseService {
)
}
const { ...rest } = update
const {
metadata,
shipping_address,
billing_address,
no_notification,
items,
...rest
} = update
if ("metadata" in update) {
order.metadata = this.setMetadata_(order, update.metadata)
order.metadata = this.setMetadata_(order, metadata)
}
if ("shipping_address" in update) {
await this.updateShippingAddress_(order, update.shipping_address)
await this.updateShippingAddress_(order, shipping_address)
}
if ("billing_address" in update) {
await this.updateBillingAddress_(order, update.billing_address)
await this.updateBillingAddress_(order, billing_address)
}
if ("no_notification" in update) {
order.no_notification = update.no_notification
order.no_notification = no_notification
}
if ("items" in update) {
for (const item of update.items) {
for (const item of items) {
await this.lineItemService_.withTransaction(manager).create({
...item,
order_id: orderId,