Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit e32b77f

Browse files
authored
Merge pull request #3119 from wordpress-mobile/woo/orders-batch-endpoint
Woo: Part one for orders/batch endpoint
2 parents 7cb4939 + 6a3a729 commit e32b77f

File tree

4 files changed

+248
-0
lines changed

4 files changed

+248
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.wordpress.android.fluxc.network.rest.wpcom.wc.order
2+
3+
import com.google.gson.GsonBuilder
4+
import org.junit.Test
5+
import org.wordpress.android.fluxc.UnitTestUtils
6+
import kotlin.test.assertEquals
7+
8+
class BatchOrderApiResponseTest {
9+
@Test
10+
fun testDeserializeBatchOrderResponse() {
11+
val testGson = GsonBuilder()
12+
.create()
13+
14+
val batchOrderJson = UnitTestUtils.getStringFromResourceFile(
15+
this.javaClass, "wc/orders-batch.json"
16+
)
17+
18+
val response = testGson.fromJson(batchOrderJson, BatchOrderApiResponse::class.java)
19+
val orders = response.update
20+
21+
assertEquals(2, orders.size)
22+
23+
val firstOrder = orders[0] as BatchOrderApiResponse.OrderResponse.Success
24+
assertEquals(1032L, firstOrder.order.id)
25+
assertEquals("224.00", firstOrder.order.total)
26+
27+
val secondOrder = orders[1] as BatchOrderApiResponse.OrderResponse.Error
28+
assertEquals(525L, secondOrder.id)
29+
assertEquals("woocommerce_rest_shop_order_invalid_id", secondOrder.error.code)
30+
assertEquals(400, secondOrder.error.data.status)
31+
}
32+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
{
2+
"update": [
3+
{
4+
"id": 1032,
5+
"parent_id": 0,
6+
"status": "completed",
7+
"currency": "USD",
8+
"version": "9.4.3",
9+
"prices_include_tax": false,
10+
"date_created": "2024-12-04T11:21:17",
11+
"date_modified": "2024-12-04T11:23:19",
12+
"discount_total": "0.00",
13+
"discount_tax": "0.00",
14+
"shipping_total": "3.00",
15+
"shipping_tax": "0.00",
16+
"cart_tax": "0.00",
17+
"total": "224.00",
18+
"total_tax": "0.00",
19+
"customer_id": 0,
20+
"order_key": "wc_order_XXXXXXXXXXXX",
21+
"billing": {
22+
"first_name": "",
23+
"last_name": "",
24+
"company": "",
25+
"address_1": "",
26+
"address_2": "",
27+
"city": "",
28+
"state": "",
29+
"postcode": "",
30+
"country": "",
31+
"email": "",
32+
"phone": ""
33+
},
34+
"shipping": {
35+
"first_name": "",
36+
"last_name": "",
37+
"company": "",
38+
"address_1": "",
39+
"address_2": "",
40+
"city": "",
41+
"state": "",
42+
"postcode": "",
43+
"country": "",
44+
"phone": ""
45+
},
46+
"payment_method": "",
47+
"payment_method_title": "",
48+
"transaction_id": "",
49+
"customer_ip_address": "",
50+
"customer_user_agent": "",
51+
"created_via": "rest-api",
52+
"customer_note": "Asdasd",
53+
"date_completed": "2024-12-04T11:23:19",
54+
"date_paid": "2024-12-04T11:23:19",
55+
"cart_hash": "",
56+
"number": "1032",
57+
"meta_data": [
58+
{
59+
"id": 18608,
60+
"key": "_wc_order_attribution_source_type",
61+
"value": "mobile_app"
62+
}
63+
],
64+
"line_items": [
65+
{
66+
"id": 162,
67+
"name": "Album",
68+
"product_id": 71,
69+
"variation_id": 0,
70+
"quantity": 1,
71+
"tax_class": "",
72+
"subtotal": "216.00",
73+
"subtotal_tax": "0.00",
74+
"total": "216.00",
75+
"total_tax": "0.00",
76+
"taxes": [],
77+
"meta_data": [],
78+
"sku": "woo-album",
79+
"price": 216,
80+
"image": {
81+
"id": "100",
82+
"src": "https://example.com/images/album-1.jpg"
83+
},
84+
"parent_name": null,
85+
"bundled_by": "",
86+
"bundled_item_title": "",
87+
"bundled_items": []
88+
}
89+
],
90+
"tax_lines": [],
91+
"shipping_lines": [
92+
{
93+
"id": 166,
94+
"method_title": "Reg ship",
95+
"method_id": "flat_rate",
96+
"instance_id": "",
97+
"total": "3.00",
98+
"total_tax": "0.00",
99+
"taxes": [],
100+
"meta_data": []
101+
}
102+
],
103+
"fee_lines": [
104+
{
105+
"id": 165,
106+
"name": "Just for you",
107+
"tax_class": "",
108+
"tax_status": "none",
109+
"amount": "",
110+
"total": "5.00",
111+
"total_tax": "0.00",
112+
"taxes": [],
113+
"meta_data": []
114+
}
115+
],
116+
"coupon_lines": [],
117+
"refunds": [],
118+
"payment_url": "https://example.com/checkout/order-pay/1032/",
119+
"is_editable": false,
120+
"needs_payment": false,
121+
"needs_processing": true,
122+
"date_created_gmt": "2024-12-04T04:21:17",
123+
"date_modified_gmt": "2024-12-04T04:23:19",
124+
"date_completed_gmt": "2024-12-04T04:23:19",
125+
"date_paid_gmt": "2024-12-04T04:23:19",
126+
"currency_symbol": "$",
127+
"_links": {
128+
"self": [
129+
{
130+
"href": "https://example.com/wp-json/wc/v3/orders/1032",
131+
"targetHints": {
132+
"allow": [
133+
"GET",
134+
"POST",
135+
"PUT",
136+
"PATCH",
137+
"DELETE"
138+
]
139+
}
140+
}
141+
],
142+
"collection": [
143+
{
144+
"href": "https://example.com/wp-json/wc/v3/orders"
145+
}
146+
]
147+
}
148+
},
149+
{
150+
"id": "525",
151+
"error": {
152+
"code": "woocommerce_rest_shop_order_invalid_id",
153+
"message": "Invalid ID.",
154+
"data": {
155+
"status": 400
156+
}
157+
}
158+
}
159+
]
160+
}

fluxc-processor/src/main/resources/wc-wp-api-endpoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
/orders/<id>/shipment-trackings/providers/
77
/orders/<id>/receipt/
88
/orders/<id>/actions/send_order_details
9+
/orders/batch
910

1011
/products/<id>/
1112
/products/<id>/variations/
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.wordpress.android.fluxc.network.rest.wpcom.wc.order
2+
3+
import com.google.gson.JsonDeserializationContext
4+
import com.google.gson.JsonDeserializer
5+
import com.google.gson.JsonElement
6+
import com.google.gson.annotations.JsonAdapter
7+
import java.lang.reflect.Type
8+
import org.wordpress.android.fluxc.network.Response
9+
10+
data class BatchOrderApiResponse(
11+
val update: List<OrderResponse>
12+
) : Response {
13+
@JsonAdapter(OrderResponseDeserializer::class)
14+
sealed class OrderResponse {
15+
data class Success(
16+
val order: OrderDto
17+
) : OrderResponse()
18+
19+
data class Error(
20+
val id: Long,
21+
val error: ErrorResponse
22+
) : OrderResponse()
23+
}
24+
25+
data class ErrorResponse(
26+
val code: String,
27+
val message: String,
28+
val data: ErrorData
29+
)
30+
31+
data class ErrorData(
32+
val status: Int
33+
)
34+
35+
private class OrderResponseDeserializer : JsonDeserializer<OrderResponse> {
36+
override fun deserialize(
37+
json: JsonElement,
38+
typeOfT: Type,
39+
context: JsonDeserializationContext
40+
): OrderResponse {
41+
val jsonObject = json.asJsonObject
42+
43+
return if (jsonObject.has("error")) {
44+
OrderResponse.Error(
45+
id = jsonObject.get("id").asLong,
46+
error = context.deserialize(jsonObject.get("error"), ErrorResponse::class.java)
47+
)
48+
} else {
49+
OrderResponse.Success(
50+
context.deserialize(jsonObject, OrderDto::class.java)
51+
)
52+
}
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)