From f6a52f0f6a41dcfd3c91f23863c2d243ca0cf47c Mon Sep 17 00:00:00 2001 From: "Sandeep.C.R" Date: Mon, 7 Nov 2016 06:35:37 +0000 Subject: [PATCH] Add housekeeping fields to Tenant --- SpockOpaleye/src/DataTypes.hs | 11 ++++++++--- SpockOpaleye/src/JsonInstances.hs | 2 +- SpockOpaleye/src/OpaleyeDef.hs | 20 ++++++++++++++++++++ SpockOpaleye/src/TenantApi.hs | 2 +- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/SpockOpaleye/src/DataTypes.hs b/SpockOpaleye/src/DataTypes.hs index ae47f24..f65be3d 100644 --- a/SpockOpaleye/src/DataTypes.hs +++ b/SpockOpaleye/src/DataTypes.hs @@ -1,4 +1,6 @@ {-# LANGUAGE DeriveGeneric #-} +{-# LANGUAGE TypeSynonymInstances #-} +{-# LANGUAGE FlexibleInstances #-} module DataTypes where @@ -6,6 +8,7 @@ import CryptoDef import Data.List.NonEmpty import Data.Text import GHC.Generics +import Data.Time(UTCTime) data ValidationResult = Valid | Invalid deriving (Eq, Show) @@ -16,8 +19,10 @@ newtype TenantId = TenantId Int data TenantStatus = TenantStatusActive | TenantStatusInActive | TenantStatusNew deriving (Show, Generic) -data TenantPoly key name fname lname email phone status owner_id b_domain = Tenant +data TenantPoly key created_at updated_at name fname lname email phone status owner_id b_domain = Tenant { tenant_id :: key + , tenant_createdat :: created_at + , tenant_updatedat :: updated_at , tenant_name :: name , tenant_firstname :: fname , tenant_lastname :: lname @@ -28,9 +33,9 @@ data TenantPoly key name fname lname email phone status owner_id b_domain = Tena , tenant_backofficedomain :: b_domain } deriving (Show, Generic) -type Tenant = TenantPoly TenantId Text Text Text Text Text TenantStatus (Maybe UserId) Text +type Tenant = TenantPoly TenantId UTCTime UTCTime Text Text Text Text Text TenantStatus (Maybe UserId) Text -type TenantIncoming = TenantPoly () Text Text Text Text Text () (Maybe UserId) Text +type TenantIncoming = TenantPoly () () () Text Text Text Text Text () (Maybe UserId) Text data UserStatus = UserStatusActive | UserStatusInActive | UserStatusBlocked deriving (Show) diff --git a/SpockOpaleye/src/JsonInstances.hs b/SpockOpaleye/src/JsonInstances.hs index bea6c5a..ff10ac6 100644 --- a/SpockOpaleye/src/JsonInstances.hs +++ b/SpockOpaleye/src/JsonInstances.hs @@ -29,7 +29,7 @@ instance FromJSON TenantStatus where instance FromJSON TenantIncoming where parseJSON (Object v) = - (Tenant ()) <$> v .: "name" <*> v .: "firstname" <*> v .: "lastname" <*> + (Tenant () () ()) <$> v .: "name" <*> v .: "firstname" <*> v .: "lastname" <*> v .: "email" <*> v .: "phone" <*> (pure ()) <*> diff --git a/SpockOpaleye/src/OpaleyeDef.hs b/SpockOpaleye/src/OpaleyeDef.hs index 714dc6d..7198509 100644 --- a/SpockOpaleye/src/OpaleyeDef.hs +++ b/SpockOpaleye/src/OpaleyeDef.hs @@ -1,4 +1,5 @@ {-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TemplateHaskell #-} @@ -11,8 +12,10 @@ import Data.Profunctor.Product import qualified Data.Profunctor.Product.Default as D import Data.Profunctor.Product.TH (makeAdaptorAndInstance) import Data.Text +import Data.Time(UTCTime) import Data.Text.Encoding import Database.PostgreSQL.Simple.FromField +import Database.PostgreSQL.Simple (Connection) import Opaleye import Control.Lens @@ -20,6 +23,8 @@ import DataTypes type TenantTableW = TenantPoly (Maybe (Column PGInt4)) + (Maybe (Column PGTimestamptz)) -- createdAt + (Maybe (Column PGTimestamptz)) -- updatedAt (Column PGText) (Column PGText) (Column PGText) @@ -31,6 +36,8 @@ type TenantTableW = TenantPoly type TenantTableR = TenantPoly (Column PGInt4) + (Column PGTimestamptz) -- createdAt + (Column PGTimestamptz) -- updatedAt (Column PGText) (Column PGText) (Column PGText) @@ -47,6 +54,8 @@ tenantTable :: Table TenantTableW TenantTableR tenantTable = Table "tenants" (pTenant Tenant { tenant_id = (optional "id"), + tenant_createdat = (optional "created_at"), + tenant_updatedat = (optional "updated_at"), tenant_name = (required "name"), tenant_firstname = (required "first_name"), tenant_lastname = (required "last_name"), @@ -276,3 +285,14 @@ instance D.Default Constant () (Maybe (Column PGText)) where instance D.Default Constant Text (Column (Nullable PGText)) where def = Constant (toNullable.pgStrictText) + +instance D.Default Constant () (Maybe (Column PGTimestamptz)) where + def = Constant (\_ -> Nothing) + +instance D.Default Constant UTCTime (Maybe (Column PGTimestamptz)) where + def = Constant (\time -> Just $ pgUTCTime time) + + +create_item :: (D.Default Constant haskells columnsW, D.Default QueryRunner returned b) + => Connection -> Table columnsW returned -> haskells -> IO b +create_item conn table item = fmap Prelude.head $ runInsertManyReturning conn table [constant item] id diff --git a/SpockOpaleye/src/TenantApi.hs b/SpockOpaleye/src/TenantApi.hs index f9c52e0..12e425b 100644 --- a/SpockOpaleye/src/TenantApi.hs +++ b/SpockOpaleye/src/TenantApi.hs @@ -27,7 +27,7 @@ import UserApi create_tenant :: Connection -> TenantIncoming -> IO Tenant create_tenant conn tenant = do - fmap Prelude.head $ runInsertManyReturning conn tenantTable [constant tenant] id + create_item conn tenantTable tenant activate_tenant :: Connection -> Tenant -> IO Tenant activate_tenant conn tenant = set_tenant_status conn tenant TenantStatusActive