Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MLT-0086 Item Model Update #56

Merged
merged 4 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import no.nb.mlt.wls.domain.model.HostName
import no.nb.mlt.wls.domain.model.Item
import no.nb.mlt.wls.domain.model.ItemCategory
import no.nb.mlt.wls.domain.model.Packaging
import no.nb.mlt.wls.domain.ports.inbound.ItemMetadata
import no.nb.mlt.wls.domain.ports.inbound.ValidationException
import org.apache.commons.validator.routines.UrlValidator

Expand Down Expand Up @@ -71,17 +70,15 @@ data class ApiItemPayload(
val callbackUrl: String?,
@Schema(
description = """Where the item is located, can be used for tracking item movement through storage systems.""",
examples = ["UNKNOWN", "WITH_LENDER", "SYNQ_WAREHOUSE", "AUTOSTORE", "KARDEX"],
required = false
examples = ["UNKNOWN", "WITH_LENDER", "SYNQ_WAREHOUSE", "AUTOSTORE", "KARDEX"]
)
val location: String?,
val location: String,
@Schema(
description = """Quantity on hand of the item, this easily denotes if the item is in the storage or not.
If the item is in storage then quantity is 1, if it's not in storage then quantity is 0.""",
examples = [ "0", "1"],
required = false
examples = [ "0", "1"]
)
val quantity: Int?
val quantity: Int
) {
fun toItem(): Item =
Item(
Expand All @@ -96,17 +93,6 @@ data class ApiItemPayload(
quantity = quantity
)

fun toItemMetadata(): ItemMetadata =
ItemMetadata(
hostId = hostId,
hostName = hostName,
description = description,
itemCategory = itemCategory,
preferredEnvironment = preferredEnvironment,
packaging = packaging,
callbackUrl = callbackUrl
)

@Throws(ValidationException::class)
fun validate() {
if (hostId.isBlank()) {
Expand All @@ -117,12 +103,12 @@ data class ApiItemPayload(
throw ValidationException("The item's 'description' is required, and it cannot be blank")
}

if (location != null && location.isBlank()) {
throw ValidationException("The item's 'location' cannot be blank if set")
if (location.isBlank()) {
throw ValidationException("The item's 'location' is required, and it cannot be blank")
}

if (quantity != null && quantity != 0 && quantity != 1) {
throw ValidationException("The item's 'quantity' must be one or zero if set")
if (quantity != 0 && quantity != 1) {
throw ValidationException("The item's 'quantity' must be one or zero")
}

if (callbackUrl != null && !isValidUrl(callbackUrl)) {
Expand Down
14 changes: 4 additions & 10 deletions src/main/kotlin/no/nb/mlt/wls/domain/model/Item.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ data class Item(
val preferredEnvironment: Environment,
val packaging: Packaging,
val callbackUrl: String?,
val location: String?,
val quantity: Int?
val location: String,
val quantity: Int
) {
fun pickItem(amountPicked: Int): Item {
val itemsInStockQuantity = quantity ?: 0
val itemsInStockQuantity = quantity

// In the case of over-picking, log it and set quantity to zero.
// This is in hope that on return the database recovers
Expand All @@ -31,14 +31,8 @@ data class Item(
val location: String =
if (quantity == 0) {
"WITH_LENDER"
} else if (location != null) {
location
} else {
// Rare edge case. Log it until we can determine if this actually happens in production
logger.error {
"Item with ID '$hostId' for host '$hostName' without a location was picked. Location was set to 'UNKNOWN'."
}
"UNKNOWN"
location
}
return this.copy(quantity = quantity, location = location)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ data class ItemMetadata(
* are always zero/empty, as they must be registered before being inserted into storage
*/
fun ItemMetadata.toItem(
quantity: Int? = 0,
location: String? = null
quantity: Int = 0,
location: String = "UNKNOWN"
) = Item(
this.hostId,
this.hostName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,13 @@ data class NotificationItemPayload(
examples = ["UNKNOWN", "WITH_LENDER", "SYNQ_WAREHOUSE", "AUTOSTORE", "KARDEX"],
required = false
)
val location: String?,
val location: String,
@Schema(
description = """Quantity on hand of the item, this easily denotes if the item is in the storage or not.
If the item is in storage then quantity is 1, if it's not in storage then quantity is 0.""",
examples = [ "0.0", "1.0"],
required = false
examples = [ "0.0", "1.0"]
)
val quantity: Int?
val quantity: Int
)

fun NotificationItemPayload.toItem(): Item {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ data class MongoItem(
val preferredEnvironment: Environment,
val packaging: Packaging,
val callbackUrl: String?,
val location: String?,
val quantity: Int?
val location: String,
val quantity: Int
)

fun Item.toMongoItem() =
Expand Down
4 changes: 2 additions & 2 deletions src/test/kotlin/no/nb/mlt/wls/domain/WLSServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,8 @@ class WLSServiceTest {
preferredEnvironment = Environment.NONE,
packaging = Packaging.NONE,
callbackUrl = "https://callback-wls.no/item",
location = null,
quantity = null
location = "UNKNOWN",
quantity = 0
)

private val testOrder =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class ItemControllerTest(
assertThat(item)
.isNotNull
.extracting("description", "location", "quantity")
.containsExactly(testItemPayload.description, null, 0)
.containsExactly(testItemPayload.description, "UNKNOWN", 0)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class ItemModelValidationTest {
fun `item with minimal valid data should pass validation`() {
val item =
validItem.copy(
quantity = null,
location = null,
quantity = 0,
location = "UNKNOWN",
callbackUrl = null
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ class SynqControllerTest(
preferredEnvironment = Environment.FRYS,
packaging = Packaging.BOX,
callbackUrl = "https://callback-wls.no/item",
location = null,
location = "UNKNOWN",
quantity = 0
)

Expand All @@ -378,7 +378,7 @@ class SynqControllerTest(
preferredEnvironment = Environment.FRYS,
packaging = Packaging.BOX,
callbackUrl = "https://callback-wls.no/item",
location = null,
location = "UNKNOWN",
quantity = 0
)

Expand Down