diff --git a/SpockOpaleye/src/DataTypes.hs b/SpockOpaleye/src/DataTypes.hs index f65be3d..413cb27 100644 --- a/SpockOpaleye/src/DataTypes.hs +++ b/SpockOpaleye/src/DataTypes.hs @@ -19,8 +19,8 @@ newtype TenantId = TenantId Int data TenantStatus = TenantStatusActive | TenantStatusInActive | TenantStatusNew deriving (Show, Generic) -data TenantPoly key created_at updated_at name fname lname email phone status owner_id b_domain = Tenant - { tenant_id :: key +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 @@ -31,7 +31,7 @@ data TenantPoly key created_at updated_at name fname lname email phone status ow , tenant_status :: status , tenant_ownerid :: owner_id , tenant_backofficedomain :: b_domain - } deriving (Show, Generic) +} deriving (Show, Generic) type Tenant = TenantPoly TenantId UTCTime UTCTime Text Text Text Text Text TenantStatus (Maybe UserId) Text @@ -43,17 +43,19 @@ data UserStatus = UserStatusActive | UserStatusInActive | UserStatusBlocked newtype UserId = UserId Int deriving (Show, Generic) -data UserPoly key tenant_id username password firstname lastname status = User { - user_id :: key, - user_tenantid :: tenant_id, - user_username :: username, - user_password :: password, - user_firstname :: firstname, - user_lastname :: lastname, - user_status :: status +data UserPoly key created_at updated_at tenant_id username password firstname lastname status = User { + user_id :: key + , user_createdat :: created_at + , user_updatedat :: updated_at + , user_tenantid :: tenant_id + , user_username :: username + , user_password :: password + , user_firstname :: firstname + , user_lastname :: lastname + , user_status :: status } -type User = UserPoly UserId TenantId Text BcryptPassword (Maybe Text) (Maybe Text) UserStatus +type User = UserPoly UserId UTCTime UTCTime TenantId Text BcryptPassword (Maybe Text) (Maybe Text) UserStatus data Permission = Read | Create | Update | Delete deriving (Show) @@ -61,11 +63,13 @@ data Permission = Read | Create | Update | Delete newtype RoleId = RoleId Int deriving (Show) -data RolePoly key tenant_id name permission = Role - { role_id :: key +data RolePoly key created_at updated_at tenant_id name permission = Role { + role_id :: key + , role_createdat :: created_at + , role_updatedat :: updated_at , role_tenantid :: tenant_id , role_name :: name , role_permission :: permission - } deriving (Show) +} deriving (Show) -type Role = RolePoly RoleId TenantId Text (NonEmpty Permission) +type Role = RolePoly RoleId UTCTime UTCTime TenantId Text (NonEmpty Permission) diff --git a/SpockOpaleye/src/OpaleyeDef.hs b/SpockOpaleye/src/OpaleyeDef.hs index 7198509..89533a4 100644 --- a/SpockOpaleye/src/OpaleyeDef.hs +++ b/SpockOpaleye/src/OpaleyeDef.hs @@ -69,6 +69,8 @@ tenantTable = Table "tenants" (pTenant type UserTableW = UserPoly (Maybe (Column PGInt4)) + (Maybe (Column PGTimestamptz)) -- createdAt + (Maybe (Column PGTimestamptz)) -- updatedAt (Column PGInt4) (Column PGText) (Column PGBytea) @@ -78,6 +80,8 @@ type UserTableW = UserPoly type UserTableR = UserPoly (Column PGInt4) + (Column PGTimestamptz) -- createdAt + (Column PGTimestamptz) -- updatedAt (Column PGInt4) (Column PGText) (Column PGBytea) @@ -92,22 +96,28 @@ userTable :: Table UserTableW UserTableR userTable = Table "users" (pUser User { user_id = optional "id" - , user_tenantid = required "tenant_id" - , user_username = required "username" - , user_password = required "password" - , user_firstname = optional "first_name" - , user_lastname = optional "last_name" - , user_status = required "status" + , user_createdat = (optional "created_at") + , user_updatedat = (optional "updated_at") + , user_tenantid = required "tenant_id" + , user_username = required "username" + , user_password = required "password" + , user_firstname = optional "first_name" + , user_lastname = optional "last_name" + , user_status = required "status" }) type RoleTableW = RolePoly (Maybe (Column PGInt4)) + (Maybe (Column PGTimestamptz)) -- createdAt + (Maybe (Column PGTimestamptz)) -- updatedAt (Column PGInt4) (Column PGText) (Column (PGArray PGText)) type RoleTableR = RolePoly (Column PGInt4) + (Column PGTimestamptz) -- createdAt + (Column PGTimestamptz) -- updatedAt (Column PGInt4) (Column PGText) (Column (PGArray PGText)) @@ -118,6 +128,8 @@ $(makeLensesWith abbreviatedFields ''RolePoly) roleTable :: Table RoleTableW RoleTableR roleTable = Table "roles" (pRole Role { role_id = optional "id", + role_createdat = optional "created_at", + role_updatedat = optional "updated_at", role_tenantid = required "tenant_id", role_name = required "name", role_permission = required "permissions"}) @@ -292,7 +304,6 @@ instance D.Default Constant () (Maybe (Column PGTimestamptz)) where 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