2022-09-05 05:27:52 +00:00
|
|
|
package grpcapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.coopgo.io/coopgo-platform/agenda/storage"
|
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
|
|
"google.golang.org/protobuf/types/known/structpb"
|
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (e Event) ToStorageType() storage.Event {
|
|
|
|
event := storage.Event{
|
|
|
|
ID: e.Id,
|
|
|
|
Namespace: e.Namespace,
|
|
|
|
Owners: e.Owners,
|
|
|
|
RestrictedTo: e.RestrictedTo,
|
|
|
|
Type: e.Type,
|
|
|
|
Name: e.Name,
|
|
|
|
Description: e.Description,
|
|
|
|
Startdate: e.Startdate.AsTime(),
|
|
|
|
Enddate: e.Enddate.AsTime(),
|
|
|
|
Starttime: e.Starttime,
|
|
|
|
Endtime: e.Endtime,
|
|
|
|
Allday: e.Allday,
|
|
|
|
MaxSubscribers: e.MaxSubscribers,
|
2022-10-17 03:00:35 +00:00
|
|
|
Subscriptions: []storage.Subscription{},
|
2023-02-15 09:14:06 +00:00
|
|
|
DeletedSubscription: []storage.Subscription{},
|
2022-09-05 05:27:52 +00:00
|
|
|
Data: map[string]any{},
|
2023-02-01 08:58:37 +00:00
|
|
|
Deleted: e.Deleted,
|
2022-09-05 05:27:52 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 03:00:35 +00:00
|
|
|
for _, v := range e.Subscriptions {
|
|
|
|
event.Subscriptions = append(event.Subscriptions, v.ToStorageType())
|
|
|
|
}
|
|
|
|
|
2023-02-15 09:14:06 +00:00
|
|
|
for _, v := range e.DeletedSubscription {
|
|
|
|
event.DeletedSubscription = append(event.DeletedSubscription, v.ToStorageType())
|
|
|
|
}
|
|
|
|
|
2022-09-05 05:27:52 +00:00
|
|
|
for k, d := range e.Data.GetFields() {
|
|
|
|
jsondata, err := protojson.Marshal(d)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
var data any
|
|
|
|
json.Unmarshal(jsondata, &data)
|
|
|
|
event.Data[k] = data
|
|
|
|
}
|
|
|
|
|
|
|
|
return event
|
|
|
|
}
|
|
|
|
|
2022-10-17 03:00:35 +00:00
|
|
|
func (s Subscription) ToStorageType() storage.Subscription {
|
|
|
|
subscription := storage.Subscription{
|
|
|
|
Subscriber: s.Subscriber,
|
|
|
|
Tags: s.Tags,
|
2022-12-05 16:21:12 +00:00
|
|
|
Data: s.Data.AsMap(),
|
|
|
|
CreatedAt: s.CreatedAt.AsTime(),
|
2022-10-17 03:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return subscription
|
|
|
|
}
|
|
|
|
|
2022-09-05 05:27:52 +00:00
|
|
|
func EventFromStorageType(event *storage.Event) (*Event, error) {
|
|
|
|
|
|
|
|
d, err := sanitizeData(event.Data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := structpb.NewStruct(d)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-17 03:00:35 +00:00
|
|
|
subscriptions := []*Subscription{}
|
2023-02-15 09:14:06 +00:00
|
|
|
deletedsubscription := []*Subscription{}
|
2022-10-17 03:00:35 +00:00
|
|
|
|
|
|
|
for _, v := range event.Subscriptions {
|
|
|
|
sub, _ := SubscriptionFromStorageType(v)
|
|
|
|
subscriptions = append(subscriptions, sub)
|
|
|
|
}
|
|
|
|
|
2023-02-15 09:14:06 +00:00
|
|
|
for _, v := range event.DeletedSubscription {
|
|
|
|
sub, _ := SubscriptionFromStorageType(v)
|
|
|
|
deletedsubscription = append(deletedsubscription, sub)
|
|
|
|
}
|
|
|
|
|
2022-09-05 05:27:52 +00:00
|
|
|
return &Event{
|
|
|
|
Id: event.ID,
|
|
|
|
Namespace: event.Namespace,
|
|
|
|
Type: event.Type,
|
|
|
|
Owners: event.Owners,
|
|
|
|
RestrictedTo: event.RestrictedTo,
|
|
|
|
Name: event.Name,
|
|
|
|
Description: event.Description,
|
|
|
|
Startdate: timestamppb.New(event.Startdate),
|
|
|
|
Enddate: timestamppb.New(event.Enddate),
|
|
|
|
Starttime: event.Starttime,
|
|
|
|
Endtime: event.Endtime,
|
|
|
|
Allday: event.Allday,
|
|
|
|
MaxSubscribers: event.MaxSubscribers,
|
2022-10-17 03:00:35 +00:00
|
|
|
Subscriptions: subscriptions,
|
2023-02-15 09:14:06 +00:00
|
|
|
DeletedSubscription: deletedsubscription,
|
2022-09-05 05:27:52 +00:00
|
|
|
Data: data,
|
2023-02-01 08:58:37 +00:00
|
|
|
Deleted: event.Deleted,
|
2022-09-05 05:27:52 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-10-17 03:00:35 +00:00
|
|
|
func SubscriptionFromStorageType(s storage.Subscription) (*Subscription, error) {
|
|
|
|
|
|
|
|
d, err := sanitizeData(s.Data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := structpb.NewStruct(d)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
subscription := Subscription{
|
|
|
|
Subscriber: s.Subscriber,
|
|
|
|
Tags: s.Tags,
|
|
|
|
Data: data,
|
2022-12-05 16:21:12 +00:00
|
|
|
CreatedAt: timestamppb.New(s.CreatedAt),
|
2022-10-17 03:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &subscription, nil
|
|
|
|
}
|
|
|
|
|
2022-09-05 05:27:52 +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
|
|
|
|
}
|