invite.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package models
  2. import (
  3. "time"
  4. "gorm.io/gorm"
  5. )
  6. // Invite type that extends gorm.Model
  7. type Invite struct {
  8. gorm.Model
  9. Token string `gorm:"unique"`
  10. Expiry *time.Time
  11. Email string
  12. // Kind is the role kind that this refers to
  13. Kind string
  14. ProjectID uint
  15. UserID uint
  16. }
  17. // InviteExternal represents the Invite type that is sent over REST
  18. type InviteExternal struct {
  19. ID uint `json:"id"`
  20. Token string `json:"token"`
  21. Expired bool `json:"expired"`
  22. Email string `json:"email"`
  23. Accepted bool `json:"accepted"`
  24. Kind string `json:"kind"`
  25. }
  26. // Externalize generates an external Invite to be shared over REST
  27. func (i *Invite) Externalize() *InviteExternal {
  28. return &InviteExternal{
  29. ID: i.Model.ID,
  30. Token: i.Token,
  31. Email: i.Email,
  32. Expired: i.IsExpired(),
  33. Accepted: i.IsAccepted(),
  34. Kind: i.Kind,
  35. }
  36. }
  37. func (i *Invite) IsExpired() bool {
  38. timeLeft := i.Expiry.Sub(time.Now())
  39. return timeLeft < 0
  40. }
  41. func (i *Invite) IsAccepted() bool {
  42. return i.UserID != 0
  43. }