account.go 1.5 KB

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