Skip to content

Commit

Permalink
feat: implement ans ship facility usecase (#54)
Browse files Browse the repository at this point in the history
Signed-off-by: Kathurima Kimathi <kathurimakimathi415@gmail.com>
  • Loading branch information
KathurimaKimathi committed Oct 29, 2021
1 parent 62a1d52 commit 03a073b
Show file tree
Hide file tree
Showing 54 changed files with 1,209 additions and 12,070 deletions.
4 changes: 0 additions & 4 deletions go.mod
Expand Up @@ -12,7 +12,6 @@ require (
firebase.google.com/go v3.13.0+incompatible
github.com/99designs/gqlgen v0.13.0
github.com/GoogleCloudPlatform/cloudsql-proxy v1.26.0
github.com/Pallinder/go-randomdata v1.2.0
github.com/casbin/casbin/v2 v2.31.3
github.com/google/uuid v1.3.0
github.com/gorilla/handlers v1.5.1
Expand All @@ -22,7 +21,6 @@ require (
github.com/lib/pq v1.10.3
github.com/savannahghi/converterandformatter v0.0.9
github.com/savannahghi/enumutils v0.0.3
github.com/savannahghi/errorcodeutil v0.0.1
github.com/savannahghi/feedlib v0.0.4
github.com/savannahghi/firebasetools v0.0.15
github.com/savannahghi/interserviceclient v0.0.13
Expand All @@ -32,7 +30,6 @@ require (
github.com/savannahghi/serverutils v0.0.6
github.com/segmentio/ksuid v1.0.4
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0
github.com/tj/assert v0.0.3
github.com/vektah/gqlparser/v2 v2.1.0
go.opencensus.io v0.23.0
Expand All @@ -41,5 +38,4 @@ require (
gorm.io/datatypes v1.0.2
gorm.io/driver/postgres v1.1.2
gorm.io/gorm v1.21.16
moul.io/http2curl v1.0.0 // indirect
)
8 changes: 0 additions & 8 deletions go.sum
Expand Up @@ -88,8 +88,6 @@ github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/Pallinder/go-randomdata v1.2.0 h1:DZ41wBchNRb/0GfsePLiSwb0PHZmT67XY00lCDlaYPg=
github.com/Pallinder/go-randomdata v1.2.0/go.mod h1:yHmJgulpD2Nfrm0cR9tI/+oAgRqCQQixsA8HyRZfV9Y=
github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398/go.mod h1:a1uqRtAwp2Xwc6WNPJEufxJ7fx3npB4UV/JOLmbu5I0=
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
Expand Down Expand Up @@ -340,7 +338,6 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m
github.com/googleapis/gax-go/v2 v2.1.0/go.mod h1:Q3nei7sK6ybPYH7twZdmQpAd1MKb7pfu6SK+H1/DsU0=
github.com/googleapis/gax-go/v2 v2.1.1 h1:dp3bWCh+PPO1zjRRiCSczJav13sBvG4UhNyVTa1KqdU=
github.com/googleapis/gax-go/v2 v2.1.1/go.mod h1:hddJymUZASv3XPyGkUpKj8pPO47Rmb0eJc8R6ouapiM=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/context v0.0.0-20160226214623-1ea25387ff6f/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
Expand Down Expand Up @@ -476,7 +473,6 @@ github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/u
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k=
Expand Down Expand Up @@ -685,9 +681,7 @@ github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMB
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJOjmxWY=
Expand Down Expand Up @@ -1334,8 +1328,6 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
moul.io/http2curl v1.0.0 h1:6XwpyZOYsgZJrU8exnG87ncVkU1FVCcTRpwzOkTDUi8=
moul.io/http2curl v1.0.0/go.mod h1:f6cULg+e4Md/oW1cYmwW4IWQOVl2lGbmCNGOHvzX2kE=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
Expand Down
125 changes: 0 additions & 125 deletions pkg/onboarding/infrastructure/database/postgres/gorm/create.go
Expand Up @@ -8,19 +8,6 @@ import (
// Create contains all the methods used to perform a create operation in DB
type Create interface {
GetOrCreateFacility(ctx context.Context, facility *Facility) (*Facility, error)
CollectMetrics(ctx context.Context, metrics *Metric) (*Metric, error)
SavePin(ctx context.Context, pinData *PINData) (bool, error)
RegisterStaffUser(ctx context.Context, user *User, staff *StaffProfile) (*StaffUserProfile, error)
RegisterClient(
ctx context.Context,
userInput *User,
clientInput *ClientProfile,
) (*ClientUserProfile, error)

AddIdentifier(
ctx context.Context,
identifier *Identifier,
) (*Identifier, error)
}

// GetOrCreateFacility ...
Expand All @@ -33,115 +20,3 @@ func (db *PGInstance) GetOrCreateFacility(ctx context.Context, facility *Facilit

return facility, nil
}

// SavePin does the actual saving of the users PIN in the database
func (db *PGInstance) SavePin(ctx context.Context, pinData *PINData) (bool, error) {
err := db.DB.Create(pinData).Error

if err != nil {
return false, fmt.Errorf("failed to save pin data: %v", err)
}

return true, nil
}

// CollectMetrics takes the collected metrics and saves them in the database.
func (db *PGInstance) CollectMetrics(ctx context.Context, metrics *Metric) (*Metric, error) {
err := db.DB.Create(metrics).Error

if err != nil {
return nil, fmt.Errorf("failed to create a facility: %v", err)
}

return metrics, nil
}

// RegisterStaffUser creates both the user profile and the staff profile.
func (db *PGInstance) RegisterStaffUser(ctx context.Context, user *User, staff *StaffProfile) (*StaffUserProfile, error) {
// Initialize a database transaction
tx := db.DB.Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()
if err := tx.Error; err != nil {
return nil, fmt.Errorf("failied initialize database transaction %v", err)
}

// create a user profile, then rollback the transaction if it is unsuccessful
if err := tx.Create(user).Error; err != nil {
tx.Rollback()
return nil, fmt.Errorf("failed to create a user %v", err)
}

// assign userID in staff a value due to foreign keys constraint
staff.UserID = user.UserID

// create a staff profile, then rollback the transaction if it is unsuccessful
if err := tx.Create(staff).Error; err != nil {
tx.Rollback()
return nil, fmt.Errorf("failed to create a staff profile %v", err)
}

// try to commit the transactions
if err := tx.Commit().Error; err != nil {
tx.Rollback()
return nil, fmt.Errorf("transaction commit to create a staff profile failed: %v", err)
}

return &StaffUserProfile{
User: user,
Staff: staff,
}, nil
}

// RegisterClient picks the clients registration details and saves them in the database
func (db *PGInstance) RegisterClient(
ctx context.Context,
user *User,
clientProfile *ClientProfile,
) (*ClientUserProfile, error) {
// begin a transaction
tx := db.DB.Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()

if err := tx.Error; err != nil {
return nil, err
}

if err := tx.Create(user).Error; err != nil {
tx.Rollback()
return nil, fmt.Errorf("failed to create a user %v", err)
}

clientProfile.UserID = user.UserID

if err := tx.Create(clientProfile).Error; err != nil {
tx.Rollback()
return nil, fmt.Errorf("failed to create a client profile %v", err)
}

if err := tx.Commit().Error; err != nil {
tx.Rollback()
return nil, fmt.Errorf("transaction commit to create a staff profile failed: %v", err)
}

return &ClientUserProfile{
User: user,
Client: clientProfile,
}, nil

}

// AddIdentifier saves a client's identifier record to the database
func (db *PGInstance) AddIdentifier(ctx context.Context, identifier *Identifier) (*Identifier, error) {
if err := db.DB.Create(identifier).Error; err != nil {
return nil, fmt.Errorf("failed to create identifier: %v", err)
}
return identifier, nil
}

0 comments on commit 03a073b

Please sign in to comment.