-
Notifications
You must be signed in to change notification settings - Fork 4
/
aggregate.scala
175 lines (139 loc) · 4.91 KB
/
aggregate.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package net.debasishg.snippet.domainpatterns
import java.util.Date
import scalaz._
import Scalaz._
import \/._
import PLens._
object aggregate {
type ValidationStatus[S] = \/[String, S]
type ProcessingStatus[S] = \/[String, S]
type ReaderTStatus[A, S] = ReaderT[ValidationStatus, A, S]
object ReaderTStatus extends KleisliInstances with KleisliFunctions {
def apply[A, S](f: A => ValidationStatus[S]): ReaderTStatus[A, S] = kleisli(f)
}
sealed trait Item {
def itemCode: String
}
case class ItemA(itemCode: String, desc: Option[String], minPurchaseUnit: Int) extends Item
case class ItemB(itemCode: String, desc: Option[String], nutritionInfo: String) extends Item
case class LineItem(item: Item, quantity: BigDecimal, value: Option[BigDecimal] = None, discount: Option[BigDecimal] = None)
case class Customer(custId: String, name: String, category: Int)
sealed trait OrderStatus
case object Placed extends OrderStatus
case object Validated extends OrderStatus
case class Address(number: String, street: String, city: String, zip: String)
case class ShipTo(name: String, address: Address)
case class Order(orderNo: String, orderDate: Date, customer: Customer,
lineItems: Vector[LineItem], shipTo: ShipTo, netOrderValue: Option[BigDecimal] = None, status: OrderStatus = Placed)
/**
* Specifications
*/
def isReadyForFulfilment(order: Order) = {
val s = for {
_ <- validate
_ <- approve
_ <- checkCustomerStatus(order.customer)
c <- checkInventory
} yield c
s(order)
}
private def validate = ReaderTStatus[Order, Boolean] {order =>
if (order.lineItems isEmpty) left(s"Validation failed for order $order") else right(true)
}
private def approve = ReaderTStatus[Order, Boolean] {order =>
println("approved")
right(true)
}
private def checkCustomerStatus(customer: Customer) = ReaderTStatus[Order, Boolean] {order =>
right(true)
}
private def checkInventory = ReaderTStatus[Order, Boolean] {order =>
println("inventory checked")
right(true)
}
/**
* lens definitions for update of aggregate root
*/
val orderStatus = Lens.lensu[Order, OrderStatus] (
(o, value) => o.copy(status = value),
_.status
)
val orderLineItems = Lens.lensu[Order, Vector[LineItem]] (
(o, lis) => o.copy(lineItems = lis),
_.lineItems
)
val lineItemValue = Lens.lensu[LineItem, Option[BigDecimal]] (
(l, v) => l.copy(value = v),
_.value
)
val lineItemDiscount = Lens.lensu[LineItem, Option[BigDecimal]] (
(l, value) => l.copy(discount = value),
_.discount
)
def lineItemValues(i: Int) = ~lineItemValue compose vectorNthPLens(i)
def lineItemDiscounts(i: Int) = ~lineItemDiscount compose vectorNthPLens(i)
val orderShipTo = Lens.lensu[Order, ShipTo] (
(o, sh) => o.copy(shipTo = sh),
_.shipTo
)
val shipToAddress = Lens.lensu[ShipTo, Address] (
(sh, add) => sh.copy(address = add),
_.address
)
val addressToCity = Lens.lensu[Address, String] (
(add, c) => add.copy(city = c),
_.city
)
def orderShipToCity = orderShipTo andThen shipToAddress andThen addressToCity
def valueOrder = Kleisli[ProcessingStatus, Order, Order] {order =>
val o = orderLineItems.set(
order,
setLineItemValues(order.lineItems)
)
o.lineItems.map(_.value).sequenceU match {
case Some(_) => right(o)
case _ => left("Missing value for items")
}
}
private def setLineItemValues(lis: Vector[LineItem]) = {
(0 to lis.length - 1).foldLeft(lis) {(s, i) =>
val li = lis(i)
lineItemValues(i).set(s, unitPrice(li.item).map(_ * li.quantity)).getOrElse(s)
}
}
def applyDiscounts = Kleisli[ProcessingStatus, Order, Order] {order =>
val o = orderLineItems.set(
order,
setLineItemValues(order.lineItems)
)
o.lineItems.map(_.discount).sequenceU match {
case Some(_) => right(o)
case _ => left("Missing discount for items")
}
}
private def setLineItemDiscounts(lis: Vector[LineItem], customer: Customer) = {
(0 to lis.length - 1).foldLeft(lis) {(s, i) =>
val li = lis(i)
lineItemDiscounts(i).set(s, discount(li.item, customer)).getOrElse(s)
}
}
val orderNetValue = Lens.lensu[Order, Option[BigDecimal]] (
(o, v) => o.copy(netOrderValue = v),
_.netOrderValue
)
def checkOut = Kleisli[ProcessingStatus, Order, Order] {order =>
val netOrderValue = order.lineItems.foldLeft(BigDecimal(0).some) {(s, i) =>
s |+| (i.value |+| i.discount.map(d => Tags.Multiplication(BigDecimal(-1)) |+| Tags.Multiplication(d)))
}
right(orderNetValue.set(order, netOrderValue))
}
private def unitPrice(item: Item): Option[BigDecimal] = {
BigDecimal(12).some
}
private def discount(item: Item, customer: Customer) = {
BigDecimal(5).some
}
def process(order: Order) = {
(valueOrder andThen applyDiscounts andThen checkOut) =<< right(orderStatus.set(order, Validated))
}
}