Maintainer : team@gargantext.org
Stability : experimental
Portability : POSIX
+
-}
-{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
-{-# OPTIONS_GHC -fno-warn-orphans #-}
module Gargantext.Database.Action.User
- where
+ where
+import Data.Text (Text)
+import Gargantext.Core.Types.Individu (User(..))
+import Gargantext.Database.Admin.Types.Node
+import Gargantext.Database.Prelude (Cmd)
+import Gargantext.Database.Query.Table.Node
import Gargantext.Database.Query.Table.User
-import Gargantext.Core.Types.Individu
-import Gargantext.Database.Prelude
+import Gargantext.Database.Query.Table.Node.Error
+import Gargantext.Database.Schema.Node
import Gargantext.Prelude
-import Gargantext.Database.Query.Table.Node.Error (HasNodeError(..))
-import Gargantext.Database.Action.Flow (getOrMkRoot)
+------------------------------------------------------------------------
+getUserLightWithId :: HasNodeError err => Int -> Cmd err UserLight
+getUserLightWithId i = do
+ candidates <- head <$> getUsersWithId i
+ case candidates of
+ Nothing -> nodeError NoUserFound
+ Just u -> pure u
+
+getUserLightDB :: HasNodeError err => User -> Cmd err UserLight
+getUserLightDB u = do
+ userId <- getUserId u
+ userLight <- getUserLightWithId userId
+ pure userLight
+
+------------------------------------------------------------------------
+getUserId :: HasNodeError err
+ => User
+ -> Cmd err UserId
+getUserId u = do
+ maybeUser <- getUserId' u
+ case maybeUser of
+ Nothing -> nodeError NoUserFound
+ Just u' -> pure u'
+
+getUserId' :: HasNodeError err
+ => User
+ -> Cmd err (Maybe UserId)
+getUserId' (UserDBId uid) = pure (Just uid)
+getUserId' (RootId rid) = do
+ n <- getNode rid
+ pure $ Just $ _node_user_id n
+getUserId' (UserName u ) = do
+ muser <- getUser u
+ case muser of
+ Just user -> pure $ Just $ userLight_id user
+ Nothing -> pure Nothing
+getUserId' UserPublic = pure Nothing
-mkUser :: HasNodeError err => NewUser GargPassword -> Cmd err Int64
-mkUser u = mkUsers [u]
+------------------------------------------------------------------------
+-- | Username = Text
+-- UserName is User
+-- that is confusing, we should change this
+type Username = Text
+getUsername :: HasNodeError err
+ => User
+ -> Cmd err Username
+getUsername (UserName u) = pure u
+getUsername (UserDBId i) = do
+ users <- getUsersWithId i
+ case head users of
+ Just u -> pure $ userLight_username u
+ Nothing -> nodeError $ NodeError "G.D.A.U.getUserName: User not found with that id"
+getUsername (RootId rid) = do
+ n <- getNode rid
+ getUsername (UserDBId $ _node_user_id n)
+getUsername UserPublic = pure "UserPublic"
-mkUsers :: HasNodeError err => [NewUser GargPassword] -> Cmd err Int64
-mkUsers us = do
- us' <- liftBase $ mapM toUserHash us
- r <- insertUsers $ map toUserWrite us'
- _ <- mapM getOrMkRoot $ map (\u -> UserName (_nu_username u)) us
- pure r
+--------------------------------------------------------------------------
+-- getRootId is in Gargantext.Database.Query.Tree.Root
--- | TODO
-rmUser :: HasNodeError err => User -> Cmd err Int64
-rmUser = undefined