2 Module : Gargantext.API.Node.Contact
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 {-# OPTIONS_GHC -fno-warn-orphans #-}
14 {-# LANGUAGE ConstraintKinds #-}
15 {-# LANGUAGE KindSignatures #-}
16 {-# LANGUAGE ScopedTypeVariables #-}
17 {-# LANGUAGE TemplateHaskell #-}
18 {-# LANGUAGE TypeFamilies #-}
19 {-# LANGUAGE TypeOperators #-}
22 module Gargantext.API.Node.Contact
26 import Data.Either (Either(Right))
27 import Data.Maybe (Maybe(..))
29 import Data.Text (Text)
30 import GHC.Generics (Generic)
32 import Servant.Job.Async (JobFunction(..), serveJobsAPI)
33 import Test.QuickCheck (elements)
34 import Test.QuickCheck.Arbitrary
36 import Gargantext.API.Admin.Orchestrator.Types (JobLog(..), AsyncJobs)
37 import Gargantext.API.Admin.Types (HasSettings)
38 import Gargantext.API.Node
39 import Gargantext.API.Prelude (GargServer, simuLogs)
40 import Gargantext.Core (Lang(..))
41 import Gargantext.Core.Text.Terms (TermType(..))
42 import Gargantext.Core.Types.Individu (User(..))
43 import Gargantext.Database.Action.Flow (flow)
44 import Gargantext.Database.Action.Flow.Types (FlowCmdM)
45 import Gargantext.Database.Admin.Types.Hyperdata (HyperdataAnnuaire(..), HyperdataContact)
46 import Gargantext.Database.Admin.Types.Hyperdata.Contact (hyperdataContact)
47 import Gargantext.Database.Admin.Types.Node
48 import Gargantext.Prelude (($), liftBase, (.), printDebug, pure)
50 ------------------------------------------------------------------------
51 type API = "contact" :> Summary "Contact endpoint"
53 :<|> Capture "contact_id" NodeId
54 :> NodeNodeAPI HyperdataContact
57 api :: UserId -> CorpusId -> GargServer API
58 api uid cid = (api_async (RootId (NodeId uid)) cid)
59 :<|> (nodeNodeAPI (Proxy :: Proxy HyperdataContact) uid cid)
61 type API_Async = AsyncJobs JobLog '[JSON] AddContactParams JobLog
62 ------------------------------------------------------------------------
63 data AddContactParams = AddContactParams { firstname :: !Text, lastname :: !Text }
64 | AddContactParamsAdvanced { firstname :: !Text
66 -- TODO add others fields
70 ----------------------------------------------------------------------
71 api_async :: User -> NodeId -> GargServer API_Async
74 JobFunction (\p log ->
77 printDebug "addContact" x
79 in addContact u nId p (liftBase . log')
82 addContact :: (HasSettings env, FlowCmdM env err m)
88 addContact u nId (AddContactParams fn ln) logStatus = do
90 logStatus JobLog { _scst_succeeded = Just 1
91 , _scst_failed = Just 0
92 , _scst_remaining = Just 1
93 , _scst_events = Just []
95 _ <- flow (Nothing :: Maybe HyperdataAnnuaire) u (Right [nId]) (Multi EN) [[hyperdataContact fn ln]]
97 pure JobLog { _scst_succeeded = Just 2
98 , _scst_failed = Just 0
99 , _scst_remaining = Just 0
100 , _scst_events = Just []
102 addContact _uId _nId _p logStatus = do
103 simuLogs logStatus 10
105 ------------------------------------------------------------------------
106 -- TODO unPrefix "pn_" FromJSON, ToJSON, ToSchema, adapt frontend.
107 instance FromJSON AddContactParams where
108 parseJSON = genericParseJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
110 instance ToJSON AddContactParams where
111 toJSON = genericToJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
113 instance ToSchema AddContactParams
114 instance Arbitrary AddContactParams where
115 arbitrary = elements [AddContactParams "Pierre" "Dupont"]
117 ------------------------------------------------------------------------