42 lines
1.1 KiB
Go
42 lines
1.1 KiB
Go
package useCases
|
|
|
|
import (
|
|
"RenewCMS/internal/domain/hash"
|
|
"RenewCMS/internal/domain/invite"
|
|
"time"
|
|
)
|
|
|
|
type GetInviteUseCase struct {
|
|
inviteRepository invite.Repository
|
|
hasher hash.Hasher
|
|
}
|
|
|
|
func NewGetInviteUseCase(inviteRepository invite.Repository, hasher hash.Hasher) *GetInviteUseCase {
|
|
return &GetInviteUseCase{inviteRepository, hasher}
|
|
}
|
|
|
|
func (g *GetInviteUseCase) GetInvite(code string) (invite.Invite, error) {
|
|
return findUsableInvite(g.inviteRepository, g.hasher, code, time.Now())
|
|
}
|
|
|
|
func findUsableInvite(inviteRepository invite.Repository, hasher hash.Hasher, code string, now time.Time) (invite.Invite, error) {
|
|
if code == "" {
|
|
return invite.Invite{}, invite.ErrInviteInvalid
|
|
}
|
|
|
|
for _, candidate := range inviteRepository.GetAll() {
|
|
if candidate.AcceptedAt != nil {
|
|
continue
|
|
}
|
|
if hasher.Compare(candidate.HashedCode, code) != nil {
|
|
continue
|
|
}
|
|
if candidate.StatusAt(now) == invite.StatusExpired {
|
|
return invite.Invite{}, invite.ErrInviteExpired
|
|
}
|
|
return candidate, nil
|
|
}
|
|
|
|
return invite.Invite{}, invite.ErrInviteInvalid
|
|
}
|