2 Module : Gargantext.Database.Node.Contact
3 Description : Update Node in Database (Postgres)
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 {-# LANGUAGE DeriveGeneric #-}
13 {-# LANGUAGE NoImplicitPrelude #-}
14 {-# LANGUAGE FunctionalDependencies #-}
15 {-# LANGUAGE OverloadedStrings #-}
16 {-# LANGUAGE FlexibleInstances #-}
17 {-# LANGUAGE MultiParamTypeClasses #-}
18 {-# LANGUAGE TemplateHaskell #-}
20 module Gargantext.Database.Node.Contact
23 import Control.Lens (makeLenses)
24 import Data.Time.Segment (jour)
25 import Data.Aeson.TH (deriveJSON)
26 import Data.Swagger (ToSchema(..), genericDeclareNamedSchema)
27 import Data.Text (Text)
28 import Data.Time (UTCTime)
29 import Database.PostgreSQL.Simple.FromField (FromField, fromField)
30 import GHC.Generics (Generic)
31 import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger)
32 import Gargantext.Database.Types.Node (Node,Hyperdata)
33 import Gargantext.Database.Utils (fromField')
34 import Gargantext.Prelude
35 import Opaleye (QueryRunnerColumnDefault, queryRunnerColumnDefault, PGJsonb, fieldQueryRunnerColumn)
36 import Test.QuickCheck (elements)
37 import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
39 ------------------------------------------------------------------------
41 type NodeContact = Node HyperdataContact
43 data HyperdataContact =
44 HyperdataContact { _hc_bdd :: Maybe Text -- ID of Database source
45 , _hc_who :: Maybe ContactWho
46 , _hc_where :: [ContactWhere]
47 , _hc_title :: Maybe Text -- TODO remove (only demo)
48 , _hc_source :: Maybe Text -- TODO remove (only demo)
49 , _hc_lastValidation :: Maybe Text -- TODO UTCTime
50 , _hc_uniqIdBdd :: Maybe Text
51 , _hc_uniqId :: Maybe Text
53 } deriving (Eq, Show, Generic)
56 fake_HyperdataContact :: HyperdataContact
57 fake_HyperdataContact = HyperdataContact (Just "bdd")
58 (Just fake_ContactWho)
62 (Just "TODO lastValidation date")
63 (Just "DO NOT expose this")
64 (Just "DO NOT expose this")
67 -- TOD0 contact metadata (Type is too flat)
68 data ContactMetaData =
69 ContactMetaData { _cm_bdd :: Maybe Text
70 , _cm_lastValidation :: Maybe Text -- TODO UTCTIME
71 } deriving (Eq, Show, Generic)
73 fake_ContactMetaData :: ContactMetaData
74 fake_ContactMetaData = ContactMetaData (Just "bdd") (Just "TODO UTCTime")
76 arbitraryHyperdataContact :: HyperdataContact
77 arbitraryHyperdataContact = HyperdataContact Nothing Nothing []
78 Nothing Nothing Nothing
83 ContactWho { _cw_id :: Maybe Text
84 , _cw_firstName :: Maybe Text
85 , _cw_lastName :: Maybe Text
86 , _cw_keywords :: [Text]
87 , _cw_freetags :: [Text]
88 } deriving (Eq, Show, Generic)
90 fake_ContactWho :: ContactWho
91 fake_ContactWho = ContactWho (Just "123123")
98 ContactWhere { _cw_organization :: [Text]
99 , _cw_labTeamDepts :: [Text]
101 , _cw_role :: Maybe Text
103 , _cw_office :: Maybe Text
104 , _cw_country :: Maybe Text
105 , _cw_city :: Maybe Text
107 , _cw_touch :: Maybe ContactTouch
109 , _cw_entry :: Maybe UTCTime
110 , _cw_exit :: Maybe UTCTime
111 } deriving (Eq, Show, Generic)
113 fake_ContactWhere :: ContactWhere
114 fake_ContactWhere = ContactWhere ["Organization A"]
120 (Just fake_ContactTouch)
121 (Just $ jour 01 01 2020)
122 (Just $ jour 01 01 2029)
125 ContactTouch { _ct_mail :: Maybe Text
126 , _ct_phone :: Maybe Text
127 , _ct_url :: Maybe Text
128 } deriving (Eq, Show, Generic)
130 fake_ContactTouch :: ContactTouch
131 fake_ContactTouch = ContactTouch (Just "email@data.com")
132 (Just "+336 328 283 288")
133 (Just "https://url.com")
135 -- | ToSchema instances
136 instance ToSchema HyperdataContact where
137 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_hc_")
138 instance ToSchema ContactWho where
139 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_cw_")
140 instance ToSchema ContactWhere where
141 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_cw_")
142 instance ToSchema ContactTouch where
143 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_ct_")
144 instance ToSchema ContactMetaData where
145 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_cm_")
147 -- | Arbitrary instances
148 instance Arbitrary HyperdataContact where
149 arbitrary = elements [HyperdataContact Nothing Nothing [] Nothing Nothing Nothing Nothing Nothing]
151 -- | Specific Gargantext instance
152 instance Hyperdata HyperdataContact
154 -- | Database (Posgresql-simple instance)
155 instance FromField HyperdataContact where
156 fromField = fromField'
158 -- | Database (Opaleye instance)
159 instance QueryRunnerColumnDefault PGJsonb HyperdataContact where
160 queryRunnerColumnDefault = fieldQueryRunnerColumn
163 makeLenses ''ContactWho
164 makeLenses ''ContactWhere
165 makeLenses ''ContactTouch
166 makeLenses ''ContactMetaData
167 makeLenses ''HyperdataContact
169 -- | All Json instances
170 $(deriveJSON (unPrefix "_cw_") ''ContactWho)
171 $(deriveJSON (unPrefix "_cw_") ''ContactWhere)
172 $(deriveJSON (unPrefix "_ct_") ''ContactTouch)
173 $(deriveJSON (unPrefix "_cm_") ''ContactMetaData)
174 $(deriveJSON (unPrefix "_hc_") ''HyperdataContact)