parcoursmob/services/agenda.go

66 lines
1.4 KiB
Go
Raw Normal View History

2022-09-05 05:25:05 +00:00
package services
import (
"context"
"fmt"
"time"
2022-09-05 05:25:05 +00:00
agenda "git.coopgo.io/coopgo-platform/agenda/grpcapi"
"git.coopgo.io/coopgo-platform/agenda/storage"
2022-09-05 05:25:05 +00:00
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/timestamppb"
2022-09-05 05:25:05 +00:00
)
type AgendaService struct {
agenda.AgendaClient
}
func NewAgendaService(dial string) (*AgendaService, error) {
conn, err := grpc.Dial(dial, grpc.WithInsecure())
client := agenda.NewAgendaClient(conn)
if err != nil {
return nil, err
}
return &AgendaService{
AgendaClient: client,
}, nil
}
func (s *ServicesHandler) GetAgendaEvents() ([]AgendaEvent, error) {
resp, err := s.GRPC.Agenda.GetEvents(context.TODO(), &agenda.GetEventsRequest{
Namespaces: []string{"parcoursmob_dispositifs"},
Mindate: timestamppb.New(time.Now().Add(-24 * time.Hour)),
})
if err != nil {
return nil, err
}
groups, err := s.GetGroupsMap()
if err != nil {
return nil, fmt.Errorf("error in groups request : %w", err)
}
events := []AgendaEvent{}
for _, e := range resp.Events {
newEvent := AgendaEvent{
Event: e.ToStorageType(),
}
for _, o := range e.Owners {
newEvent.OwnersGroups = append(newEvent.OwnersGroups, GroupsManagementGroup{Group: groups[o]})
}
events = append(events, newEvent)
}
return events, nil
}
// Enriched types
type AgendaEvent struct {
OwnersGroups []GroupsManagementGroup
storage.Event
}