package bootstrap import ( "context" "fmt" "log/slog" "time" "github.com/google/uuid" "lincodex/backend/config" v1 "lincodex/backend/internal/adapter/primary/http/v1" "lincodex/backend/internal/adapter/secondary/postgres" "lincodex/backend/internal/domain" "lincodex/backend/internal/port/outbound" "lincodex/backend/internal/usecase/companyquota" "lincodex/backend/internal/usecase/consent" "lincodex/backend/internal/usecase/identity" "lincodex/backend/internal/usecase/invitation" "lincodex/backend/internal/usecase/notification" "lincodex/backend/internal/usecase/notification/mailtemplate" ) const invitationPoolMaxConns = 4 // newInvitationHandler builds the invitation 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. A send // charges the shared company quota and records its notification and email // through the outbox of the same transaction. 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) } renderer, err := mailtemplate.NewRenderer(settings.AppPublicURL) if err != nil { pool.Close() return nil, nil, fmt.Errorf("bootstrap.newInvitationHandler - create mail renderer: %w", err) } notifier, err := notification.NewInvitationNotifier(notification.NewMailRequester(notification.NewTemplateRenderer(renderer), time.Now)) if err != nil { pool.Close() return nil, nil, fmt.Errorf("bootstrap.newInvitationHandler - create notifier: %w", err) } quota := companyquota.NewService(time.Now) service, err := invitation.NewService(invitation.Dependencies{ Transactor: transactor, Keyring: keyring, PseudonymKey: key, Candidates: views.ResolveCandidateHandle, Quota: func(ctx context.Context, unit outbound.UnitOfWork, organization domain.Organization, invitationID uuid.UUID) (outbound.CompanyQuotaDecision, error) { return quota.ReserveInvitation(ctx, unit, organization, invitationID) }, Notifier: notifier, Consents: consent.NewService(transactor, time.Now), 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 }