Refactor previous COOPGO Identity service - Initial commit
This commit is contained in:
74
grpcapi/accounts.go
Normal file
74
grpcapi/accounts.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package grpcapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"git.coopgo.io/coopgo-platform/mobility-accounts/storage"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
"google.golang.org/protobuf/types/known/structpb"
|
||||
)
|
||||
|
||||
func (a Account) ToStorageType() storage.Account {
|
||||
var localauth storage.LocalAuth
|
||||
if a.Authentication != nil && a.Authentication.Local != nil {
|
||||
localauth = a.Authentication.Local.ToStorageType()
|
||||
}
|
||||
account := storage.Account{
|
||||
ID: a.Id,
|
||||
Namespace: a.Namespace,
|
||||
Data: map[string]any{},
|
||||
Authentication: storage.AccountAuth{
|
||||
Local: localauth,
|
||||
},
|
||||
}
|
||||
|
||||
for k, d := range a.Data.GetFields() {
|
||||
jsondata, err := protojson.Marshal(d)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
break
|
||||
}
|
||||
var data any
|
||||
json.Unmarshal(jsondata, &data)
|
||||
account.Data[k] = data
|
||||
}
|
||||
|
||||
return account
|
||||
}
|
||||
|
||||
func (lc LocalAuth) ToStorageType() storage.LocalAuth {
|
||||
return storage.LocalAuth{
|
||||
Username: lc.Username,
|
||||
Password: lc.Password,
|
||||
Email: lc.Email,
|
||||
PhoneNumber: lc.PhoneNumber,
|
||||
}
|
||||
}
|
||||
|
||||
func AccountFromStorageType(account *storage.Account) *Account {
|
||||
lc := LocalAuthFromStorageType(account.Authentication.Local)
|
||||
|
||||
data, err := structpb.NewStruct(account.Data)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &Account{
|
||||
Id: account.ID,
|
||||
Namespace: account.Namespace,
|
||||
Data: data,
|
||||
Authentication: &AccountAuth{
|
||||
Local: lc,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func LocalAuthFromStorageType(lc storage.LocalAuth) *LocalAuth {
|
||||
return &LocalAuth{
|
||||
Username: lc.Username,
|
||||
Password: lc.Password,
|
||||
Email: lc.Email,
|
||||
PhoneNumber: lc.PhoneNumber,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user