2 Module : Gargantext.Database.Query.Table.User
3 Description : User Database management tools
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Functions to deal with users, database side.
13 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
14 {-# OPTIONS_GHC -fno-warn-orphans #-}
16 {-# LANGUAGE TemplateHaskell #-}
17 {-# LANGUAGE FlexibleContexts #-}
18 {-# LANGUAGE FlexibleInstances #-}
19 {-# LANGUAGE MultiParamTypeClasses #-}
20 {-# LANGUAGE FunctionalDependencies #-}
21 {-# LANGUAGE Arrows #-}
22 {-# LANGUAGE NoImplicitPrelude #-}
23 {-# LANGUAGE OverloadedStrings #-}
24 {-# LANGUAGE RankNTypes #-}
26 module Gargantext.Database.Query.Table.User
36 , module Gargantext.Database.Schema.User
40 import Control.Arrow (returnA)
41 import Data.Eq(Eq(..))
42 import Data.List (find)
43 import Data.Maybe (Maybe)
44 import Data.Text (Text)
45 import Data.Time (UTCTime)
46 import Gargantext.Core.Types.Individu
47 import Gargantext.Database.Schema.User
48 import Gargantext.Database.Admin.Utils
49 import Gargantext.Prelude
52 ------------------------------------------------------------------------
53 -- TODO: on conflict, nice message
54 insertUsers :: [UserWrite] -> Cmd err Int64
55 insertUsers us = mkCmd $ \c -> runInsert_ c insert
57 insert = Insert userTable us rCount Nothing
60 gargantextUser :: Username -> UserWrite
61 gargantextUser u = UserDB (Nothing) (pgStrictText "password")
62 (Nothing) (pgBool True) (pgStrictText u)
63 (pgStrictText "first_name")
64 (pgStrictText "last_name")
65 (pgStrictText "e@mail")
66 (pgBool True) (pgBool True) (Nothing)
68 insertUsersDemo :: Cmd err Int64
69 insertUsersDemo = insertUsers $ map (\u -> gargantextUser u) arbitraryUsername
71 ------------------------------------------------------------------
72 queryUserTable :: Query UserRead
73 queryUserTable = queryTable userTable
75 selectUsersLight :: Query UserRead
76 selectUsersLight = proc () -> do
77 row@(UserDB i _p _ll _is _un _fn _ln _m _iff _ive _dj) <- queryUserTable -< ()
79 --returnA -< User i p ll is un fn ln m iff ive dj
81 ------------------------------------------------------------------
82 -- | Select User with some parameters
83 -- Not optimized version
84 userWith :: (Eq a1, Foldable t) => (a -> a1) -> a1 -> t a -> Maybe a
85 userWith f t xs = find (\x -> f x == t) xs
87 -- | Select User with Username
88 userWithUsername :: Text -> [UserDB] -> Maybe UserDB
89 userWithUsername t xs = userWith user_username t xs
91 userWithId :: Int -> [UserDB] -> Maybe UserDB
92 userWithId t xs = userWith user_id t xs
94 userLightWithUsername :: Text -> [UserLight] -> Maybe UserLight
95 userLightWithUsername t xs = userWith userLight_username t xs
97 userLightWithId :: Int -> [UserLight] -> Maybe UserLight
98 userLightWithId t xs = userWith userLight_id t xs
101 instance QueryRunnerColumnDefault PGTimestamptz (Maybe UTCTime) where
102 queryRunnerColumnDefault = fieldQueryRunnerColumn
105 users :: Cmd err [UserDB]
106 users = runOpaQuery queryUserTable
108 usersLight :: Cmd err [UserLight]
109 usersLight = map toUserLight <$> users
111 getUser :: Username -> Cmd err (Maybe UserLight)
112 getUser u = userLightWithUsername u <$> usersLight