2022-08-02 10:26:28 +00:00
|
|
|
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{
|
2023-03-29 10:59:08 +00:00
|
|
|
Username: lc.Username,
|
|
|
|
Password: lc.Password,
|
|
|
|
Email: lc.Email,
|
|
|
|
EmailValidation: storage.Validation{
|
|
|
|
Validated: lc.EmailValidation.Validated,
|
|
|
|
ValidationCode: lc.EmailValidation.ValidationCode,
|
|
|
|
},
|
2022-08-02 10:26:28 +00:00
|
|
|
PhoneNumber: lc.PhoneNumber,
|
2023-03-29 10:59:08 +00:00
|
|
|
PhoneNumberValidation: storage.Validation{
|
|
|
|
Validated: lc.PhoneNumberValidation.Validated,
|
|
|
|
ValidationCode: lc.PhoneNumberValidation.ValidationCode,
|
|
|
|
},
|
2022-08-02 10:26:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 15:14:21 +00:00
|
|
|
func AccountFromStorageType(account *storage.Account) (*Account, error) {
|
2022-08-02 10:26:28 +00:00
|
|
|
lc := LocalAuthFromStorageType(account.Authentication.Local)
|
|
|
|
|
2022-08-11 15:14:21 +00:00
|
|
|
d, err := sanitizeData(account.Data)
|
2022-08-02 10:26:28 +00:00
|
|
|
if err != nil {
|
2022-08-11 15:14:21 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := structpb.NewStruct(d)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, err
|
2022-08-02 10:26:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Account{
|
|
|
|
Id: account.ID,
|
|
|
|
Namespace: account.Namespace,
|
|
|
|
Data: data,
|
|
|
|
Authentication: &AccountAuth{
|
|
|
|
Local: lc,
|
|
|
|
},
|
2022-08-11 15:14:21 +00:00
|
|
|
}, nil
|
2022-08-02 10:26:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func LocalAuthFromStorageType(lc storage.LocalAuth) *LocalAuth {
|
|
|
|
return &LocalAuth{
|
|
|
|
Username: lc.Username,
|
|
|
|
Password: lc.Password,
|
|
|
|
Email: lc.Email,
|
|
|
|
PhoneNumber: lc.PhoneNumber,
|
2023-03-29 10:59:08 +00:00
|
|
|
EmailValidation: &Validation{
|
|
|
|
Validated: lc.EmailValidation.Validated,
|
|
|
|
ValidationCode: lc.EmailValidation.ValidationCode,
|
|
|
|
},
|
|
|
|
PhoneNumberValidation: &Validation{
|
|
|
|
Validated: lc.PhoneNumberValidation.Validated,
|
|
|
|
ValidationCode: lc.PhoneNumberValidation.ValidationCode,
|
|
|
|
},
|
2022-08-02 10:26:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-11 15:14:21 +00:00
|
|
|
|
|
|
|
func sanitizeData(data map[string]any) (d map[string]any, err error) {
|
|
|
|
j, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = json.Unmarshal(j, &d); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return d, nil
|
|
|
|
}
|