69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package useCases
|
|
|
|
import (
|
|
"RenewCMS/internal/domain/invite"
|
|
"RenewCMS/internal/domain/user"
|
|
"sort"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
EntryKindUser = "user"
|
|
EntryKindInvite = "invite"
|
|
)
|
|
|
|
type ListUsersAndInvitesUseCase struct {
|
|
userRepository user.Repository
|
|
inviteRepository invite.Repository
|
|
}
|
|
|
|
type DirectoryEntry struct {
|
|
ID uint32
|
|
Kind string
|
|
Username string
|
|
DisplayName string
|
|
Email string
|
|
Status string
|
|
CreatedAt time.Time
|
|
}
|
|
|
|
func NewListUsersAndInvitesUseCase(userRepository user.Repository, inviteRepository invite.Repository) *ListUsersAndInvitesUseCase {
|
|
return &ListUsersAndInvitesUseCase{userRepository, inviteRepository}
|
|
}
|
|
|
|
func (g *ListUsersAndInvitesUseCase) ListUsersAndInvites() []DirectoryEntry {
|
|
now := time.Now()
|
|
entries := []DirectoryEntry{}
|
|
|
|
for _, existingUser := range g.userRepository.GetAll() {
|
|
entries = append(entries, DirectoryEntry{
|
|
ID: existingUser.ID,
|
|
Kind: EntryKindUser,
|
|
Username: existingUser.Username,
|
|
DisplayName: existingUser.DisplayName,
|
|
Status: string(invite.StatusAccepted),
|
|
CreatedAt: existingUser.CreatedAt,
|
|
})
|
|
}
|
|
|
|
for _, existingInvite := range g.inviteRepository.GetAll() {
|
|
status := existingInvite.StatusAt(now)
|
|
if status == invite.StatusAccepted {
|
|
continue
|
|
}
|
|
entries = append(entries, DirectoryEntry{
|
|
ID: existingInvite.ID,
|
|
Kind: EntryKindInvite,
|
|
Email: existingInvite.Email,
|
|
Status: string(status),
|
|
CreatedAt: existingInvite.CreatedAt,
|
|
})
|
|
}
|
|
|
|
sort.SliceStable(entries, func(i, j int) bool {
|
|
return entries[i].CreatedAt.After(entries[j].CreatedAt)
|
|
})
|
|
|
|
return entries
|
|
}
|