github.com/condensat/bank-core@v0.1.0/database/model/accountstate.go (about) 1 // Copyright 2020 Condensat Tech. All rights reserved. 2 // Use of this source code is governed by a MIT 3 // license that can be found in the LICENSE file. 4 5 package model 6 7 import ( 8 "errors" 9 ) 10 11 type AccountStatus String 12 13 const ( 14 AccountStatusInvalid AccountStatus = "" 15 16 AccountStatusCreated AccountStatus = "created" 17 AccountStatusNormal AccountStatus = "normal" 18 AccountStatusLocked AccountStatus = "locked" 19 AccountStatusDisabled AccountStatus = "disabled" 20 ) 21 22 var ( 23 ErrAccountStatusInvalid = errors.New("Invalid AccountStatus") 24 ) 25 26 type AccountState struct { 27 AccountID AccountID `gorm:"unique_index;not null"` // [FK] Reference to Account table 28 State AccountStatus `gorm:"index;not null;type:varchar(16)"` // AccountStatus [normal, locked, disabled] 29 } 30 31 func (p AccountStatus) Valid() bool { 32 switch p { 33 case AccountStatusCreated: 34 fallthrough 35 case AccountStatusNormal: 36 fallthrough 37 case AccountStatusLocked: 38 fallthrough 39 case AccountStatusDisabled: 40 return true 41 42 default: 43 return false 44 } 45 } 46 47 func ParseAccountStatus(str string) AccountStatus { 48 ret := AccountStatus(str) 49 if !ret.Valid() { 50 return AccountStatusInvalid 51 } 52 return ret 53 } 54 55 func (p AccountStatus) String() string { 56 if !p.Valid() { 57 return string(AccountStatusInvalid) 58 } 59 return string(p) 60 } 61 62 func knownAccountStatus() []AccountStatus { 63 return []AccountStatus{ 64 AccountStatusInvalid, 65 66 AccountStatusCreated, 67 AccountStatusNormal, 68 AccountStatusLocked, 69 AccountStatusDisabled, 70 } 71 }