Skip to content

Commit

Permalink
Signed-off-by: Kathurima Kimathi <kathurimakimathi415@gmail.com>
Browse files Browse the repository at this point in the history
add onboarding depedency injection
  • Loading branch information
KathurimaKimathi committed Sep 1, 2021
1 parent b028b22 commit 37458b3
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 16 deletions.
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/savannahghi/interserviceclient"
"github.com/savannahghi/onboarding/pkg/onboarding/application/extension"
"github.com/savannahghi/onboarding/pkg/onboarding/domain"
"github.com/savannahghi/onboarding/pkg/onboarding/infrastructure/database"
"github.com/savannahghi/onboarding/pkg/onboarding/infrastructure/database/fb"
"github.com/savannahghi/profileutils"
"github.com/savannahghi/scalarutils"
Expand Down Expand Up @@ -104,6 +105,7 @@ func TestMain(m *testing.M) {
}

func InitializeTestService(ctx context.Context) (*interactor.Interactor, error) {
db := database.NewDbService()
fsc, fbc := InitializeTestFirebaseClient(ctx)
if fsc == nil {
return nil, fmt.Errorf("failed to initialize test FireStore client")
Expand Down Expand Up @@ -139,7 +141,7 @@ func InitializeTestService(ctx context.Context) (*interactor.Interactor, error)
ps, err := pubsubmessaging.NewServicePubSubMessaging(
pubSubClient,
ext,
fr,
*db,
)
if err != nil {
return nil, fmt.Errorf("unable to initialize new pubsub messaging service: %w", err)
Expand Down
147 changes: 143 additions & 4 deletions pkg/onboarding/infrastructure/infrastructure.go
Expand Up @@ -2,33 +2,77 @@ package infrastructure

import (
"context"
"fmt"

"cloud.google.com/go/pubsub"
"github.com/savannahghi/enumutils"
"github.com/savannahghi/feedlib"
"github.com/savannahghi/firebasetools"
"github.com/savannahghi/onboarding/pkg/onboarding/application/dto"
"github.com/savannahghi/onboarding/pkg/onboarding/application/extension"
"github.com/savannahghi/onboarding/pkg/onboarding/domain"
"github.com/savannahghi/onboarding/pkg/onboarding/infrastructure/database"
"github.com/savannahghi/onboarding/pkg/onboarding/infrastructure/services/engagement"
pubsubmessaging "github.com/savannahghi/onboarding/pkg/onboarding/infrastructure/services/pubsub"
"github.com/savannahghi/profileutils"
"github.com/savannahghi/serverutils"
)

const (
// ServiceName ..
ServiceName = "onboarding"

// TopicVersion ...
TopicVersion = "v1"
)

// Infrastructure defines the contract provided by the infrastructure layer
// It's a combination of interactions with external services/dependencies
type Infrastructure interface {
database.Repository
engagement.ServiceEngagement
pubsubmessaging.ServicePubSub
}

// Interactor is an implementation of the infrastructure interface
// It combines each individual service implementation
type Interactor struct {
database *database.DbService
database *database.DbService
Engagement engagement.ServiceEngagement
PubSub pubsubmessaging.ServicePubSub
}

// NewInfrastructureInteractor initializes a new infrastructure interactor
func NewInfrastructureInteractor() *Interactor {
func NewInfrastructureInteractor() (*Interactor, error) {
ctx := context.Background()
db := database.NewDbService()
return &Interactor{
database: db,
iscExt := extension.NewISCExtension()

fc := &firebasetools.FirebaseClient{}

projectID, err := serverutils.GetEnvVar(serverutils.GoogleCloudProjectIDEnvVarName)
if err != nil {
return nil, err
}

pubSubClient, err := pubsub.NewClient(ctx, projectID)
if err != nil {
return nil, err
}

baseExt := extension.NewBaseExtensionImpl(fc)

engagement := engagement.NewServiceEngagementImpl(iscExt, baseExt)
pubsub, err := pubsubmessaging.NewServicePubSubMessaging(pubSubClient, baseExt, *db)
if err != nil {
return nil, fmt.Errorf("unable to initialize new pubsub messaging service: %w", err)
}

return &Interactor{
database: db,
Engagement: engagement,
PubSub: pubsub,
}, nil
}

// CheckPreconditions ensures correct initialization
Expand Down Expand Up @@ -415,3 +459,98 @@ func (i Interactor) SetUserCommunicationsSettings(ctx context.Context, profileID
allowWhatsApp *bool, allowTextSms *bool, allowPush *bool, allowEmail *bool) (*profileutils.UserCommunicationsSetting, error) {
return i.database.SetUserCommunicationsSettings(ctx, profileID, allowWhatsApp, allowTextSms, allowPush, allowEmail)
}

// ResolveDefaultNudgeByTitle ...
func (i Interactor) ResolveDefaultNudgeByTitle(ctx context.Context, UID string, flavour feedlib.Flavour,
nudgeTitle string) error {
return i.Engagement.ResolveDefaultNudgeByTitle(ctx, UID, flavour, nudgeTitle)
}

// SendMail ...
func (i Interactor) SendMail(ctx context.Context, email string, message string, subject string) error {
return i.Engagement.SendMail(ctx, email, message, subject)
}

// GenerateAndSendOTP ...
func (i Interactor) GenerateAndSendOTP(ctx context.Context, phone string, appID *string) (*profileutils.OtpResponse, error) {
return i.Engagement.GenerateAndSendOTP(ctx, phone, appID)
}

// SendRetryOTP ...
func (i Interactor) SendRetryOTP(ctx context.Context, msisdn string, retryStep int, appID *string) (*profileutils.OtpResponse, error) {
return i.Engagement.SendRetryOTP(ctx, msisdn, retryStep, appID)
}

// VerifyOTP ...
func (i Interactor) VerifyOTP(ctx context.Context, phone, OTP string) (bool, error) {
return i.Engagement.VerifyOTP(ctx, phone, OTP)
}

// VerifyEmailOTP ...
func (i Interactor) VerifyEmailOTP(ctx context.Context, email, OTP string) (bool, error) {
return i.Engagement.VerifyEmailOTP(ctx, email, OTP)
}

// SendSMS ...
func (i Interactor) SendSMS(ctx context.Context, phoneNumbers []string, message string) error {
return i.Engagement.SendSMS(ctx, phoneNumbers, message)
}

// AddEngagementPubsubNameSpace creates a namespaced topic that resembles the one in
// engagement service, which is prepended with the word "engagement". This solves the problem
// where namespaced topics from "onboarding" are different from the ones in engagement.
// This fix allows for uniformity of topic names between the engagement and onboarding services.
func (i Interactor) AddEngagementPubsubNameSpace(
topic string,
) string {
return i.PubSub.AddEngagementPubsubNameSpace(topic)
}

// AddPubSubNamespace creates a namespaced topic name
func (i Interactor) AddPubSubNamespace(topicName string) string {
return i.PubSub.AddPubSubNamespace(topicName)
}

// TopicIDs returns the known (registered) topic IDs
func (i Interactor) TopicIDs() []string {
return i.PubSub.TopicIDs()
}

// PublishToPubsub sends a message to a specifeid Topic
func (i Interactor) PublishToPubsub(
ctx context.Context,
topicID string,
payload []byte,
) error {
return i.PubSub.PublishToPubsub(
ctx,
topicID,
payload,
)
}

// EnsureTopicsExist creates the topic(s) in the suppplied list if they do not
// already exist.
func (i Interactor) EnsureTopicsExist(
ctx context.Context,
topicIDs []string,
) error {
return i.PubSub.EnsureTopicsExist(
ctx,
topicIDs,
)
}

// EnsureSubscriptionsExist ensures that the subscriptions named in the supplied
// topic:subscription map exist. If any does not exist, it is created.
func (i Interactor) EnsureSubscriptionsExist(
ctx context.Context,
) error {

return i.PubSub.EnsureSubscriptionsExist(ctx)
}

// SubscriptionIDs returns a map of topic IDs to subscription IDs
func (i Interactor) SubscriptionIDs() map[string]string {
return i.PubSub.SubscriptionIDs()
}
16 changes: 8 additions & 8 deletions pkg/onboarding/infrastructure/services/pubsub/service.go
Expand Up @@ -7,7 +7,7 @@ import (

"cloud.google.com/go/pubsub"
"github.com/savannahghi/onboarding/pkg/onboarding/application/extension"
"github.com/savannahghi/onboarding/pkg/onboarding/repository"
"github.com/savannahghi/onboarding/pkg/onboarding/infrastructure/database"
)

const (
Expand Down Expand Up @@ -49,21 +49,21 @@ type ServicePubSub interface {

// ServicePubSubMessaging sends "real" (production) notifications
type ServicePubSubMessaging struct {
client *pubsub.Client
baseExt extension.BaseExtension
repo repository.OnboardingRepository
client *pubsub.Client
baseExt extension.BaseExtension
database database.DbService
}

// NewServicePubSubMessaging ...
func NewServicePubSubMessaging(
client *pubsub.Client,
ext extension.BaseExtension,
repo repository.OnboardingRepository,
db database.DbService,
) (*ServicePubSubMessaging, error) {
s := &ServicePubSubMessaging{
client: client,
baseExt: ext,
repo: repo,
client: client,
baseExt: ext,
database: db,
}

ctx := context.Background()
Expand Down
4 changes: 3 additions & 1 deletion pkg/onboarding/presentation/config.go
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/savannahghi/onboarding/pkg/onboarding/application/extension"
"github.com/savannahghi/onboarding/pkg/onboarding/application/utils"
"github.com/savannahghi/onboarding/pkg/onboarding/domain"
"github.com/savannahghi/onboarding/pkg/onboarding/infrastructure/database"
"github.com/savannahghi/onboarding/pkg/onboarding/infrastructure/database/fb"
"github.com/savannahghi/onboarding/pkg/onboarding/infrastructure/services/engagement"

Expand Down Expand Up @@ -60,6 +61,7 @@ var allowedHeaders = []string{
// Router sets up the ginContext router
func Router(ctx context.Context) (*mux.Router, error) {
fc := &firebasetools.FirebaseClient{}
db := database.NewDbService()
firebaseApp, err := fc.InitFirebase()
if err != nil {
return nil, err
Expand Down Expand Up @@ -108,7 +110,7 @@ func Router(ctx context.Context) (*mux.Router, error) {
pubSub, err := pubsubmessaging.NewServicePubSubMessaging(
pubSubClient,
baseExt,
repo,
*db,
)
if err != nil {
return nil, fmt.Errorf("unable to initialize new pubsub messaging service: %w", err)
Expand Down
4 changes: 3 additions & 1 deletion pkg/onboarding/usecases/login_test.go
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/savannahghi/onboarding/pkg/onboarding/application/exceptions"
"github.com/savannahghi/onboarding/pkg/onboarding/application/utils"
"github.com/savannahghi/onboarding/pkg/onboarding/domain"
"github.com/savannahghi/onboarding/pkg/onboarding/infrastructure/database"
"github.com/savannahghi/onboarding/pkg/onboarding/infrastructure/database/fb"
"github.com/savannahghi/onboarding/pkg/onboarding/presentation/interactor"
"github.com/savannahghi/onboarding/pkg/onboarding/repository"
Expand Down Expand Up @@ -130,6 +131,7 @@ func InitializeTestFirebaseClient(ctx context.Context) (*firestore.Client, *auth
}

func InitializeTestService(ctx context.Context) (*interactor.Interactor, error) {
db := database.NewDbService()
fc := firebasetools.FirebaseClient{}
fa, err := fc.InitFirebase()
if err != nil {
Expand Down Expand Up @@ -175,7 +177,7 @@ func InitializeTestService(ctx context.Context) (*interactor.Interactor, error)
ps, err := pubsubmessaging.NewServicePubSubMessaging(
pubSubClient,
ext,
repo,
*db,
)
if err != nil {
return nil, fmt.Errorf("unable to initialize new pubsub messaging service: %w", err)
Expand Down
4 changes: 3 additions & 1 deletion tests/config_test.go
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/savannahghi/onboarding/pkg/onboarding/application/extension"
"github.com/savannahghi/onboarding/pkg/onboarding/application/utils"
"github.com/savannahghi/onboarding/pkg/onboarding/domain"
"github.com/savannahghi/onboarding/pkg/onboarding/infrastructure/database"
"github.com/savannahghi/onboarding/pkg/onboarding/infrastructure/database/fb"
"github.com/savannahghi/onboarding/pkg/onboarding/infrastructure/services/engagement"
"github.com/savannahghi/onboarding/pkg/onboarding/presentation/interactor"
Expand Down Expand Up @@ -87,6 +88,7 @@ func initializeAcceptanceTestFirebaseClient(ctx context.Context) (*firestore.Cli
}

func InitializeTestService(ctx context.Context) (*interactor.Interactor, error) {
db := database.NewDbService()
var repo repository.OnboardingRepository

if serverutils.MustGetEnvVar(domain.Repo) == domain.FirebaseRepository {
Expand Down Expand Up @@ -117,7 +119,7 @@ func InitializeTestService(ctx context.Context) (*interactor.Interactor, error)
ps, err := pubsubmessaging.NewServicePubSubMessaging(
pubSubClient,
ext,
repo,
*db,
)
if err != nil {
return nil, fmt.Errorf("unable to initialize new pubsub messaging service: %w", err)
Expand Down

0 comments on commit 37458b3

Please sign in to comment.