1package invite
2
3import (
4 "context"
5 "fmt"
6 "time"
7
8 "tug/internal/storage"
9)
10
11type Issued struct {
12 ID string
13 Label string
14 URL string
15 ExpiresAt time.Time
16}
17
18func (s *Service) CreateMany(ctx context.Context, createdBy string, labels, groupIDs []string, validFor time.Duration) ([]Issued, error) {
19 input := storage.CreateInvitationsInput{GroupIDs: groupIDs, CreatedBy: createdBy, ValidFor: validFor, AdminGroup: s.cfg.AdminGroup}
20 tokens := make([]string, 0, len(labels))
21 for _, label := range labels {
22 token, hash, err := newToken()
23 if err != nil {
24 return nil, err
25 }
26 tokens = append(tokens, token)
27 input.Invitations = append(input.Invitations, storage.NewInvitation{TokenHash: hash, Label: label})
28 }
29 records, err := s.store.CreateInvitationsAudited(ctx, input)
30 if err != nil {
31 return nil, fmt.Errorf("invite: create invitations: %w", err)
32 }
33 issued := make([]Issued, 0, len(records))
34 for i, r := range records {
35 issued = append(issued, Issued{ID: r.ID, Label: r.Label, URL: s.url(tokens[i]), ExpiresAt: r.ExpiresAt})
36 }
37 return issued, nil
38}