Skip to content

Commit 2be1ec9

Browse files
authored
Merge pull request #14603 from woocommerce/issue/woomob-1312-woo-poslocal-catalog-localsiteid-is-always-0
[WOOMOB-1312] Fix localSiteId mapping for WooPosProductEntity
2 parents 8e63181 + eedfeae commit 2be1ec9

File tree

14 files changed

+64
-62
lines changed

14 files changed

+64
-62
lines changed

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/common/data/WooPosVariationMapper.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import com.woocommerce.android.ui.woopos.common.data.models.WooPosProductModel
99
import com.woocommerce.android.util.WooLog
1010
import com.woocommerce.android.viewmodel.ResourceProvider
1111
import org.wordpress.android.fluxc.model.WCProductVariationModel
12-
import org.wordpress.android.fluxc.persistence.entity.pos.WCPosVariationModel
12+
import org.wordpress.android.fluxc.persistence.entity.pos.WooPosVariationEntity
1313
import javax.inject.Inject
1414
import javax.inject.Singleton
1515

@@ -74,7 +74,7 @@ class WooPosVariationMapper @Inject constructor(
7474
}
7575

7676
@Suppress("SwallowedException")
77-
fun fromWCPosVariationModel(model: WCPosVariationModel): WooPosVariation {
77+
fun fromWooPosVariationEntity(model: WooPosVariationEntity): WooPosVariation {
7878
val attributesList = parseAttributesJson(model.attributesJson)
7979

8080
return WooPosVariation(
@@ -162,8 +162,8 @@ fun ProductVariation.toWooPosVariation(mapper: WooPosVariationMapper): WooPosVar
162162
fun WCProductVariationModel.toWooPosVariation(mapper: WooPosVariationMapper): WooPosVariation =
163163
mapper.fromWCProductVariationModel(this)
164164

165-
fun WCPosVariationModel.toWooPosVariation(mapper: WooPosVariationMapper): WooPosVariation =
166-
mapper.fromWCPosVariationModel(this)
165+
fun WooPosVariationEntity.toWooPosVariation(mapper: WooPosVariationMapper): WooPosVariation =
166+
mapper.fromWooPosVariationEntity(this)
167167

168168
fun WooPosVariation.getNameForPOS(
169169
mapper: WooPosVariationMapper,

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/common/data/models/WooPosProductModelMapper.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import com.google.gson.JsonSyntaxException
55
import com.google.gson.reflect.TypeToken
66
import com.woocommerce.android.ui.woopos.common.util.WooPosLogWrapper
77
import dagger.Reusable
8-
import org.wordpress.android.fluxc.persistence.entity.pos.WCPosProductEntity
8+
import org.wordpress.android.fluxc.persistence.entity.pos.WooPosProductEntity
99
import java.math.BigDecimal
1010
import javax.inject.Inject
1111

@@ -18,7 +18,7 @@ import javax.inject.Inject
1818
@Reusable
1919
class WooPosProductModelMapper @Inject constructor(val logger: WooPosLogWrapper) {
2020
private val gson = Gson()
21-
fun fromEntity(entity: WCPosProductEntity): WooPosProductModel {
21+
fun fromEntity(entity: WooPosProductEntity): WooPosProductModel {
2222
return WooPosProductModel(
2323
remoteId = entity.remoteId.value,
2424
parentId = entity.parentId,
@@ -45,7 +45,7 @@ class WooPosProductModelMapper @Inject constructor(val logger: WooPosLogWrapper)
4545
)
4646
}
4747

48-
fun fromEntities(entities: List<WCPosProductEntity>): List<WooPosProductModel> {
48+
fun fromEntities(entities: List<WooPosProductEntity>): List<WooPosProductModel> {
4949
return entities.map { fromEntity(it) }
5050
}
5151

WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/common/data/models/WooPosProductModelMapperTest.kt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import org.assertj.core.api.Assertions.assertThat
77
import org.junit.Test
88
import org.mockito.Mockito.mock
99
import org.wordpress.android.fluxc.model.LocalOrRemoteId
10-
import org.wordpress.android.fluxc.persistence.entity.pos.WCPosProductEntity
10+
import org.wordpress.android.fluxc.persistence.entity.pos.WooPosProductEntity
1111
import java.math.BigDecimal
1212

1313
@ExperimentalCoroutinesApi
@@ -44,7 +44,7 @@ class WooPosProductModelMapperTest : BaseUnitTest() {
4444
@Test
4545
fun `given entity with pricing information, when mapping to model, then pricing is handled correctly`() {
4646
// Regular pricing only
47-
var entity = WCPosProductEntity(
47+
var entity = WooPosProductEntity(
4848
remoteId = LocalOrRemoteId.RemoteId(123),
4949
regularPrice = "25.99",
5050
onSale = false
@@ -55,7 +55,7 @@ class WooPosProductModelMapperTest : BaseUnitTest() {
5555
assertThat(regularPricing.price).isEqualTo(BigDecimal("25.99"))
5656

5757
// Sale pricing
58-
entity = WCPosProductEntity(
58+
entity = WooPosProductEntity(
5959
remoteId = LocalOrRemoteId.RemoteId(123),
6060
regularPrice = "29.99",
6161
salePrice = "19.99",
@@ -68,7 +68,7 @@ class WooPosProductModelMapperTest : BaseUnitTest() {
6868
assertThat(salePricing.salePrice).isEqualTo(BigDecimal("19.99"))
6969

7070
// No pricing
71-
entity = WCPosProductEntity(
71+
entity = WooPosProductEntity(
7272
remoteId = LocalOrRemoteId.RemoteId(123),
7373
price = "",
7474
regularPrice = "",
@@ -82,7 +82,7 @@ class WooPosProductModelMapperTest : BaseUnitTest() {
8282

8383
@Test
8484
fun `given entity with invalid price format, when mapping to model, then pricing defaults to NoPricing`() {
85-
val entity = WCPosProductEntity(
85+
val entity = WooPosProductEntity(
8686
remoteId = LocalOrRemoteId.RemoteId(123),
8787
price = "invalid",
8888
regularPrice = "not-a-number",
@@ -99,7 +99,7 @@ class WooPosProductModelMapperTest : BaseUnitTest() {
9999
@Test
100100
fun `given entity with JSON images, when mapping to model, then images are parsed correctly`() {
101101
val imagesJson = """[{"id": 1, "src": "https://example.com/image.jpg", "name": "Image 1", "alt": "Alt text"}]"""
102-
val entity = WCPosProductEntity(
102+
val entity = WooPosProductEntity(
103103
remoteId = LocalOrRemoteId.RemoteId(123),
104104
images = imagesJson
105105
)
@@ -117,7 +117,7 @@ class WooPosProductModelMapperTest : BaseUnitTest() {
117117

118118
@Test
119119
fun `given entity with malformed JSON, when mapping to model, then returns empty collections gracefully`() {
120-
val entity = WCPosProductEntity(
120+
val entity = WooPosProductEntity(
121121
remoteId = LocalOrRemoteId.RemoteId(123),
122122
images = "malformed json",
123123
attributes = "invalid json",
@@ -135,7 +135,7 @@ class WooPosProductModelMapperTest : BaseUnitTest() {
135135

136136
@Test
137137
fun `given entity with empty JSON collections, when mapping to model, then returns empty lists`() {
138-
val entity = WCPosProductEntity(
138+
val entity = WooPosProductEntity(
139139
remoteId = LocalOrRemoteId.RemoteId(123),
140140
images = "",
141141
attributes = "",
@@ -155,7 +155,7 @@ class WooPosProductModelMapperTest : BaseUnitTest() {
155155
fun `given entity with valid JSON attributes, when mapping to model, then attributes are parsed correctly`() {
156156
val attributesJson =
157157
"""[{"id": 1, "name": "Color", "options": ["Red", "Blue"], "visible": true, "variation": false}]"""
158-
val entity = WCPosProductEntity(
158+
val entity = WooPosProductEntity(
159159
remoteId = LocalOrRemoteId.RemoteId(123),
160160
attributes = attributesJson
161161
)
@@ -174,7 +174,7 @@ class WooPosProductModelMapperTest : BaseUnitTest() {
174174

175175
@Test
176176
fun `given product model with JSON collections, when accessing multiple times, then returns same parsed data`() {
177-
val entity = WCPosProductEntity(
177+
val entity = WooPosProductEntity(
178178
remoteId = LocalOrRemoteId.RemoteId(123),
179179
images = """[{"id": 1, "src": "test.jpg"}]""",
180180
attributes = """[{"id": 2, "name": "Size"}]"""
@@ -232,7 +232,7 @@ class WooPosProductModelMapperTest : BaseUnitTest() {
232232
)
233233

234234
testCases.forEach { (typeString, expectedType) ->
235-
val entity = WCPosProductEntity(
235+
val entity = WooPosProductEntity(
236236
remoteId = LocalOrRemoteId.RemoteId(123),
237237
type = typeString
238238
)
@@ -255,7 +255,7 @@ class WooPosProductModelMapperTest : BaseUnitTest() {
255255
)
256256

257257
testCases.forEach { (statusString, expectedStatus) ->
258-
val entity = WCPosProductEntity(
258+
val entity = WooPosProductEntity(
259259
remoteId = LocalOrRemoteId.RemoteId(123),
260260
status = statusString
261261
)
@@ -317,7 +317,7 @@ class WooPosProductModelMapperTest : BaseUnitTest() {
317317
assertThat(mapper.mapProductStatus("unknown")).isEqualTo(WooPosProductModel.WooPosProductStatus.UNKNOWN)
318318
}
319319

320-
private fun createCompleteEntity() = WCPosProductEntity(
320+
private fun createCompleteEntity() = WooPosProductEntity(
321321
localSiteId = LocalOrRemoteId.LocalId(1),
322322
remoteId = LocalOrRemoteId.RemoteId(123),
323323
name = "Test Product",
@@ -342,7 +342,7 @@ class WooPosProductModelMapperTest : BaseUnitTest() {
342342
dateModified = "2023-01-01T00:00:00"
343343
)
344344

345-
private fun createMinimalEntity(id: Long) = WCPosProductEntity(
345+
private fun createMinimalEntity(id: Long) = WooPosProductEntity(
346346
remoteId = LocalOrRemoteId.RemoteId(id)
347347
)
348348
}

libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/network/rest/wpcom/wc/product/ProductApiResponse.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ typealias ProductDto = ProductApiResponse
1111
@Suppress("ConstructorParameterNaming")
1212
data class ProductApiResponse(
1313
val id: Long? = null,
14-
val localSiteId: Int = 0,
1514
val name: String? = null,
1615
val slug: String? = null,
1716
val permalink: String? = null,

libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/network/rest/wpcom/wc/product/pos/WooPosProductResponseToEntityMapper.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package org.wordpress.android.fluxc.network.rest.wpcom.wc.product.pos
33
import org.wordpress.android.fluxc.model.LocalOrRemoteId
44
import org.wordpress.android.fluxc.model.pos.WooPosVariationApiResponse
55
import org.wordpress.android.fluxc.network.rest.wpcom.wc.product.ProductApiResponse
6-
import org.wordpress.android.fluxc.persistence.entity.pos.WCPosProductEntity
7-
import org.wordpress.android.fluxc.persistence.entity.pos.WCPosVariationModel
6+
import org.wordpress.android.fluxc.persistence.entity.pos.WooPosProductEntity
7+
import org.wordpress.android.fluxc.persistence.entity.pos.WooPosVariationEntity
88

99
@Suppress("CyclomaticComplexMethod")
10-
fun ProductApiResponse.mapToPOSEntity(): WCPosProductEntity =
11-
WCPosProductEntity(
12-
localSiteId = LocalOrRemoteId.LocalId(this.localSiteId),
10+
fun ProductApiResponse.mapToWooPOSEntity(localSiteId: LocalOrRemoteId.LocalId): WooPosProductEntity =
11+
WooPosProductEntity(
12+
localSiteId = localSiteId,
1313
remoteId = LocalOrRemoteId.RemoteId(this.id ?: 0),
1414
name = this.name ?: "",
1515
dateModified = this.date_modified ?: "",
@@ -40,8 +40,8 @@ fun ProductApiResponse.mapToPOSEntity(): WCPosProductEntity =
4040

4141
fun WooPosVariationApiResponse.mapToPosVariationModel(
4242
localSiteId: LocalOrRemoteId.LocalId
43-
): WCPosVariationModel {
44-
return WCPosVariationModel(
43+
): WooPosVariationEntity {
44+
return WooPosVariationEntity(
4545
localSiteId = localSiteId,
4646
remoteProductId = LocalOrRemoteId.RemoteId(this.productId),
4747
remoteVariationId = LocalOrRemoteId.RemoteId(this.id),

libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/persistence/WCAndroidDatabase.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ import org.wordpress.android.fluxc.persistence.entity.WooPaymentsManualDepositEn
8888
import org.wordpress.android.fluxc.persistence.entity.WooShippingLabelEntity
8989
import org.wordpress.android.fluxc.persistence.entity.WooShippingPackagesEntity
9090
import org.wordpress.android.fluxc.persistence.entity.WooShippingShipmentEntity
91-
import org.wordpress.android.fluxc.persistence.entity.pos.WCPosProductEntity
92-
import org.wordpress.android.fluxc.persistence.entity.pos.WCPosVariationModel
91+
import org.wordpress.android.fluxc.persistence.entity.pos.WooPosProductEntity
92+
import org.wordpress.android.fluxc.persistence.entity.pos.WooPosVariationEntity
9393
import org.wordpress.android.fluxc.persistence.migrations.AutoMigration13to14
9494
import org.wordpress.android.fluxc.persistence.migrations.AutoMigration14to15
9595
import org.wordpress.android.fluxc.persistence.migrations.AutoMigration16to17
@@ -145,8 +145,8 @@ const val WC_DATABASE_VERSION = 64
145145
ShippingMethodEntity::class,
146146
CustomerFromAnalyticsEntity::class,
147147
WCProductModel::class,
148-
WCPosProductEntity::class,
149-
WCPosVariationModel::class,
148+
WooPosProductEntity::class,
149+
WooPosVariationEntity::class,
150150
WCProductCategoryModel::class,
151151
WCProductVariationModel::class,
152152
WCProductTagModel::class,

libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/persistence/dao/pos/WooPosProductsDao.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ import androidx.room.Upsert
66
import kotlinx.coroutines.flow.Flow
77
import org.wordpress.android.fluxc.model.LocalOrRemoteId.LocalId
88
import org.wordpress.android.fluxc.model.LocalOrRemoteId.RemoteId
9-
import org.wordpress.android.fluxc.persistence.entity.pos.WCPosProductEntity
9+
import org.wordpress.android.fluxc.persistence.entity.pos.WooPosProductEntity
1010

1111
@Dao
1212
abstract class WooPosProductsDao {
1313
@Query("SELECT * FROM PosProductEntity WHERE localSiteId = :localSiteId")
14-
abstract fun observeAllProducts(localSiteId: LocalId): Flow<List<WCPosProductEntity>>
14+
abstract fun observeAllProducts(localSiteId: LocalId): Flow<List<WooPosProductEntity>>
1515

1616
@Query("SELECT * FROM PosProductEntity WHERE localSiteId = :localSiteId AND remoteId = :remoteId")
17-
abstract suspend fun getProduct(localSiteId: LocalId, remoteId: RemoteId): WCPosProductEntity?
17+
abstract suspend fun getProduct(localSiteId: LocalId, remoteId: RemoteId): WooPosProductEntity?
1818

1919
@Upsert
20-
abstract suspend fun upsertProducts(products: List<WCPosProductEntity>)
20+
abstract suspend fun upsertProducts(products: List<WooPosProductEntity>)
2121

2222
@Query("DELETE FROM PosProductEntity WHERE localSiteId = :localSiteId AND remoteId = :remoteId")
2323
abstract suspend fun deleteProduct(localSiteId: LocalId, remoteId: RemoteId)

libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/persistence/dao/pos/WooPosVariationsDao.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,29 @@ import androidx.room.Upsert
66
import kotlinx.coroutines.flow.Flow
77
import org.wordpress.android.fluxc.model.LocalOrRemoteId.LocalId
88
import org.wordpress.android.fluxc.model.LocalOrRemoteId.RemoteId
9-
import org.wordpress.android.fluxc.persistence.entity.pos.WCPosVariationModel
9+
import org.wordpress.android.fluxc.persistence.entity.pos.WooPosVariationEntity
1010

1111
@Dao
1212
abstract class WooPosVariationsDao {
1313

1414
@Query("SELECT * FROM PosVariationEntity WHERE localSiteId = :localSiteId AND remoteProductId = :productId ORDER BY variationName ASC")
15-
abstract fun observeVariationsForProduct(localSiteId: LocalId, productId: RemoteId): Flow<List<WCPosVariationModel>>
15+
abstract fun observeVariationsForProduct(
16+
localSiteId: LocalId,
17+
productId: RemoteId
18+
): Flow<List<WooPosVariationEntity>>
1619

1720
@Query("SELECT * FROM PosVariationEntity WHERE localSiteId = :localSiteId AND remoteProductId = :productId AND remoteVariationId = :variationId")
1821
abstract suspend fun getVariation(
1922
localSiteId: LocalId,
2023
productId: RemoteId,
2124
variationId: RemoteId
22-
): WCPosVariationModel?
25+
): WooPosVariationEntity?
2326

2427
@Upsert
25-
abstract suspend fun upsertVariations(variations: List<WCPosVariationModel>)
28+
abstract suspend fun upsertVariations(variations: List<WooPosVariationEntity>)
2629

2730
@Upsert
28-
abstract suspend fun upsertVariation(variation: WCPosVariationModel)
31+
abstract suspend fun upsertVariation(variation: WooPosVariationEntity)
2932

3033
@Query("DELETE FROM PosVariationEntity WHERE localSiteId = :localSiteId AND remoteProductId = :productId AND remoteVariationId = :variationId")
3134
abstract suspend fun deleteVariation(localSiteId: LocalId, productId: RemoteId, variationId: RemoteId)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import org.wordpress.android.fluxc.model.LocalOrRemoteId.RemoteId
1111
tableName = "PosProductEntity",
1212
primaryKeys = ["localSiteId", "remoteId"],
1313
)
14-
data class WCPosProductEntity(
14+
data class WooPosProductEntity(
1515
val localSiteId: LocalId = LocalId(0),
1616
val remoteId: RemoteId = RemoteId(0),
1717
val name: String = "",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import org.wordpress.android.fluxc.model.LocalOrRemoteId.RemoteId
1111
tableName = "PosVariationEntity",
1212
primaryKeys = ["localSiteId", "remoteProductId", "remoteVariationId"],
1313
)
14-
data class WCPosVariationModel(
14+
data class WooPosVariationEntity(
1515
val localSiteId: LocalId = LocalId(0),
1616
val remoteProductId: RemoteId = RemoteId(0),
1717
val remoteVariationId: RemoteId = RemoteId(0),

0 commit comments

Comments
 (0)