Files
old-svijetlastrana/server/router/eligibilityroute/controller.go

125 lines
3.1 KiB
Go
Raw Normal View History

2018-04-25 13:16:36 +02:00
package eligibilityroute
import (
2018-05-16 18:30:59 +02:00
"fmt"
2018-05-25 09:12:42 +02:00
"math/rand"
2018-04-25 13:16:36 +02:00
"sync"
2018-05-25 09:12:42 +02:00
"time"
2018-04-25 13:16:36 +02:00
"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/viewmodel"
2018-05-25 09:12:42 +02:00
"bitbucket.org/nemt/nemt-portal-api/infra/auth"
2018-04-25 13:16:36 +02:00
"bitbucket.org/nemt/nemt-portal-api/infra/config"
"bitbucket.org/nemt/nemt-portal-api/server/router/routeutils"
"github.com/labstack/echo"
)
var (
instance *controller
once sync.Once
)
type controller struct {
cfg *config.Config
svc *applicationservice.Service
bcbsi *bcbsi.Service
}
func controllerInstance(cfg *config.Config, svc *applicationservice.Service) *controller {
once.Do(func() {
instance = &controller{
cfg: cfg,
svc: svc,
bcbsi: bcbsi.New(cfg),
}
})
return instance
}
2018-05-25 09:12:42 +02:00
func (c *controller) rangeIn(low, hi int) string {
result := low + rand.Intn(hi-low)
return fmt.Sprintf("%v", result)
}
func (c *controller) generatePassword(n int) string {
const (
charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
)
return c.stringWithCharset(n, charset)
}
func (c *controller) stringWithCharset(length int, charset string) string {
b := make([]byte, length)
var seededRand *rand.Rand = rand.New(
rand.NewSource(time.Now().UnixNano()))
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}
2018-04-25 13:16:36 +02:00
func (c *controller) handleEligibility(ctx echo.Context) error {
var eligibility viewmodel.Eligibility
if err := ctx.Bind(&eligibility); err != nil {
return routeutils.HandleAPIError(ctx, err)
}
2018-05-25 09:12:42 +02:00
authUser, err := auth.GetUserDetail(ctx, c.cfg)
if err != nil {
return routeutils.HandleAPIError(ctx, err)
}
2018-05-30 08:45:32 +02:00
2018-06-01 10:39:46 +02:00
var provider viewmodel.ProviderResp
provider, err = c.svc.Provider.GetByNPI(eligibility.RawProvider.FivePartKeyGroups[0].ProviderNum, authUser)
2018-04-25 13:16:36 +02:00
if err != nil {
return routeutils.HandleAPIError(ctx, err)
}
2018-06-01 10:39:46 +02:00
if provider.ProviderUUID == "" {
org := viewmodel.Organization{
Type: viewmodel.OrganizationType{
Key: "provider",
Name: "Provider",
},
Name: eligibility.RawProvider.OrgName,
Description: eligibility.RawProvider.OrgName,
Main: false,
Author: authUser,
LastEditor: authUser,
Reference: eligibility.RawProvider,
2018-05-16 18:30:59 +02:00
}
2018-06-01 10:39:46 +02:00
org, err = c.svc.Organization.AddOrganization(org, authUser)
2018-05-25 09:12:42 +02:00
if err != nil {
return routeutils.HandleAPIError(ctx, err)
}
2018-06-01 10:39:46 +02:00
provider, err = c.svc.Provider.GetByOrganization(org.UUID, authUser)
2018-05-25 09:12:42 +02:00
if err != nil {
return routeutils.HandleAPIError(ctx, err)
}
2018-06-01 10:39:46 +02:00
}
2018-05-25 09:12:42 +02:00
2018-06-01 10:39:46 +02:00
user, err := c.svc.Users.CheckAndCreateMember(eligibility.User, provider, authUser)
if err != nil {
if validationError, ok := err.(*viewmodel.ValidationError); ok {
if len(validationError.Errors) > 0 {
return routeutils.ResponseAPICustomValidationError(ctx, validationError.Message, validationError.Errors)
} else {
return routeutils.ResponseAPIServiceError(ctx, validationError.Message)
2018-05-25 09:12:42 +02:00
}
2018-06-01 10:39:46 +02:00
} else {
2018-05-25 09:12:42 +02:00
return routeutils.HandleAPIError(ctx, err)
}
2018-06-01 10:39:46 +02:00
}
2018-05-25 09:12:42 +02:00
2018-06-01 10:39:46 +02:00
if authUser.Profiles[0].Key == "VIRPT" {
return ctx.JSON(204, nil)
2018-05-16 18:30:59 +02:00
} else {
2018-06-01 10:39:46 +02:00
return ctx.JSON(200, user)
2018-05-16 18:30:59 +02:00
}
2018-04-25 13:16:36 +02:00
}