Skip to content

Commit

Permalink
CU-863h6d948 | API for get earliest possible release date for Outlets
Browse files Browse the repository at this point in the history
  • Loading branch information
wlara committed Jul 14, 2023
1 parent 6fa542f commit 6b47f9b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import io.newm.server.features.distribution.model.ValidateAlbumResponse
import io.newm.server.features.song.model.Song
import io.newm.server.features.user.model.User
import java.io.File
import java.time.LocalDate
import java.util.UUID

/**
* Higher level api for working with a music distribution service
Expand Down Expand Up @@ -105,4 +107,6 @@ interface DistributionRepository {
suspend fun distributionOutletReleaseStatus(user: User, releaseId: Long): DistributionOutletReleaseStatusResponse

suspend fun distributeSong(song: Song)

suspend fun getEarliestReleaseDate(userId: UUID): LocalDate
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import io.ktor.server.response.respond
import io.ktor.server.routing.Routing
import io.ktor.server.routing.route
import io.newm.server.auth.jwt.AUTH_JWT
import io.newm.server.features.distribution.model.GetDateResponse
import io.newm.server.features.model.CountResponse
import io.newm.server.ktx.myUserId
import io.newm.shared.koin.inject
import io.newm.shared.ktx.get

private const val ROLES_PATH = "v1/distribution/roles"
private const val GENRES_PATH = "v1/distribution/genres"
private const val COUNTRIES_PATH = "v1/distribution/countries"
private const val LANGUAGES_PATH = "v1/distribution/languages"
private const val EARLIEST_RELEASE_DATE_PATH = "v1/distribution/earliest-release-date"

@Suppress("unused")
fun Routing.createDistributionRoutes() {
Expand Down Expand Up @@ -51,5 +54,8 @@ fun Routing.createDistributionRoutes() {
respond(CountResponse(distributionRepository.getLanguages().languages.size.toLong()))
}
}
get(EARLIEST_RELEASE_DATE_PATH) {
respond(GetDateResponse(distributionRepository.getEarliestReleaseDate(myUserId)))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ import io.newm.server.ktx.getSecureConfigString
import io.newm.server.ktx.toAudioContentType
import io.newm.server.ktx.toBucketAndKey
import io.newm.server.logging.logRequestJson
import io.newm.shared.exception.HttpServiceUnavailableException
import io.newm.shared.exception.HttpStatusException.Companion.toException
import io.newm.shared.koin.inject
import io.newm.shared.ktx.getConfigString
Expand All @@ -114,6 +115,7 @@ import java.time.Instant
import java.time.LocalDate
import java.time.temporal.ChronoUnit
import java.util.Date
import java.util.UUID
import kotlin.time.Duration.Companion.minutes

class EvearaDistributionRepositoryImpl(
Expand Down Expand Up @@ -1057,7 +1059,7 @@ class EvearaDistributionRepositoryImpl(
outletsDetails = getOutlets(user).outlets.map { evearaOutlet ->
OutletsDetail(
storeId = evearaOutlet.storeId,
releaseStartDate = LocalDate.now().plusDays(evearaOutlet.processDurationDates + 1)
releaseStartDate = evearaOutlet.processDurationDates.toReleaseDate()
)
}
).logRequestJson(log)
Expand Down Expand Up @@ -1436,6 +1438,14 @@ class EvearaDistributionRepositoryImpl(
log.info { "Distributed release ${songToRelease.title} with id ${songToRelease.distributionReleaseId} to future outlets: ${distributeFutureReleaseResponse.message}" }
}

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

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

@Serializable
private data class EvearaApiGetTokenRequest(
@SerialName("grant_type")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.newm.server.features.distribution.model

import io.newm.shared.serialization.LocalDateSerializer
import kotlinx.serialization.Serializable
import java.time.LocalDate

@Serializable
data class GetDateResponse(
@Serializable(with = LocalDateSerializer::class)
val date: LocalDate
)
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ class HttpUnprocessableEntityException(message: String) :

class HttpConflictException(message: String) : HttpStatusException(HttpStatusCode.Conflict, message)

class HttpServiceUnavailableException(message: String) : HttpStatusException(HttpStatusCode.ServiceUnavailable, message)

class HttpUnknownException(httpStatusCode: HttpStatusCode, message: String) :
HttpStatusException(httpStatusCode, message)

0 comments on commit 6b47f9b

Please sign in to comment.