Contact.hs 4.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
{-|
Module      : Gargantext.Database.Node.Contact
Description : Update Node in Database (Postgres)
Copyright   : (c) CNRS, 2017-Present
License     : AGPL + CECILL v3
Maintainer  : team@gargantext.org
Stability   : experimental
Portability : POSIX

-}

{-# LANGUAGE DeriveGeneric          #-}
{-# LANGUAGE NoImplicitPrelude      #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE OverloadedStrings      #-}
{-# LANGUAGE FlexibleInstances      #-}
{-# LANGUAGE MultiParamTypeClasses  #-}
{-# LANGUAGE TemplateHaskell        #-}

20
module Gargantext.Database.Node.Contact
21 22
  where

23
import Control.Lens (makeLenses)
24
import Data.Aeson.TH (deriveJSON)
25
import Data.Swagger (ToSchema)
26
import Data.Text (Text)
27
import Data.Time (UTCTime)
28 29
import Database.PostgreSQL.Simple.FromField (FromField, fromField)
import GHC.Generics (Generic)
30
import Gargantext.Core.Utils.Prefix (unPrefix)
31
import Gargantext.Database.Schema.Node (NodeWrite, Name, node)
32
import Gargantext.Database.Types.Node (Node,Hyperdata,NodeType(..), UserId, AnnuaireId)
33 34 35
import Gargantext.Database.Utils (fromField')
import Gargantext.Prelude
import Opaleye (QueryRunnerColumnDefault, queryRunnerColumnDefault, PGJsonb, fieldQueryRunnerColumn)
36 37
import Test.QuickCheck (elements)
import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
38 39 40 41 42 43

------------------------------------------------------------------------

type NodeContact  = Node HyperdataContact

data HyperdataContact =
44 45
     HyperdataContact { _hc_bdd    :: Maybe Text           -- ID of Database source
                      , _hc_who    :: Maybe ContactWho
46
                      , _hc_where  :: [ContactWhere]
47 48 49 50 51
                      , _hc_title  :: Maybe Text -- TODO remove (only demo)
                      , _hc_source :: Maybe Text -- TODO remove (only demo)
                      , _hc_lastValidation  :: Maybe Text
                      , _hc_uniqIdBdd       :: Maybe Text
                      , _hc_uniqId          :: Maybe Text
52 53 54

  } deriving (Eq, Show, Generic)

55
-- TOD0 contact metadata (Type is too flat)
56 57 58 59 60 61
data ContactMetaData =
     ContactMetaData { _cm_bdd :: Maybe Text
                     , _cm_lastValidation  :: Maybe Text
  } deriving (Eq, Show, Generic)


62
arbitraryHyperdataContact :: HyperdataContact
63
arbitraryHyperdataContact = HyperdataContact Nothing Nothing []
64
                                             Nothing Nothing Nothing
65
                                             Nothing Nothing
66 67

data ContactWho = 
68
     ContactWho { _cw_id          :: Maybe Text
69 70
                , _cw_firstName   :: Maybe Text
                , _cw_lastName    :: Maybe Text
71 72
                , _cw_keywords :: [Text]
                , _cw_freetags :: [Text]
73 74 75
  } deriving (Eq, Show, Generic)

data ContactWhere =
76 77
     ContactWhere { _cw_organization :: [Text]
                  , _cw_labTeamDepts :: [Text]
78
                  
79
                  , _cw_role         :: Maybe Text
80
                  
81 82 83
                  , _cw_office       :: Maybe Text
                  , _cw_country      :: Maybe Text
                  , _cw_city         :: Maybe Text
84
                  
85
                  , _cw_touch        :: Maybe ContactTouch
86 87 88
                  
                  , _cw_entry        :: Maybe UTCTime
                  , _cw_exit         :: Maybe UTCTime
89 90 91 92 93 94 95 96 97 98
  } deriving (Eq, Show, Generic)

data ContactTouch =
     ContactTouch { _ct_mail      :: Maybe Text
                  , _ct_phone     :: Maybe Text
                  , _ct_url       :: Maybe Text
  } deriving (Eq, Show, Generic)


nodeContactW :: Maybe Name -> Maybe HyperdataContact
99
             -> AnnuaireId -> UserId -> NodeWrite
100 101 102 103 104 105 106
nodeContactW maybeName maybeContact aId = 
  node NodeContact name contact (Just aId)
    where
      name    = maybe "Contact" identity maybeName
      contact = maybe arbitraryHyperdataContact identity maybeContact


107
-- | Main instances of Contact
108 109 110 111 112 113
instance ToSchema HyperdataContact
instance ToSchema ContactWho
instance ToSchema ContactWhere
instance ToSchema ContactTouch

instance Arbitrary HyperdataContact where
114
  arbitrary = elements [HyperdataContact Nothing Nothing [] Nothing Nothing Nothing Nothing Nothing]
115

116

117
-- | Specific Gargantext instance
118
instance Hyperdata HyperdataContact
119 120

-- | Database (Posgresql-simple instance)
121 122
instance FromField HyperdataContact where
  fromField = fromField'
123 124

-- | Database (Opaleye instance)
125 126 127
instance QueryRunnerColumnDefault PGJsonb HyperdataContact   where
  queryRunnerColumnDefault = fieldQueryRunnerColumn

128
-- | All lenses
129 130 131
makeLenses ''ContactWho
makeLenses ''ContactWhere
makeLenses ''ContactTouch
132
makeLenses ''ContactMetaData
133 134
makeLenses ''HyperdataContact

135
-- | All Json instances
136 137 138
$(deriveJSON (unPrefix "_cw_") ''ContactWho)
$(deriveJSON (unPrefix "_cw_") ''ContactWhere)
$(deriveJSON (unPrefix "_ct_") ''ContactTouch)
139
$(deriveJSON (unPrefix "_cm_") ''ContactMetaData)
140
$(deriveJSON (unPrefix "_hc_") ''HyperdataContact)