packages feed

keiro-dsl-0.17.0.0: test/conformance-process-state-authority/Generated/IncidentResponse/Escalation/Codec.hs

-- @generated by keiro-dsl 0.17.0.0 (language keiro-dsl 6) from aggregate Escalation; do not edit.
module Generated.IncidentResponse.Escalation.Codec (
    escalationCodec,
    parseEscalationEvent,
    encodeEscalationEvent,
) where

import Generated.IncidentResponse.Escalation.Domain
import Generated.IncidentResponse.Nominals (incidentIdText)
import Generated.IncidentResponse.Nominals.Internal (unsafeIncidentIdFromLegacyText)
import Data.Aeson (Value, object, withObject, (.:), (.=))
import Data.Aeson.Types (parseEither)
import Data.List.NonEmpty (NonEmpty (..))
import Data.List.NonEmpty qualified as NonEmpty
import Data.Text (Text)
import qualified Data.Text as T
import Keiro.Codec (Codec (..), EventType (..))





escalationEventTypes :: NonEmpty EventType
escalationEventTypes = EventType "RaisedNoted" :| [EventType "Acknowledged", EventType "DormantActivated"]

escalationCodec :: Codec EscalationEvent
escalationCodec =
  Codec
    { eventTypes = escalationEventTypes
    , eventType = \case
        RaisedNoted{} -> EventType "RaisedNoted"
        Acknowledged{} -> EventType "Acknowledged"
        DormantActivated{} -> EventType "DormantActivated"
    , schemaVersion = 1
    , encode = encodeEscalationEvent
    , decode = parseEscalationEvent
    , upcasters = []
    }

encodeEscalationEvent :: EscalationEvent -> Value
encodeEscalationEvent = \case
  RaisedNoted payload ->
    object
      [ "kind" .= ("RaisedNoted" :: Text)
      , "incidentId" .= incidentIdText payload.incidentId
      ]
  Acknowledged payload ->
    object
      [ "kind" .= ("Acknowledged" :: Text)
      , "incidentId" .= incidentIdText payload.incidentId
      ]
  DormantActivated payload ->
    object
      [ "kind" .= ("DormantActivated" :: Text)
      , "incidentId" .= incidentIdText payload.incidentId
      ]

parseEscalationEvent :: EventType -> Value -> Either Text EscalationEvent
parseEscalationEvent (EventType tag) = mapLeftText . parseEither (withObject "EscalationEvent" go)
  where
    go o = do
      case tag of
        "RaisedNoted" ->
          RaisedNoted
            <$> ( RaisedNotedData
                    <$> (unsafeIncidentIdFromLegacyText <$> o .: "incidentId")
                )
        "Acknowledged" ->
          Acknowledged
            <$> ( AcknowledgedData
                    <$> (unsafeIncidentIdFromLegacyText <$> o .: "incidentId")
                )
        "DormantActivated" ->
          DormantActivated
            <$> ( DormantActivatedData
                    <$> (unsafeIncidentIdFromLegacyText <$> o .: "incidentId")
                )
        _ -> fail ("unknown event type " <> show tag <> "; expected one of: " <> renderExpectedEventTypes escalationEventTypes)

mapLeftText :: Either String b -> Either Text b
mapLeftText = either (Left . T.pack) Right

renderExpectedEventTypes :: NonEmpty EventType -> String
renderExpectedEventTypes =
  T.unpack
    . T.intercalate ", "
    . map (\(EventType eventTypeName) -> eventTypeName)
    . NonEmpty.toList