-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_in_by_token_id.go
83 lines (69 loc) · 2.44 KB
/
get_in_by_token_id.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
package queries
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/DIMO-Network/device-definitions-api/internal/core/mediator"
coremodels "github.com/DIMO-Network/device-definitions-api/internal/core/models"
"github.com/DIMO-Network/device-definitions-api/internal/infrastructure/db/models"
"github.com/DIMO-Network/device-definitions-api/internal/infrastructure/exceptions"
"github.com/DIMO-Network/shared/db"
"github.com/rs/zerolog"
"github.com/volatiletech/null/v8"
)
type GetIntegrationByTokenIDQuery struct {
TokenID int `json:"tokenId" validate:"required"`
}
func (*GetIntegrationByTokenIDQuery) Key() string {
return "GetIntegrationByTokenIDQuery"
}
type GetIntegrationByTokenIDQueryHandler struct {
DBS func() *db.ReaderWriter
log *zerolog.Logger
}
func NewGetIntegrationByTokenIDQueryHandler(dbs func() *db.ReaderWriter, log *zerolog.Logger) GetIntegrationByTokenIDQueryHandler {
return GetIntegrationByTokenIDQueryHandler{
DBS: dbs,
log: log,
}
}
func (ch GetIntegrationByTokenIDQueryHandler) Handle(ctx context.Context, query mediator.Message) (interface{}, error) {
qry := query.(*GetIntegrationByTokenIDQuery)
integration, err := models.Integrations(
models.IntegrationWhere.TokenID.EQ(null.IntFrom(qry.TokenID)),
).One(ctx, ch.DBS().Reader)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, &exceptions.NotFoundError{
Err: fmt.Errorf("could not find integration with tokenID: %d ", qry.TokenID),
}
}
return nil, &exceptions.InternalError{
Err: fmt.Errorf("failed to get integration using tokenID: %d", qry.TokenID),
}
}
response := coremodels.GetIntegrationQueryResult{}
im := new(coremodels.IntegrationsMetadata)
if integration.Metadata.Valid {
err = integration.Metadata.Unmarshal(&im)
if err != nil {
return nil, &exceptions.InternalError{
Err: fmt.Errorf("failed to unmarshall integration metadata id %s", integration.ID),
}
}
}
response = coremodels.GetIntegrationQueryResult{
ID: integration.ID,
Type: integration.Type,
Style: integration.Style,
Vendor: integration.Vendor,
AutoPiDefaultTemplateID: im.AutoPiDefaultTemplateID,
RefreshLimitSecs: integration.RefreshLimitSecs,
TokenID: integration.TokenID.Int,
}
if im.AutoPiPowertrainToTemplateID != nil {
response.AutoPiPowertrainToTemplateID = im.AutoPiPowertrainToTemplateID
}
return response, nil
}