Skip to content

Instantly share code, notes, and snippets.

@danielsiwiec
Last active May 25, 2022 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielsiwiec/a4d61328ade213fb974d7b284c59fc1f to your computer and use it in GitHub Desktop.
Save danielsiwiec/a4d61328ade213fb974d7b284c59fc1f to your computer and use it in GitHub Desktop.
Order Processing - Orchestration
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