account.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. Name string
  26. Company string
  27. NextDashboardId int
  28. UsingAccountId int
  29. Collaborators []CollaboratorLink
  30. CreatedOn time.Time
  31. ModifiedOn time.Time
  32. LastLoginOn time.Time
  33. }
  34. func (account *Account) AddCollaborator(newCollaborator *Account) error {
  35. for _, collaborator := range account.Collaborators {
  36. if collaborator.AccountId == newCollaborator.Id {
  37. return errors.New("Collaborator already exists")
  38. }
  39. }
  40. account.Collaborators = append(account.Collaborators, CollaboratorLink{
  41. AccountId: newCollaborator.Id,
  42. Email: newCollaborator.Email,
  43. Role: "admin",
  44. CreatedOn: time.Now(),
  45. ModifiedOn: time.Now(),
  46. })
  47. return nil
  48. }
  49. func (account *Account) RemoveCollaborator(accountId int) {
  50. list := account.Collaborators
  51. for i, collaborator := range list {
  52. if collaborator.AccountId == accountId {
  53. account.Collaborators = append(list[:i], list[i+1:]...)
  54. break
  55. }
  56. }
  57. }
  58. func (account *Account) HasCollaborator(accountId int) bool {
  59. for _, collaborator := range account.Collaborators {
  60. if collaborator.AccountId == accountId {
  61. return true
  62. }
  63. }
  64. return false
  65. }