@@ -8,7 +8,6 @@ import androidx.room.Transaction
88import kotlinx.coroutines.flow.Flow
99import org.wordpress.android.fluxc.model.LocalOrRemoteId.LocalId
1010import org.wordpress.android.fluxc.network.rest.wpcom.wc.bookings.BookingFilters
11- import org.wordpress.android.fluxc.network.rest.wpcom.wc.bookings.BookingsFilterOption
1211import org.wordpress.android.fluxc.network.rest.wpcom.wc.bookings.BookingsOrderOption
1312import org.wordpress.android.fluxc.persistence.entity.BookingEntity
1413import org.wordpress.android.fluxc.persistence.entity.BookingResourceEntity
@@ -57,18 +56,18 @@ interface BookingsDao {
5756 fun observeBookingsCount (localSiteId : LocalId ): Flow <Long >
5857
5958 @Insert(onConflict = OnConflictStrategy .REPLACE )
60- suspend fun insertOrReplace (entity : BookingEntity ): Long
59+ suspend fun upsert (entity : BookingEntity ): Long
6160
6261 @Insert(onConflict = OnConflictStrategy .REPLACE )
63- suspend fun insertOrReplace (entities : List <BookingEntity >)
62+ suspend fun upsert (entities : List <BookingEntity >)
6463
6564 @Query(" DELETE FROM Bookings WHERE localSiteId = :localSiteId" )
6665 suspend fun deleteAllForSite (localSiteId : LocalId )
6766
6867 @Transaction
6968 suspend fun replaceAllForSite (siteId : LocalId , entities : List <BookingEntity >) {
7069 deleteAllForSite(siteId)
71- insertOrReplace (entities)
70+ upsert (entities)
7271 }
7372
7473 @Suppress(" LongParameterList" )
@@ -78,46 +77,69 @@ interface BookingsDao {
7877 WHERE localSiteId = :localSiteId
7978 AND (:startDateBefore IS NULL OR start <= :startDateBefore)
8079 AND (:startDateAfter IS NULL OR start >= :startDateAfter)
81- AND ((:idsSize = 0) OR id NOT IN (:ids))
80+ AND (:customerId IS NULL OR customerId = :customerId)
81+ AND ((:attendanceStatusesSize = 0) OR attendanceStatus IN (:attendanceStatuses))
82+ AND ((:resourceIdsSize = 0) OR resourceId IN (:resourceIds))
83+ AND ((:productIdsSize = 0) OR productId IN (:productIds))
84+ AND ((:keepIdsSize = 0) OR id NOT IN (:keepIds))
8285 """
8386 )
84- suspend fun deleteForSiteWithDateRangeFilter (
87+ suspend fun deleteStaleBookings (
8588 localSiteId : LocalId ,
8689 startDateBefore : Long? ,
8790 startDateAfter : Long? ,
88- ids : List <Long >,
89- idsSize : Int ,
91+ customerId : Long? ,
92+ resourceIds : List <Long >,
93+ resourceIdsSize : Int ,
94+ attendanceStatuses : List <String >,
95+ attendanceStatusesSize : Int ,
96+ productIds : List <Long >,
97+ productIdsSize : Int ,
98+ keepIds : List <Long >,
99+ keepIdsSize : Int ,
90100 )
91101
92- private suspend fun deleteForSiteWithDateRangeFilter (
102+ private suspend fun deleteStaleBookings (
93103 localSiteId : LocalId ,
94- dateRange : BookingsFilterOption . DateRange ,
95- ids : List <Long >
104+ filters : BookingFilters ,
105+ keepIds : List <Long >
96106 ) {
97- deleteForSiteWithDateRangeFilter(
107+ val resourceIdsKeySet = filters.teamMembers.values.map { it.value }
108+ val attendanceStatusKeySet = filters.attendanceStatuses.values.map { it.key }
109+ val productIds = filters.serviceEvents.values.map { it.productId }
110+
111+ deleteStaleBookings(
98112 localSiteId = localSiteId,
99- startDateBefore = dateRange.before?.epochSecond,
100- startDateAfter = dateRange.after?.epochSecond,
101- ids = ids,
102- idsSize = ids.size,
113+ startDateBefore = filters.dateRange.before?.epochSecond,
114+ startDateAfter = filters.dateRange.after?.epochSecond,
115+ customerId = filters.customer?.customerId,
116+ resourceIds = resourceIdsKeySet.toList(),
117+ resourceIdsSize = resourceIdsKeySet.size,
118+ attendanceStatuses = attendanceStatusKeySet.toList(),
119+ attendanceStatusesSize = attendanceStatusKeySet.size,
120+ productIds = productIds,
121+ productIdsSize = productIds.size,
122+ keepIds = keepIds,
123+ keepIdsSize = keepIds.size
103124 )
104125 }
105126
106127 /* *
107- * Delete Booking entities that are not present in the new list and then insert the new entities
128+ * Delete Booking entities that match the filters but are not present in the new list,
129+ * and then insert the new entities.
108130 */
109131 @Transaction
110132 suspend fun cleanAndUpsertBookings (
111133 localSiteId : LocalId ,
112- dateRange : BookingsFilterOption . DateRange ,
134+ filters : BookingFilters ,
113135 entities : List <BookingEntity >,
114136 ) {
115- deleteForSiteWithDateRangeFilter (
137+ deleteStaleBookings (
116138 localSiteId = localSiteId,
117- dateRange = dateRange ,
118- ids = entities.map { it.id.value },
139+ filters = filters ,
140+ keepIds = entities.map { it.id.value },
119141 )
120- insertOrReplace (entities)
142+ upsert (entities)
121143 }
122144
123145 fun observeBookings (
@@ -161,17 +183,17 @@ interface BookingsDao {
161183 suspend fun getResource (localSiteId : LocalId , resourceId : Long ): BookingResourceEntity ?
162184
163185 @Insert(onConflict = OnConflictStrategy .REPLACE )
164- suspend fun insertOrReplace (resource : BookingResourceEntity ): Long
186+ suspend fun upsert (resource : BookingResourceEntity ): Long
165187
166188 @Insert(onConflict = OnConflictStrategy .REPLACE )
167- suspend fun insertOrReplaceResources (resources : List <BookingResourceEntity >)
189+ suspend fun upsertResources (resources : List <BookingResourceEntity >)
168190
169191 @Query(" DELETE FROM BookingResources WHERE localSiteId = :localSiteId" )
170192 suspend fun deleteAllResourcesForSite (localSiteId : LocalId )
171193
172194 @Transaction
173195 suspend fun replaceAllResourcesForSite (siteId : LocalId , resources : List <BookingResourceEntity >) {
174196 deleteAllResourcesForSite(siteId)
175- insertOrReplaceResources (resources)
197+ upsertResources (resources)
176198 }
177199}
0 commit comments