2022-08-02 10:26:28 +00:00
|
|
|
package grpcapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
context "context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"git.coopgo.io/coopgo-platform/mobility-accounts/handlers"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
codes "google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/reflection"
|
|
|
|
status "google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MobilityAccountsServerImpl struct {
|
|
|
|
handler handlers.MobilityAccountsHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMobilityAccountsServer(h handlers.MobilityAccountsHandler) *MobilityAccountsServerImpl {
|
|
|
|
return &MobilityAccountsServerImpl{
|
|
|
|
handler: h,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s MobilityAccountsServerImpl) Login(ctx context.Context, req *LoginRequest) (*LoginResponse, error) {
|
|
|
|
account, err := s.handler.Login(req.Username, req.Password, req.Namespace)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "could not log in : %v", err)
|
|
|
|
}
|
2022-08-11 15:14:21 +00:00
|
|
|
response, err := AccountFromStorageType(account)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, status.Errorf(codes.Internal, "issue while retrieving account : %v", err)
|
|
|
|
}
|
2022-08-02 10:26:28 +00:00
|
|
|
return &LoginResponse{Account: response}, nil
|
|
|
|
}
|
|
|
|
func (s MobilityAccountsServerImpl) Register(ctx context.Context, req *RegisterRequest) (*RegisterResponse, error) {
|
|
|
|
a := req.Account.ToStorageType()
|
|
|
|
account, err := s.handler.Register(a)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, status.Errorf(codes.AlreadyExists, "account creation failed : %v", err)
|
|
|
|
}
|
2022-08-11 15:14:21 +00:00
|
|
|
response, err := AccountFromStorageType(account)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, status.Errorf(codes.Internal, "issue while retrieving account : %v", err)
|
|
|
|
}
|
2022-08-02 10:26:28 +00:00
|
|
|
return &RegisterResponse{Account: response}, nil
|
|
|
|
}
|
2022-08-11 15:14:21 +00:00
|
|
|
func (s MobilityAccountsServerImpl) UpdateData(ctx context.Context, req *UpdateDataRequest) (*UpdateDataResponse, error) {
|
|
|
|
a := req.Account.ToStorageType()
|
|
|
|
account, err := s.handler.UpdateData(a.ID, a.Data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "issue while apdating account : %v", err)
|
|
|
|
}
|
|
|
|
response, err := AccountFromStorageType(account)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, status.Errorf(codes.Internal, "issue while retrieving account : %v", err)
|
|
|
|
}
|
|
|
|
return &UpdateDataResponse{Account: response}, nil
|
2022-08-02 10:26:28 +00:00
|
|
|
}
|
2023-03-29 10:59:08 +00:00
|
|
|
func (s MobilityAccountsServerImpl) UpdatePhoneNumber(ctx context.Context, req *UpdatePhoneNumberRequest) (*UpdatePhoneNumberResponse, error) {
|
|
|
|
err := s.handler.UpdatePhoneNumber(
|
|
|
|
req.Id,
|
|
|
|
req.PhoneNumber,
|
|
|
|
req.Verified,
|
|
|
|
req.VerificationCode,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "issue while updating phone number : %v", err)
|
|
|
|
}
|
|
|
|
return &UpdatePhoneNumberResponse{Ok: true}, nil
|
|
|
|
}
|
2022-08-11 15:14:21 +00:00
|
|
|
func (s MobilityAccountsServerImpl) GetAccount(ctx context.Context, req *GetAccountRequest) (*GetAccountResponse, error) {
|
|
|
|
account, err := s.handler.GetAccount(req.Id)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, status.Errorf(codes.AlreadyExists, "issue while retrieving account : %v", err)
|
|
|
|
}
|
|
|
|
response, err := AccountFromStorageType(account)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, status.Errorf(codes.Internal, "issue while retrieving account : %v", err)
|
|
|
|
}
|
|
|
|
return &GetAccountResponse{Account: response}, nil
|
2022-08-02 10:26:28 +00:00
|
|
|
}
|
2022-09-06 13:03:39 +00:00
|
|
|
func (s MobilityAccountsServerImpl) GetAccountUsername(ctx context.Context, req *GetAccountUsernameRequest) (*GetAccountUsernameResponse, error) {
|
|
|
|
account, err := s.handler.GetAccountUsername(req.Username, req.Namespace)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, status.Errorf(codes.AlreadyExists, "issue while retrieving account : %v", err)
|
|
|
|
}
|
|
|
|
response, err := AccountFromStorageType(account)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, status.Errorf(codes.Internal, "issue while retrieving account : %v", err)
|
|
|
|
}
|
|
|
|
return &GetAccountUsernameResponse{Account: response}, nil
|
|
|
|
}
|
2022-08-02 10:26:28 +00:00
|
|
|
func (s MobilityAccountsServerImpl) GetAccounts(ctx context.Context, req *GetAccountsRequest) (*GetAccountsResponse, error) {
|
|
|
|
responses, err := s.handler.GetAccounts(req.Namespaces)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "could not get accounts : %v", err)
|
|
|
|
}
|
|
|
|
var accounts []*Account
|
2022-08-11 15:14:21 +00:00
|
|
|
|
2022-08-02 10:26:28 +00:00
|
|
|
for _, a := range responses {
|
2022-08-11 15:14:21 +00:00
|
|
|
account, err := AccountFromStorageType(&a)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "could not get accounts : %v", err)
|
|
|
|
}
|
|
|
|
accounts = append(accounts, account)
|
2022-08-02 10:26:28 +00:00
|
|
|
}
|
|
|
|
return &GetAccountsResponse{Accounts: accounts}, nil
|
|
|
|
}
|
2022-08-11 15:14:21 +00:00
|
|
|
func (s MobilityAccountsServerImpl) GetAccountsBatch(ctx context.Context, req *GetAccountsBatchRequest) (*GetAccountsBatchResponse, error) {
|
|
|
|
responses, err := s.handler.GetAccountsBatch(req.Accountids)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "could not get accounts : %v", err)
|
|
|
|
}
|
|
|
|
var accounts []*Account
|
|
|
|
|
|
|
|
for _, a := range responses {
|
|
|
|
account, err := AccountFromStorageType(&a)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "could not get accounts : %v", err)
|
|
|
|
}
|
|
|
|
accounts = append(accounts, account)
|
|
|
|
}
|
|
|
|
return &GetAccountsBatchResponse{Accounts: accounts}, nil
|
|
|
|
}
|
2022-10-30 19:09:51 +00:00
|
|
|
func (s MobilityAccountsServerImpl) ChangePassword(ctx context.Context, req *ChangePasswordRequest) (*ChangePasswordResponse, error) {
|
|
|
|
err := s.handler.ChangePassword(req.Id, req.Password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &ChangePasswordResponse{}, nil
|
2022-08-02 10:26:28 +00:00
|
|
|
}
|
|
|
|
func (MobilityAccountsServerImpl) mustEmbedUnimplementedMobilityAccountsServer() {}
|
|
|
|
|
|
|
|
func Run(done chan error, cfg *viper.Viper, handler handlers.MobilityAccountsHandler) {
|
|
|
|
var (
|
|
|
|
dev_env = cfg.GetBool("dev_env")
|
|
|
|
address = ":" + cfg.GetString("services.grpc.port")
|
|
|
|
)
|
|
|
|
fmt.Println("-> GRPC server on", address)
|
|
|
|
|
|
|
|
server := grpc.NewServer()
|
|
|
|
RegisterMobilityAccountsServer(server, NewMobilityAccountsServer(handler))
|
|
|
|
l, err := net.Listen("tcp", address)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if dev_env {
|
|
|
|
reflection.Register(server)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := server.Serve(l); err != nil {
|
|
|
|
fmt.Println("gRPC service ended")
|
|
|
|
done <- err
|
|
|
|
}
|
|
|
|
}
|