invite.go 916 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. ProjectID uint
  13. UserID uint
  14. }
  15. // InviteExternal represents the Invite type that is sent over REST
  16. type InviteExternal struct {
  17. ID uint `json:"id"`
  18. Token string `json:"token"`
  19. Expired bool `json:"expired"`
  20. Email string `json:"email"`
  21. Accepted bool `json:"accepted"`
  22. }
  23. // Externalize generates an external Invite to be shared over REST
  24. func (i *Invite) Externalize() *InviteExternal {
  25. return &InviteExternal{
  26. ID: i.Model.ID,
  27. Token: i.Token,
  28. Email: i.Email,
  29. Expired: i.IsExpired(),
  30. Accepted: i.IsAccepted(),
  31. }
  32. }
  33. func (i *Invite) IsExpired() bool {
  34. timeLeft := i.Expiry.Sub(time.Now())
  35. return timeLeft < 0
  36. }
  37. func (i *Invite) IsAccepted() bool {
  38. return i.UserID != 0
  39. }