first commit
This commit is contained in:
188
services/mobility-accounts.go
Normal file
188
services/mobility-accounts.go
Normal file
@@ -0,0 +1,188 @@
|
||||
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"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/protobuf/types/known/structpb"
|
||||
)
|
||||
|
||||
type MobilityAccountService struct {
|
||||
Client mobilityaccounts.MobilityAccountsClient
|
||||
}
|
||||
|
||||
func NewMobilityAccountService(mobilityAccountsDial string) (MobilityAccountService, error) {
|
||||
mobilityAccountsConn, err := grpc.Dial(mobilityAccountsDial, grpc.WithInsecure())
|
||||
|
||||
client := mobilityaccounts.NewMobilityAccountsClient(mobilityAccountsConn)
|
||||
if err != nil {
|
||||
return MobilityAccountService{}, err
|
||||
}
|
||||
|
||||
return MobilityAccountService{
|
||||
Client: client,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s MobilityAccountService) Login(ctx context.Context, username, password, namespace string) (*models.Account, error) {
|
||||
resp, err := s.Client.Login(ctx, &mobilityaccounts.LoginRequest{
|
||||
Username: username,
|
||||
Password: password,
|
||||
Namespace: namespace,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
account := resp.Account.ToStorageType()
|
||||
return toAccountModel(account), nil
|
||||
}
|
||||
|
||||
func (s MobilityAccountService) UpdateAccountData(ctx context.Context, id string, data map[string]any) error {
|
||||
d, err := structpb.NewStruct(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err = s.Client.UpdateData(ctx, &mobilityaccounts.UpdateDataRequest{
|
||||
Account: &mobilityaccounts.Account{
|
||||
Id: id,
|
||||
Data: d,
|
||||
},
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s MobilityAccountService) UpdatePassword(ctx context.Context, id string, password string) bool {
|
||||
_, err := s.Client.ChangePassword(ctx, &mobilityaccounts.ChangePasswordRequest{
|
||||
Id: id,
|
||||
Password: password,
|
||||
})
|
||||
if err == nil {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return 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) {
|
||||
account := &ma.Account{
|
||||
Authentication: ma.AccountAuth{
|
||||
Local: ma.LocalAuth{
|
||||
Username: username,
|
||||
Password: password,
|
||||
Email: email,
|
||||
PhoneNumber: phone_number,
|
||||
},
|
||||
},
|
||||
Namespace: namespace,
|
||||
Data: data,
|
||||
}
|
||||
|
||||
acc, err := mobilityaccounts.AccountFromStorageType(account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := s.Client.Register(ctx, &mobilityaccounts.RegisterRequest{
|
||||
Account: acc,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return 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(
|
||||
ctx,
|
||||
&mobilityaccounts.UpdatePhoneNumberRequest{
|
||||
Id: accountid,
|
||||
PhoneNumber: phone_number,
|
||||
Verified: verified,
|
||||
VerificationCode: verification_code,
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s MobilityAccountService) UpdateAccountBirthDate(ctx context.Context, username string, namespace string, birthdate string) error {
|
||||
resp, err := s.Client.GetAccountUsername(ctx, &mobilityaccounts.GetAccountUsernameRequest{
|
||||
Username: username,
|
||||
Namespace: namespace,
|
||||
})
|
||||
account, err := s.Client.GetAccount(ctx, &mobilityaccounts.GetAccountRequest{
|
||||
Id: resp.Account.Id,
|
||||
})
|
||||
data := make(map[string]interface{})
|
||||
data["birthdate"] = birthdate
|
||||
|
||||
dataStruct := &structpb.Struct{
|
||||
Fields: make(map[string]*structpb.Value),
|
||||
}
|
||||
|
||||
for key, value := range data {
|
||||
stringValue, ok := value.(string)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
dataStruct.Fields[key] = &structpb.Value{
|
||||
Kind: &structpb.Value_StringValue{
|
||||
StringValue: stringValue,
|
||||
},
|
||||
}
|
||||
}
|
||||
account.Account.Data = dataStruct
|
||||
_, err = s.Client.UpdateData(ctx, &mobilityaccounts.UpdateDataRequest{
|
||||
Account: account.Account,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func 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 = ""
|
||||
}
|
||||
return &models.Account{
|
||||
ID: account.ID,
|
||||
FirstName: first_name,
|
||||
LastName: last_name,
|
||||
BirthDate: birth_date,
|
||||
LocalCredentials: models.LocalCredentials{
|
||||
Email: account.Authentication.Local.Email,
|
||||
EmailVerified: account.Authentication.Local.EmailValidation.Validated,
|
||||
EmailValidationCode: account.Authentication.Local.EmailValidation.ValidationCode,
|
||||
PhoneNumber: account.Authentication.Local.PhoneNumber,
|
||||
PhoneNumberVerified: account.Authentication.Local.PhoneNumberValidation.Validated,
|
||||
},
|
||||
}
|
||||
}
|
||||
74
services/push.go
Normal file
74
services/push.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/appleboy/gorush/rpc/proto"
|
||||
"github.com/rs/zerolog/log"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
const (
|
||||
// PlatFormIos constant is 1 for iOS
|
||||
PushToIos = iota + 1
|
||||
// PlatFormAndroid constant is 2 for Android
|
||||
PushToAndroid
|
||||
// PlatFormHuawei constant is 3 for Huawei
|
||||
PushToHuawei
|
||||
// PlatformUnifiedPush constant is 4 for UnifiedPush
|
||||
PushToUnifiedPush
|
||||
// PlatformSMSFactor constant is 5 for SMSFactor
|
||||
PushToSMSFactor
|
||||
)
|
||||
|
||||
type Notification struct {
|
||||
Platform int32
|
||||
Recipients []string
|
||||
Message string
|
||||
Title string
|
||||
}
|
||||
|
||||
type PushService struct {
|
||||
Client proto.GorushClient
|
||||
}
|
||||
|
||||
func NewPushService(address string) (*PushService, error) {
|
||||
conn, err := grpc.Dial(address, grpc.WithInsecure())
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &PushService{
|
||||
Client: proto.NewGorushClient(conn),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *PushService) Send(notification Notification) error {
|
||||
log.Debug().
|
||||
Int32("Platform", notification.Platform).
|
||||
Strs("recipients", notification.Recipients).
|
||||
Str("notification_message", notification.Message).
|
||||
Str("notification_title", notification.Title).
|
||||
Msg("Send notification")
|
||||
|
||||
resp, err := s.Client.Send(context.Background(), &proto.NotificationRequest{
|
||||
Platform: notification.Platform,
|
||||
Tokens: notification.Recipients,
|
||||
Message: notification.Message,
|
||||
Title: notification.Title,
|
||||
Priority: proto.NotificationRequest_HIGH,
|
||||
Alert: &proto.Alert{
|
||||
Title: notification.Title,
|
||||
Body: notification.Message,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Debug().Str("response", resp.String()).Msg("notification sent")
|
||||
|
||||
return nil
|
||||
}
|
||||
33
services/services.go
Normal file
33
services/services.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type ServicesHandler struct {
|
||||
MobilityAccounts MobilityAccountService
|
||||
Push *PushService
|
||||
}
|
||||
|
||||
func NewServicesHandler(cfg *viper.Viper) (*ServicesHandler, error) {
|
||||
var (
|
||||
mobilityAccountsDial = cfg.GetString("services.internal.mobility_accounts.dial")
|
||||
pushDial = cfg.GetString("services.internal.push.dial")
|
||||
)
|
||||
mobilityAccounts, err := NewMobilityAccountService(mobilityAccountsDial)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Could not connect to Mobility Accounts Service")
|
||||
return nil, err
|
||||
}
|
||||
push, err := NewPushService(pushDial)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Could not connect to Push Notifications Service")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ServicesHandler{
|
||||
MobilityAccounts: mobilityAccounts,
|
||||
Push: push,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user