feat(platform): add the notification inbox and deterministic match search
This commit is contained in:
1 parent
5edff21d2d
commit
0b11021b16
169 files changed
+17083
-443
No files matched your search
@@ -48,7 +48,10 @@ func eventHandlers(settings config.Config, logger *slog.Logger) ([]inbound.Event
|
||||
time.Now,
|
||||
logger.With(slog.String("component", "notification_delivery")),
|
||||
)
|
||||
return []inbound.EventHandler{deliveries}, nil
|
||||
// The inbox handler stores references only and reads no mail state, so it
|
||||
// needs no renderer, provider account, or clock.
|
||||
inbox := notification.NewInboxHandler(logger.With(slog.String("component", "notification_inbox")), settings.NotificationSyntheticEnabled)
|
||||
return []inbound.EventHandler{deliveries, inbox}, nil
|
||||
}
|
||||
|
||||
func newEventConsumers(
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
|
||||
"lincodex/backend/config"
|
||||
"lincodex/backend/internal/adapter/secondary/prometheus"
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/inbound"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
"lincodex/backend/internal/usecase/notification"
|
||||
@@ -77,17 +78,31 @@ func TestBuildEventConsumersSkipsWithoutWork(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBootstrapRegistersTheDeliveryHandler(t *testing.T) {
|
||||
func TestBootstrapRegistersTheDeliveryAndInboxHandlers(t *testing.T) {
|
||||
handlers, err := eventHandlers(consumerTestConfig(t), slog.New(slog.DiscardHandler))
|
||||
if err != nil {
|
||||
t.Fatalf("eventHandlers() error = %v", err)
|
||||
}
|
||||
if len(handlers) != 1 || handlers[0].ConsumerName() != notification.DeliveryConsumerName {
|
||||
if len(handlers) != 2 || handlers[0].ConsumerName() != notification.DeliveryConsumerName ||
|
||||
handlers[1].ConsumerName() != notification.InboxConsumerName {
|
||||
t.Fatalf("eventHandlers() = %v; update the documentation when the registered consumers change", handlers)
|
||||
}
|
||||
if !handlers[0].Handles(notification.MailRequestedEventType) {
|
||||
t.Fatalf("the delivery handler does not handle %s", notification.MailRequestedEventType)
|
||||
}
|
||||
if !handlers[1].Handles(string(domain.NotificationInboxInvitationDelivered)) ||
|
||||
handlers[1].Handles(notification.MailRequestedEventType) {
|
||||
t.Fatal("the inbox handler must handle the invitation event and no mail request")
|
||||
}
|
||||
if handlers[1].Handles(string(domain.NotificationInboxSyntheticDelivered)) {
|
||||
t.Fatal("the synthetic inbox event is handled although its switch is off by default")
|
||||
}
|
||||
settings := consumerTestConfig(t)
|
||||
settings.NotificationSyntheticEnabled = true
|
||||
enabled, err := eventHandlers(settings, slog.New(slog.DiscardHandler))
|
||||
if err != nil || !enabled[1].Handles(string(domain.NotificationInboxSyntheticDelivered)) {
|
||||
t.Fatalf("the synthetic inbox event is not handled with its switch on: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBootstrapRefusesAnInvalidMailAccountForDeliveries(t *testing.T) {
|
||||
|
||||
@@ -46,6 +46,8 @@ func newAPIHandler(
|
||||
assessmentSignalHandler *v1.AssessmentSignalHandler,
|
||||
privacyHandler *v1.PrivacyHandler,
|
||||
evidenceHandler *v1.EvidenceHandler,
|
||||
notificationHandler *v1.NotificationHandler,
|
||||
matchHandler *v1.MatchHandler,
|
||||
) *v1.Handler {
|
||||
return v1.NewHandler(health, problems,
|
||||
v1.WithIdentity(identityHandler),
|
||||
@@ -60,6 +62,8 @@ func newAPIHandler(
|
||||
v1.WithAssessmentSignals(assessmentSignalHandler),
|
||||
v1.WithPrivacy(privacyHandler),
|
||||
v1.WithEvidence(evidenceHandler),
|
||||
v1.WithNotifications(notificationHandler),
|
||||
v1.WithMatches(matchHandler),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
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/port/outbound"
|
||||
"lincodex/backend/internal/usecase/candidateview"
|
||||
"lincodex/backend/internal/usecase/identity"
|
||||
"lincodex/backend/internal/usecase/matching"
|
||||
)
|
||||
|
||||
const matchesPoolMaxConns = 4
|
||||
|
||||
func newMatchHandler(
|
||||
ctx context.Context,
|
||||
settings config.Config,
|
||||
logger *slog.Logger,
|
||||
keyring *identity.Keyring,
|
||||
) (*v1.MatchHandler, func(), error) {
|
||||
if settings.DatabaseURL == "" {
|
||||
logger.Warn("match search is not served because DATABASE_URL is not set")
|
||||
return nil, func() {}, nil
|
||||
}
|
||||
key, err := keyring.CandidateViewPseudonymKey()
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("bootstrap.newMatchHandler - derive projection key: %w", err)
|
||||
}
|
||||
pool, err := postgres.NewSizedPool(ctx, settings.DatabaseURL, matchesPoolMaxConns)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("bootstrap.newMatchHandler - 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.newMatchHandler - create view service: %w", err)
|
||||
}
|
||||
service, err := matching.NewService(matching.Dependencies{
|
||||
Transactor: transactor, Cards: views.ListVisibleCards, Views: searchViews(views), Clock: time.Now, Logger: logger,
|
||||
})
|
||||
if err != nil {
|
||||
pool.Close()
|
||||
return nil, nil, fmt.Errorf("bootstrap.newMatchHandler - create service: %w", err)
|
||||
}
|
||||
return v1.NewMatchHandler(service), pool.Close, nil
|
||||
}
|
||||
|
||||
// searchViews adapts the card view service to the search, which charges and
|
||||
// records the candidates it reveals like card reads.
|
||||
func searchViews(views *candidateview.Service) matching.BeginViews {
|
||||
return func(ctx context.Context, unit outbound.UnitOfWork, actorID, organizationID uuid.UUID) (matching.ViewSession, error) {
|
||||
session, err := views.BeginSearchViews(ctx, unit, actorID, organizationID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return session, nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
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/notification"
|
||||
)
|
||||
|
||||
const notificationPoolMaxConns = 4
|
||||
|
||||
// newNotificationHandler builds the inbox operations and the PostgreSQL pool
|
||||
// they own. Nothing connects here, so an unavailable database never blocks
|
||||
// startup. It returns no handler when DATABASE_URL is unset, and the
|
||||
// operations then answer 501.
|
||||
func newNotificationHandler(
|
||||
ctx context.Context,
|
||||
settings config.Config,
|
||||
logger *slog.Logger,
|
||||
) (*v1.NotificationHandler, func(), error) {
|
||||
if settings.DatabaseURL == "" {
|
||||
logger.Warn("notification inbox operations are not served because DATABASE_URL is not set")
|
||||
return nil, func() {}, nil
|
||||
}
|
||||
pool, err := postgres.NewSizedPool(ctx, settings.DatabaseURL, notificationPoolMaxConns)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("bootstrap.newNotificationHandler - create pool: %w", err)
|
||||
}
|
||||
service, err := notification.NewInboxService(
|
||||
postgres.NewTransactor(pool),
|
||||
notification.NewInboxContentResolver(notification.DefaultInboxSourceResolvers(settings.NotificationSyntheticEnabled)),
|
||||
time.Now,
|
||||
)
|
||||
if err != nil {
|
||||
pool.Close()
|
||||
return nil, nil, fmt.Errorf("bootstrap.newNotificationHandler - create service: %w", err)
|
||||
}
|
||||
return v1.NewNotificationHandler(service), pool.Close, nil
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"lincodex/backend/config"
|
||||
"lincodex/backend/internal/adapter/secondary/postgres"
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
"lincodex/backend/internal/usecase/policy"
|
||||
)
|
||||
|
||||
@@ -45,13 +46,34 @@ func seedDemoPolicy(ctx context.Context, settings config.Config, logger *slog.Lo
|
||||
logger.Info("product policy already present; demo seed skipped",
|
||||
slog.String("policy_key", string(domain.ProductPolicyKey)),
|
||||
)
|
||||
return nil
|
||||
} else {
|
||||
logger.Info("demo product policy published",
|
||||
slog.String("policy_key", string(result.Version.Key())),
|
||||
slog.Int("policy_version", int(result.Version.Version())),
|
||||
slog.String("policy_kind", string(result.Version.Kind())),
|
||||
slog.String("policy_version_id", result.Version.ID().String()),
|
||||
)
|
||||
}
|
||||
logger.Info("demo product policy published",
|
||||
slog.String("policy_key", string(result.Version.Key())),
|
||||
slog.Int("policy_version", int(result.Version.Version())),
|
||||
slog.String("policy_kind", string(result.Version.Kind())),
|
||||
slog.String("policy_version_id", result.Version.ID().String()),
|
||||
)
|
||||
warnWithoutMatchingSection(ctx, postgres.NewPolicyStore(pool).Policies(), logger)
|
||||
return nil
|
||||
}
|
||||
|
||||
// warnWithoutMatchingSection tells the operator at startup that the match
|
||||
// search fails closed: the effective product policy has no matching section, and
|
||||
// the demo seed publishes only the first version. It only warns; a policy the
|
||||
// service cannot read must not stop startup either.
|
||||
func warnWithoutMatchingSection(ctx context.Context, policies outbound.PolicyRepository, logger *slog.Logger) {
|
||||
version, err := policies.GetEffective(ctx, domain.ProductPolicyKey, time.Now())
|
||||
if err != nil {
|
||||
logger.Warn("the effective product policy could not be checked for the matching section",
|
||||
slog.String("policy_key", string(domain.ProductPolicyKey)),
|
||||
)
|
||||
return
|
||||
}
|
||||
if version.Policy().Matching == nil {
|
||||
logger.Warn("the effective product policy has no matching section; match search answers with an internal error until an operator publishes it (make -C backend policy-publish ARGS='matching --actor operator:<handle>')",
|
||||
slog.String("policy_key", string(version.Key())),
|
||||
slog.Int("policy_version", int(version.Version())),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -58,6 +58,8 @@ func initialize(
|
||||
newAssessmentSignalHandler,
|
||||
newPrivacyHandler,
|
||||
newEvidenceHandler,
|
||||
newNotificationHandler,
|
||||
newMatchHandler,
|
||||
newSessionSecurity,
|
||||
newRouter,
|
||||
newOutboxRelay,
|
||||
|
||||
@@ -202,10 +202,7 @@ func initialize(ctx context.Context, config2 config.Config, logger *slog.Logger,
|
||||
cleanup()
|
||||
return nil, nil, err
|
||||
}
|
||||
handler := newAPIHandler(healthHandler, responder, identityHandler, consentHandler, profileHandler, questionnaireHandler, catalogHandler, organizationHandler, needHandler, supportHandler, assessmentHandler, assessmentSignalHandler, privacyHandler, evidenceHandler)
|
||||
sessionSecurity := newSessionSecurity(identityHandler)
|
||||
v := newTrustedProxies(config2)
|
||||
httpHandler, err := newRouter(healthHandler, handler, sessionSecurity, responder, metrics, logger, v, organizationHandler)
|
||||
notificationHandler, cleanup16, err := newNotificationHandler(ctx, config2, logger)
|
||||
if err != nil {
|
||||
cleanup15()
|
||||
cleanup14()
|
||||
@@ -224,26 +221,7 @@ func initialize(ctx context.Context, config2 config.Config, logger *slog.Logger,
|
||||
cleanup()
|
||||
return nil, nil, err
|
||||
}
|
||||
relay, cleanup16, err := newOutboxRelay(ctx, config2, logger, metrics)
|
||||
if err != nil {
|
||||
cleanup15()
|
||||
cleanup14()
|
||||
cleanup13()
|
||||
cleanup12()
|
||||
cleanup11()
|
||||
cleanup10()
|
||||
cleanup9()
|
||||
cleanup8()
|
||||
cleanup7()
|
||||
cleanup6()
|
||||
cleanup5()
|
||||
cleanup4()
|
||||
cleanup3()
|
||||
cleanup2()
|
||||
cleanup()
|
||||
return nil, nil, err
|
||||
}
|
||||
bootstrapEventConsumers, cleanup17, err := newEventConsumers(ctx, config2, logger, metrics)
|
||||
matchHandler, cleanup17, err := newMatchHandler(ctx, config2, logger, keyring)
|
||||
if err != nil {
|
||||
cleanup16()
|
||||
cleanup15()
|
||||
@@ -263,7 +241,10 @@ func initialize(ctx context.Context, config2 config.Config, logger *slog.Logger,
|
||||
cleanup()
|
||||
return nil, nil, err
|
||||
}
|
||||
worker, cleanup18, err := newJobWorker(ctx, config2, logger, metrics)
|
||||
handler := newAPIHandler(healthHandler, responder, identityHandler, consentHandler, profileHandler, questionnaireHandler, catalogHandler, organizationHandler, needHandler, supportHandler, assessmentHandler, assessmentSignalHandler, privacyHandler, evidenceHandler, notificationHandler, matchHandler)
|
||||
sessionSecurity := newSessionSecurity(identityHandler)
|
||||
v := newTrustedProxies(config2)
|
||||
httpHandler, err := newRouter(healthHandler, handler, sessionSecurity, responder, metrics, logger, v, organizationHandler)
|
||||
if err != nil {
|
||||
cleanup17()
|
||||
cleanup16()
|
||||
@@ -284,7 +265,28 @@ func initialize(ctx context.Context, config2 config.Config, logger *slog.Logger,
|
||||
cleanup()
|
||||
return nil, nil, err
|
||||
}
|
||||
mailsenderWorker, cleanup19, err := newMailSenderWorker(ctx, config2, logger, metrics, mailSender)
|
||||
relay, cleanup18, err := newOutboxRelay(ctx, config2, logger, metrics)
|
||||
if err != nil {
|
||||
cleanup17()
|
||||
cleanup16()
|
||||
cleanup15()
|
||||
cleanup14()
|
||||
cleanup13()
|
||||
cleanup12()
|
||||
cleanup11()
|
||||
cleanup10()
|
||||
cleanup9()
|
||||
cleanup8()
|
||||
cleanup7()
|
||||
cleanup6()
|
||||
cleanup5()
|
||||
cleanup4()
|
||||
cleanup3()
|
||||
cleanup2()
|
||||
cleanup()
|
||||
return nil, nil, err
|
||||
}
|
||||
bootstrapEventConsumers, cleanup19, err := newEventConsumers(ctx, config2, logger, metrics)
|
||||
if err != nil {
|
||||
cleanup18()
|
||||
cleanup17()
|
||||
@@ -306,7 +308,7 @@ func initialize(ctx context.Context, config2 config.Config, logger *slog.Logger,
|
||||
cleanup()
|
||||
return nil, nil, err
|
||||
}
|
||||
sampler, cleanup20, err := newDeliveryMetrics(ctx, config2, logger, metrics)
|
||||
worker, cleanup20, err := newJobWorker(ctx, config2, logger, metrics)
|
||||
if err != nil {
|
||||
cleanup19()
|
||||
cleanup18()
|
||||
@@ -329,8 +331,59 @@ func initialize(ctx context.Context, config2 config.Config, logger *slog.Logger,
|
||||
cleanup()
|
||||
return nil, nil, err
|
||||
}
|
||||
mailsenderWorker, cleanup21, err := newMailSenderWorker(ctx, config2, logger, metrics, mailSender)
|
||||
if err != nil {
|
||||
cleanup20()
|
||||
cleanup19()
|
||||
cleanup18()
|
||||
cleanup17()
|
||||
cleanup16()
|
||||
cleanup15()
|
||||
cleanup14()
|
||||
cleanup13()
|
||||
cleanup12()
|
||||
cleanup11()
|
||||
cleanup10()
|
||||
cleanup9()
|
||||
cleanup8()
|
||||
cleanup7()
|
||||
cleanup6()
|
||||
cleanup5()
|
||||
cleanup4()
|
||||
cleanup3()
|
||||
cleanup2()
|
||||
cleanup()
|
||||
return nil, nil, err
|
||||
}
|
||||
sampler, cleanup22, err := newDeliveryMetrics(ctx, config2, logger, metrics)
|
||||
if err != nil {
|
||||
cleanup21()
|
||||
cleanup20()
|
||||
cleanup19()
|
||||
cleanup18()
|
||||
cleanup17()
|
||||
cleanup16()
|
||||
cleanup15()
|
||||
cleanup14()
|
||||
cleanup13()
|
||||
cleanup12()
|
||||
cleanup11()
|
||||
cleanup10()
|
||||
cleanup9()
|
||||
cleanup8()
|
||||
cleanup7()
|
||||
cleanup6()
|
||||
cleanup5()
|
||||
cleanup4()
|
||||
cleanup3()
|
||||
cleanup2()
|
||||
cleanup()
|
||||
return nil, nil, err
|
||||
}
|
||||
application := NewApplication(config2, httpHandler, metrics, logger, mailSender, relay, bootstrapEventConsumers, worker, mailsenderWorker, sampler)
|
||||
return application, func() {
|
||||
cleanup22()
|
||||
cleanup21()
|
||||
cleanup20()
|
||||
cleanup19()
|
||||
cleanup18()
|
||||
|
||||
Reference in new issue
Block a user