Skip to content

Commit

Permalink
chore: improve dependency injection role repository (#120)
Browse files Browse the repository at this point in the history
Signed-off-by: Otieno Calvine <nyarangaotieno@gmail.com>
  • Loading branch information
NYARAS committed Aug 30, 2021
1 parent a9a60ab commit 995e973
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 1 deletion.
69 changes: 68 additions & 1 deletion pkg/onboarding/infrastructure/database/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/savannahghi/profileutils"
)

// OnboardingRepository interface that provide access to all persistent storage operations
// Repository interface that provide access to all persistent storage operations
type Repository interface {
UserProfileRepository

Expand Down Expand Up @@ -270,3 +270,70 @@ func (d DbService) CheckPreconditions() {
func (d DbService) StageProfileNudge(ctx context.Context, nudge *feedlib.Nudge) error {
return d.firestore.StageProfileNudge(ctx, nudge)
}

// CreateRole creates a new role and persists it to the database
func (d DbService) CreateRole(
ctx context.Context,
profileID string,
input dto.RoleInput,
) (*profileutils.Role, error) {
return d.firestore.CreateRole(ctx, profileID, input)
}

// GetAllRoles returns a list of all created roles
func (d DbService) GetAllRoles(ctx context.Context) (*[]profileutils.Role, error) {
return d.firestore.GetAllRoles(ctx)
}

// GetRoleByID gets role with matching id
func (d DbService) GetRoleByID(ctx context.Context, roleID string) (*profileutils.Role, error) {
return d.firestore.GetRoleByID(ctx, roleID)
}

// GetRoleByName retrieves a role using it's name
func (d DbService) GetRoleByName(ctx context.Context, roleName string) (*profileutils.Role, error) {
return d.firestore.GetRoleByName(ctx, roleName)
}

// GetRolesByIDs gets all roles matching provided roleIDs if specified otherwise all roles
func (d DbService) GetRolesByIDs(ctx context.Context, roleIDs []string) (*[]profileutils.Role, error) {
return d.firestore.GetRolesByIDs(ctx, roleIDs)
}

// CheckIfRoleNameExists checks if a role with a similar name exists
// Ensures unique name for each role during creation
func (d DbService) CheckIfRoleNameExists(ctx context.Context, name string) (bool, error) {
return d.firestore.CheckIfRoleNameExists(ctx, name)
}

// UpdateRoleDetails updates the details of a role
func (d DbService) UpdateRoleDetails(ctx context.Context, profileID string, role profileutils.Role) (*profileutils.Role, error) {
return d.firestore.UpdateRoleDetails(ctx, profileID, role)
}

// DeleteRole removes a role permanently from the database
func (d DbService) DeleteRole(ctx context.Context, roleID string) (bool, error) {
return d.firestore.DeleteRole(ctx, roleID)
}

//CheckIfUserHasPermission checks if a user has the required permission
func (d DbService) CheckIfUserHasPermission(
ctx context.Context,
UID string,
requiredPermission profileutils.Permission,
) (bool, error) {
return d.firestore.CheckIfUserHasPermission(ctx, UID, requiredPermission)
}

// GetUserProfilesByRoleID returns a list of user profiles with the role ID
// i.e users assigned a particular role
func (d DbService) GetUserProfilesByRoleID(ctx context.Context, role string) ([]*profileutils.UserProfile, error) {
return d.firestore.GetUserProfilesByRoleID(ctx, role)
}

// SaveRoleRevocation records a log for a role revocation
//
// userId is the ID of the user removing a role from a user
func (d DbService) SaveRoleRevocation(ctx context.Context, userID string, revocation dto.RoleRevocationInput) error {
return d.firestore.SaveRoleRevocation(ctx, userID, revocation)
}
69 changes: 69 additions & 0 deletions pkg/onboarding/infrastructure/infrastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"

"github.com/savannahghi/feedlib"
"github.com/savannahghi/onboarding/pkg/onboarding/application/dto"
"github.com/savannahghi/onboarding/pkg/onboarding/infrastructure/database"
"github.com/savannahghi/profileutils"
)

// Infrastructure defines the contract provided by the infrastructure layer
Expand Down Expand Up @@ -34,3 +36,70 @@ func (i Interactor) CheckPreconditions() {}
func (i Interactor) StageProfileNudge(ctx context.Context, nudge *feedlib.Nudge) error {
return i.database.StageProfileNudge(ctx, nudge)
}

// CreateRole creates a new role and persists it to the database
func (i Interactor) CreateRole(
ctx context.Context,
profileID string,
input dto.RoleInput,
) (*profileutils.Role, error) {
return i.database.CreateRole(ctx, profileID, input)
}

// GetAllRoles returns a list of all created roles
func (i Interactor) GetAllRoles(ctx context.Context) (*[]profileutils.Role, error) {
return i.database.GetAllRoles(ctx)
}

// GetRoleByID gets role with matching id
func (i Interactor) GetRoleByID(ctx context.Context, roleID string) (*profileutils.Role, error) {
return i.database.GetRoleByID(ctx, roleID)
}

// GetRoleByName retrieves a role using it's name
func (i Interactor) GetRoleByName(ctx context.Context, roleName string) (*profileutils.Role, error) {
return i.database.GetRoleByName(ctx, roleName)
}

// GetRolesByIDs gets all roles matching provided roleIDs if specified otherwise all roles
func (i Interactor) GetRolesByIDs(ctx context.Context, roleIDs []string) (*[]profileutils.Role, error) {
return i.database.GetRolesByIDs(ctx, roleIDs)
}

// CheckIfRoleNameExists checks if a role with a similar name exists
// Ensures unique name for each role during creation
func (i Interactor) CheckIfRoleNameExists(ctx context.Context, name string) (bool, error) {
return i.database.CheckIfRoleNameExists(ctx, name)
}

// UpdateRoleDetails updates the details of a role
func (i Interactor) UpdateRoleDetails(ctx context.Context, profileID string, role profileutils.Role) (*profileutils.Role, error) {
return i.database.UpdateRoleDetails(ctx, profileID, role)
}

// DeleteRole removes a role permanently from the database
func (i Interactor) DeleteRole(ctx context.Context, roleID string) (bool, error) {
return i.database.DeleteRole(ctx, roleID)
}

//CheckIfUserHasPermission checks if a user has the required permission
func (i Interactor) CheckIfUserHasPermission(
ctx context.Context,
UID string,
requiredPermission profileutils.Permission,
) (bool, error) {
return i.database.CheckIfUserHasPermission(ctx, UID, requiredPermission)
}

// GetUserProfilesByRoleID returns a list of user profiles with the role ID
// i.e users assigned a particular role
func (i Interactor) GetUserProfilesByRoleID(ctx context.Context, role string) ([]*profileutils.UserProfile, error) {
return i.database.GetUserProfilesByRoleID(ctx, role)
}

// SaveRoleRevocation records a log for a role revocation
//
// userId is the ID of the user removing a role from a user
func (i Interactor) SaveRoleRevocation(ctx context.Context, userID string, revocation dto.RoleRevocationInput) error {
return i.database.SaveRoleRevocation(ctx, userID, revocation)
}

0 comments on commit 995e973

Please sign in to comment.