Skip to content

Commit

Permalink
Merge pull request #37 from KjellBerlin/Get-orders-by-user-endpoint
Browse files Browse the repository at this point in the history
Get Orders by user and order status
  • Loading branch information
KjellBerlin authored Aug 6, 2024
2 parents 3b811bd + 0fe16f8 commit f1e8e59
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/main/kotlin/com/carbonara/core/order/OrderDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ data class OrderDao(
val products: List<ProductDao>,
val additionalDetails: String?,
val paymentDetails: PaymentDetails,
val orderStatus: OrderStatus,
val createdAt: String = OffsetDateTime.now().toString(),
val updatedAt: String = OffsetDateTime.now().toString()
) {
Expand All @@ -31,7 +32,8 @@ data class OrderDao(
deliveryAddress = deliveryAddress,
productDtos = products.map { it.toProductDao() },
additionalDetails = additionalDetails,
paymentRedirectionLink = paymentDetails.paymentRedirectLink
paymentRedirectionLink = paymentDetails.paymentRedirectLink,
orderStatus = orderStatus
)
}
}
4 changes: 3 additions & 1 deletion src/main/kotlin/com/carbonara/core/order/OrderDto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
16 changes: 16 additions & 0 deletions src/main/kotlin/com/carbonara/core/order/OrderQuery.kt
Original file line number Diff line number Diff line change
@@ -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 getOrdersByUserId(userId: String): List<OrderDto> {
return orderService.getOrdersByAuth0UserId(userId)
}
}
3 changes: 3 additions & 0 deletions src/main/kotlin/com/carbonara/core/order/OrderRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ interface OrderRepository: ReactiveMongoRepository<OrderDao, ObjectId> {

@Query("{'paymentDetails.paymentId': ?0}")
fun findFirstByPaymentId(paymentId: String): Mono<OrderDao>

@Query("{'auth0UserId': ?0}")
fun findAllByAuth0UserId(auth0UserId: String): Mono<List<OrderDao>>
}
12 changes: 11 additions & 1 deletion src/main/kotlin/com/carbonara/core/order/OrderService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -66,6 +67,15 @@ class OrderService(
}
}

suspend fun getOrdersByAuth0UserId(
auth0UserId: String
): List<OrderDto> {
return orderRepository.findAllByAuth0UserId(auth0UserId)
.awaitSingleOrNull()
?.map { it.toOrderDto() }
?: emptyList()
}

private fun createPaymentDescription(products: List<ProductDao>): String {
return if (products.size == 1) {
products.first().productName
Expand Down
9 changes: 9 additions & 0 deletions src/main/kotlin/com/carbonara/core/order/OrderStatus.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.carbonara.core.order

enum class OrderStatus {
PROCESSING_ORDER,
FINDING_AVAILABLE_RIDER,
DELIVERY_IN_PROGRESS,
DELIVERED,
CANCELLED
}
25 changes: 25 additions & 0 deletions src/test/kotlin/com/carbonara/core/order/OrderServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,30 @@ class OrderServiceTest {
}
}

@Nested
inner class GetOrdersByAuth0UserIdTests {

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

val result = runBlocking { orderService.getOrdersByAuth0UserId(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<OrderDao>().toMono()

val result = runBlocking { orderService.getOrdersByAuth0UserId(AUTH0_USER_ID) }
assertEquals(emptyList<OrderDao>(), 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"
Expand Down Expand Up @@ -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()
)
Expand Down

0 comments on commit f1e8e59

Please sign in to comment.