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 #-}
20 {-# LANGUAGE UndecidableInstances #-}
23 module Gargantext.API.Node.Contact
27 import Data.Either (Either(Right))
28 import Data.Maybe (Maybe(..))
30 import Data.Text (Text)
31 import GHC.Generics (Generic)
33 import Servant.Job.Async (JobFunction(..), serveJobsAPI)
34 import Test.QuickCheck (elements)
35 import Test.QuickCheck.Arbitrary
37 import Gargantext.API.Admin.Orchestrator.Types (JobLog(..), AsyncJobs)
38 import Gargantext.API.Admin.Settings (HasSettings)
39 import Gargantext.API.Node
40 import Gargantext.API.Prelude (GargServer, simuLogs)
41 import Gargantext.Core (Lang(..))
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)
49 import Gargantext.Text.Terms (TermType(..))
51 ------------------------------------------------------------------------
52 type API = "contact" :> Summary "Contact endpoint"
54 :<|> Capture "contact_id" NodeId
55 :> NodeNodeAPI HyperdataContact
58 api :: UserId -> CorpusId -> GargServer API
59 api uid cid = (api_async (RootId (NodeId uid)) cid)
60 :<|> (nodeNodeAPI (Proxy :: Proxy HyperdataContact) uid cid)
62 type API_Async = AsyncJobs JobLog '[JSON] AddContactParams JobLog
63 ------------------------------------------------------------------------
64 data AddContactParams = AddContactParams { firstname :: !Text, lastname :: !Text }
65 | AddContactParamsAdvanced { firstname :: !Text
67 -- TODO add others fields
71 ----------------------------------------------------------------------
72 api_async :: User -> NodeId -> GargServer API_Async
75 JobFunction (\p log ->
78 printDebug "addContact" x
80 in addContact u nId p (liftBase . log')
83 addContact :: (HasSettings env, FlowCmdM env err m)
89 addContact u nId (AddContactParams fn ln) logStatus = do
91 logStatus JobLog { _scst_succeeded = Just 1
92 , _scst_failed = Just 0
93 , _scst_remaining = Just 1
94 , _scst_events = Just []
96 _ <- flow (Nothing :: Maybe HyperdataAnnuaire) u (Right [nId]) (Multi EN) [[hyperdataContact fn ln]]
98 pure JobLog { _scst_succeeded = Just 2
99 , _scst_failed = Just 0
100 , _scst_remaining = Just 0
101 , _scst_events = Just []
105 addContact _uId _nId _p logStatus = do
106 simuLogs logStatus 10
108 ------------------------------------------------------------------------
109 -- TODO unPrefix "pn_" FromJSON, ToJSON, ToSchema, adapt frontend.
110 instance FromJSON AddContactParams where
111 parseJSON = genericParseJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
113 instance ToJSON AddContactParams where
114 toJSON = genericToJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
116 instance ToSchema AddContactParams
117 instance Arbitrary AddContactParams where
118 arbitrary = elements [AddContactParams "Pierre" "Dupont"]
120 ------------------------------------------------------------------------