1 {-# LANGUAGE DeriveAnyClass #-}
2 {-# LANGUAGE DuplicateRecordFields #-}
4 module Gargantext.API.GraphQL.Annuaire where
7 import Data.Morpheus.Types
14 import Data.Text (Text)
15 import Gargantext.API.Prelude (GargM, GargError)
16 import Gargantext.Core.Mail.Types (HasMail)
17 import Gargantext.Database.Admin.Types.Hyperdata.Contact
23 import Gargantext.Database.Admin.Types.Node (NodeId(..))
24 import Gargantext.Database.Prelude (HasConnectionPool, HasConfig)
25 import Gargantext.Database.Query.Table.Context (getContextWith)
26 import Gargantext.Database.Schema.Node (node_hyperdata)
27 import Gargantext.Prelude
28 import GHC.Generics (Generic)
30 data AnnuaireContact = AnnuaireContact
32 , ac_firstName :: Maybe Text
33 , ac_lastName :: Maybe Text
35 deriving (Generic, GQLType, Show)
37 -- | Arguments to the "user info" query.
38 data AnnuaireContactArgs
41 } deriving (Generic, GQLType)
43 type GqlM e env = Resolver QUERY e (GargM env GargError)
45 -- | Function to resolve user from a query.
46 resolveAnnuaireContacts
47 :: (HasConnectionPool env, HasConfig env, HasMail env)
48 => AnnuaireContactArgs -> GqlM e env [AnnuaireContact]
49 resolveAnnuaireContacts AnnuaireContactArgs { contact_id } = dbAnnuaireContacts contact_id
51 -- | Inner function to fetch the user from DB.
53 :: (HasConnectionPool env, HasConfig env, HasMail env)
54 => Int -> GqlM e env [AnnuaireContact]
55 dbAnnuaireContacts contact_id = do
56 -- lift $ printDebug "[dbUsers]" user_id
57 -- user <- getUsersWithId user_id
58 -- hyperdata <- getUserHyperdata user_id
59 -- lift (map toUser <$> zip user hyperdata)
60 c <- lift $ getContextWith (NodeId contact_id) (Proxy :: Proxy HyperdataContact)
61 pure [toAnnuaireContact (contact_id, c ^. node_hyperdata)]
63 toAnnuaireContact :: (Int, HyperdataContact) -> AnnuaireContact
64 toAnnuaireContact (c_id, c_hyperdata) =
65 AnnuaireContact { ac_id = c_id
66 , ac_firstName = c_hyperdata ^. ac_firstNameL
67 , ac_lastName = c_hyperdata ^. ac_lastNameL }
69 contactWhoL :: Traversal' HyperdataContact ContactWho
70 contactWhoL = hc_who . _Just
71 ac_firstNameL :: Traversal' HyperdataContact (Maybe Text)
72 ac_firstNameL = contactWhoL . cw_firstName
73 ac_lastNameL :: Traversal' HyperdataContact (Maybe Text)
74 ac_lastNameL = contactWhoL . cw_lastName