Files
uPortal2/backend/internal/meeting/service.go
T

140 lines
3.7 KiB
Go

package meeting
import (
"context"
"log/slog"
"strings"
"time"
"goAuth/internal/user"
)
// MeetingRetention is how long a meeting is kept after its scheduled time
// before the background cleanup removes it.
const MeetingRetention = 7 * 24 * time.Hour
type Service struct {
r *Repository
}
func NewService(r *Repository) (*Service, error) {
return &Service{r: r}, nil
}
// Create validates and stores a new meeting authored by u. When autoJoin is
// true the author's "join" vote is cast immediately so the meeting reflects
// their intent to attend.
func (s *Service) Create(ctx context.Context, u *user.User, title, intent, room string, scheduledAt time.Time, autoJoin bool) (*Meeting, error) {
if u == nil {
slog.Error("nil user", slog.String("op", "service.Create"))
return nil, ErrInternal
}
title = strings.TrimSpace(title)
intent = strings.TrimSpace(intent)
room = strings.TrimSpace(room)
if title == "" || scheduledAt.IsZero() {
return nil, ErrInvalid
}
created, err := s.r.CreateMeeting(ctx, &Meeting{
Title: title,
Intent: intent,
Room: room,
ScheduledAt: scheduledAt.UTC(),
CreatedBy: u.Id,
CreatedAt: time.Now().UTC(),
})
if err != nil {
return nil, err
}
// Optionally record the author's own "join" vote straight away.
if autoJoin {
if err := s.r.UpsertVote(ctx, created.Id, u.Id, VoteJoin); err != nil {
return nil, err
}
}
// Re-read so the response carries the author name and vote aggregates.
return s.r.MeetingById(ctx, u.Id, created.Id)
}
// Meetings returns all meetings visible to every registered user.
func (s *Service) Meetings(ctx context.Context, u *user.User, limit, offset int64) ([]*Meeting, error) {
if u == nil {
slog.Error("nil user", slog.String("op", "service.Meetings"))
return nil, ErrInternal
}
return s.r.Meetings(ctx, u.Id, limit, offset)
}
func (s *Service) MeetingById(ctx context.Context, u *user.User, id int64) (*Meeting, error) {
if u == nil {
slog.Error("nil user", slog.String("op", "service.MeetingById"))
return nil, ErrInternal
}
return s.r.MeetingById(ctx, u.Id, id)
}
// Delete removes a meeting. Its author may delete it manually, and admins may
// delete any meeting.
func (s *Service) Delete(ctx context.Context, u *user.User, id int64) error {
if u == nil {
slog.Error("nil user", slog.String("op", "service.Delete"))
return ErrInternal
}
existing, err := s.r.MeetingById(ctx, u.Id, id)
if err != nil {
return err
}
if existing.CreatedBy != u.Id && !u.IsAdmin {
return ErrForbidden
}
return s.r.DeleteMeeting(ctx, id)
}
// Vote records the user's vote (join or decline) and returns the updated meeting.
func (s *Service) Vote(ctx context.Context, u *user.User, id int64, vote string) (*Meeting, error) {
if u == nil {
slog.Error("nil user", slog.String("op", "service.Vote"))
return nil, ErrInternal
}
if !IsValidVote(vote) {
return nil, ErrInvalid
}
if _, err := s.r.MeetingById(ctx, u.Id, id); err != nil {
return nil, err
}
if err := s.r.UpsertVote(ctx, id, u.Id, vote); err != nil {
return nil, err
}
return s.r.MeetingById(ctx, u.Id, id)
}
// RetractVote removes the user's vote and returns the updated meeting.
func (s *Service) RetractVote(ctx context.Context, u *user.User, id int64) (*Meeting, error) {
if u == nil {
slog.Error("nil user", slog.String("op", "service.RetractVote"))
return nil, ErrInternal
}
if _, err := s.r.MeetingById(ctx, u.Id, id); err != nil {
return nil, err
}
if err := s.r.DeleteVote(ctx, id, u.Id); err != nil {
return nil, err
}
return s.r.MeetingById(ctx, u.Id, id)
}
// Cleanup deletes meetings that occurred more than MeetingRetention ago.
func (s *Service) Cleanup(ctx context.Context) error {
return s.r.CleanupMeetings(ctx, time.Now().UTC().Add(-MeetingRetention))
}