refacto
This commit is contained in:
@@ -2,6 +2,7 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.coopgo.io/coopgo-apps/silvermobi/models"
|
||||
mobilityaccounts "git.coopgo.io/coopgo-platform/mobility-accounts/grpcapi"
|
||||
ma "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
|
||||
@@ -42,6 +43,7 @@ func (s MobilityAccountService) Login(ctx context.Context, username, password, n
|
||||
|
||||
func (s MobilityAccountService) UpdateAccountData(ctx context.Context, id string, data map[string]any) error {
|
||||
d, err := structpb.NewStruct(data)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -63,14 +65,17 @@ func (s MobilityAccountService) UpdatePassword(ctx context.Context, id string, p
|
||||
Id: id,
|
||||
Password: password,
|
||||
})
|
||||
if err == nil {
|
||||
return true
|
||||
} else {
|
||||
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (s MobilityAccountService) GetAccountUsername(ctx context.Context, username string, namespace string) (*models.Account, error) {
|
||||
func (s MobilityAccountService) GetAccountUsername(ctx context.Context,
|
||||
username string, namespace string) (*models.Account, error) {
|
||||
|
||||
resp, err := s.Client.GetAccountUsername(ctx, &mobilityaccounts.GetAccountUsernameRequest{
|
||||
Username: username,
|
||||
Namespace: namespace,
|
||||
@@ -78,17 +83,22 @@ func (s MobilityAccountService) GetAccountUsername(ctx context.Context, username
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.ToAccountModel(resp.Account.ToStorageType()), nil
|
||||
}
|
||||
|
||||
func (s MobilityAccountService) Register(ctx context.Context, username string, password string, email string, phone_number string, data map[string]any, namespace string) (*models.Account, error) {
|
||||
func (s MobilityAccountService) Register(ctx context.Context, username string, password string,
|
||||
email string, phoneNumber string, data map[string]any, namespace string) (*models.Account, error) {
|
||||
|
||||
var resp *mobilityaccounts.RegisterResponse
|
||||
|
||||
account := &ma.Account{
|
||||
Authentication: ma.AccountAuth{
|
||||
Local: ma.LocalAuth{
|
||||
Username: username,
|
||||
Password: password,
|
||||
Email: email,
|
||||
PhoneNumber: phone_number,
|
||||
PhoneNumber: phoneNumber,
|
||||
},
|
||||
},
|
||||
Namespace: namespace,
|
||||
@@ -100,28 +110,26 @@ func (s MobilityAccountService) Register(ctx context.Context, username string, p
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := s.Client.Register(ctx, &mobilityaccounts.RegisterRequest{
|
||||
if resp, err = s.Client.Register(ctx, &mobilityaccounts.RegisterRequest{
|
||||
Account: acc,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.ToAccountModel(resp.Account.ToStorageType()), nil
|
||||
}
|
||||
func (s MobilityAccountService) UpdatePhoneNumber(ctx context.Context, accountid string, phone_number string, verified bool, verification_code string) error {
|
||||
_, err := s.Client.UpdatePhoneNumber(
|
||||
func (s MobilityAccountService) UpdatePhoneNumber(ctx context.Context, accountID string, phoneNumber string,
|
||||
verified bool, verificationCode string) error {
|
||||
|
||||
if _, err := s.Client.UpdatePhoneNumber(
|
||||
ctx,
|
||||
&mobilityaccounts.UpdatePhoneNumberRequest{
|
||||
Id: accountid,
|
||||
PhoneNumber: phone_number,
|
||||
Id: accountID,
|
||||
PhoneNumber: phoneNumber,
|
||||
Verified: verified,
|
||||
VerificationCode: verification_code,
|
||||
VerificationCode: verificationCode,
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -129,20 +137,30 @@ func (s MobilityAccountService) UpdatePhoneNumber(ctx context.Context, accountid
|
||||
}
|
||||
|
||||
func (s MobilityAccountService) SetAccountType(ctx context.Context, id string, accountType string) error {
|
||||
account, err := s.Client.GetAccount(ctx, &mobilityaccounts.GetAccountRequest{
|
||||
|
||||
var (
|
||||
account *mobilityaccounts.GetAccountResponse
|
||||
err error
|
||||
)
|
||||
|
||||
if account, err = s.Client.GetAccount(ctx, &mobilityaccounts.GetAccountRequest{
|
||||
Id: id,
|
||||
})
|
||||
if err != nil {
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data := make(map[string]interface{})
|
||||
|
||||
data["type"] = accountType
|
||||
|
||||
dataStruct := &structpb.Struct{
|
||||
Fields: make(map[string]*structpb.Value),
|
||||
}
|
||||
|
||||
for key, value := range data {
|
||||
|
||||
stringValue, ok := value.(string)
|
||||
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
@@ -153,22 +171,34 @@ func (s MobilityAccountService) SetAccountType(ctx context.Context, id string, a
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
account.Account.Data = dataStruct
|
||||
_, err = s.Client.UpdateData(ctx, &mobilityaccounts.UpdateDataRequest{
|
||||
|
||||
if _, err = s.Client.UpdateData(ctx, &mobilityaccounts.UpdateDataRequest{
|
||||
Account: account.Account,
|
||||
})
|
||||
if err != nil {
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (s MobilityAccountService) UpdateAccountBirthDate(ctx context.Context, id string, namespace string, birthdate string) error {
|
||||
account, err := s.Client.GetAccount(ctx, &mobilityaccounts.GetAccountRequest{
|
||||
func (s MobilityAccountService) UpdateAccountBirthDate(ctx context.Context, id string,
|
||||
namespace string, birthdate string) error {
|
||||
|
||||
var (
|
||||
account *mobilityaccounts.GetAccountResponse
|
||||
err error
|
||||
)
|
||||
|
||||
if account, err = s.Client.GetAccount(ctx, &mobilityaccounts.GetAccountRequest{
|
||||
Id: id,
|
||||
})
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
data := make(map[string]interface{})
|
||||
|
||||
data["birthdate"] = birthdate
|
||||
|
||||
dataStruct := &structpb.Struct{
|
||||
@@ -177,6 +207,7 @@ func (s MobilityAccountService) UpdateAccountBirthDate(ctx context.Context, id s
|
||||
|
||||
for key, value := range data {
|
||||
stringValue, ok := value.(string)
|
||||
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
@@ -187,47 +218,56 @@ func (s MobilityAccountService) UpdateAccountBirthDate(ctx context.Context, id s
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
account.Account.Data = dataStruct
|
||||
_, err = s.Client.UpdateData(ctx, &mobilityaccounts.UpdateDataRequest{
|
||||
|
||||
if _, err = s.Client.UpdateData(ctx, &mobilityaccounts.UpdateDataRequest{
|
||||
Account: account.Account,
|
||||
})
|
||||
if err != nil {
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s MobilityAccountService) ToAccountModel(account ma.Account) *models.Account {
|
||||
first_name := account.Data["first_name"].(string)
|
||||
last_name := account.Data["last_name"].(string)
|
||||
birth_date, ok := account.Data["birthdate"].(string)
|
||||
if !ok {
|
||||
birth_date = ""
|
||||
|
||||
var (
|
||||
accountType, birthDate, phoneNumber, email string
|
||||
ok bool
|
||||
)
|
||||
|
||||
firstName := account.Data["first_name"].(string)
|
||||
lastName := account.Data["last_name"].(string)
|
||||
|
||||
if birthDate, ok = account.Data["birthdate"].(string); !ok {
|
||||
birthDate = ""
|
||||
}
|
||||
accountType, ok := account.Data["type"].(string)
|
||||
if !ok {
|
||||
|
||||
if accountType, ok = account.Data["type"].(string); !ok {
|
||||
accountType = ""
|
||||
}
|
||||
phone_number, ok := account.Data["phone_number"].(string)
|
||||
if !ok {
|
||||
phone_number = ""
|
||||
|
||||
if phoneNumber, ok = account.Data["phone_number"].(string); !ok {
|
||||
phoneNumber = ""
|
||||
}
|
||||
email, ok := account.Data["email"].(string)
|
||||
if !ok {
|
||||
|
||||
if email, ok = account.Data["email"].(string); !ok {
|
||||
email = ""
|
||||
}
|
||||
|
||||
return &models.Account{
|
||||
ID: account.ID,
|
||||
FirstName: first_name,
|
||||
LastName: last_name,
|
||||
FirstName: firstName,
|
||||
LastName: lastName,
|
||||
Email: email,
|
||||
BirthDate: birth_date,
|
||||
BirthDate: birthDate,
|
||||
Type: accountType,
|
||||
LocalCredentials: models.LocalCredentials{
|
||||
Email: account.Authentication.Local.Email,
|
||||
EmailVerified: account.Authentication.Local.EmailValidation.Validated,
|
||||
EmailValidationCode: account.Authentication.Local.EmailValidation.ValidationCode,
|
||||
PhoneNumber: phone_number,
|
||||
PhoneNumber: phoneNumber,
|
||||
PhoneNumberVerified: account.Authentication.Local.PhoneNumberValidation.Validated,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -2,11 +2,11 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"google.golang.org/protobuf/types/known/structpb"
|
||||
|
||||
"github.com/appleboy/gorush/rpc/proto"
|
||||
"github.com/rs/zerolog/log"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/protobuf/types/known/structpb"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -55,7 +55,7 @@ func (s *PushService) Send(notification Notification) error {
|
||||
Str("notification_title", notification.Title).
|
||||
Msg("Send notification")
|
||||
|
||||
resp, err := s.Client.Send(context.Background(), &proto.NotificationRequest{
|
||||
if resp, err := s.Client.Send(context.Background(), &proto.NotificationRequest{
|
||||
Data: notification.Data,
|
||||
ID: notification.ID,
|
||||
Platform: notification.Platform,
|
||||
@@ -67,9 +67,7 @@ func (s *PushService) Send(notification Notification) error {
|
||||
Title: notification.Title,
|
||||
Body: notification.Message,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -7,48 +7,53 @@ import (
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type ServicesHandler struct {
|
||||
type ServiceHandler struct {
|
||||
MobilityAccounts MobilityAccountService
|
||||
Push *PushService
|
||||
Geocoder geocode.Geocoder
|
||||
Routing routing.RoutingService
|
||||
}
|
||||
|
||||
func NewServicesHandler(cfg *viper.Viper) (*ServicesHandler, error) {
|
||||
func NewServicesHandler(cfg *viper.Viper) (*ServiceHandler, error) {
|
||||
var (
|
||||
mobilityAccountsDial = cfg.GetString("services.internal.mobility_accounts.dial")
|
||||
pushDial = cfg.GetString("services.internal.push.dial")
|
||||
geocoder_type = cfg.GetString("geocoder.type")
|
||||
pelias_base_url = cfg.GetString("geocoder.pelias.base_url")
|
||||
routing_service_type = cfg.GetString("routing.type")
|
||||
valhalla_base_url = cfg.GetString("routing.valhalla.base_url")
|
||||
geocoderType = cfg.GetString("geocoder.type")
|
||||
peliasBaseUrl = cfg.GetString("geocoder.pelias.base_url")
|
||||
routingServiceType = cfg.GetString("routing.type")
|
||||
valhallaBaseUrl = cfg.GetString("routing.valhalla.base_url")
|
||||
|
||||
mobilityAccounts MobilityAccountService
|
||||
push *PushService
|
||||
geocoder geocode.Geocoder
|
||||
routingsvc routing.RoutingService
|
||||
err error
|
||||
)
|
||||
mobilityAccounts, err := NewMobilityAccountService(mobilityAccountsDial)
|
||||
if err != nil {
|
||||
|
||||
if mobilityAccounts, err = NewMobilityAccountService(mobilityAccountsDial); err != nil {
|
||||
log.Fatal().Err(err).Msg("Could not connect to Mobility Accounts Service")
|
||||
return nil, err
|
||||
}
|
||||
push, err := NewPushService(pushDial)
|
||||
if err != nil {
|
||||
|
||||
if push, err = NewPushService(pushDial); err != nil {
|
||||
log.Fatal().Err(err).Msg("Could not connect to Push Notifications Service")
|
||||
return nil, err
|
||||
}
|
||||
geocoder, err := geocode.NewGeocoder(geocoder_type, pelias_base_url)
|
||||
if err != nil {
|
||||
|
||||
if geocoder, err = geocode.NewGeocoder(geocoderType, peliasBaseUrl); err != nil {
|
||||
log.Fatal().Err(err).Msg("Could not initiate the Geocoder service")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
routing, err := routing.NewRoutingService(routing_service_type, valhalla_base_url)
|
||||
if err != nil {
|
||||
if routingsvc, err = routing.NewRoutingService(routingServiceType, valhallaBaseUrl); err != nil {
|
||||
log.Fatal().Err(err).Msg("Could not initiate the routing service")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ServicesHandler{
|
||||
return &ServiceHandler{
|
||||
MobilityAccounts: mobilityAccounts,
|
||||
Push: push,
|
||||
Geocoder: geocoder,
|
||||
Routing: routing,
|
||||
Geocoder: geocoder,
|
||||
Routing: routingsvc,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user