Skip to content

Commit

Permalink
fix: paginating fetch multiple users request [WPB-4999]
Browse files Browse the repository at this point in the history
  • Loading branch information
saleniuk committed Oct 20, 2023
1 parent 2d2f00f commit cd165d9
Showing 1 changed file with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,23 +209,35 @@ internal class UserDataSource internal constructor(
wrapApiRequest { userDetailsApi.getUserInfo(userId.toApi()) }
.flatMap { userProfileDTO -> persistUsers(listOf(userProfileDTO)) }

override suspend fun fetchUsersByIds(qualifiedUserIdList: Set<UserId>): Either<CoreFailure, Unit> {
@Suppress("MagicNumber")
override suspend fun fetchUsersByIds(qualifiedUserIdList: Set<UserId>): Either<CoreFailure, Unit> =
if (qualifiedUserIdList.isEmpty()) {
return Either.Right(Unit)
}

return wrapApiRequest {
userDetailsApi.getMultipleUsers(
ListUserRequest.qualifiedIds(qualifiedUserIdList.map { userId -> userId.toApi() })
)
}.flatMap { listUserProfileDTO ->
if (listUserProfileDTO.usersFailed.isNotEmpty()) {
kaliumLogger.d("Handling ${listUserProfileDTO.usersFailed.size} failed users")
persistIncompleteUsers(listUserProfileDTO.usersFailed)
}
persistUsers(listUserProfileDTO.usersFound)
Either.Right(Unit)
} else {
qualifiedUserIdList
.chunked(500)
.foldToEitherWhileRight(ListUsersDTO(emptyList(), emptyList())) { chunk, acc ->
wrapApiRequest {
kaliumLogger.d("Fetching ${chunk.size} users")
userDetailsApi.getMultipleUsers(
ListUserRequest.qualifiedIds(chunk.map { userId -> userId.toApi() })
)
}.map {
kaliumLogger.d("Found ${it.usersFound} users and ${it.usersFailed} failed users")
acc.copy(
usersFound = (acc.usersFound + it.usersFound).distinct(),
usersFailed = (acc.usersFailed + it.usersFailed).distinct(),
)
}
}
.flatMap { listUserProfileDTO ->
if (listUserProfileDTO.usersFailed.isNotEmpty()) {
kaliumLogger.d("Handling ${listUserProfileDTO.usersFailed.size} failed users")
persistIncompleteUsers(listUserProfileDTO.usersFailed)
}
persistUsers(listUserProfileDTO.usersFound)
}
}
}

private suspend fun persistIncompleteUsers(usersFailed: List<NetworkQualifiedId>) = wrapStorageRequest {
usersFailed.map { userMapper.fromFailedUserToEntity(it) }.forEach {
Expand Down

0 comments on commit cd165d9

Please sign in to comment.