packages feed

keiro-dsl-0.17.0.0: test/conformance-structural-nominals/Generated/StructuralNominalLeaves/TemplateDeliveryRouter/Router.hs

-- @generated by keiro-dsl 0.17.0.0 (language keiro-dsl 6) from router TemplateDeliveryRouter; do not edit.
module Generated.StructuralNominalLeaves.TemplateDeliveryRouter.Router
  ( templateDeliveryRouterName
  , templateDeliveryRouterWorkerOptions
  , templateDeliveryRouterSelectionFingerprint
  , templateDeliveryRouterSelectionContract
  , templateDeliveryRouterSelect
  , templateDeliveryRouter
  ) where

import Data.Text (Text)
import Effectful (Eff, IOE, (:>))
import Generated.StructuralNominalLeaves.StructuralProjections qualified as StructuralProjections
import Generated.StructuralNominalLeaves.TemplateLookup.QueryContract (TemplateLookupQueryInput)
import Generated.StructuralNominalLeaves.TemplateLookup.ReadModel qualified as SelectionQuery
import Generated.StructuralNominalLeaves.TemplateCatalog.Domain qualified as TargetDomain
import Generated.StructuralNominalLeaves.TemplateCatalog.EventStream qualified as TargetStream
import Keiki.Core (HsPred, fieldWitnessGet)
import Keiro.ProcessManager (PMCommand (..), PoisonPolicy (..), RejectedCommandPolicy (..), WorkerOptions (..))
import Keiro.ReadModel (runQuery)
import Keiro.Router
  ( DeclarativeRouter (..)
  , EmptySelectionPolicy (..)
  , PartialDispatchPolicy (..)
  , RedeliveryPolicy (..)
  , RouterSelectionContract (..)
  , RouterSelectionFailure (..)
  , SelectionDedupe (..)
  , SelectionFailurePolicy (..)
  , SelectionFingerprint (..)
  , SelectionIdentity (..)
  , SelectionOrder (..)
  , mkRecipientLimit
  , mkSelectionVersion
  )
import Keiro.Stream (entityStream)
import Kiroku.Store.Effect (Store)
import Shibuya.Core.Ack (RetryDelay (..))

-- The STABLE router name. It remains part of every target-keyed
-- deterministic router command id; selection metadata never re-keys dispatches.
templateDeliveryRouterName :: Text
templateDeliveryRouterName = "template-delivery-router"

-- SHA-256 of the checked selection semantics (locations and formatting excluded).
templateDeliveryRouterSelectionFingerprint :: Text
templateDeliveryRouterSelectionFingerprint = "ebf9d308e0fc1f40291e800d9d0ca3e75eaf3cf2d3a4fc1ec56e8f5971366488"

templateDeliveryRouterSelectionContract :: RouterSelectionContract
templateDeliveryRouterSelectionContract =
  RouterSelectionContract
    { identity = SelectionIdentity "template-nominal-selection"
    , version = checkedSelectionVersion
    , fingerprint = SelectionFingerprint templateDeliveryRouterSelectionFingerprint
    , limit = checkedRecipientLimit
    , order = OrderByTargetStream
    , dedupe = DedupeByTargetStream
    , emptyPolicy = EmptyAck
    , failurePolicy = FailureRetry
    , redeliveryPolicy = StableUnion
    , partialPolicy = RetainSuccesses
    }
  where
    checkedSelectionVersion = case mkSelectionVersion 1 of
      Right value -> value
      Left _ -> error "keiro-dsl emitted a non-positive checked selection version"
    checkedRecipientLimit = case mkRecipientLimit 16 of
      Right value -> value
      Left _ -> error "keiro-dsl emitted a non-positive checked recipient limit"

templateDeliveryRouterSelect ::
  (IOE :> es, Store :> es) =>
  TemplateLookupQueryInput ->
  Eff es (Either RouterSelectionFailure [PMCommand TargetDomain.TemplateCatalogCommand])
templateDeliveryRouterSelect input = do
  queryResult <- runQuery Nothing SelectionQuery.templateLookupReadModel input
  pure $ case queryResult of
    Left _ -> Left (SelectionQueryFailed "read-model template_lookup query failed")
    Right rows ->
      Right
        [ PMCommand
            { target = entityStream TargetStream.templateCatalogCommandCategory ((fieldWitnessGet StructuralProjections.templateLookupRowTemplateIdWitness row))
            , command = TargetDomain.RouteTemplate (TargetDomain.RouteTemplateData ((StructuralProjections.templateLookupRowTemplateIdRawGet row)) ((StructuralProjections.templateLookupRowClaimIdRawGet row)))
            }
        | row <- rows
        , (((fieldWitnessGet StructuralProjections.templateLookupRowClaimIdWitness row) == (fieldWitnessGet StructuralProjections.templateLookupInputClaimIdWitness input)) && ((fieldWitnessGet StructuralProjections.templateLookupRowTemplateIdWitness row) == "template_01h455vb4pex5vsknk084sn02q"))
        ]

templateDeliveryRouter ::
  (IOE :> es, Store :> es) =>
  DeclarativeRouter
    TemplateLookupQueryInput
    (HsPred TargetDomain.TemplateCatalogRegs TargetDomain.TemplateCatalogCommand)
    TargetDomain.TemplateCatalogRegs
    TargetDomain.TemplateCatalogVertex
    TargetDomain.TemplateCatalogCommand
    TargetDomain.TemplateCatalogEvent
    es
templateDeliveryRouter =
  DeclarativeRouter
    { name = templateDeliveryRouterName
    , key = \input -> (fieldWitnessGet StructuralProjections.templateLookupInputClaimIdWitness input)
    , selectionContract = templateDeliveryRouterSelectionContract
    , select = templateDeliveryRouterSelect
    , targetEventStream = TargetStream.templateCatalogEventStream
    , targetProjections = const []
    }

-- Node-level worker policy. Pair it with runDeclarativeRouterWorkerWith.
-- Selection empty/failure policy remains in the generated selection contract.
templateDeliveryRouterWorkerOptions :: WorkerOptions es msg
templateDeliveryRouterWorkerOptions =
  WorkerOptions
    { poisonPolicy = PoisonHalt,
      rejectedCommandPolicy = RejectedDeadLetter,
      transientRetryDelay = RetryDelay 5, -- matches defaultWorkerOptions; runtime tuning
      metrics = Nothing -- runtime configuration; install at call site
    }