-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_job_by_ethereum_address.go
67 lines (53 loc) · 1.88 KB
/
get_job_by_ethereum_address.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
package queries
import (
"context"
"fmt"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/DIMO-Network/shared/db"
"github.com/DIMO-Network/vehicle-signal-decoding/internal/core/common"
"github.com/DIMO-Network/vehicle-signal-decoding/internal/infrastructure/db/models"
"github.com/DIMO-Network/vehicle-signal-decoding/internal/infrastructure/exceptions"
p_grpc "github.com/DIMO-Network/vehicle-signal-decoding/pkg/grpc"
"github.com/rs/zerolog"
)
type GetJobByEthereumAddressQueryHandler struct {
DBS func() *db.ReaderWriter
logger *zerolog.Logger
}
func NewGetJobByEthereumAddressQueryHandler(dbs func() *db.ReaderWriter, logger *zerolog.Logger) GetJobByEthereumAddressQueryHandler {
return GetJobByEthereumAddressQueryHandler{
DBS: dbs,
logger: logger,
}
}
type GetJobByyEthereumAddressQueryRequest struct {
EtherumAddress string
}
func (h GetJobByEthereumAddressQueryHandler) Handle(ctx context.Context, query *GetJobByyEthereumAddressQueryRequest) (*p_grpc.GetJobsByEtherumAddressResponse, error) {
ethAddrBytes, err := common.ResolveEtherumAddressFromString(query.EtherumAddress)
if err != nil {
return nil, &exceptions.ValidationError{
Err: fmt.Errorf("invalid ethereum address: %w", err),
}
}
jobs, err := models.Jobs(models.JobWhere.DeviceEthereumAddress.EQ(ethAddrBytes)).All(ctx, h.DBS().Reader)
if err != nil {
return nil, &exceptions.InternalError{
Err: fmt.Errorf("failed to get jobs"),
}
}
result := &p_grpc.GetJobsByEtherumAddressResponse{}
for _, item := range jobs {
jobItem := &p_grpc.GetJobsByEtherumAddressItemResponse{
Id: item.ID,
Command: item.Command,
Status: item.Status,
CreatedAt: timestamppb.New(item.CreatedAt),
}
if item.LastExecution.Valid {
jobItem.LastExecution = timestamppb.New(item.LastExecution.Time)
}
result.Items = append(result.Items, jobItem)
}
return result, nil
}