account.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package models
  2. import (
  3. "errors"
  4. "time"
  5. )
  6. var (
  7. CreateAccount func(acccount *Account) error
  8. UpdateAccount func(acccount *Account) error
  9. GetAccountByLogin func(emailOrName string) (*Account, error)
  10. GetAccount func(accountId int) (*Account, error)
  11. GetOtherAccountsFor func(accountId int) ([]*OtherAccount, error)
  12. )
  13. // Typed errors
  14. var (
  15. ErrAccountNotFound = errors.New("Account not found")
  16. )
  17. type CollaboratorLink struct {
  18. AccountId int
  19. Role string
  20. Email string
  21. ModifiedOn time.Time
  22. CreatedOn time.Time
  23. }
  24. type OtherAccount struct {
  25. Id int `gorethink:"id"`
  26. Name string
  27. Role string
  28. }
  29. type Account struct {
  30. Id int `gorethink:"id"`
  31. Version int
  32. Login string
  33. Email string
  34. AccountName string
  35. Password string
  36. Name string
  37. Company string
  38. NextDashboardId int
  39. UsingAccountId int
  40. Collaborators []CollaboratorLink
  41. CreatedOn time.Time
  42. ModifiedOn time.Time
  43. LastLoginOn time.Time
  44. }
  45. func (account *Account) AddCollaborator(newCollaborator *Account) error {
  46. for _, collaborator := range account.Collaborators {
  47. if collaborator.AccountId == newCollaborator.Id {
  48. return errors.New("Collaborator already exists")
  49. }
  50. }
  51. account.Collaborators = append(account.Collaborators, CollaboratorLink{
  52. AccountId: newCollaborator.Id,
  53. Email: newCollaborator.Email,
  54. Role: "admin",
  55. CreatedOn: time.Now(),
  56. ModifiedOn: time.Now(),
  57. })
  58. return nil
  59. }
  60. func (account *Account) RemoveCollaborator(accountId int) {
  61. list := account.Collaborators
  62. for i, collaborator := range list {
  63. if collaborator.AccountId == accountId {
  64. account.Collaborators = append(list[:i], list[i+1:]...)
  65. break
  66. }
  67. }
  68. }
  69. func (account *Account) HasCollaborator(accountId int) bool {
  70. for _, collaborator := range account.Collaborators {
  71. if collaborator.AccountId == accountId {
  72. return true
  73. }
  74. }
  75. return false
  76. }