144 lines
4.0 KiB
Go
144 lines
4.0 KiB
Go
package selfregisterroute
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
|
|
b64 "encoding/base64"
|
|
|
|
"bitbucket.org/nemt/nemt-portal-api/application/applicationservice"
|
|
"bitbucket.org/nemt/nemt-portal-api/application/third/eligibility/bcbsi"
|
|
"bitbucket.org/nemt/nemt-portal-api/application/third/npd/npdmodel"
|
|
"bitbucket.org/nemt/nemt-portal-api/application/viewmodel"
|
|
"bitbucket.org/nemt/nemt-portal-api/infra/auth"
|
|
"bitbucket.org/nemt/nemt-portal-api/infra/config"
|
|
"bitbucket.org/nemt/nemt-portal-api/server/router/routeutils"
|
|
"bitbucket.org/nemt/nemt-portal-api/server/validation"
|
|
"github.com/labstack/echo"
|
|
)
|
|
|
|
var (
|
|
instance *controller
|
|
once sync.Once
|
|
)
|
|
|
|
type controller struct {
|
|
svc *applicationservice.Service
|
|
cfg *config.Config
|
|
bcbsi *bcbsi.Service
|
|
}
|
|
|
|
func controllerInstance(svc *applicationservice.Service, cfg *config.Config) *controller {
|
|
once.Do(func() {
|
|
instance = &controller{
|
|
svc: svc,
|
|
cfg: cfg,
|
|
bcbsi: bcbsi.New(cfg),
|
|
}
|
|
})
|
|
return instance
|
|
}
|
|
|
|
func (c *controller) handle(ctx echo.Context) error {
|
|
var user viewmodel.User
|
|
if err := ctx.Bind(&user); err != nil {
|
|
return routeutils.HandleAPIError(ctx, err)
|
|
}
|
|
|
|
authUser, err := auth.GetUserDetail(ctx, c.cfg)
|
|
if err != nil {
|
|
return routeutils.HandleAPIError(ctx, err)
|
|
}
|
|
if user.PhoneNumber == nil || len(*user.PhoneNumber) == 0 {
|
|
return routeutils.ResponseAPIValidationError(ctx, "phonenumber is required")
|
|
}
|
|
|
|
if user.Email == nil || len(*user.Email) == 0 {
|
|
return routeutils.ResponseAPIValidationError(ctx, "email is required")
|
|
}
|
|
|
|
if len(user.Pass) == 0 {
|
|
return routeutils.ResponseAPIValidationError(ctx, "password is required")
|
|
}
|
|
|
|
pass, err := b64.StdEncoding.DecodeString(user.Pass)
|
|
if err != nil {
|
|
return routeutils.ResponseAPIValidationError(ctx, "Invalid password")
|
|
}
|
|
user.Pass = string(pass)
|
|
|
|
if passwordValidationErrors := validation.ValidatePassword(&user); len(passwordValidationErrors) > 0 {
|
|
return routeutils.ResponseAPICustomValidationError(ctx, "Password not strong enough", passwordValidationErrors)
|
|
}
|
|
|
|
if len(user.Name) == 0 && len(user.First) == 0 && len(user.Last) == 0 {
|
|
return routeutils.ResponseAPIValidationError(ctx, "name is required")
|
|
}
|
|
|
|
if len(user.First) != 0 && len(user.Last) != 0 {
|
|
user.Name = fmt.Sprintf("%s %s", user.First, user.Last)
|
|
}
|
|
|
|
if len(user.Provider.InternalID) == 0 || len(user.Provider.InternalID) > 10 {
|
|
return routeutils.ResponseAPIValidationError(ctx, "Provider NPI is invalid")
|
|
}
|
|
|
|
if len(user.Provider.OrganizatioName) == 0 {
|
|
return routeutils.ResponseAPIValidationError(ctx, "Provider Organization Name is invalid")
|
|
}
|
|
|
|
provider, err := c.svc.Provider.GetByNPI(user.Provider.InternalID, authUser)
|
|
if err != nil {
|
|
fmt.Println("Error to create organization", err)
|
|
return routeutils.HandleAPIError(ctx, err)
|
|
}
|
|
|
|
var organization viewmodel.Organization
|
|
if provider.ProviderUUID == "" {
|
|
org := viewmodel.Organization{
|
|
Author: authUser,
|
|
LastEditor: authUser,
|
|
Name: user.Provider.OrganizatioName,
|
|
Type: viewmodel.OrganizationType{
|
|
Key: "provider",
|
|
Name: "Provider",
|
|
},
|
|
Reference: npdmodel.ProviderResponse{
|
|
OrgName: user.Provider.OrganizatioName,
|
|
FivePartKeyGroups: []npdmodel.PartKeyGroup{
|
|
npdmodel.PartKeyGroup{
|
|
ProviderNum: user.Provider.InternalID,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
organization, err = c.svc.Organization.AddOrganization(org, authUser)
|
|
if err != nil {
|
|
fmt.Println("Error to create organization", err)
|
|
return routeutils.HandleAPIError(ctx, err)
|
|
}
|
|
|
|
if organization.UUID == "" {
|
|
return routeutils.ResponseAPIValidationError(ctx, "Error to create organization")
|
|
}
|
|
} else {
|
|
organization = provider.Organization
|
|
}
|
|
|
|
user.Organizations = []viewmodel.Organization{organization}
|
|
user.Profiles = []viewmodel.Profile{viewmodel.Profile{
|
|
Name: "Visit Reporter",
|
|
Key: "VIRPT",
|
|
Organization: organization,
|
|
}}
|
|
|
|
user, err = c.svc.Users.Create(user, authUser)
|
|
if err != nil {
|
|
fmt.Println("Error to create user", err)
|
|
return routeutils.HandleAPIError(ctx, err)
|
|
}
|
|
|
|
return routeutils.ResponseAPIOK(ctx, user)
|
|
}
|