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

Commit e26e2ba

Browse files
Merge pull request #3023 from wordpress-mobile/issue/11558-fetch-product-sales-reports
Issue/11558 fetch product sales reports
2 parents 47aa870 + ea81016 commit e26e2ba

File tree

6 files changed

+109
-5
lines changed

6 files changed

+109
-5
lines changed

example/src/main/java/org/wordpress/android/fluxc/example/ui/products/WooProductsFragment.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import org.wordpress.android.fluxc.model.WCProductImageModel
3131
import org.wordpress.android.fluxc.network.rest.wpcom.wc.product.CoreProductStockStatus
3232
import org.wordpress.android.fluxc.store.MediaStore
3333
import org.wordpress.android.fluxc.store.WCAddonsStore
34-
import org.wordpress.android.fluxc.store.WCProductStockReportStore
34+
import org.wordpress.android.fluxc.store.WCProductReportsStore
3535
import org.wordpress.android.fluxc.store.WCProductStore
3636
import org.wordpress.android.fluxc.store.WCProductStore.AddProductTagsPayload
3737
import org.wordpress.android.fluxc.store.WCProductStore.DeleteProductPayload
@@ -64,7 +64,7 @@ class WooProductsFragment : StoreSelectingFragment() {
6464
@Inject lateinit var addonsStore: WCAddonsStore
6565
@Inject internal lateinit var wooCommerceStore: WooCommerceStore
6666
@Inject internal lateinit var mediaStore: MediaStore
67-
@Inject internal lateinit var productStockReportStore: WCProductStockReportStore
67+
@Inject internal lateinit var productStockReportStore: WCProductReportsStore
6868

6969
private var pendingFetchSingleProductShippingClassRemoteId: Long? = null
7070

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
/subscriptions/<id>/
118118

119119
/reports/products
120+
/reports/variations
120121

121122
/options
122123

plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/network/rest/wpcom/wc/reports/ReportsProductApiResponse.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import com.google.gson.annotations.SerializedName
55
data class ReportsProductApiResponse(
66
@SerializedName("product_id")
77
val productId: Long? = null,
8+
@SerializedName("variation_id")
9+
val variationId: Long? = null,
810
@SerializedName("items_sold")
911
val itemsSold: Int? = null,
1012
@SerializedName("net_revenue")
@@ -36,7 +38,7 @@ data class ProductStockItemApiResponse(
3638
@SerializedName("id")
3739
val productId: Long? = null,
3840
@SerializedName("parent_id")
39-
val parentId: Int? = null, // When the product is a variation, this is the parent product ID
41+
val parentId: Long? = null, // When the product is a variation, this is the parent product ID
4042
@SerializedName("name")
4143
val name: String? = null,
4244
@SerializedName("stock_status")

plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/network/rest/wpcom/wc/reports/ReportsRestClient.kt

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.wordpress.android.fluxc.network.rest.wpcom.wc.reports
22

33
import org.wordpress.android.fluxc.generated.endpoint.WOOCOMMERCE
44
import org.wordpress.android.fluxc.model.SiteModel
5+
import org.wordpress.android.fluxc.network.rest.wpapi.WPAPIResponse
56
import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooNetwork
67
import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooPayload
78
import org.wordpress.android.fluxc.utils.toWooPayload
@@ -63,4 +64,70 @@ class ReportsRestClient @Inject constructor(private val wooNetwork: WooNetwork)
6364

6465
return response.toWooPayload()
6566
}
67+
68+
suspend fun fetchProductSalesReport(
69+
site: SiteModel,
70+
startDate: String,
71+
endDate: String,
72+
productIds: List<Long>
73+
): WooPayload<Array<ReportsProductApiResponse>> {
74+
val url = WOOCOMMERCE.reports.products.pathV4Analytics
75+
val response = fetchSales(
76+
endDate = endDate,
77+
startDate = startDate,
78+
productIds = productIds,
79+
site = site,
80+
url = url,
81+
quantity = productIds.size
82+
)
83+
return response.toWooPayload()
84+
}
85+
86+
suspend fun fetchProductVariationsSalesReport(
87+
site: SiteModel,
88+
startDate: String,
89+
endDate: String,
90+
variationIds: List<Long>
91+
): WooPayload<Array<ReportsProductApiResponse>> {
92+
val url = WOOCOMMERCE.reports.variations.pathV4Analytics
93+
val response = fetchSales(
94+
endDate = endDate,
95+
startDate = startDate,
96+
variationIds = variationIds,
97+
site = site,
98+
url = url,
99+
quantity = variationIds.size
100+
)
101+
return response.toWooPayload()
102+
}
103+
104+
private suspend fun fetchSales(
105+
endDate: String,
106+
startDate: String,
107+
productIds: List<Long> = emptyList(),
108+
variationIds: List<Long> = emptyList(),
109+
site: SiteModel,
110+
url: String,
111+
quantity: Int
112+
): WPAPIResponse<Array<ReportsProductApiResponse>> {
113+
val parameters = mapOf(
114+
"before" to endDate,
115+
"after" to startDate,
116+
"products" to productIds.joinToString(","),
117+
"variations" to variationIds.joinToString(","),
118+
"extended_info" to "true",
119+
"orderby" to "items_sold",
120+
"order" to "desc",
121+
"page" to "1",
122+
"per_page" to quantity.toString()
123+
)
124+
125+
val response = wooNetwork.executeGetGsonRequest(
126+
site = site,
127+
path = url,
128+
clazz = Array<ReportsProductApiResponse>::class.java,
129+
params = parameters
130+
)
131+
return response
132+
}
66133
}

plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/store/WCLeaderboardsStore.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ class WCLeaderboardsStore @Inject constructor(
156156
val invalidatedTopPerformers =
157157
topPerformersDao.getTopPerformerProductsForSite(site.localId())
158158
.map { it.copy(millisSinceLastUpdated = 0) }
159-
topPerformersDao.updateTopPerformerProductsForSite(site.localId(), invalidatedTopPerformers)
159+
topPerformersDao.updateTopPerformerProductsForSite(
160+
site.localId(),
161+
invalidatedTopPerformers
162+
)
160163
}
161164
}
162165
}

plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/store/WCProductStockReportStore.kt renamed to plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/store/WCProductReportsStore.kt

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import org.wordpress.android.fluxc.model.SiteModel
44
import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooResult
55
import org.wordpress.android.fluxc.network.rest.wpcom.wc.product.CoreProductStockStatus
66
import org.wordpress.android.fluxc.network.rest.wpcom.wc.reports.ProductStockItemApiResponse
7+
import org.wordpress.android.fluxc.network.rest.wpcom.wc.reports.ReportsProductApiResponse
78
import org.wordpress.android.fluxc.network.rest.wpcom.wc.reports.ReportsRestClient
89
import org.wordpress.android.fluxc.tools.CoroutineEngine
910
import org.wordpress.android.util.AppLog.T.API
1011
import javax.inject.Inject
1112
import javax.inject.Singleton
1213

1314
@Singleton
14-
class WCProductStockReportStore @Inject constructor(
15+
class WCProductReportsStore @Inject constructor(
1516
private val reportsRestClient: ReportsRestClient,
1617
private val coroutineEngine: CoroutineEngine,
1718
) {
@@ -34,6 +35,36 @@ class WCProductStockReportStore @Inject constructor(
3435
quantity = quantity
3536
)
3637
}.asWooResult()
38+
39+
suspend fun fetchProductSalesReport(
40+
site: SiteModel,
41+
startDate: String,
42+
endDate: String,
43+
productIds: List<Long>
44+
): WooResult<Array<ReportsProductApiResponse>> =
45+
coroutineEngine.withDefaultContext(API, this, "fetchProductSalesReport") {
46+
reportsRestClient.fetchProductSalesReport(
47+
site,
48+
startDate,
49+
endDate,
50+
productIds
51+
)
52+
}.asWooResult()
53+
54+
suspend fun fetchProductVariationsSalesReport(
55+
site: SiteModel,
56+
startDate: String,
57+
endDate: String,
58+
productVariationIds: List<Long>
59+
): WooResult<Array<ReportsProductApiResponse>> =
60+
coroutineEngine.withDefaultContext(API, this, "fetchProductVariationsSalesReport") {
61+
reportsRestClient.fetchProductVariationsSalesReport(
62+
site,
63+
startDate,
64+
endDate,
65+
productVariationIds
66+
)
67+
}.asWooResult()
3768
}
3869

3970
typealias ProductStockItems = Array<ProductStockItemApiResponse>

0 commit comments

Comments
 (0)