78 lines
1.6 KiB
Plaintext
78 lines
1.6 KiB
Plaintext
flow authorize(granted: Bool, level: Int) (out: Bool) {
|
|
admin = level > 0
|
|
out = rule (admin, granted) {
|
|
(true, -) => true
|
|
(false, true) => true
|
|
(false, false) => requireGrant(granted)
|
|
}
|
|
}
|
|
|
|
flow display(value: Text?) (out: Text) {
|
|
out = rule value {
|
|
null => "guest"
|
|
other => value
|
|
}
|
|
}
|
|
|
|
requireGrant(granted: Bool) (out: Bool) {
|
|
check granted else Forbidden
|
|
out = granted
|
|
}
|
|
|
|
type Line {
|
|
amount: Int
|
|
discounted: Bool
|
|
}
|
|
|
|
loop totals(lines: List<Line>) (amount: Int, discounted: Bool)
|
|
line in lines {
|
|
amount = sum line.amount
|
|
discounted = any line.discounted
|
|
}
|
|
|
|
flow checkout(level: Int, granted: Bool, lines: List<Line>) (amount: Int) {
|
|
allowed = authorize(granted, level)
|
|
check allowed else Forbidden
|
|
result_row = totals(lines)
|
|
amount = result_row.amount
|
|
}
|
|
|
|
loop cartStats(lines: List<Line>) (discount: Int, units: Int)
|
|
line in lines {
|
|
discount = rule line.discounted {
|
|
true => sum line.amount
|
|
false => skip
|
|
}
|
|
units = count
|
|
}
|
|
|
|
flow discountTotal(orders: List<List<Line>>) (out: Int) {
|
|
out = loop order in orders {
|
|
sum loop item in order {
|
|
rule item.discounted {
|
|
true => sum item.amount
|
|
false => skip
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
type Refund { id: Int, limited: Bool }
|
|
|
|
refundOne(order: Refund) (acc: Int?, stop: Bool) {
|
|
check order.id > 0 else Invalid
|
|
acc = pendingRefund(order.limited, order.id)
|
|
stop = order.limited
|
|
}
|
|
|
|
rule pendingRefund(limited: Bool, id: Int) (out: Int?) limited {
|
|
true => id
|
|
false => null
|
|
}
|
|
|
|
flow runRefunds(orders: List<Refund>) (pending: Int?) {
|
|
pending = loop order in orders {
|
|
foldUntil(previous = null, refundOne(order))
|
|
}
|
|
}
|