]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/GraphQL/UserInfo.hs
Merge remote-tracking branch 'origin/119-dev-hal-notebook' into dev-merge
[gargantext.git] / src / Gargantext / API / GraphQL / UserInfo.hs
1 {-# LANGUAGE DeriveAnyClass #-}
2 {-# LANGUAGE DuplicateRecordFields #-}
3
4 module Gargantext.API.GraphQL.UserInfo where
5
6 import Control.Lens
7 import Data.Maybe (fromMaybe)
8 import Data.Morpheus.Types
9 ( GQLType
10 , Resolver
11 , ResolverM
12 , QUERY
13 , lift
14 )
15 import Data.Text (Text)
16 import qualified Data.Text as T
17 import Gargantext.API.Prelude (GargM, GargError)
18 import Gargantext.Core.Mail.Types (HasMail)
19 import Gargantext.Database.Admin.Types.Hyperdata
20 ( HyperdataUser(..)
21 , hc_source
22 , hc_title
23 , hu_shared)
24 import Gargantext.Database.Admin.Types.Hyperdata.Contact
25 ( HyperdataContact
26 , ContactWho
27 , ContactWhere
28 , cw_city
29 , cw_country
30 , cw_firstName
31 , cw_lastName
32 , cw_labTeamDepts
33 , cw_office
34 , cw_organization
35 , cw_role
36 , cw_touch
37 , ct_mail
38 , ct_phone
39 , hc_who
40 , hc_where)
41 import Gargantext.Database.Prelude (HasConnectionPool, HasConfig)
42 import Gargantext.Database.Query.Table.Node.UpdateOpaleye (updateHyperdata)
43 import Gargantext.Database.Query.Table.User (getUsersWithHyperdata, getUsersWithNodeHyperdata, updateUserEmail)
44 import Gargantext.Database.Schema.User (UserLight(..))
45 import Gargantext.Database.Schema.Node (node_id, node_hyperdata)
46 import Gargantext.Prelude
47 import GHC.Generics (Generic)
48
49 data UserInfo = UserInfo
50 { ui_id :: Int
51 , ui_username :: Text
52 , ui_email :: Text
53 , ui_title :: Maybe Text
54 , ui_source :: Maybe Text
55 , ui_cwFirstName :: Maybe Text
56 , ui_cwLastName :: Maybe Text
57 , ui_cwCity :: Maybe Text
58 , ui_cwCountry :: Maybe Text
59 , ui_cwOrganization :: [Text]
60 , ui_cwLabTeamDepts :: [Text]
61 , ui_cwOffice :: Maybe Text
62 , ui_cwRole :: Maybe Text
63 , ui_cwTouchPhone :: Maybe Text
64 , ui_cwTouchMail :: Maybe Text -- TODO: Remove. userLight_email should be used instead
65 }
66 deriving (Generic, GQLType, Show)
67
68 -- | Arguments to the "user info" query.
69 data UserInfoArgs
70 = UserInfoArgs
71 { user_id :: Int
72 } deriving (Generic, GQLType)
73
74 -- | Arguments to the "user info" mutation,
75 data UserInfoMArgs
76 = UserInfoMArgs
77 { ui_id :: Int
78 , ui_username :: Maybe Text
79 , ui_email :: Maybe Text
80 , ui_title :: Maybe Text
81 , ui_source :: Maybe Text
82 , ui_cwFirstName :: Maybe Text
83 , ui_cwLastName :: Maybe Text
84 , ui_cwCity :: Maybe Text
85 , ui_cwCountry :: Maybe Text
86 , ui_cwOrganization :: Maybe [Text]
87 , ui_cwLabTeamDepts :: Maybe [Text]
88 , ui_cwOffice :: Maybe Text
89 , ui_cwRole :: Maybe Text
90 , ui_cwTouchPhone :: Maybe Text
91 , ui_cwTouchMail :: Maybe Text
92 } deriving (Generic, GQLType)
93
94 type GqlM e env = Resolver QUERY e (GargM env GargError)
95
96 -- | Function to resolve user from a query.
97 resolveUserInfos
98 :: (HasConnectionPool env, HasConfig env, HasMail env)
99 => UserInfoArgs -> GqlM e env [UserInfo]
100 resolveUserInfos UserInfoArgs { user_id } = dbUsers user_id
101
102 -- | Mutation for user info
103 updateUserInfo
104 :: (HasConnectionPool env, HasConfig env, HasMail env)
105 => UserInfoMArgs -> ResolverM e (GargM env GargError) Int
106 updateUserInfo (UserInfoMArgs { ui_id, .. }) = do
107 -- lift $ printDebug "[updateUserInfo] ui_id" ui_id
108 users <- lift (getUsersWithNodeHyperdata ui_id)
109 case users of
110 [] -> panic $ "[updateUserInfo] User with id " <> (T.pack $ show ui_id) <> " doesn't exist."
111 ((UserLight { .. }, node_u):_) -> do
112 let u_hyperdata = node_u ^. node_hyperdata
113 -- lift $ printDebug "[updateUserInfo] u" u
114 let u_hyperdata' = uh ui_titleL ui_title $
115 uh ui_sourceL ui_source $
116 uh ui_cwFirstNameL ui_cwFirstName $
117 uh ui_cwLastNameL ui_cwLastName $
118 uh ui_cwCityL ui_cwCity $
119 uh ui_cwCountryL ui_cwCountry $
120 uh' ui_cwLabTeamDeptsL ui_cwLabTeamDepts $
121 uh' ui_cwOrganizationL ui_cwOrganization $
122 uh ui_cwOfficeL ui_cwOffice $
123 uh ui_cwRoleL ui_cwRole $
124 uh ui_cwTouchMailL ui_cwTouchMail $
125 uh ui_cwTouchPhoneL ui_cwTouchPhone $
126 u_hyperdata
127 -- NOTE: We have 1 username and 2 emails: userLight_email and ui_cwTouchMail
128 -- The userLight_email is more important: it is used for login and sending mail.
129 -- Therefore we update ui_cwTouchMail and userLight_email.
130 -- ui_cwTouchMail is to be removed in the future.
131 let u' = UserLight { userLight_id
132 , userLight_username
133 , userLight_email = fromMaybe userLight_email $ view ui_cwTouchMailL u_hyperdata
134 , userLight_password }
135 -- lift $ printDebug "[updateUserInfo] with firstName" u_hyperdata'
136 _ <- lift $ updateHyperdata (node_u ^. node_id) u_hyperdata'
137 _ <- lift $ updateUserEmail u'
138 --let _newUser = toUser (u, u_hyperdata')
139 pure 1
140 where
141 uh _ Nothing u_hyperdata = u_hyperdata
142 uh lens' (Just val) u_hyperdata = u_hyperdata & lens' .~ Just val
143 uh' _ Nothing u_hyperdata = u_hyperdata
144 uh' lens' (Just val) u_hyperdata = u_hyperdata & lens' .~ val
145
146 -- | Inner function to fetch the user from DB.
147 dbUsers
148 :: (HasConnectionPool env, HasConfig env, HasMail env)
149 => Int -> GqlM e env [UserInfo]
150 dbUsers user_id = do
151 -- lift $ printDebug "[dbUsers]" user_id
152 -- user <- getUsersWithId user_id
153 -- hyperdata <- getUserHyperdata user_id
154 -- lift (map toUser <$> zip user hyperdata)
155 lift (map toUser <$> (getUsersWithHyperdata user_id))
156
157 toUser :: (UserLight, HyperdataUser) -> UserInfo
158 toUser (UserLight { .. }, u_hyperdata) =
159 UserInfo { ui_id = userLight_id
160 , ui_username = userLight_username
161 , ui_email = userLight_email
162 , ui_title = u_hyperdata ^. ui_titleL
163 , ui_source = u_hyperdata ^. ui_sourceL
164 , ui_cwFirstName = u_hyperdata ^. ui_cwFirstNameL
165 , ui_cwLastName = u_hyperdata ^. ui_cwLastNameL
166 , ui_cwCity = u_hyperdata ^. ui_cwCityL
167 , ui_cwCountry = u_hyperdata ^. ui_cwCountryL
168 , ui_cwLabTeamDepts = u_hyperdata ^. ui_cwLabTeamDeptsL
169 , ui_cwOrganization = u_hyperdata ^. ui_cwOrganizationL
170 , ui_cwOffice = u_hyperdata ^. ui_cwOfficeL
171 , ui_cwRole = u_hyperdata ^. ui_cwRoleL
172 --, ui_cwTouchMail = u_hyperdata ^. ui_cwTouchMailL
173 , ui_cwTouchMail = Just userLight_email
174 , ui_cwTouchPhone = u_hyperdata ^. ui_cwTouchPhoneL }
175
176 sharedL :: Traversal' HyperdataUser HyperdataContact
177 sharedL = hu_shared . _Just
178 ui_titleL :: Traversal' HyperdataUser (Maybe Text)
179 ui_titleL = sharedL . hc_title
180 ui_sourceL :: Traversal' HyperdataUser (Maybe Text)
181 ui_sourceL = sharedL . hc_source
182 contactWhoL :: Traversal' HyperdataUser ContactWho
183 contactWhoL = sharedL . hc_who . _Just
184 ui_cwFirstNameL :: Traversal' HyperdataUser (Maybe Text)
185 ui_cwFirstNameL = contactWhoL . cw_firstName
186 ui_cwLastNameL :: Traversal' HyperdataUser (Maybe Text)
187 ui_cwLastNameL = contactWhoL . cw_lastName
188 contactWhereL :: Traversal' HyperdataUser ContactWhere
189 contactWhereL = sharedL . hc_where . (ix 0)
190 ui_cwCityL :: Traversal' HyperdataUser (Maybe Text)
191 ui_cwCityL = contactWhereL . cw_city
192 ui_cwCountryL :: Traversal' HyperdataUser (Maybe Text)
193 ui_cwCountryL = contactWhereL . cw_country
194 ui_cwLabTeamDeptsL :: Traversal' HyperdataUser [Text]
195 ui_cwLabTeamDeptsL = hu_shared . _Just . (hc_where . (ix 0) . cw_labTeamDepts)
196 ui_cwOrganizationL :: Traversal' HyperdataUser [Text]
197 ui_cwOrganizationL = hu_shared . _Just . (hc_where . (ix 0) . cw_organization)
198 ui_cwOfficeL :: Traversal' HyperdataUser (Maybe Text)
199 ui_cwOfficeL = contactWhereL . cw_office
200 ui_cwRoleL :: Traversal' HyperdataUser (Maybe Text)
201 ui_cwRoleL = contactWhereL . cw_role
202 ui_cwTouchMailL :: Traversal' HyperdataUser (Maybe Text)
203 ui_cwTouchMailL = hu_shared . _Just . (hc_where . (ix 0) . cw_touch . _Just . ct_mail)
204 --ui_cwTouchMailL = contactWhereL . cw_touch . _Just . ct_mail
205 ui_cwTouchPhoneL :: Traversal' HyperdataUser (Maybe Text)
206 ui_cwTouchPhoneL = hu_shared . _Just . (hc_where . (ix 0) . cw_touch . _Just . ct_phone)
207 --ui_cwTouchPhoneL = contactWhereL . cw_touch . _Just . ct_phone