packages feed

keiro-dsl-0.7.0.0: test/conformance-skeletons/SkelProcess/Generated/MyService/HospitalSurge/Process.hs

-- @generated by keiro-dsl; do not edit. Regenerated from the .keiro spec.
module SkelProcess.Generated.MyService.HospitalSurge.Process
  ( hospitalSurgeProcessName
  , hospitalSurgeCategory
  , hospitalSurgeProcessWorkerOptions
  , hospitalSurgeTimerRequest
  , hospitalSurgeFireOutcome
  ) where

import Data.Aeson (Value, object, (.=))
import Data.Text (Text)
import qualified Data.Text as T
import Data.Time (UTCTime)
import Data.UUID (UUID)
import qualified Data.UUID.V5 as UUID.V5
import Keiro.Command (CommandError (..))
import Keiro.Stream qualified as Stream
import Keiro.Timer (TimerId (..), TimerRequest (..))
import Keiro.ProcessManager (PoisonPolicy (..), RejectedCommandPolicy (..), WorkerOptions (..))
import Shibuya.Core.Ack (RetryDelay (..))

-- The define-once ProcessManager name (hole-kind 5: referenced, never retyped).
hospitalSurgeProcessName :: Text
hospitalSurgeProcessName = "hospital-surge"

-- The validated saga stream category (hole-kind 5: referenced, never retyped).
-- Saga streams are '<category>-<correlationId>' via Keiro.Stream.entityStream.
-- categoryUnsafe is safe here because keiro-dsl check proved the literal legal.
hospitalSurgeCategory :: Stream.StreamCategory a
hospitalSurgeCategory = Stream.categoryUnsafe "hospitalSurge"

-- Node-level worker policy lowered from the spec. Pass this value to
-- Keiro.ProcessManager.runProcessManagerWorkerWith.
hospitalSurgeProcessWorkerOptions :: WorkerOptions es msg
hospitalSurgeProcessWorkerOptions =
  WorkerOptions
    { poisonPolicy = PoisonHalt
    , rejectedCommandPolicy = RejectedHalt
    , transientRetryDelay = RetryDelay 5 -- matches defaultWorkerOptions; runtime tuning
    , metrics = Nothing                  -- runtime configuration; install at call site
    }

-- The deterministic timer-request builder: id derived from the correlation
-- key (hole-kind 1), processManagerName referenced, payload from the spec.
-- (timer id derived as uuidv5 of "hospital-surge-timer:" <> correlationId)
hospitalSurgeTimerRequest :: Text -> UTCTime -> TimerRequest
hospitalSurgeTimerRequest correlationId fireAtTime =
  TimerRequest
    { timerId = TimerId (namedUuid ("hospital-surge-timer:" <> correlationId))
    , processManagerName = hospitalSurgeProcessName
    , correlationId = correlationId
    , fireAt = fireAtTime
    , payload = object [ "kind" .= ("hospital-surge-follow-up" :: Value) ]
    }

-- The timer-fire disposition table (hole-kind 2), derived from the spec.
-- on-reject => Fired is the benign inversion.
-- A duplicate append reaches on-error unless it is confirmed against the
-- target stream. Use Keiro.ProcessManager.confirmBenignDuplicate:
--   StreamName -> EventId -> CommandError -> Eff es Bool
-- Fold True into the duplicate result and surface False as the failure.
hospitalSurgeFireOutcome :: Either CommandError a -> Maybe ()
hospitalSurgeFireOutcome result = case result of
  Right{} -> Just ()  -- Fired
  Left CommandRejected -> Just ()  -- Fired
  Left (CommandAmbiguous _) -> Nothing  -- Retry  -- explicit definition-bug arm
  Left{} -> Nothing  -- Retry

-- max-attempts = 5, dead-letter = "surge timer exceeded ceiling"
-- (the timer worker must pass Just 5 to runTimerWorkerWith, never the
--  defaultTimerWorkerOptions Nothing ceiling that retries forever).

-- deterministic v5 UUID of a correlation-keyed string (hole-kind 1).
namedUuid :: Text -> UUID
namedUuid v = UUID.V5.generateNamed UUID.V5.namespaceURL (map (fromIntegral . fromEnum) (T.unpack v))