-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOnNewCustomerAction.kts
43 lines (40 loc) · 1.59 KB
/
OnNewCustomerAction.kts
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
import arrow.core.Either
import arrow.core.extensions.fx
import org.ostelco.prime.dsl.WriteTransaction
import org.ostelco.prime.dsl.withSku
import org.ostelco.prime.model.Customer
import org.ostelco.prime.model.Identity
import org.ostelco.prime.model.Product
import org.ostelco.prime.model.PurchaseRecord
import org.ostelco.prime.storage.StoreError
import org.ostelco.prime.storage.graph.Neo4jStoreSingleton.applyProduct
import org.ostelco.prime.storage.graph.Neo4jStoreSingleton.createPurchaseRecord
import org.ostelco.prime.storage.graph.OnNewCustomerAction
import org.ostelco.prime.storage.graph.PrimeTransaction
import java.time.Instant
import java.util.*
object : OnNewCustomerAction {
override fun apply(identity: Identity,
customer: Customer,
transaction: PrimeTransaction): Either<StoreError, Unit> {
val welcomePackProductSku = "1GB_FREE_ON_JOINING"
return Either.fx {
WriteTransaction(transaction).apply {
val (product) = get(Product withSku welcomePackProductSku)
createPurchaseRecord(
customer.id,
PurchaseRecord(
id = UUID.randomUUID().toString(),
product = product,
timestamp = Instant.now().toEpochMilli()
)
).bind()
applyProduct(
customerId = customer.id,
product = product
).bind()
}
Unit
}
}
}