]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/GraphQL/Annuaire.hs
[FIX] Limit Size
[gargantext.git] / src / Gargantext / API / GraphQL / Annuaire.hs
1 {-# LANGUAGE DeriveAnyClass #-}
2 {-# LANGUAGE DuplicateRecordFields #-}
3
4 module Gargantext.API.GraphQL.Annuaire where
5
6 import Control.Lens
7 import Data.Morpheus.Types
8 ( GQLType
9 , Resolver
10 , QUERY
11 , lift
12 )
13 import Data.Proxy
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
18 ( HyperdataContact
19 , ContactWho
20 , cw_firstName
21 , cw_lastName
22 , hc_who)
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)
29
30 data AnnuaireContact = AnnuaireContact
31 { ac_id :: Int
32 , ac_firstName :: Maybe Text
33 , ac_lastName :: Maybe Text
34 }
35 deriving (Generic, GQLType, Show)
36
37 -- | Arguments to the "user info" query.
38 data AnnuaireContactArgs
39 = AnnuaireContactArgs
40 { contact_id :: Int
41 } deriving (Generic, GQLType)
42
43 type GqlM e env = Resolver QUERY e (GargM env GargError)
44
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
50
51 -- | Inner function to fetch the user from DB.
52 dbAnnuaireContacts
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)]
62
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 }
68
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
75