-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathProjection.hs
81 lines (71 loc) · 2.98 KB
/
Projection.hs
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
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
-- https://downloads.haskell.org/ghc/latest/docs/users_guide/using-warnings.html#ghc-flag--Wmissing-deriving-strategies
{-# OPTIONS_GHC -Wno-missing-deriving-strategies #-}
-- https://downloads.haskell.org/ghc/latest/docs/users_guide/using-warnings.html#ghc-flag--Wunticked-promoted-constructors
{-# OPTIONS_GHC -Wno-unticked-promoted-constructors #-}
-- https://downloads.haskell.org/ghc/latest/docs/users_guide/using-warnings.html#ghc-flag--Wunused-type-patterns
{-# OPTIONS_GHC -Wno-unused-type-patterns #-}
module Crem.Example.RiskManager.Projection where
import Crem.BaseMachine
import Crem.Example.RiskManager.Domain
import Crem.Render.RenderableVertices (AllVertices (..), RenderableVertices)
import Crem.Topology
import "base" Data.Monoid (Last (..))
import "base" GHC.Generics (Generic)
import "singletons-base" Data.Singletons.Base.TH
data ReceivedData = ReceivedData
{ receivedUserData :: Maybe UserData
, receivedLoanDetails :: Maybe LoanDetails
, receivedCreditBureauData :: Maybe CreditBureauData
}
deriving stock (Eq, Show, Generic)
instance Semigroup ReceivedData where
(<>) :: ReceivedData -> ReceivedData -> ReceivedData
r1 <> r2 =
ReceivedData
{ receivedUserData =
getLast $ Last (receivedUserData r1) <> Last (receivedUserData r2)
, receivedLoanDetails =
getLast $ Last (receivedLoanDetails r1) <> Last (receivedLoanDetails r2)
, receivedCreditBureauData =
getLast $ Last (receivedCreditBureauData r1) <> Last (receivedCreditBureauData r2)
}
instance Monoid ReceivedData where
mempty :: ReceivedData
mempty =
ReceivedData
{ receivedUserData = Nothing
, receivedLoanDetails = Nothing
, receivedCreditBureauData = Nothing
}
$( singletons
[d|
data ProjectionVertex
= SingleProjectionVertex
deriving stock (Eq, Show, Enum, Bounded)
projectionTopology :: Topology ProjectionVertex
projectionTopology =
Topology []
|]
)
deriving via AllVertices ProjectionVertex instance RenderableVertices ProjectionVertex
data ProjectionState (vertex :: ProjectionVertex) where
SingleProjectionState :: ReceivedData -> ProjectionState 'SingleProjectionVertex
riskProjection :: BaseMachine ProjectionTopology RiskEvent ReceivedData
riskProjection =
BaseMachineT
{ initialState = InitialState (SingleProjectionState mempty)
, action = \(SingleProjectionState receivedData) input ->
let
newReceivedData = case input of
UserDataRegistered ud -> receivedData {receivedUserData = Just ud}
LoanDetailsProvided ld -> receivedData {receivedLoanDetails = Just ld}
CreditBureauDataReceived cbd -> receivedData {receivedCreditBureauData = Just cbd}
in
pureResult newReceivedData (SingleProjectionState newReceivedData)
}