@@ -3,17 +3,21 @@ package service
33import (
44 "context"
55 "log"
6+ "net/http"
67 "os"
78
89 "github.com/IBM/sarama"
10+ "github.com/kelseyhightower/envconfig"
911
1012 eventsCommon "github.com/tidepool-org/go-common/events"
1113
1214 abbottClient "github.com/tidepool-org/platform-plugin-abbott/abbott/client"
1315 abbottProvider "github.com/tidepool-org/platform-plugin-abbott/abbott/provider"
1416 abbottWork "github.com/tidepool-org/platform-plugin-abbott/abbott/work"
1517
18+ confirmationClient "github.com/tidepool-org/hydrophone/client"
1619 "github.com/tidepool-org/platform/application"
20+ "github.com/tidepool-org/platform/auth"
1721 "github.com/tidepool-org/platform/clinics"
1822 dataDeduplicatorDeduplicator "github.com/tidepool-org/platform/data/deduplicator/deduplicator"
1923 dataDeduplicatorFactory "github.com/tidepool-org/platform/data/deduplicator/factory"
@@ -29,6 +33,7 @@ import (
2933 "github.com/tidepool-org/platform/errors"
3034 "github.com/tidepool-org/platform/events"
3135 logInternal "github.com/tidepool-org/platform/log"
36+ "github.com/tidepool-org/platform/mailer"
3237 metricClient "github.com/tidepool-org/platform/metric/client"
3338 oauthProvider "github.com/tidepool-org/platform/oauth/provider"
3439 "github.com/tidepool-org/platform/permission"
@@ -41,8 +46,12 @@ import (
4146 summaryClient "github.com/tidepool-org/platform/summary/client"
4247 syncTaskMongo "github.com/tidepool-org/platform/synctask/store/mongo"
4348 "github.com/tidepool-org/platform/twiist"
49+ "github.com/tidepool-org/platform/user"
50+ userClient "github.com/tidepool-org/platform/user/client"
4451 workService "github.com/tidepool-org/platform/work/service"
4552 workStoreStructuredMongo "github.com/tidepool-org/platform/work/store/structured/mongo"
53+
54+ "github.com/tidepool-org/platform/work/service/emailnotificationsprocessor"
4655)
4756
4857type Standard struct {
@@ -55,13 +64,16 @@ type Standard struct {
5564 syncTaskStore * syncTaskMongo.Store
5665 workStructuredStore * workStoreStructuredMongo.Store
5766 dataDeduplicatorFactory * dataDeduplicatorFactory.Factory
58- clinicsClient * clinics.Client
67+ clinicsClient clinics.Client
5968 dataClient * Client
6069 dataRawClient * dataRawService.Client
6170 dataSourceClient * dataSourceServiceClient.Client
71+ mailerClient mailer.Mailer
6272 summaryClient * summaryClient.Client
6373 workClient * workService.Client
6474 abbottClient * abbottClient.Client
75+ userClient user.Client
76+ confirmationClient confirmationClient.ClientWithResponsesInterface
6577 workCoordinator * workService.Coordinator
6678 userEventsHandler events.Runner
6779 twiistServiceAccountAuthorizer * twiist.ServiceAccountAuthorizer
@@ -116,9 +128,18 @@ func (s *Standard) Initialize(provider application.Provider) error {
116128 if err := s .initializeDataSourceClient (); err != nil {
117129 return err
118130 }
131+ if err := s .initializeMailerClient (); err != nil {
132+ return err
133+ }
134+ if err := s .initializeUserClient (); err != nil {
135+ return err
136+ }
119137 if err := s .initializeSummaryClient (); err != nil {
120138 return err
121139 }
140+ if err := s .initializeConfirmationClient (); err != nil {
141+ return err
142+ }
122143 if err := s .initializeWorkClient (); err != nil {
123144 return err
124145 }
@@ -470,7 +491,7 @@ func (s *Standard) initializeClinicsClient() error {
470491 if err != nil {
471492 return errors .Wrap (err , "unable to create clinics client" )
472493 }
473- s .clinicsClient = & clnt
494+ s .clinicsClient = clnt
474495
475496 return nil
476497}
@@ -511,6 +532,65 @@ func (s *Standard) initializeDataSourceClient() error {
511532 return nil
512533}
513534
535+ func (s * Standard ) initializeMailerClient () error {
536+ s .Logger ().Debug ("Initializing mailer client" )
537+ client , err := mailer .Client ()
538+ if err != nil {
539+ return errors .Wrap (err , "unable to create mailer client" )
540+ }
541+ s .mailerClient = client
542+ return nil
543+ }
544+
545+ func (s * Standard ) initializeUserClient () error {
546+ s .Logger ().Debug ("Initializing user client" )
547+ client , err := userClient .NewDefaultClient (userClient.Params {
548+ ConfigReporter : s .ConfigReporter (),
549+ Logger : s .Logger (),
550+ UserAgent : s .UserAgent (),
551+ })
552+ if err != nil {
553+ return errors .Wrap (err , "unable to create user client" )
554+ }
555+ s .userClient = client
556+ return nil
557+ }
558+
559+ type confirmationClientConfig struct {
560+ ServiceAddress string `envconfig:"TIDEPOOL_CONFIRMATION_CLIENT_ADDRESS"`
561+ }
562+
563+ func (c * confirmationClientConfig ) Load () error {
564+ return envconfig .Process ("" , c )
565+ }
566+
567+ func (s * Standard ) initializeConfirmationClient () error {
568+ s .Logger ().Debug ("Initializing confirmation client" )
569+
570+ cfg := & confirmationClientConfig {}
571+ if err := cfg .Load (); err != nil {
572+ return errors .Wrap (err , "unable to load confirmations client config" )
573+ }
574+
575+ opts := confirmationClient .WithRequestEditorFn (func (ctx context.Context , req * http.Request ) error {
576+ token , err := s .AuthClient ().ServerSessionToken ()
577+ if err != nil {
578+ return err
579+ }
580+
581+ req .Header .Add (auth .TidepoolSessionTokenHeaderKey , token )
582+ return nil
583+ })
584+
585+ client , err := confirmationClient .NewClientWithResponses (cfg .ServiceAddress , opts )
586+ if err != nil {
587+ return errors .Wrap (err , "unable to create confirmations client" )
588+ }
589+ s .confirmationClient = client
590+
591+ return nil
592+ }
593+
514594func (s * Standard ) initializeSummaryClient () error {
515595 s .Logger ().Debug ("Creating summarizer registry" )
516596
@@ -618,6 +698,23 @@ func (s *Standard) initializeWorkCoordinator() error {
618698 return errors .Wrap (err , "unable to register abbott processors" )
619699 }
620700
701+ emailDependencies := emailnotificationsprocessor.Dependencies {
702+ DataSources : s .dataSourceStructuredStore .NewDataSourcesRepository (),
703+ Mailer : s .mailerClient ,
704+ Auth : s .AuthClient (),
705+ Users : s .userClient ,
706+ Clinics : s .clinicsClient ,
707+ Confirmations : s .confirmationClient ,
708+ }
709+ emailNotifProcessors , err := emailnotificationsprocessor .NewProcessors (emailDependencies )
710+ if err != nil {
711+ return errors .Wrap (err , "unable to create email notifications processor" )
712+ }
713+
714+ if err = s .workCoordinator .RegisterProcessors (emailNotifProcessors ); err != nil {
715+ return errors .Wrap (err , "unable to register email notifications processor" )
716+ }
717+
621718 s .Logger ().Debug ("Starting work coordinator" )
622719
623720 s .workCoordinator .Start ()
0 commit comments