Last active
May 25, 2022 11:03
-
-
Save danielsiwiec/a4d61328ade213fb974d7b284c59fc1f to your computer and use it in GitHub Desktop.
Order Processing - Orchestration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OrderService( | |
inventoryService: InventoryService, | |
paymentService: PaymentService, | |
shipmentService: ShipmentService, | |
emailService: EmailService) { | |
fun processOrder(order: Order): Status { | |
val customerEmail = customerService.getEmailAddress(order.customerId) | |
val checkItemAvailability = { | |
if (order.items.all { inventoryService.isAvailable(it) }) SUBMITTED else ITEMS_UNAVAILABLE | |
} | |
val processPayment = { | |
val total = order.items.sumOf { catalogService.getPrice(it) } | |
val accountNumber = customerService.getAccountNumber(order.customerId) | |
if (paymentService.processPayment(total, accountNumber)) PAID else PAYMENT_FAILED | |
} | |
val sendShipment = { | |
shipmentService.shipOrder(order) | |
order.items.forEach { inventoryService.decreaseInventory(it) } | |
SHIPPED | |
} | |
return sequenceOf( | |
checkItemAvailability, | |
processPayment, | |
sendShipment | |
) | |
.map { it() } | |
.onEach { emailService.notify(customerEmail, it) } | |
.firstOrNull { !it.isOk() } ?: SHIPPED | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment