invite.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package models
  2. import (
  3. "time"
  4. "gorm.io/gorm"
  5. "github.com/porter-dev/porter/api/types"
  6. )
  7. // Invite type that extends gorm.Model
  8. type Invite struct {
  9. gorm.Model
  10. Token string `gorm:"unique"`
  11. Expiry *time.Time
  12. Email string
  13. // Kind is the role kind that this refers to
  14. Kind string
  15. ProjectID uint
  16. UserID uint
  17. InvitingUserID uint
  18. Status InviteStatus
  19. }
  20. type InviteStatus string
  21. const (
  22. InvitePending InviteStatus = "pending"
  23. InviteAccepted InviteStatus = "accepted"
  24. InviteDeclined InviteStatus = "declined"
  25. )
  26. // ToInviteType generates an external Invite to be shared over REST
  27. func (i *Invite) ToInviteType() *types.Invite {
  28. return &types.Invite{
  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. Status: string(i.Status),
  36. InvitingUserID: i.InvitingUserID,
  37. }
  38. }
  39. func (i *Invite) IsExpired() bool {
  40. timeLeft := i.Expiry.Sub(time.Now())
  41. return timeLeft < 0
  42. }
  43. func (i *Invite) IsAccepted() bool {
  44. return i.UserID != 0
  45. }