42 lines
970 B
Go
42 lines
970 B
Go
package invite
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
var ErrEmailHasAccount = errors.New("email already has an account")
|
|
var ErrPendingInvite = errors.New("pending invite already exists")
|
|
var ErrInviteNotFound = errors.New("invite not found")
|
|
var ErrInviteEmailNotSent = errors.New("invite email not sent")
|
|
|
|
const Lifetime = 3 * 24 * time.Hour
|
|
|
|
type Status string
|
|
|
|
const (
|
|
StatusPending Status = "pending"
|
|
StatusExpired Status = "expired"
|
|
StatusAccepted Status = "accepted"
|
|
)
|
|
|
|
type Invite struct {
|
|
ID uint32 `json:"id"`
|
|
Email string `json:"email"`
|
|
HashedCode string `json:"hashed_code"`
|
|
ExpiresAt time.Time `json:"expires_at"`
|
|
InvitedBy uint32 `json:"invited_by"`
|
|
AcceptedAt *time.Time `json:"accepted_at"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
}
|
|
|
|
func (i Invite) StatusAt(now time.Time) Status {
|
|
if i.AcceptedAt != nil {
|
|
return StatusAccepted
|
|
}
|
|
if i.ExpiresAt.After(now) {
|
|
return StatusPending
|
|
}
|
|
return StatusExpired
|
|
}
|