Skip to content

Commit a5b1aa3

Browse files
Don't return error when refreshing the order fails
1 parent 04fb7aa commit a5b1aa3

File tree

2 files changed

+55
-45
lines changed
  • libs/fluxc-plugin/src

2 files changed

+55
-45
lines changed

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -164,18 +164,17 @@ class BookingsStore @Inject internal constructor(
164164
response.result != null -> {
165165
val bookingDto = response.result
166166

167-
val updatedBookingEntityResult = if (refreshOrder) {
167+
val updatedBookingEntity = if (refreshOrder) {
168168
getBookingEntityWithRefreshedOrder(site, bookingDto)
169+
?: getBookingEntityWithLocalOrder(site, bookingDto) // Fallback to local
169170
} else {
170171
getBookingEntityWithLocalOrder(site, bookingDto)
171172
}
172-
if (updatedBookingEntityResult.isError) {
173-
return@withDefaultContext WooResult(updatedBookingEntityResult.error)
173+
if (updatedBookingEntity == null) {
174+
return@withDefaultContext WooResult(WooError(GENERIC_ERROR, UNKNOWN))
174175
} else {
175-
updatedBookingEntityResult.model?.let {
176-
bookingsDao.insertOrReplace(it)
177-
}
178-
return@withDefaultContext WooResult(updatedBookingEntityResult.model)
176+
bookingsDao.insertOrReplace(updatedBookingEntity)
177+
return@withDefaultContext WooResult(updatedBookingEntity)
179178
}
180179
}
181180

@@ -203,31 +202,31 @@ class BookingsStore @Inject internal constructor(
203202
private suspend fun getBookingEntityWithRefreshedOrder(
204203
site: SiteModel,
205204
bookingDto: BookingDto
206-
): WooResult<BookingEntity> {
205+
): BookingEntity? {
207206
val orderResult = orderStore.fetchSingleOrderSync(site, bookingDto.orderId)
208207
if (orderResult.isError) {
209-
return WooResult(orderResult.error)
208+
return null
210209
} else {
211210
val entity = with(bookingDtoMapper) {
212211
bookingDto.toEntity(
213212
localSiteId = site.localId(),
214213
orderEntity = orderResult.model,
215214
)
216215
}
217-
return WooResult(entity)
216+
return entity
218217
}
219218
}
220219

221220
private suspend fun getBookingEntityWithLocalOrder(
222221
site: SiteModel,
223222
bookingDto: BookingDto
224-
): WooResult<BookingEntity> {
223+
): BookingEntity? {
225224
val storedBooking = bookingsDao.getBooking(
226225
localSiteId = site.localId(),
227226
bookingId = bookingDto.id,
228227
)
229228
if (storedBooking == null) {
230-
return WooResult(WooError(GENERIC_ERROR, UNKNOWN))
229+
return null
231230
}
232231
val entity = with(bookingDtoMapper) {
233232
bookingDto.toEntity(
@@ -238,6 +237,6 @@ class BookingsStore @Inject internal constructor(
238237
order = storedBooking.order,
239238
)
240239
}
241-
return WooResult(entity)
240+
return entity
242241
}
243242
}

libs/fluxc-plugin/src/test/java/org/wordpress/android/fluxc/network/rest/wpcom/wc/bookings/BookingsStoreTest.kt

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import org.wordpress.android.fluxc.model.SiteModel
1616
import org.wordpress.android.fluxc.network.BaseRequest.GenericErrorType.UNKNOWN
1717
import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooError
1818
import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooErrorType.GENERIC_ERROR
19+
import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooPayload
1920
import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooResult
2021
import org.wordpress.android.fluxc.persistence.dao.BookingsDao
2122
import org.wordpress.android.fluxc.persistence.entity.BookingEntity
@@ -58,7 +59,7 @@ class BookingsStoreTest {
5859
val storedBooking = sampleBookingEntity(order = BookingOrderInfo(productInfo = null))
5960
whenever(bookingsDao.getBooking(TEST_LOCAL_SITE_ID, dto.id)).thenReturn(storedBooking)
6061
whenever(bookingsRestClient.updateBooking(site, dto.id, BookingUpdatePayload(note = "n")))
61-
.thenReturn(org.wordpress.android.fluxc.network.rest.wpcom.wc.WooPayload(dto))
62+
.thenReturn(WooPayload(dto))
6263
whenever(bookingsDao.insertOrReplace(any<BookingEntity>())).thenReturn(1L)
6364

6465
// when
@@ -85,7 +86,7 @@ class BookingsStoreTest {
8586
val fetchedOrder = OrderEntity(orderId = dto.orderId, localSiteId = TEST_LOCAL_SITE_ID)
8687
whenever(orderStore.fetchSingleOrderSync(site, dto.orderId)).thenReturn(WooResult(fetchedOrder))
8788
whenever(bookingsRestClient.updateBooking(site, dto.id, BookingUpdatePayload(status = Status.Confirmed)))
88-
.thenReturn(org.wordpress.android.fluxc.network.rest.wpcom.wc.WooPayload(dto))
89+
.thenReturn(WooPayload(dto))
8990
whenever(bookingsDao.insertOrReplace(any<BookingEntity>())).thenReturn(1L)
9091

9192
// when
@@ -101,6 +102,7 @@ class BookingsStoreTest {
101102
val expected = with(bookingDtoMapper) { dto.toEntity(TEST_LOCAL_SITE_ID, fetchedOrder) }
102103
assertThat(result.model).isEqualTo(expected)
103104
verify(bookingsDao).insertOrReplace(expected)
105+
verify(bookingsDao, never()).getBooking(TEST_LOCAL_SITE_ID, dto.id)
104106
}
105107

106108
@Test
@@ -109,7 +111,7 @@ class BookingsStoreTest {
109111
val site = SiteModel().apply { id = TEST_LOCAL_SITE_ID.value }
110112
val error = WooError(GENERIC_ERROR, UNKNOWN)
111113
whenever(bookingsRestClient.updateBooking(site, TEST_BOOKING_ID, BookingUpdatePayload(note = "n")))
112-
.thenReturn(org.wordpress.android.fluxc.network.rest.wpcom.wc.WooPayload(error))
114+
.thenReturn(WooPayload(error))
113115

114116
// when
115117
val result = sut.updateBooking(
@@ -125,38 +127,47 @@ class BookingsStoreTest {
125127
}
126128

127129
@Test
128-
fun `given order refresh fails, when updateBooking with refreshOrder true, then returns error and does not insert`(): Unit = runBlocking {
129-
// given
130-
val site = SiteModel().apply { id = TEST_LOCAL_SITE_ID.value }
131-
val dto = sampleBookingDto()
132-
whenever(
133-
bookingsRestClient.updateBooking(
134-
site,
135-
dto.id,
136-
BookingUpdatePayload(attendanceStatus = AttendanceStatus.CheckedIn)
130+
fun `given order refresh fails, when updateBooking with refreshOrder true, then fallbacks to local entity and inserts it`(): Unit =
131+
runBlocking {
132+
// given
133+
val site = SiteModel().apply { id = TEST_LOCAL_SITE_ID.value }
134+
val dto = sampleBookingDto()
135+
val storedBooking = sampleBookingEntity(
136+
order = BookingOrderInfo(
137+
productInfo = null,
138+
status = "paid",
139+
)
137140
)
138-
)
139-
.thenReturn(org.wordpress.android.fluxc.network.rest.wpcom.wc.WooPayload(dto))
140-
141-
whenever(
142-
orderStore.fetchSingleOrderSync(
143-
site,
144-
dto.orderId
141+
whenever(bookingsDao.getBooking(TEST_LOCAL_SITE_ID, dto.id)).thenReturn(storedBooking)
142+
whenever(
143+
bookingsRestClient.updateBooking(
144+
site,
145+
dto.id,
146+
BookingUpdatePayload(attendanceStatus = AttendanceStatus.CheckedIn)
147+
)
148+
).thenReturn(WooPayload(dto))
149+
150+
whenever(
151+
orderStore.fetchSingleOrderSync(
152+
site,
153+
dto.orderId
154+
)
155+
).thenReturn(WooResult(error = WooError(GENERIC_ERROR, UNKNOWN)))
156+
157+
// when
158+
val result = sut.updateBooking(
159+
site = site,
160+
bookingId = dto.id,
161+
bookingUpdatePayload = BookingUpdatePayload(attendanceStatus = AttendanceStatus.CheckedIn),
162+
refreshOrder = true
145163
)
146-
).thenReturn(WooResult(error = WooError(GENERIC_ERROR, UNKNOWN)))
147-
148-
// when
149-
val result = sut.updateBooking(
150-
site = site,
151-
bookingId = dto.id,
152-
bookingUpdatePayload = BookingUpdatePayload(attendanceStatus = AttendanceStatus.CheckedIn),
153-
refreshOrder = true
154-
)
155164

156-
// then
157-
assertThat(result.isError).isTrue()
158-
verify(bookingsDao, never()).insertOrReplace(any<BookingEntity>())
159-
}
165+
// then
166+
assertThat(result.isError).isFalse
167+
verify(bookingsDao).getBooking(TEST_LOCAL_SITE_ID, dto.id)
168+
// The store preserves the stored order on the mapped entity
169+
verify(bookingsDao).insertOrReplace(argThat<BookingEntity> { this.order == storedBooking.order })
170+
}
160171

161172
private fun sampleBookingDto(): BookingDto = BookingDto(
162173
id = TEST_BOOKING_ID,

0 commit comments

Comments
 (0)