Skip to content

Commit

Permalink
Update OrderLine Swagger examples (#61)
Browse files Browse the repository at this point in the history
Update OrderLines with new status and fix Swagger examples
  • Loading branch information
anotheroneofthese authored Feb 3, 2025
1 parent f773ec8 commit f9041a5
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import org.apache.commons.validator.routines.UrlValidator
"hostOrderId": "mlt-12345-order",
"orderLine": [
{
"hostId": "mlt-12345",
"status": "NOT_STARTED"
"hostId": "mlt-12345"
}
],
"orderType": "LOAN",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ data class OrderLine(
val hostId: String,
@Schema(
description = """Current status for the ordered item.""",
examples = ["NOT_STARTED", "PICKED", "FAILED"]
examples = ["NOT_STARTED", "PICKED", "RETURNED", "FAILED"],
accessMode = READ_ONLY
)
val status: Order.OrderItem.Status?
) {
Expand Down
52 changes: 41 additions & 11 deletions src/main/kotlin/no/nb/mlt/wls/domain/model/Order.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package no.nb.mlt.wls.domain.model

import no.nb.mlt.wls.domain.model.Order.Address
import no.nb.mlt.wls.domain.model.Order.OrderItem.Status.FAILED
import no.nb.mlt.wls.domain.model.Order.OrderItem.Status.PICKED
import no.nb.mlt.wls.domain.ports.inbound.IllegalOrderStateException
import no.nb.mlt.wls.domain.ports.inbound.ValidationException
import java.net.URI
Expand Down Expand Up @@ -35,7 +33,7 @@ data class Order(
hostIds: List<String>,
status: OrderItem.Status
): Order {
if (isOrderClosed()) {
if (isOrderClosed() && status != OrderItem.Status.RETURNED) {
throw IllegalOrderStateException("Order is already closed with status: $status")
}

Expand Down Expand Up @@ -72,7 +70,16 @@ data class Order(
}

private fun updateOrderStatusFromOrderLines(): Order {
// This might benefit from a small refactor of sorts
return when {
orderLine.all(OrderItem::isReturned) -> {
this.copy(status = Status.RETURNED)
}

orderLine.all(OrderItem::isComplete) -> {
this.copy(status = Status.COMPLETED)
}

orderLine.all(OrderItem::isPickedOrFailed) -> {
this.copy(status = Status.COMPLETED)
}
Expand All @@ -86,7 +93,7 @@ data class Order(
}

private fun isOrderClosed(): Boolean {
return listOf(Status.COMPLETED, Status.DELETED).contains(status)
return listOf(Status.COMPLETED, Status.DELETED, Status.RETURNED).contains(status)
}

private fun isOrderProcessingStarted(): Boolean {
Expand All @@ -106,7 +113,8 @@ data class Order(
): Order {
throwIfInProgress()

return this.setOrderLines(itemIds)
return this
.setOrderLines(itemIds)
.setCallbackUrl(callbackUrl)
.setOrderType(orderType)
.setAddress(address)
Expand Down Expand Up @@ -143,7 +151,7 @@ data class Order(
}

fun pickOrder(itemIds: List<String>): Order {
return this.setOrderLineStatus(itemIds, PICKED)
return this.setOrderLineStatus(itemIds, OrderItem.Status.PICKED)
}

private fun throwIfInvalidUrl(url: String) {
Expand All @@ -161,7 +169,33 @@ data class Order(
enum class Status {
NOT_STARTED,
PICKED,
FAILED
FAILED,
RETURNED
}

fun isPickedOrFailed(): Boolean {
return when (this.status) {
Status.NOT_STARTED -> false
Status.PICKED -> true
Status.FAILED -> true
Status.RETURNED -> false
}
}

/**
* Used to infer from the OrderItems in OrderLines that the Order is complete
*/
fun isComplete(): Boolean {
return when (this.status) {
Status.NOT_STARTED -> false
Status.PICKED -> true
Status.FAILED -> true
Status.RETURNED -> true
}
}

fun isReturned(): Boolean {
return this.status == Status.RETURNED
}
}

Expand Down Expand Up @@ -213,8 +247,4 @@ data class Order(
}
}

fun Order.OrderItem.isPickedOrFailed(): Boolean {
return this.status == PICKED || this.status == FAILED
}

fun createOrderAddress(): Address = Address(null, null, null, null, null, null, null)
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,23 @@ class OrderControllerTest(
}
}

@Test
fun `updateOrder when order is complete returns 409`() {
val testPayload = completeOrder.toApiOrderPayload().copy(orderType = Order.Type.DIGITIZATION)
runTest {
repository.save(completeOrder.toMongoOrder()).awaitSingle()

webTestClient
.mutateWith(csrf())
.mutateWith(mockJwt().authorities(SimpleGrantedAuthority("ROLE_order"), SimpleGrantedAuthority(clientRole)))
.put()
.bodyValue(testPayload)
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isEqualTo(HttpStatus.CONFLICT)
}
}

@Test
fun `deleteOrder with valid data deletes order`() =
runTest {
Expand Down Expand Up @@ -529,6 +546,31 @@ class OrderControllerTest(
callbackUrl = "https://callback-wls.no/order"
)

private val completeOrder =
Order(
hostName = HostName.AXIELL,
hostOrderId = "order-completed",
status = Order.Status.COMPLETED,
orderLine =
listOf(
Order.OrderItem("item-123", Order.OrderItem.Status.PICKED)
),
orderType = Order.Type.LOAN,
address =
Order.Address(
recipient = "recipient",
addressLine1 = "addressLine1",
addressLine2 = "addressLine2",
postcode = "postcode",
city = "city",
region = "region",
country = "country"
),
contactPerson = "named person",
note = "note",
callbackUrl = "https://callback-wls.no/order"
)

/**
* Populate the database with items and orders for testing
*/
Expand Down

0 comments on commit f9041a5

Please sign in to comment.