/
trb_request.go
190 lines (166 loc) · 5.17 KB
/
trb_request.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package storage
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
"go.uber.org/zap"
"github.com/cmsgov/easi-app/pkg/appcontext"
"github.com/cmsgov/easi-app/pkg/apperrors"
"github.com/cmsgov/easi-app/pkg/models"
"github.com/cmsgov/easi-app/pkg/sqlqueries"
"github.com/cmsgov/easi-app/pkg/sqlutils"
_ "embed"
)
// CreateTRBRequest creates a new TRBRequest record
// Note this will be refactored to not use the store, but is left now for organization
func (s *Store) CreateTRBRequest(ctx context.Context, np sqlutils.NamedPreparer, trb *models.TRBRequest) (*models.TRBRequest, error) {
if trb.ID == uuid.Nil {
trb.ID = uuid.New()
}
stmt, err := np.PrepareNamed(sqlqueries.TRBRequest.Create)
if err != nil {
appcontext.ZLogger(ctx).Error(
fmt.Sprintf("Failed to create trb request with error %s", err),
zap.String("user", trb.CreatedBy),
)
return nil, err
}
defer stmt.Close()
retTRB := models.TRBRequest{}
err = stmt.Get(&retTRB, trb)
if err != nil {
appcontext.ZLogger(ctx).Error(
fmt.Sprintf("Failed to create trb request with error %s", err),
zap.String("user", trb.CreatedBy),
)
return nil, err
}
return &retTRB, nil
}
// GetTRBRequestByID takes in a NamedPreparer (db, tx) and returns an TRBRequest from the db for a given id
func (s *Store) GetTRBRequestByID(ctx context.Context, id uuid.UUID) (*models.TRBRequest, error) {
return sqlutils.WithTransactionRet[*models.TRBRequest](ctx, s, func(tx *sqlx.Tx) (*models.TRBRequest, error) {
return s.GetTRBRequestByIDNP(ctx, tx, id)
})
}
// GetTRBRequestByIDNP returns an TRBRequest from the db for a given id
func (s *Store) GetTRBRequestByIDNP(ctx context.Context, np sqlutils.NamedPreparer, id uuid.UUID) (*models.TRBRequest, error) {
trb := models.TRBRequest{}
stmt, err := np.PrepareNamed(sqlqueries.TRBRequest.GetByID)
if err != nil {
appcontext.ZLogger(ctx).Error(
"Failed to fetch TRB request",
zap.Error(err),
zap.String("id", id.String()),
)
return nil, err
}
defer stmt.Close()
arg := map[string]interface{}{"id": id}
err = stmt.Get(&trb, arg)
if err != nil {
appcontext.ZLogger(ctx).Error(
"Failed to fetch TRB request",
zap.Error(err),
zap.String("id", id.String()),
)
return nil, &apperrors.QueryError{
Err: err,
Model: trb,
Operation: apperrors.QueryFetch,
}
}
return &trb, err
}
// UpdateTRBRequest returns an TRBRequest from the db for a given id
func (s *Store) UpdateTRBRequest(ctx context.Context, trbRequest *models.TRBRequest) (*models.TRBRequest, error) {
return sqlutils.WithTransactionRet[*models.TRBRequest](ctx, s, func(tx *sqlx.Tx) (*models.TRBRequest, error) {
return s.UpdateTRBRequestNP(ctx, tx, trbRequest)
})
}
// UpdateTRBRequestNP takes in a NamedPreparer (db, tx) and returns an TRBRequest from the db for a given id
func (s *Store) UpdateTRBRequestNP(ctx context.Context, np sqlutils.NamedPreparer, trb *models.TRBRequest) (*models.TRBRequest, error) {
stmt, err := np.PrepareNamed(sqlqueries.TRBRequest.Update)
if err != nil {
appcontext.ZLogger(ctx).Error(
fmt.Sprintf("Failed to update TRB request %s", err),
zap.String("id", trb.ID.String()),
)
return nil, err
}
defer stmt.Close()
retTRB := models.TRBRequest{}
err = stmt.Get(&retTRB, trb)
if err != nil {
appcontext.ZLogger(ctx).Error(
fmt.Sprintf("Failed to update TRB request %s", err),
zap.String("id", trb.ID.String()),
)
return nil, &apperrors.QueryError{
Err: err,
Model: trb,
Operation: apperrors.QueryUpdate,
}
}
return &retTRB, err
}
// GetTRBRequests returns the collection of TRB requests
func (s *Store) GetTRBRequests(ctx context.Context, archived bool) ([]*models.TRBRequest, error) {
trbRequests := []*models.TRBRequest{}
stmt, err := s.db.PrepareNamed(sqlqueries.TRBRequest.CollectionGet)
if err != nil {
appcontext.ZLogger(ctx).Error(
"Failed to fetch trb requests",
zap.Error(err),
)
return nil, err
}
defer stmt.Close()
arg := map[string]interface{}{
"archived": archived,
}
err = stmt.Select(&trbRequests, arg)
if err != nil {
appcontext.ZLogger(ctx).Error(
"Failed to fetch trb requests",
zap.Error(err),
)
return nil, &apperrors.QueryError{
Err: err,
Model: models.TRBRequest{},
Operation: apperrors.QueryFetch,
}
}
return trbRequests, err
}
// GetMyTRBRequests returns the collection of TRB requests that belong to the user in the context
func (s *Store) GetMyTRBRequests(ctx context.Context, archived bool) ([]*models.TRBRequest, error) {
trbRequests := []*models.TRBRequest{}
stmt, err := s.db.PrepareNamed(sqlqueries.TRBRequest.CollectionGetByUserAndArchivedState)
if err != nil {
appcontext.ZLogger(ctx).Error(
"Failed to fetch user's trb requests",
zap.Error(err),
)
return nil, err
}
defer stmt.Close()
arg := map[string]interface{}{
"archived": archived,
"created_by": appcontext.Principal(ctx).ID(),
}
err = stmt.Select(&trbRequests, arg)
if err != nil {
appcontext.ZLogger(ctx).Error(
"Failed to fetch user's trb requests",
zap.Error(err),
)
return nil, &apperrors.QueryError{
Err: err,
Model: models.TRBRequest{},
Operation: apperrors.QueryFetch,
}
}
return trbRequests, err
}