2018-05-18 18:55:00 +02:00
package selfregisterroute
import (
2018-05-22 12:40:22 +02:00
"fmt"
2018-05-18 18:55:00 +02:00
"sync"
2018-05-22 12:40:22 +02:00
b64 "encoding/base64"
2018-05-18 18:55:00 +02:00
"bitbucket.org/nemt/nemt-portal-api/application/applicationservice"
"bitbucket.org/nemt/nemt-portal-api/application/third/eligibility/bcbsi"
2018-05-22 12:40:22 +02:00
"bitbucket.org/nemt/nemt-portal-api/application/third/npd/npdmodel"
"bitbucket.org/nemt/nemt-portal-api/application/viewmodel"
2018-05-18 18:55:00 +02:00
"bitbucket.org/nemt/nemt-portal-api/infra/config"
"bitbucket.org/nemt/nemt-portal-api/server/router/routeutils"
2018-05-22 12:40:22 +02:00
"bitbucket.org/nemt/nemt-portal-api/server/validation"
2018-05-18 18:55:00 +02:00
"github.com/labstack/echo"
)
2018-05-29 17:22:48 +02:00
const (
notificationEmailSubject = "Registration sucessful"
notificationEmailBody = "You have registered as a Visit Reporter for CHM NEMT.\nLogin: https://portal.bcbsinstitute.com\nReset PW: https://portal.bcbsinstitute.com/forgot"
notificationSmsBody = "You have registered as a Visit Reporter for CHM NEMT. Login: https://portal.bcbsinstitute.com Reset PW: https://portal.bcbsinstitute.com/forgot"
)
2018-05-18 18:55:00 +02:00
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 {
2018-05-22 12:40:22 +02:00
var user viewmodel . User
if err := ctx . Bind ( & user ) ; err != nil {
return routeutils . HandleAPIError ( ctx , err )
}
2018-05-28 08:31:58 +02:00
authUser , err := c . svc . Users . GetByUUID ( "573c70ff-733d-11e7-ba8f-0a6ad3fcdeaa" , "" )
2018-05-22 12:40:22 +02:00
if err != nil {
return routeutils . HandleAPIError ( ctx , err )
}
2018-05-28 08:31:58 +02:00
2018-05-22 12:40:22 +02:00
pass , err := b64 . StdEncoding . DecodeString ( user . Pass )
if err != nil {
return routeutils . ResponseAPIValidationError ( ctx , "Invalid password" )
}
user . Pass = string ( pass )
2018-05-29 17:22:48 +02:00
if validationErrors := validation . ValidateSelfregistration ( & user ) ; len ( validationErrors ) > 0 {
return routeutils . ResponseAPICustomValidationError ( ctx , "Self registration failed" , validationErrors )
2018-05-22 12:40:22 +02:00
}
if len ( user . First ) != 0 && len ( user . Last ) != 0 {
user . Name = fmt . Sprintf ( "%s %s" , user . First , user . Last )
}
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 )
}
2018-05-18 18:55:00 +02:00
2018-05-29 17:22:48 +02:00
//Send email notification to Authorized user
notification := viewmodel . Notification {
Type : applicationservice . NotificationTypeEmail ,
From : c . cfg . Email . Sender ,
To : * user . Email ,
Subject : notificationEmailSubject ,
Message : notificationEmailBody ,
}
notification , err = c . svc . Notification . SendNotificationWithoutWritingToDatabase ( notification )
if err != nil {
return routeutils . HandleAPIError ( ctx , err )
}
//Send sms notification to Authorized user
notification = viewmodel . Notification {
Type : applicationservice . NOtificationTypeSMS ,
To : * user . PhoneNumber ,
Message : notificationSmsBody ,
}
notification , err = c . svc . Notification . SendNotificationWithoutWritingToDatabase ( notification )
if err != nil {
return routeutils . HandleAPIError ( ctx , err )
}
2018-05-22 12:40:22 +02:00
return routeutils . ResponseAPIOK ( ctx , user )
2018-05-18 18:55:00 +02:00
}