From 497d112d507402a21d1c30b830d3c8b07b1dc363 Mon Sep 17 00:00:00 2001 From: KjellBerlin Date: Mon, 5 Aug 2024 17:08:20 +0200 Subject: [PATCH 1/2] Get Orders by user and order status --- .../com/carbonara/core/order/OrderDao.kt | 4 ++- .../com/carbonara/core/order/OrderDto.kt | 4 ++- .../com/carbonara/core/order/OrderQuery.kt | 16 ++++++++++++ .../carbonara/core/order/OrderRepository.kt | 3 +++ .../com/carbonara/core/order/OrderService.kt | 12 ++++++++- .../com/carbonara/core/order/OrderStatus.kt | 9 +++++++ .../carbonara/core/order/OrderServiceTest.kt | 25 +++++++++++++++++++ 7 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 src/main/kotlin/com/carbonara/core/order/OrderQuery.kt create mode 100644 src/main/kotlin/com/carbonara/core/order/OrderStatus.kt diff --git a/src/main/kotlin/com/carbonara/core/order/OrderDao.kt b/src/main/kotlin/com/carbonara/core/order/OrderDao.kt index c00e246..5fe0354 100644 --- a/src/main/kotlin/com/carbonara/core/order/OrderDao.kt +++ b/src/main/kotlin/com/carbonara/core/order/OrderDao.kt @@ -19,6 +19,7 @@ data class OrderDao( val products: List, val additionalDetails: String?, val paymentDetails: PaymentDetails, + val orderStatus: OrderStatus, val createdAt: String = OffsetDateTime.now().toString(), val updatedAt: String = OffsetDateTime.now().toString() ) { @@ -31,7 +32,8 @@ data class OrderDao( deliveryAddress = deliveryAddress, productDtos = products.map { it.toProductDao() }, additionalDetails = additionalDetails, - paymentRedirectionLink = paymentDetails.paymentRedirectLink + paymentRedirectionLink = paymentDetails.paymentRedirectLink, + orderStatus = orderStatus ) } } diff --git a/src/main/kotlin/com/carbonara/core/order/OrderDto.kt b/src/main/kotlin/com/carbonara/core/order/OrderDto.kt index dff9020..aeb162e 100644 --- a/src/main/kotlin/com/carbonara/core/order/OrderDto.kt +++ b/src/main/kotlin/com/carbonara/core/order/OrderDto.kt @@ -20,5 +20,7 @@ data class OrderDto ( @GraphQLDescription("Additional order details") val additionalDetails: String?, @GraphQLDescription("Payment redirection link") - val paymentRedirectionLink: String + val paymentRedirectionLink: String, + @GraphQLDescription("Order status") + val orderStatus: OrderStatus ) diff --git a/src/main/kotlin/com/carbonara/core/order/OrderQuery.kt b/src/main/kotlin/com/carbonara/core/order/OrderQuery.kt new file mode 100644 index 0000000..b3a7183 --- /dev/null +++ b/src/main/kotlin/com/carbonara/core/order/OrderQuery.kt @@ -0,0 +1,16 @@ +package com.carbonara.core.order + +import com.expediagroup.graphql.generator.annotations.GraphQLDescription +import com.expediagroup.graphql.server.operations.Query +import org.springframework.stereotype.Component + +@Component +class OrderQuery( + private val orderService: OrderService +): Query { + + @GraphQLDescription("Get all orders for a user") + suspend fun getOrdersByAuth0UserId(auth0UserId: String): List { + return orderService.getOrdersByUserId(auth0UserId) + } +} diff --git a/src/main/kotlin/com/carbonara/core/order/OrderRepository.kt b/src/main/kotlin/com/carbonara/core/order/OrderRepository.kt index a20169f..1fd5ca2 100644 --- a/src/main/kotlin/com/carbonara/core/order/OrderRepository.kt +++ b/src/main/kotlin/com/carbonara/core/order/OrderRepository.kt @@ -9,4 +9,7 @@ interface OrderRepository: ReactiveMongoRepository { @Query("{'paymentDetails.paymentId': ?0}") fun findFirstByPaymentId(paymentId: String): Mono + + @Query("{'auth0UserId': ?0}") + fun findAllByAuth0UserId(auth0UserId: String): Mono> } diff --git a/src/main/kotlin/com/carbonara/core/order/OrderService.kt b/src/main/kotlin/com/carbonara/core/order/OrderService.kt index e0f9764..d918589 100644 --- a/src/main/kotlin/com/carbonara/core/order/OrderService.kt +++ b/src/main/kotlin/com/carbonara/core/order/OrderService.kt @@ -37,7 +37,8 @@ class OrderService( deliveryAddress = createOrderInput.deliveryAddress, products = productDaos, additionalDetails = createOrderInput.additionalDetails, - paymentDetails = paymentDetails + paymentDetails = paymentDetails, + orderStatus = OrderStatus.PROCESSING_ORDER ) ).awaitSingleOrNull()?.toOrderDto() ?: run { log.error("Failed to save order for user {} to database", createOrderInput.userName) @@ -66,6 +67,15 @@ class OrderService( } } + suspend fun getOrdersByUserId( + auth0UserId: String + ): List { + return orderRepository.findAllByAuth0UserId(auth0UserId) + .awaitSingleOrNull() + ?.map { it.toOrderDto() } + ?: emptyList() + } + private fun createPaymentDescription(products: List): String { return if (products.size == 1) { products.first().productName diff --git a/src/main/kotlin/com/carbonara/core/order/OrderStatus.kt b/src/main/kotlin/com/carbonara/core/order/OrderStatus.kt new file mode 100644 index 0000000..6d68c63 --- /dev/null +++ b/src/main/kotlin/com/carbonara/core/order/OrderStatus.kt @@ -0,0 +1,9 @@ +package com.carbonara.core.order + +enum class OrderStatus { + PROCESSING_ORDER, + FINDING_AVAILABLE_RIDER, + DELIVERY_IN_PROGRESS, + DELIVERED, + CANCELLED +} diff --git a/src/test/kotlin/com/carbonara/core/order/OrderServiceTest.kt b/src/test/kotlin/com/carbonara/core/order/OrderServiceTest.kt index b56062a..45f6755 100644 --- a/src/test/kotlin/com/carbonara/core/order/OrderServiceTest.kt +++ b/src/test/kotlin/com/carbonara/core/order/OrderServiceTest.kt @@ -142,6 +142,30 @@ class OrderServiceTest { } } + @Nested + inner class GetOrdersByUserIdTests { + + @Test + fun `Happy case`() { + coEvery { orderRepository.findAllByAuth0UserId(AUTH0_USER_ID) } returns listOf(ORDER_DAO).toMono() + + val result = runBlocking { orderService.getOrdersByUserId(AUTH0_USER_ID) } + assertEquals(listOf(ORDER_DAO.toOrderDto()), result) + + coVerify(exactly = 1) { orderRepository.findAllByAuth0UserId(AUTH0_USER_ID) } + } + + @Test + fun `No orders found`() { + coEvery { orderRepository.findAllByAuth0UserId(AUTH0_USER_ID) } returns emptyList().toMono() + + val result = runBlocking { orderService.getOrdersByUserId(AUTH0_USER_ID) } + assertEquals(emptyList(), result) + + coVerify(exactly = 1) { orderRepository.findAllByAuth0UserId(AUTH0_USER_ID) } + } + } + companion object { val TIME: OffsetDateTime = OffsetDateTime.parse("2024-06-01T14:00:00.0+02:00") private const val AUTH0_USER_ID = "auth0Id1" @@ -185,6 +209,7 @@ class OrderServiceTest { products = listOf(TEST_PRODUCT), additionalDetails = CREATE_ORDER_INPUT.additionalDetails, paymentDetails = MOLLIE_PAYMENT_DETAILS, + orderStatus = OrderStatus.PROCESSING_ORDER, createdAt = TIME.toString(), updatedAt = TIME.toString() ) From 0fe16f8c66c9c7b90a07d5b72dfbd6384bcdc75a Mon Sep 17 00:00:00 2001 From: KjellBerlin Date: Mon, 5 Aug 2024 17:10:18 +0200 Subject: [PATCH 2/2] Minor renamings --- src/main/kotlin/com/carbonara/core/order/OrderQuery.kt | 4 ++-- src/main/kotlin/com/carbonara/core/order/OrderService.kt | 2 +- .../kotlin/com/carbonara/core/order/OrderServiceTest.kt | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/com/carbonara/core/order/OrderQuery.kt b/src/main/kotlin/com/carbonara/core/order/OrderQuery.kt index b3a7183..14465b7 100644 --- a/src/main/kotlin/com/carbonara/core/order/OrderQuery.kt +++ b/src/main/kotlin/com/carbonara/core/order/OrderQuery.kt @@ -10,7 +10,7 @@ class OrderQuery( ): Query { @GraphQLDescription("Get all orders for a user") - suspend fun getOrdersByAuth0UserId(auth0UserId: String): List { - return orderService.getOrdersByUserId(auth0UserId) + suspend fun getOrdersByUserId(userId: String): List { + return orderService.getOrdersByAuth0UserId(userId) } } diff --git a/src/main/kotlin/com/carbonara/core/order/OrderService.kt b/src/main/kotlin/com/carbonara/core/order/OrderService.kt index d918589..eda203a 100644 --- a/src/main/kotlin/com/carbonara/core/order/OrderService.kt +++ b/src/main/kotlin/com/carbonara/core/order/OrderService.kt @@ -67,7 +67,7 @@ class OrderService( } } - suspend fun getOrdersByUserId( + suspend fun getOrdersByAuth0UserId( auth0UserId: String ): List { return orderRepository.findAllByAuth0UserId(auth0UserId) diff --git a/src/test/kotlin/com/carbonara/core/order/OrderServiceTest.kt b/src/test/kotlin/com/carbonara/core/order/OrderServiceTest.kt index 45f6755..cb1715f 100644 --- a/src/test/kotlin/com/carbonara/core/order/OrderServiceTest.kt +++ b/src/test/kotlin/com/carbonara/core/order/OrderServiceTest.kt @@ -143,13 +143,13 @@ class OrderServiceTest { } @Nested - inner class GetOrdersByUserIdTests { + inner class GetOrdersByAuth0UserIdTests { @Test fun `Happy case`() { coEvery { orderRepository.findAllByAuth0UserId(AUTH0_USER_ID) } returns listOf(ORDER_DAO).toMono() - val result = runBlocking { orderService.getOrdersByUserId(AUTH0_USER_ID) } + val result = runBlocking { orderService.getOrdersByAuth0UserId(AUTH0_USER_ID) } assertEquals(listOf(ORDER_DAO.toOrderDto()), result) coVerify(exactly = 1) { orderRepository.findAllByAuth0UserId(AUTH0_USER_ID) } @@ -159,7 +159,7 @@ class OrderServiceTest { fun `No orders found`() { coEvery { orderRepository.findAllByAuth0UserId(AUTH0_USER_ID) } returns emptyList().toMono() - val result = runBlocking { orderService.getOrdersByUserId(AUTH0_USER_ID) } + val result = runBlocking { orderService.getOrdersByAuth0UserId(AUTH0_USER_ID) } assertEquals(emptyList(), result) coVerify(exactly = 1) { orderRepository.findAllByAuth0UserId(AUTH0_USER_ID) }