Skip to content

Commit

Permalink
Add housekeeping fields to Tenant
Browse files Browse the repository at this point in the history
  • Loading branch information
sras committed Nov 7, 2016
1 parent 19b697b commit f6a52f0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
11 changes: 8 additions & 3 deletions SpockOpaleye/src/DataTypes.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}

module DataTypes where

import CryptoDef
import Data.List.NonEmpty
import Data.Text
import GHC.Generics
import Data.Time(UTCTime)

data ValidationResult = Valid | Invalid
deriving (Eq, Show)
Expand All @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion SpockOpaleye/src/JsonInstances.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()) <*>
Expand Down
20 changes: 20 additions & 0 deletions SpockOpaleye/src/OpaleyeDef.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
Expand All @@ -11,15 +12,19 @@ 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
import DataTypes

type TenantTableW = TenantPoly
(Maybe (Column PGInt4))
(Maybe (Column PGTimestamptz)) -- createdAt
(Maybe (Column PGTimestamptz)) -- updatedAt
(Column PGText)
(Column PGText)
(Column PGText)
Expand All @@ -31,6 +36,8 @@ type TenantTableW = TenantPoly

type TenantTableR = TenantPoly
(Column PGInt4)
(Column PGTimestamptz) -- createdAt
(Column PGTimestamptz) -- updatedAt
(Column PGText)
(Column PGText)
(Column PGText)
Expand All @@ -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"),
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion SpockOpaleye/src/TenantApi.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f6a52f0

Please sign in to comment.