]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/GraphQL/Annuaire.hs
[DOC] Database Schema SVG file. Thank you Seeg for having generated it with Wolfram
[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.Database.Admin.Types.Hyperdata.Contact
17 ( HyperdataContact
18 , ContactWho
19 , cw_firstName
20 , cw_lastName
21 , hc_who, ContactWhere, hc_where, cw_organization, cw_labTeamDepts, cw_role, cw_office, cw_country, cw_city, cw_touch, ct_mail, ct_phone, ct_url, hc_title, hc_source)
22 import Gargantext.Database.Admin.Types.Node (NodeId(..))
23 import Gargantext.Database.Prelude (CmdCommon)
24 import Gargantext.Database.Query.Table.Context (getContextWith)
25 import Gargantext.Database.Schema.Node (node_hyperdata)
26 import Gargantext.Prelude
27 import GHC.Generics (Generic)
28
29 data AnnuaireContact = AnnuaireContact
30 { ac_title :: !(Maybe Text)
31 , ac_source :: !(Maybe Text)
32 , ac_id :: !Int
33 , ac_firstName :: !(Maybe Text)
34 , ac_lastName :: !(Maybe Text)
35 , ac_labTeamDepts :: ![Text]
36 , ac_organization :: ![Text]
37 , ac_role :: !(Maybe Text)
38 , ac_office :: !(Maybe Text)
39 , ac_country :: !(Maybe Text)
40 , ac_city :: !(Maybe Text)
41 , ac_touchMail :: !(Maybe Text)
42 , ac_touchPhone :: !(Maybe Text)
43 , ac_touchUrl :: !(Maybe Text)
44 }
45 deriving (Generic, GQLType, Show)
46
47 -- | Arguments to the "user info" query.
48 data AnnuaireContactArgs
49 = AnnuaireContactArgs
50 { contact_id :: Int
51 } deriving (Generic, GQLType)
52
53 type GqlM e env = Resolver QUERY e (GargM env GargError)
54
55 -- | Function to resolve user from a query.
56 resolveAnnuaireContacts
57 :: (CmdCommon env)
58 => AnnuaireContactArgs -> GqlM e env [AnnuaireContact]
59 resolveAnnuaireContacts AnnuaireContactArgs { contact_id } = dbAnnuaireContacts contact_id
60
61 -- | Inner function to fetch the user from DB.
62 dbAnnuaireContacts
63 :: CmdCommon env
64 => Int -> GqlM e env [AnnuaireContact]
65 dbAnnuaireContacts contact_id = do
66 -- lift $ printDebug "[dbUsers]" user_id
67 -- user <- getUsersWithId user_id
68 -- hyperdata <- getUserHyperdata user_id
69 -- lift (map toUser <$> zip user hyperdata)
70 c <- lift $ getContextWith (NodeId contact_id) (Proxy :: Proxy HyperdataContact)
71 pure [toAnnuaireContact (contact_id, c ^. node_hyperdata)]
72
73 toAnnuaireContact :: (Int, HyperdataContact) -> AnnuaireContact
74 toAnnuaireContact (c_id, c_hyperdata) =
75 AnnuaireContact { ac_title = c_hyperdata ^. ac_titleL
76 , ac_source = c_hyperdata ^. ac_sourceL
77 , ac_id = c_id
78 , ac_firstName = c_hyperdata ^. ac_firstNameL
79 , ac_lastName = c_hyperdata ^. ac_lastNameL
80 , ac_organization = c_hyperdata ^. ac_organizationL
81 , ac_labTeamDepts = c_hyperdata ^. ac_labTeamDeptsL
82 , ac_role = c_hyperdata ^. ac_roleL
83 , ac_office = c_hyperdata ^. ac_officeL
84 , ac_country = c_hyperdata ^. ac_countryL
85 , ac_city = c_hyperdata ^. ac_cityL
86 , ac_touchMail = c_hyperdata ^. ac_touchMailL
87 , ac_touchPhone = c_hyperdata ^. ac_touchPhoneL
88 , ac_touchUrl = c_hyperdata ^. ac_touchUrlL }
89
90 ac_titleL :: Traversal' HyperdataContact (Maybe Text)
91 ac_titleL = hc_title
92 ac_sourceL :: Traversal' HyperdataContact (Maybe Text)
93 ac_sourceL = hc_source
94 contactWhoL :: Traversal' HyperdataContact ContactWho
95 contactWhoL = hc_who . _Just
96 ac_firstNameL :: Traversal' HyperdataContact (Maybe Text)
97 ac_firstNameL = contactWhoL . cw_firstName
98 ac_lastNameL :: Traversal' HyperdataContact (Maybe Text)
99 ac_lastNameL = contactWhoL . cw_lastName
100 contactWhereL :: Traversal' HyperdataContact ContactWhere
101 contactWhereL = hc_where . ix 0
102 ac_organizationL :: Traversal' HyperdataContact [Text]
103 ac_organizationL = contactWhereL . cw_organization
104 ac_labTeamDeptsL :: Traversal' HyperdataContact [Text]
105 ac_labTeamDeptsL = contactWhereL . cw_labTeamDepts
106 ac_roleL :: Traversal' HyperdataContact (Maybe Text)
107 ac_roleL = contactWhereL . cw_role
108 ac_officeL :: Traversal' HyperdataContact (Maybe Text)
109 ac_officeL = contactWhereL . cw_office
110 ac_countryL :: Traversal' HyperdataContact (Maybe Text)
111 ac_countryL = contactWhereL . cw_country
112 ac_cityL :: Traversal' HyperdataContact (Maybe Text)
113 ac_cityL = contactWhereL . cw_city
114 ac_touchMailL :: Traversal' HyperdataContact (Maybe Text)
115 ac_touchMailL = contactWhereL . cw_touch . _Just . ct_mail
116 ac_touchPhoneL :: Traversal' HyperdataContact (Maybe Text)
117 ac_touchPhoneL = contactWhereL . cw_touch . _Just . ct_phone
118 ac_touchUrlL :: Traversal' HyperdataContact (Maybe Text)
119 ac_touchUrlL = contactWhereL . cw_touch . _Just . ct_url