Skip to content

Commit

Permalink
Create distribution User if needed when querying earliest release date
Browse files Browse the repository at this point in the history
  • Loading branch information
wlara committed Jul 14, 2023
1 parent 6b47f9b commit 1533bd1
Showing 1 changed file with 33 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1132,33 +1132,7 @@ class EvearaDistributionRepositoryImpl(
requireNotNull(song.ownerId) { "Song.ownerId must not be null!" }
val user = userRepository.get(song.ownerId)
requireNotNull(user.id) { "User.id must not be null!" }

// Create the distribution user if they don't yet exist
val getUserResponse = getUser(user)
if (getUserResponse.totalRecords > 0) {
val existingUser = getUserResponse.users.first()
log.info { "Found existing distribution user ${user.email} with id ${existingUser.uuid}" }
if (user.distributionUserId == null) {
user.distributionUserId = existingUser.uuid
userRepository.updateUserData(user.id, user)
} else {
require(user.distributionUserId == existingUser.uuid) { "User.distributionUserId: ${user.distributionUserId} does not match existing distribution user! ${existingUser.uuid}" }
}
// validate that the user's information matches eveara
if (existingUser.email != user.email ||
existingUser.firstName != user.firstName?.ifBlank { "---" } ||
existingUser.lastName != user.lastName?.ifBlank { "---" }
) {
val response = updateUser(user)
log.info { "Updated distribution user ${user.email} with id ${user.distributionUserId}: ${response.message}" }
}
} else {
require(user.distributionUserId == null) { "User.distributionUserId: ${user.distributionUserId} not found in Eveara!" }
val response = addUser(user)
log.info { "Created distribution user ${user.email} with id ${response.uuid}: ${response.message}" }
user.distributionUserId = response.uuid
userRepository.updateUserData(user.id, user)
}
createDistributionUserIfNeeded(user)

val collabs = collabRepository.getAll(
userId = user.id,
Expand Down Expand Up @@ -1439,11 +1413,42 @@ class EvearaDistributionRepositoryImpl(
}

override suspend fun getEarliestReleaseDate(userId: UUID): LocalDate {
val maxDays = getOutlets(userRepository.get(userId)).outlets.maxOfOrNull { it.processDurationDates }
val user = userRepository.get(userId)
createDistributionUserIfNeeded(user)
val maxDays = getOutlets(user).outlets.maxOfOrNull { it.processDurationDates }
?: throw HttpServiceUnavailableException("No Distribution Outlets available - retry later")
return maxDays.toReleaseDate()
}

private suspend fun createDistributionUserIfNeeded(user: User) {
// Create the distribution user if they don't yet exist
val getUserResponse = getUser(user)
if (getUserResponse.totalRecords > 0) {
val existingUser = getUserResponse.users.first()
log.info { "Found existing distribution user ${user.email} with id ${existingUser.uuid}" }
if (user.distributionUserId == null) {
user.distributionUserId = existingUser.uuid
userRepository.updateUserData(user.id!!, user)
} else {
require(user.distributionUserId == existingUser.uuid) { "User.distributionUserId: ${user.distributionUserId} does not match existing distribution user! ${existingUser.uuid}" }
}
// validate that the user's information matches eveara
if (existingUser.email != user.email ||
existingUser.firstName != user.firstName?.ifBlank { "---" } ||
existingUser.lastName != user.lastName?.ifBlank { "---" }
) {
val response = updateUser(user)
log.info { "Updated distribution user ${user.email} with id ${user.distributionUserId}: ${response.message}" }
}
} else {
require(user.distributionUserId == null) { "User.distributionUserId: ${user.distributionUserId} not found in Eveara!" }
val response = addUser(user)
log.info { "Created distribution user ${user.email} with id ${response.uuid}: ${response.message}" }
user.distributionUserId = response.uuid
userRepository.updateUserData(user.id!!, user)
}
}

private fun Long.toReleaseDate() = LocalDate.now().plusDays(this + 1)

@Serializable
Expand Down

0 comments on commit 1533bd1

Please sign in to comment.