format phone number; dont break if notification fails

This commit is contained in:
GotPPay
2018-05-29 20:19:33 +02:00
parent f1ac874276
commit 54ebc056d5

View File

@@ -2,6 +2,7 @@ package selfregisterroute
import (
"fmt"
"strings"
"sync"
b64 "encoding/base64"
@@ -22,6 +23,10 @@ const (
notificationSmsBody = "You have registered as a Visit Reporter for CHM NEMT. Login: https://portal.bcbsinstitute.com Reset PW: https://portal.bcbsinstitute.com/forgot"
)
const (
phoneNumberMaxLength = 12
)
var (
instance *controller
once sync.Once
@@ -44,12 +49,34 @@ func controllerInstance(svc *applicationservice.Service, cfg *config.Config) *co
return instance
}
func removeNonNumberChars(input string) string {
result := ""
for _, char := range input {
if char >= '0' && char <= '9' {
result += string(char)
}
}
return result
}
func (c *controller) handle(ctx echo.Context) error {
var user viewmodel.User
if err := ctx.Bind(&user); err != nil {
return routeutils.HandleAPIError(ctx, err)
}
//format phone number - max length in database is 12 chars
formatedPhoneNumber := strings.TrimSpace(*user.PhoneNumber)
formatedPhoneNumber = strings.Replace(formatedPhoneNumber, "+1", "", -1)
formatedPhoneNumber = removeNonNumberChars(formatedPhoneNumber)
if len(formatedPhoneNumber) > phoneNumberMaxLength {
formatedPhoneNumber = formatedPhoneNumber[:phoneNumberMaxLength]
}
*user.PhoneNumber = formatedPhoneNumber
authUser, err := c.svc.Users.GetByUUID("573c70ff-733d-11e7-ba8f-0a6ad3fcdeaa", "")
if err != nil {
return routeutils.HandleAPIError(ctx, err)
@@ -132,10 +159,14 @@ func (c *controller) handle(ctx echo.Context) error {
notification, err = c.svc.Notification.SendNotificationWithoutWritingToDatabase(notification)
if err != nil {
return routeutils.HandleAPIError(ctx, err)
logger := ctx.Logger()
logger.Warnf("Application Error: Could not send email notification to user email : %s", *user.Email)
}
//Send sms notification to Authorized user
formatedPhoneNumber = *user.PhoneNumber
notification = viewmodel.Notification{
Type: applicationservice.NOtificationTypeSMS,
To: *user.PhoneNumber,
@@ -144,7 +175,8 @@ func (c *controller) handle(ctx echo.Context) error {
notification, err = c.svc.Notification.SendNotificationWithoutWritingToDatabase(notification)
if err != nil {
return routeutils.HandleAPIError(ctx, err)
logger := ctx.Logger()
logger.Warnf("Application Error: Could not send sms notification to user mobile : %s", *user.PhoneNumber)
}
return routeutils.ResponseAPIOK(ctx, user)