Skip to content

Commit

Permalink
Fix order repo
Browse files Browse the repository at this point in the history
  • Loading branch information
KjellBerlin committed Aug 6, 2024
1 parent 32455b4 commit a6163a3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/main/kotlin/com/carbonara/core/order/OrderRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.carbonara.core.order
import org.bson.types.ObjectId
import org.springframework.data.mongodb.repository.Query
import org.springframework.data.mongodb.repository.ReactiveMongoRepository
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono

interface OrderRepository: ReactiveMongoRepository<OrderDao, ObjectId> {
Expand All @@ -11,5 +12,5 @@ interface OrderRepository: ReactiveMongoRepository<OrderDao, ObjectId> {
fun findFirstByPaymentId(paymentId: String): Mono<OrderDao>

@Query("{'auth0UserId': ?0, 'paymentDetails.paid': true}")
fun findAllByAuth0UserIdAndPaid(auth0UserId: String): Mono<List<OrderDao>>
fun findAllByAuth0UserIdAndPaid(auth0UserId: String): Flux<OrderDao>
}
4 changes: 1 addition & 3 deletions src/main/kotlin/com/carbonara/core/order/OrderService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ class OrderService(
auth0UserId: String
): List<OrderDto> {
return orderRepository.findAllByAuth0UserIdAndPaid(auth0UserId)
.awaitSingleOrNull()
?.map { it.toOrderDto() }
?: emptyList()
.collectList().awaitSingleOrNull()?.map { it.toOrderDto() } ?: emptyList()
}

private fun createPaymentDescription(products: List<ProductDao>): String {
Expand Down
5 changes: 3 additions & 2 deletions src/test/kotlin/com/carbonara/core/order/OrderServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import reactor.kotlin.core.publisher.toFlux
import reactor.kotlin.core.publisher.toMono
import java.time.OffsetDateTime

Expand Down Expand Up @@ -147,7 +148,7 @@ class OrderServiceTest {

@Test
fun `Happy case`() {
coEvery { orderRepository.findAllByAuth0UserIdAndPaid(AUTH0_USER_ID) } returns listOf(ORDER_DAO).toMono()
coEvery { orderRepository.findAllByAuth0UserIdAndPaid(AUTH0_USER_ID) } returns listOf(ORDER_DAO).toFlux()

val result = runBlocking { orderService.getPaidOrdersByAuth0UserId(AUTH0_USER_ID) }
assertEquals(listOf(ORDER_DAO.toOrderDto()), result)
Expand All @@ -157,7 +158,7 @@ class OrderServiceTest {

@Test
fun `No orders found`() {
coEvery { orderRepository.findAllByAuth0UserIdAndPaid(AUTH0_USER_ID) } returns emptyList<OrderDao>().toMono()
coEvery { orderRepository.findAllByAuth0UserIdAndPaid(AUTH0_USER_ID) } returns emptyList<OrderDao>().toFlux()

val result = runBlocking { orderService.getPaidOrdersByAuth0UserId(AUTH0_USER_ID) }
assertEquals(emptyList<OrderDao>(), result)
Expand Down

0 comments on commit a6163a3

Please sign in to comment.