-
Notifications
You must be signed in to change notification settings - Fork 16
/
ocpi.go
98 lines (90 loc) · 3.06 KB
/
ocpi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// SPDX-License-Identifier: Apache-2.0
package firestore
import (
"context"
"errors"
"fmt"
"github.com/thoughtworks/maeve-csms/manager/store"
"golang.org/x/exp/slog"
"google.golang.org/api/iterator"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (s *Store) SetRegistrationDetails(ctx context.Context, token string, registration *store.OcpiRegistration) error {
slog.Info("setting registration", "token", token, "status", registration.Status)
regRef := s.client.Doc(fmt.Sprintf("OcpiRegistration/%s", token))
_, err := regRef.Set(ctx, registration)
if err != nil {
return fmt.Errorf("setting registration: %s: %w", token, err)
}
return nil
}
func (s *Store) GetRegistrationDetails(ctx context.Context, token string) (*store.OcpiRegistration, error) {
regRef := s.client.Doc(fmt.Sprintf("OcpiRegistration/%s", token))
snap, err := regRef.Get(ctx)
if err != nil {
if status.Code(err) == codes.NotFound {
return nil, nil
}
}
var registration store.OcpiRegistration
err = snap.DataTo(®istration)
if err != nil {
return nil, fmt.Errorf("map registration %s: %w", token, err)
}
return ®istration, nil
}
func (s *Store) DeleteRegistrationDetails(ctx context.Context, token string) error {
regRef := s.client.Doc(fmt.Sprintf("OcpiRegistration/%s", token))
_, err := regRef.Delete(ctx)
if err != nil {
return fmt.Errorf("delete registration %s: %w", token, err)
}
return nil
}
func (s *Store) SetPartyDetails(ctx context.Context, partyDetails *store.OcpiParty) error {
partyRef := s.client.Doc(fmt.Sprintf("OcpiParty/%s/Id/%s:%s", partyDetails.Role, partyDetails.CountryCode, partyDetails.PartyId))
_, err := partyRef.Set(ctx, partyDetails)
if err != nil {
return fmt.Errorf("setting party %s/%s:%s: %w", partyDetails.Role, partyDetails.CountryCode, partyDetails.PartyId, err)
}
return nil
}
func (s *Store) GetPartyDetails(ctx context.Context, role, countryCode, partyId string) (*store.OcpiParty, error) {
partyRef := s.client.Doc(fmt.Sprintf("OcpiParty/%s/Id/%s:%s", role, countryCode, partyId))
snap, err := partyRef.Get(ctx)
if err != nil {
if status.Code(err) == codes.NotFound {
return nil, nil
}
return nil, fmt.Errorf("lookup party details %s/%s:%s: %w", role, countryCode, partyId, err)
}
var registration store.OcpiParty
err = snap.DataTo(®istration)
if err != nil {
return nil, fmt.Errorf("map party details %s/%s:%s: %w", role, countryCode, partyId, err)
}
return ®istration, nil
}
func (s *Store) ListPartyDetailsForRole(context context.Context, role string) ([]*store.OcpiParty, error) {
var parties []*store.OcpiParty
iter := s.client.Collection(fmt.Sprintf("OcpiParty/%s/Id", role)).Documents(context)
for {
doc, err := iter.Next()
if errors.Is(err, iterator.Done) {
break
}
if err != nil {
return nil, fmt.Errorf("next ocpiParty: %w", err)
}
var party store.OcpiParty
if err = doc.DataTo(&party); err != nil {
return nil, fmt.Errorf("map ocpiParty: %w", err)
}
parties = append(parties, &party)
}
if parties == nil {
parties = make([]*store.OcpiParty, 0)
}
return parties, nil
}