-
Notifications
You must be signed in to change notification settings - Fork 11
/
business_case.go
424 lines (400 loc) · 15.1 KB
/
business_case.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package storage
import (
"context"
"database/sql"
"errors"
"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/sqlutils"
)
// IntakeExistsMsg is the error we see when there is no valid system intake
const IntakeExistsMsg = "Could not query model *models.BusinessCase with operation Create, received error: pq: insert or update on table \"business_cases\" violates foreign key constraint \"business_case_system_intake_fkey\""
// EuaIDMsg is the error we see when EUA doesn't meet EUA ID constraints
const EuaIDMsg = "Could not query model *models.BusinessCase with operation Create, received error: pq: new row for relation \"business_cases\" violates check constraint \"eua_id_check\""
// ValidStatusMsg is a match for an error we see when there is no valid status
const ValidStatusMsg = "pq: invalid input value for enum business_case_status: "
// UniqueIntakeMsg is a match for an error we see when the system intake already has a biz case
const UniqueIntakeMsg = "pq: duplicate key value violates unique constraint \"unique_intake_per_biz_case\""
// FetchBusinessCaseByID queries the DB for a business case matching the given ID
func (s *Store) FetchBusinessCaseByID(ctx context.Context, businessCaseID uuid.UUID) (*models.BusinessCase, error) {
businessCase := models.BusinessCase{}
const fetchBusinessCaseSQL = `
SELECT
business_cases.*,
coalesce(json_agg(estimated_lifecycle_costs) filter (where estimated_lifecycle_costs.* IS NOT NULL), '[]') as lifecycle_cost_lines
FROM
business_cases
LEFT JOIN estimated_lifecycle_costs ON business_cases.id = estimated_lifecycle_costs.business_case
JOIN system_intakes ON business_cases.system_intake = system_intakes.id
WHERE
business_cases.id = $1
GROUP BY estimated_lifecycle_costs.business_case, business_cases.id, system_intakes.id`
// Unsafe() is used to avoid errors from the initial_submitted_at and last_submitted_at columns that are in the database, but not in the Go model
// see https://jiraent.cms.gov/browse/EASI-1693
err := s.db.Unsafe().Get(&businessCase, fetchBusinessCaseSQL, businessCaseID)
if err != nil {
appcontext.ZLogger(ctx).Error(
fmt.Sprintf("Failed to fetch business case %s", err),
zap.String("id", businessCaseID.String()),
)
if errors.Is(err, sql.ErrNoRows) {
return nil, &apperrors.ResourceNotFoundError{Err: err, Resource: models.BusinessCase{}}
}
return nil, err
}
return &businessCase, nil
}
// FetchBusinessCaseBySystemIntakeID queries the DB for a business case matching the given ID of the System Intake
func (s *Store) FetchBusinessCaseBySystemIntakeID(ctx context.Context, systemIntakeID uuid.UUID) (*models.BusinessCase, error) {
businessCase := models.BusinessCase{}
const fetchBusinessCaseSQL = `
SELECT
business_cases.*,
coalesce(json_agg(estimated_lifecycle_costs) filter (where estimated_lifecycle_costs.* is NOT NULL), '[]') as lifecycle_cost_lines
FROM
business_cases
LEFT JOIN estimated_lifecycle_costs ON business_cases.id = estimated_lifecycle_costs.business_case
JOIN system_intakes ON business_cases.system_intake = system_intakes.id
WHERE
business_cases.system_intake = $1
GROUP BY estimated_lifecycle_costs.business_case, business_cases.id, system_intakes.id`
// Unsafe() is used to avoid errors from the initial_submitted_at and last_submitted_at columns that are in the database, but not in the Go model
// see https://jiraent.cms.gov/browse/EASI-1693
err := s.db.Unsafe().Get(&businessCase, fetchBusinessCaseSQL, systemIntakeID)
if err != nil {
// This function, unlike a few others in this file, does NOT error out if there is no business case, since it's
// totally valid to have a system intake without a business case, so we check the error type BEFORE logging
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
appcontext.ZLogger(ctx).Error(
fmt.Sprintf("Failed to fetch business case by SystemIntakeID %s", err),
zap.String("systemIntakeId", systemIntakeID.String()),
)
return nil, err
}
return &businessCase, nil
}
// FetchOpenBusinessCaseByIntakeID queries the DB for an open business case matching the given intake ID
func (s *Store) FetchOpenBusinessCaseByIntakeID(ctx context.Context, intakeID uuid.UUID) (*models.BusinessCase, error) {
businessCase := models.BusinessCase{}
const fetchBusinessCaseSQL = `
SELECT
business_cases.*,
coalesce(json_agg(estimated_lifecycle_costs) filter (where estimated_lifecycle_costs.* is NOT NULL), '[]') as lifecycle_cost_lines
FROM
business_cases
LEFT JOIN estimated_lifecycle_costs ON business_cases.id = estimated_lifecycle_costs.business_case
WHERE
business_cases.system_intake = $1 AND business_cases.status = 'OPEN'
GROUP BY estimated_lifecycle_costs.business_case, business_cases.id`
// Unsafe() is used to avoid errors from the initial_submitted_at and last_submitted_at columns that are in the database, but not in the Go model
// see https://jiraent.cms.gov/browse/EASI-1693
err := s.db.Unsafe().Get(&businessCase, fetchBusinessCaseSQL, intakeID)
if err != nil {
appcontext.ZLogger(ctx).Error(
fmt.Sprintf("Failed to fetch business case %s", err),
zap.String("id", intakeID.String()),
)
if errors.Is(err, sql.ErrNoRows) {
return nil, &apperrors.ResourceNotFoundError{Err: err, Resource: models.BusinessCase{}}
}
return nil, err
}
fmt.Println(businessCase)
return &businessCase, nil
}
func createEstimatedLifecycleCosts(ctx context.Context, tx *sqlx.Tx, businessCase *models.BusinessCase) error {
const createEstimatedLifecycleCostSQL = `
INSERT INTO estimated_lifecycle_costs (
id,
business_case,
solution,
year,
phase,
cost
)
VALUES (
:id,
:business_case,
:solution,
:year,
:phase,
:cost
)
`
for i := range businessCase.LifecycleCostLines {
costLine := businessCase.LifecycleCostLines[i]
// if cost is `nil`, we don't want to attempt to insert that into the DB
// this is a known behavior -- the frontend will try and send `null` values in the array of costs,
// so we just catch them here before they make it into the DB
if costLine.Cost == nil {
continue
}
costLine.ID = uuid.New()
costLine.BusinessCaseID = businessCase.ID
_, err := tx.NamedExec(createEstimatedLifecycleCostSQL, &costLine)
if err != nil {
appcontext.ZLogger(ctx).Error(
fmt.Sprintf(
"Failed to create cost %s %s with error %s",
costLine.Solution,
costLine.Year,
err,
),
zap.String("EUAUserID", businessCase.EUAUserID),
zap.String("SystemIntakeID", businessCase.SystemIntakeID.String()),
)
return err
}
}
return nil
}
// CreateBusinessCase creates a business case
func (s *Store) CreateBusinessCase(ctx context.Context, businessCase *models.BusinessCase) (*models.BusinessCase, error) {
return sqlutils.WithTransactionRet[*models.BusinessCase](ctx, s.db, func(tx *sqlx.Tx) (*models.BusinessCase, error) {
id := uuid.New()
businessCase.ID = id
const createBusinessCaseSQL = `
INSERT INTO business_cases (
id,
eua_user_id,
system_intake,
status,
project_name,
requester,
requester_phone_number,
business_owner,
business_need,
current_solution_summary,
cms_benefit,
priority_alignment,
success_indicators,
preferred_title,
preferred_summary,
preferred_acquisition_approach,
preferred_security_is_approved,
preferred_security_is_being_reviewed,
preferred_hosting_type,
preferred_hosting_location,
preferred_hosting_cloud_service_type,
preferred_has_ui,
preferred_pros,
preferred_cons,
preferred_cost_savings,
alternative_a_title,
alternative_a_summary,
alternative_a_acquisition_approach,
alternative_a_security_is_approved,
alternative_a_security_is_being_reviewed,
alternative_a_hosting_type,
alternative_a_hosting_location,
alternative_a_hosting_cloud_service_type,
alternative_a_has_ui,
alternative_a_pros,
alternative_a_cons,
alternative_a_cost_savings,
alternative_b_title,
alternative_b_summary,
alternative_b_acquisition_approach,
alternative_b_security_is_approved,
alternative_b_security_is_being_reviewed,
alternative_b_hosting_type,
alternative_b_hosting_location,
alternative_b_hosting_cloud_service_type,
alternative_b_has_ui,
alternative_b_pros,
alternative_b_cons,
alternative_b_cost_savings,
created_at,
updated_at
)
VALUES (
:id,
:eua_user_id,
:system_intake,
:status,
:project_name,
:requester,
:requester_phone_number,
:business_owner,
:business_need,
:current_solution_summary,
:cms_benefit,
:priority_alignment,
:success_indicators,
:preferred_title,
:preferred_summary,
:preferred_acquisition_approach,
:preferred_security_is_approved,
:preferred_security_is_being_reviewed,
:preferred_hosting_type,
:preferred_hosting_location,
:preferred_hosting_cloud_service_type,
:preferred_has_ui,
:preferred_pros,
:preferred_cons,
:preferred_cost_savings,
:alternative_a_title,
:alternative_a_summary,
:alternative_a_acquisition_approach,
:alternative_a_security_is_approved,
:alternative_a_security_is_being_reviewed,
:alternative_a_hosting_type,
:alternative_a_hosting_location,
:alternative_a_hosting_cloud_service_type,
:alternative_a_has_ui,
:alternative_a_pros,
:alternative_a_cons,
:alternative_a_cost_savings,
:alternative_b_title,
:alternative_b_summary,
:alternative_b_acquisition_approach,
:alternative_b_security_is_approved,
:alternative_b_security_is_being_reviewed,
:alternative_b_hosting_type,
:alternative_b_hosting_location,
:alternative_b_hosting_cloud_service_type,
:alternative_b_has_ui,
:alternative_b_pros,
:alternative_b_cons,
:alternative_b_cost_savings,
:created_at,
:updated_at
)`
logger := appcontext.ZLogger(ctx)
_, err := tx.NamedExec(createBusinessCaseSQL, &businessCase)
if err != nil {
logger.Error(
fmt.Sprintf("Failed to create business case with error %s", err),
zap.String("EUAUserID", businessCase.EUAUserID),
zap.String("SystemIntakeID", businessCase.SystemIntakeID.String()),
)
if err.Error() == "pq: duplicate key value violates unique constraint \"unique_intake_per_biz_case\"" {
return nil,
&apperrors.ResourceConflictError{
Err: err,
Resource: models.BusinessCase{},
ResourceID: businessCase.SystemIntakeID.String(),
}
}
return nil, &apperrors.QueryError{
Err: err,
Model: businessCase,
Operation: apperrors.QueryPost,
}
}
err = createEstimatedLifecycleCosts(ctx, tx, businessCase)
if err != nil {
logger.Error(
fmt.Sprintf("Failed to create business case with lifecycle costs with error %s", err),
zap.String("EUAUserID", businessCase.EUAUserID),
zap.String("BusinessCaseID", businessCase.ID.String()),
)
return nil, &apperrors.QueryError{
Err: err,
Model: businessCase,
Operation: apperrors.QueryPost,
}
}
return businessCase, nil
})
}
// UpdateBusinessCase creates a business case
func (s *Store) UpdateBusinessCase(ctx context.Context, businessCase *models.BusinessCase) (*models.BusinessCase, error) {
return sqlutils.WithTransactionRet[*models.BusinessCase](ctx, s.db, func(tx *sqlx.Tx) (*models.BusinessCase, error) {
// We are explicitly not updating ID, EUAUserID and SystemIntakeID
const updateBusinessCaseSQL = `
UPDATE business_cases
SET
project_name = :project_name,
requester = :requester,
requester_phone_number = :requester_phone_number,
business_owner = :business_owner,
business_need = :business_need,
current_solution_summary = :current_solution_summary,
cms_benefit = :cms_benefit,
priority_alignment = :priority_alignment,
success_indicators = :success_indicators,
preferred_title = :preferred_title,
preferred_summary = :preferred_summary,
preferred_acquisition_approach = :preferred_acquisition_approach,
preferred_security_is_approved = :preferred_security_is_approved,
preferred_security_is_being_reviewed = :preferred_security_is_being_reviewed,
preferred_hosting_type = :preferred_hosting_type,
preferred_hosting_location = :preferred_hosting_location,
preferred_hosting_cloud_service_type = :preferred_hosting_cloud_service_type,
preferred_has_ui = :preferred_has_ui,
preferred_pros = :preferred_pros,
preferred_cons = :preferred_cons,
preferred_cost_savings = :preferred_cost_savings,
alternative_a_title = :alternative_a_title,
alternative_a_summary = :alternative_a_summary,
alternative_a_acquisition_approach = :alternative_a_acquisition_approach,
alternative_a_security_is_approved = :alternative_a_security_is_approved,
alternative_a_security_is_being_reviewed = :alternative_a_security_is_being_reviewed,
alternative_a_hosting_type = :alternative_a_hosting_type,
alternative_a_hosting_location = :alternative_a_hosting_location,
alternative_a_hosting_cloud_service_type = :alternative_a_hosting_cloud_service_type,
alternative_a_has_ui = :alternative_a_has_ui,
alternative_a_pros = :alternative_a_pros,
alternative_a_cons = :alternative_a_cons,
alternative_a_cost_savings = :alternative_a_cost_savings,
alternative_b_title = :alternative_b_title,
alternative_b_summary = :alternative_b_summary,
alternative_b_acquisition_approach = :alternative_b_acquisition_approach,
alternative_b_security_is_approved = :alternative_b_security_is_approved,
alternative_b_security_is_being_reviewed = :alternative_b_security_is_being_reviewed,
alternative_b_hosting_type = :alternative_b_hosting_type,
alternative_b_hosting_location = :alternative_b_hosting_location,
alternative_b_hosting_cloud_service_type = :alternative_b_hosting_cloud_service_type,
alternative_b_has_ui = :alternative_b_has_ui,
alternative_b_pros = :alternative_b_pros,
alternative_b_cons = :alternative_b_cons,
alternative_b_cost_savings = :alternative_b_cost_savings,
updated_at = :updated_at,
archived_at = :archived_at,
status = :status
WHERE business_cases.id = :id
`
const deleteLifecycleCostsSQL = `
DELETE FROM estimated_lifecycle_costs
WHERE business_case = :id
`
logger := appcontext.ZLogger(ctx)
result, err := tx.NamedExec(updateBusinessCaseSQL, &businessCase)
if err != nil {
logger.Error(
fmt.Sprintf("Failed to update business case %s", err),
zap.String("id", businessCase.ID.String()),
)
return businessCase, err
}
affectedRows, rowsAffectedErr := result.RowsAffected()
if affectedRows == 0 || rowsAffectedErr != nil {
logger.Error(
fmt.Sprintf("Failed to update business case %s", err),
zap.String("id", businessCase.ID.String()),
)
return businessCase, errors.New("business case not found")
}
_, err = tx.NamedExec(deleteLifecycleCostsSQL, &businessCase)
if err != nil {
logger.Error(
fmt.Sprintf("Failed to update pre-existing business case costs %s", err),
zap.String("id", businessCase.ID.String()),
)
return businessCase, err
}
err = createEstimatedLifecycleCosts(ctx, tx, businessCase)
if err != nil {
return businessCase, err
}
return businessCase, nil
})
}