content: module Fission.Web.Server.App.Domain.Associator
    ( module Fission.Web.Server.App.Domain.Associator.Class
    , associateWithFallback
    ) where
  
  import           Database.Persist.Types
  
  import           Fission.Prelude
  
  import           Fission.URL.Types
  
  import qualified Fission.Web.Server.App.Domain.Initializer      as AppDomain
  import           Fission.Web.Server.Models
  
  -- Re-export
  
  import           Fission.Web.Server.App.Domain.Associator.Class
  
  associateWithFallback ::
    ( MonadIO               m
    , Associator            m
    , AppDomain.Initializer m
    )
    => UserId
    -> AppId
    -> Maybe Subdomain -- ^ Optional subdomain, falls back to autogenerated
    -> UTCTime
    -> m (Either Errors' Subdomain)
  associateWithFallback userId appId Nothing now = do
    subdomain <- liftIO (generate arbitrary)
    associateWithFallback userId appId (Just subdomain) now
  
  associateWithFallback userId appId sub@(Just subdomain) now = do
    defaultDomainName <- AppDomain.initial
    associate userId appId Active defaultDomainName sub now
      <&> fmap \_ -> subdomain
  