]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Node/Contact.hs
[Annuaire] types to import users.
[gargantext.git] / src / Gargantext / Database / Node / Contact.hs
1 {-|
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
8 Portability : POSIX
9
10 -}
11
12 {-# LANGUAGE DeriveGeneric #-}
13 {-# LANGUAGE NoImplicitPrelude #-}
14 {-# LANGUAGE FunctionalDependencies #-}
15 {-# LANGUAGE OverloadedStrings #-}
16 {-# LANGUAGE FlexibleInstances #-}
17 {-# LANGUAGE MultiParamTypeClasses #-}
18 {-# LANGUAGE TemplateHaskell #-}
19
20 module Gargantext.Database.Node.Contact
21 where
22
23 import Control.Lens (makeLenses)
24 import Data.Aeson.TH (deriveJSON)
25 import Data.Text (Text)
26 import Data.Time (UTCTime)
27 import Database.PostgreSQL.Simple.FromField (FromField, fromField)
28 import GHC.Generics (Generic)
29 import Gargantext.Core.Utils.Prefix (unPrefix)
30 import Gargantext.Database.Node (NodeWrite', AnnuaireId, UserId, Name, node)
31 import Gargantext.Database.Types.Node (Node,Hyperdata,NodeType(..))
32 import Gargantext.Database.Utils (fromField')
33 import Gargantext.Prelude
34 import Opaleye (QueryRunnerColumnDefault, queryRunnerColumnDefault, PGJsonb, fieldQueryRunnerColumn)
35
36 ------------------------------------------------------------------------
37
38 type NodeContact = Node HyperdataContact
39
40 data HyperdataContact =
41 HyperdataContact { _hc_who :: Maybe ContactWho
42 , _hc_where :: Maybe [ContactWhere]
43 , _hc_metaData :: Maybe ContactMetaData
44
45 } deriving (Eq, Show, Generic)
46
47 data ContactMetaData =
48 ContactMetaData { _cm_bdd :: Maybe Text
49 , _cm_lastValidation :: Maybe Text
50 , _cm_uniqIdBdd :: Maybe Text
51 , _cm_uniqId :: Maybe Text
52 } deriving (Eq, Show, Generic)
53
54
55 arbitraryHyperdataContact :: HyperdataContact
56 arbitraryHyperdataContact = HyperdataContact Nothing Nothing Nothing
57
58 data ContactWho =
59 ContactWho { _cw_id :: Maybe Text
60 , _cw_firstName :: Maybe Text
61 , _cw_lastName :: Maybe Text
62 , _cw_keywords :: Maybe [Text]
63 , _cw_freetags :: Maybe [Text]
64 } deriving (Eq, Show, Generic)
65
66 data ContactWhere =
67 ContactWhere { _cw_organization :: Maybe [Text]
68 , _cw_labTeamDepts :: Maybe [Text]
69
70 , _cw_role :: Maybe Text
71
72 , _cw_office :: Maybe Text
73 , _cw_country :: Maybe Text
74 , _cw_city :: Maybe Text
75
76 , _cw_touch :: Maybe ContactTouch
77
78 , _cw_entry :: Maybe UTCTime
79 , _cw_exit :: Maybe UTCTime
80 } deriving (Eq, Show, Generic)
81
82 data ContactTouch =
83 ContactTouch { _ct_mail :: Maybe Text
84 , _ct_phone :: Maybe Text
85 , _ct_url :: Maybe Text
86 } deriving (Eq, Show, Generic)
87
88
89 nodeContactW :: Maybe Name -> Maybe HyperdataContact
90 -> AnnuaireId -> UserId -> NodeWrite'
91 nodeContactW maybeName maybeContact aId =
92 node NodeContact name contact (Just aId)
93 where
94 name = maybe "Contact" identity maybeName
95 contact = maybe arbitraryHyperdataContact identity maybeContact
96
97
98 -- | Main instances of Contact
99
100 -- | Specific Gargantext instance
101 instance Hyperdata HyperdataContact
102
103 -- | Database (Posgresql-simple instance)
104 instance FromField HyperdataContact where
105 fromField = fromField'
106
107 -- | Database (Opaleye instance)
108 instance QueryRunnerColumnDefault PGJsonb HyperdataContact where
109 queryRunnerColumnDefault = fieldQueryRunnerColumn
110
111 -- | All lenses
112 makeLenses ''ContactWho
113 makeLenses ''ContactWhere
114 makeLenses ''ContactTouch
115 makeLenses ''ContactMetaData
116 makeLenses ''HyperdataContact
117
118 -- | All Json instances
119 $(deriveJSON (unPrefix "_cw_") ''ContactWho)
120 $(deriveJSON (unPrefix "_cw_") ''ContactWhere)
121 $(deriveJSON (unPrefix "_ct_") ''ContactTouch)
122 $(deriveJSON (unPrefix "_cm_") ''ContactMetaData)
123 $(deriveJSON (unPrefix "_hc_") ''HyperdataContact)