-
Notifications
You must be signed in to change notification settings - Fork 11
/
trb_request_form.go
205 lines (181 loc) · 6.12 KB
/
trb_request_form.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package storage
import (
"context"
"fmt"
"github.com/google/uuid"
"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"
)
// CreateTRBRequestForm creates a new TRBRequestForm record
// Note this will be refactored to not use the store, but is left now for organization
func (s *Store) CreateTRBRequestForm(ctx context.Context, np sqlutils.NamedPreparer, form *models.TRBRequestForm) (*models.TRBRequestForm, error) {
if form.ID == uuid.Nil {
form.ID = uuid.New()
}
stmt, err := np.PrepareNamed(sqlqueries.TRBRequestForm.Create)
if err != nil {
appcontext.ZLogger(ctx).Error(
fmt.Sprintf("Failed to update TRB create form %s", err),
zap.String("id", form.ID.String()),
)
return nil, err
}
defer stmt.Close()
created := models.TRBRequestForm{}
err = stmt.Get(&created, form)
if err != nil {
appcontext.ZLogger(ctx).Error("Failed to create TRB request form with error %s", zap.Error(err))
return nil, err
}
return &created, err
}
// UpdateTRBRequestForm updates a TRB request form record in the database
func (s *Store) UpdateTRBRequestForm(ctx context.Context, form *models.TRBRequestForm) (*models.TRBRequestForm, error) {
stmt, err := s.db.PrepareNamed(`
UPDATE trb_request_forms
SET
status = :status,
component = :component,
needs_assistance_with = :needs_assistance_with,
has_solution_in_mind = :has_solution_in_mind,
proposed_solution = :proposed_solution,
where_in_process = :where_in_process,
where_in_process_other = :where_in_process_other,
has_expected_start_end_dates = :has_expected_start_end_dates,
expected_start_date = :expected_start_date,
expected_end_date = :expected_end_date,
collab_groups = :collab_groups,
collab_date_security = :collab_date_security,
collab_date_enterprise_architecture = :collab_date_enterprise_architecture,
collab_date_cloud = :collab_date_cloud,
collab_date_privacy_advisor = :collab_date_privacy_advisor,
collab_date_governance_review_board = :collab_date_governance_review_board,
collab_date_other = :collab_date_other,
collab_group_other = :collab_group_other,
collab_grb_consult_requested = :collab_grb_consult_requested,
subject_area_options = :subject_area_options,
subject_area_option_other = :subject_area_option_other,
submitted_at = :submitted_at,
modified_by = :modified_by,
modified_at = CURRENT_TIMESTAMP
WHERE trb_request_id = :trb_request_id
RETURNING *;`)
if err != nil {
appcontext.ZLogger(ctx).Error(
fmt.Sprintf("Failed to update TRB request form %s", err),
zap.String("id", form.ID.String()),
)
return nil, err
}
defer stmt.Close()
updated := models.TRBRequestForm{}
err = stmt.Get(&updated, form)
if err != nil {
appcontext.ZLogger(ctx).Error(
fmt.Sprintf("Failed to update TRB request form %s", err),
zap.String("id", form.ID.String()),
)
return nil, &apperrors.QueryError{
Err: err,
Model: form,
Operation: apperrors.QueryUpdate,
}
}
return &updated, err
}
// GetTRBRequestFormByTRBRequestID queries the DB for all the TRB request form records
// matching the given TRB request ID
func (s *Store) GetTRBRequestFormByTRBRequestID(ctx context.Context, trbRequestID uuid.UUID) (*models.TRBRequestForm, error) {
form := models.TRBRequestForm{}
stmt, err := s.db.PrepareNamed(`SELECT * FROM trb_request_forms WHERE trb_request_id=:trb_request_id`)
if err != nil {
appcontext.ZLogger(ctx).Error(
"Failed to fetch TRB request form",
zap.Error(err),
zap.String("trbRequestID", trbRequestID.String()),
)
return nil, err
}
defer stmt.Close()
arg := map[string]interface{}{"trb_request_id": trbRequestID}
err = stmt.Get(&form, arg)
if err != nil {
appcontext.ZLogger(ctx).Error(
"Failed to fetch TRB request form",
zap.Error(err),
zap.String("trbRequestID", trbRequestID.String()),
)
return nil, &apperrors.QueryError{
Err: err,
Model: form,
Operation: apperrors.QueryFetch,
}
}
return &form, err
}
// GetFundingSourcesByRequestID queries the DB for all the TRB request form funding sources
// matching the given TRB request ID
func (s *Store) GetFundingSourcesByRequestID(ctx context.Context, trbRequestID uuid.UUID) ([]*models.TRBFundingSource, error) {
fundingSources := []*models.TRBFundingSource{}
stmt, err := s.db.PrepareNamed(`SELECT * FROM trb_request_funding_sources WHERE trb_request_id=:trb_request_id`)
if err != nil {
appcontext.ZLogger(ctx).Error(
"Failed to fetch TRB request funding sources",
zap.Error(err),
zap.String("trbRequestID", trbRequestID.String()),
)
return nil, err
}
defer stmt.Close()
arg := map[string]interface{}{"trb_request_id": trbRequestID}
err = stmt.Select(&fundingSources, arg)
if err != nil {
appcontext.ZLogger(ctx).Error(
"Failed to fetch TRB request funding sources",
zap.Error(err),
zap.String("trbRequestID", trbRequestID.String()),
)
return nil, &apperrors.QueryError{
Err: err,
Model: fundingSources,
Operation: apperrors.QueryFetch,
}
}
return fundingSources, err
}
// DeleteTRBRequestForm deletes an existing TRB request form record in the database
func (s *Store) DeleteTRBRequestForm(ctx context.Context, trbRequestID uuid.UUID) (*models.TRBRequestForm, error) {
stmt, err := s.db.PrepareNamed(`
DELETE FROM trb_request_forms
WHERE trb_request_id = :trb_request_id
RETURNING *;`)
if err != nil {
appcontext.ZLogger(ctx).Error(
fmt.Sprintf("Failed to delete TRB request form %s", err),
zap.String("trbRequestID", trbRequestID.String()),
)
return nil, err
}
defer stmt.Close()
toDelete := models.TRBRequestForm{}
toDelete.ID = trbRequestID
deleted := models.TRBRequestForm{}
err = stmt.Get(&deleted, &toDelete)
if err != nil {
appcontext.ZLogger(ctx).Error(
fmt.Sprintf("Failed to delete TRB request form %s", err),
zap.String("trbRequestID", trbRequestID.String()),
)
return nil, &apperrors.QueryError{
Err: err,
Model: toDelete,
Operation: apperrors.QueryUpdate,
}
}
return &deleted, err
}