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 {-# LANGUAGE TemplateHaskell #-}
13 {-# LANGUAGE TypeOperators #-}
14 {-# OPTIONS_GHC -fno-warn-orphans #-}
16 module Gargantext.API.Node.Contact
20 import Data.Either (Either(Right))
21 import Data.Maybe (Maybe(..))
23 import Data.Text (Text)
24 import GHC.Generics (Generic)
25 import Gargantext.API.Admin.Orchestrator.Types (JobLog(..))
26 import Gargantext.API.Admin.Settings (HasSettings)
27 import Gargantext.API.Node.Corpus.New (AsyncJobs)
28 import Gargantext.API.Prelude (GargServer, simuLogs)
29 import Gargantext.Core (Lang(..))
30 import Gargantext.Core.Types.Individu (User(..))
31 import Gargantext.Database.Action.Flow (flow)
32 import Gargantext.Database.Action.Flow.Types (FlowCmdM)
33 import Gargantext.Database.Admin.Types.Hyperdata (HyperdataAnnuaire(..))
34 import Gargantext.Database.Admin.Types.Hyperdata.Contact (hyperdataContact)
35 import Gargantext.Database.Admin.Types.Node
36 import Gargantext.Prelude (($), liftBase, (.), printDebug, pure)
37 import Gargantext.Text.Terms (TermType(..))
39 import Servant.Job.Async (JobFunction(..), serveJobsAPI)
40 import Test.QuickCheck (elements)
41 import Test.QuickCheck.Arbitrary
43 ------------------------------------------------------------------------
44 type API = Summary " Add Contact to Annuaire"
45 :> AsyncJobs JobLog '[JSON] AddContactParams JobLog
47 ------------------------------------------------------------------------
48 data AddContactParams = AddContactParams { firstname :: !Text, lastname :: !Text }
49 | AddContactParamsAdvanced { firstname :: !Text
51 -- TODO add others fields
55 ----------------------------------------------------------------------
56 api :: User -> NodeId -> GargServer API
59 JobFunction (\p log ->
62 printDebug "addContact" x
64 in addContact u nId p (liftBase . log')
67 addContact :: (HasSettings env, FlowCmdM env err m)
73 addContact u nId (AddContactParams fn ln) logStatus = do
75 logStatus JobLog { _scst_succeeded = Just 1
76 , _scst_failed = Just 0
77 , _scst_remaining = Just 1
78 , _scst_events = Just []
80 _ <- flow (Nothing :: Maybe HyperdataAnnuaire) u (Right [nId]) (Multi EN) [[hyperdataContact fn ln]]
82 pure JobLog { _scst_succeeded = Just 2
83 , _scst_failed = Just 0
84 , _scst_remaining = Just 0
85 , _scst_events = Just []
89 addContact _uId _nId _p logStatus = do
92 ------------------------------------------------------------------------
93 -- TODO unPrefix "pn_" FromJSON, ToJSON, ToSchema, adapt frontend.
94 instance FromJSON AddContactParams where
95 parseJSON = genericParseJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
97 instance ToJSON AddContactParams where
98 toJSON = genericToJSON (defaultOptions { sumEncoding = ObjectWithSingleField })
100 instance ToSchema AddContactParams
101 instance Arbitrary AddContactParams where
102 arbitrary = elements [AddContactParams "Pierre" "Dupont"]
104 ------------------------------------------------------------------------