Files
lincodex/backend/internal/bootstrap/invitation.go
T

57 lines
1.9 KiB
Go

package bootstrap
import (
"context"
"fmt"
"log/slog"
"time"
"lincodex/backend/config"
v1 "lincodex/backend/internal/adapter/primary/http/v1"
"lincodex/backend/internal/adapter/secondary/postgres"
"lincodex/backend/internal/usecase/identity"
"lincodex/backend/internal/usecase/invitation"
)
const invitationPoolMaxConns = 4
// newInvitationHandler builds the invitation draft operations. They resolve a
// card handle through the same visibility service as the card read and the
// search, so the pseudonym key and the cohort rules are the ones of the cards.
func newInvitationHandler(
ctx context.Context,
settings config.Config,
logger *slog.Logger,
keyring *identity.Keyring,
) (*v1.InvitationHandler, func(), error) {
if settings.DatabaseURL == "" {
logger.Warn("invitation operations are not served because DATABASE_URL is not set")
return nil, func() {}, nil
}
if keyring == nil {
return nil, nil, fmt.Errorf("bootstrap.newInvitationHandler - identity keyring must be set")
}
key, err := keyring.CandidateViewPseudonymKey()
if err != nil {
return nil, nil, fmt.Errorf("bootstrap.newInvitationHandler - derive projection key: %w", err)
}
pool, err := postgres.NewSizedPool(ctx, settings.DatabaseURL, invitationPoolMaxConns)
if err != nil {
return nil, nil, fmt.Errorf("bootstrap.newInvitationHandler - create pool: %w", err)
}
transactor := postgres.NewTransactor(pool)
views, err := newCandidateViewService(pool, transactor, key)
if err != nil {
pool.Close()
return nil, nil, fmt.Errorf("bootstrap.newInvitationHandler - create view service: %w", err)
}
service, err := invitation.NewService(invitation.Dependencies{
Transactor: transactor, Keyring: keyring, PseudonymKey: key, Candidates: views.ResolveCandidateHandle, Clock: time.Now,
})
if err != nil {
pool.Close()
return nil, nil, fmt.Errorf("bootstrap.newInvitationHandler - create service: %w", err)
}
return v1.NewInvitationHandler(service), pool.Close, nil
}