/
trb_admin_note.go
240 lines (199 loc) · 8.93 KB
/
trb_admin_note.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
package resolvers
import (
"context"
"errors"
"fmt"
"github.com/google/uuid"
"github.com/guregu/null"
"github.com/cmsgov/easi-app/pkg/appcontext"
"github.com/cmsgov/easi-app/pkg/apperrors"
"github.com/cmsgov/easi-app/pkg/graph/model"
"github.com/cmsgov/easi-app/pkg/models"
"github.com/cmsgov/easi-app/pkg/storage"
)
func CreateTRBAdminNoteGeneralRequest(ctx context.Context, store *storage.Store, input model.CreateTRBAdminNoteGeneralRequestInput) (*models.TRBAdminNote, error) {
noteToCreate := models.TRBAdminNote{
TRBRequestID: input.TrbRequestID,
Category: models.TRBAdminNoteCategoryGeneralRequest,
NoteText: input.NoteText,
}
noteToCreate.CreatedBy = appcontext.Principal(ctx).ID()
createdNote, err := store.CreateTRBAdminNote(ctx, ¬eToCreate)
if err != nil {
return nil, err
}
return createdNote, nil
}
func CreateTRBAdminNoteInitialRequestForm(ctx context.Context, store *storage.Store, input model.CreateTRBAdminNoteInitialRequestFormInput) (*models.TRBAdminNote, error) {
noteToCreate := models.TRBAdminNote{
TRBRequestID: input.TrbRequestID,
Category: models.TRBAdminNoteCategoryInitialRequestForm,
NoteText: input.NoteText,
AppliesToBasicRequestDetails: null.BoolFrom(input.AppliesToBasicRequestDetails),
AppliesToSubjectAreas: null.BoolFrom(input.AppliesToSubjectAreas),
AppliesToAttendees: null.BoolFrom(input.AppliesToAttendees),
}
noteToCreate.CreatedBy = appcontext.Principal(ctx).ID()
createdNote, err := store.CreateTRBAdminNote(ctx, ¬eToCreate)
if err != nil {
return nil, err
}
return createdNote, nil
}
func CreateTRBAdminNoteSupportingDocuments(ctx context.Context, store *storage.Store, input model.CreateTRBAdminNoteSupportingDocumentsInput) (*models.TRBAdminNote, error) {
// it's valid for input.DocumentIDs to be empty; see note in acceptance criteria in https://jiraent.cms.gov/browse/EASI-3362
// check that the documents belong to the same TRB request
// database constraints will prevent links being created to docs on a different request
// but if we don't check, we'll still create an (invalid) admin note record
allDocsOnRequest, err := store.GetTRBRequestDocumentsByRequestID(ctx, input.TrbRequestID)
if err != nil {
return nil, err
}
if !models.ContainsAllIDs(allDocsOnRequest, input.DocumentIDs) {
return nil, &apperrors.BadRequestError{
Err: errors.New("all documents referenced in admin note must belong to the same TRB request as the admin note"),
}
}
noteToCreate := models.TRBAdminNote{
TRBRequestID: input.TrbRequestID,
Category: models.TRBAdminNoteCategorySupportingDocuments,
NoteText: input.NoteText,
}
noteToCreate.CreatedBy = appcontext.Principal(ctx).ID()
// ideally, we'd create the admin note and any links to documents in a single transaction, but we don't currently have that capability
// see Note [Database calls from resolvers aren't atomic]
// create the admin note itself (and get the result, with the generated ID)
createdNote, err := store.CreateTRBAdminNote(ctx, ¬eToCreate)
if err != nil {
return nil, err
}
// create links to documents referenced by the admin note (if any are present)
if len(input.DocumentIDs) > 0 {
_, err = store.CreateTRBAdminNoteTRBDocumentLinks(ctx, input.TrbRequestID, createdNote.ID, input.DocumentIDs)
if err != nil {
return nil, err
}
}
return createdNote, nil
}
func CreateTRBAdminNoteConsultSession(ctx context.Context, store *storage.Store, input model.CreateTRBAdminNoteConsultSessionInput) (*models.TRBAdminNote, error) {
noteToCreate := models.TRBAdminNote{
TRBRequestID: input.TrbRequestID,
Category: models.TRBAdminNoteCategoryConsultSession,
NoteText: input.NoteText,
}
noteToCreate.CreatedBy = appcontext.Principal(ctx).ID()
createdNote, err := store.CreateTRBAdminNote(ctx, ¬eToCreate)
if err != nil {
return nil, err
}
return createdNote, nil
}
func CreateTRBAdminNoteAdviceLetter(ctx context.Context, store *storage.Store, input model.CreateTRBAdminNoteAdviceLetterInput) (*models.TRBAdminNote, error) {
// it's valid for input.RecommendationIDs to be empty; see note in acceptance criteria in https://jiraent.cms.gov/browse/EASI-3362
// check that the recommendations belong to the same TRB request
// database constraints will prevent links being created to recommendations on a different request
// but if we don't check, we'll still create an (invalid) admin note record
allRecommendationsOnRequest, err := store.GetTRBAdviceLetterRecommendationsByTRBRequestID(ctx, input.TrbRequestID)
if err != nil {
return nil, err
}
if !models.ContainsAllIDs(allRecommendationsOnRequest, input.RecommendationIDs) {
return nil, &apperrors.BadRequestError{
Err: errors.New("all recommendations referenced in admin note must belong to the same TRB request as the admin note"),
}
}
noteToCreate := models.TRBAdminNote{
TRBRequestID: input.TrbRequestID,
Category: models.TRBAdminNoteCategoryAdviceLetter,
NoteText: input.NoteText,
AppliesToMeetingSummary: null.BoolFrom(input.AppliesToMeetingSummary),
AppliesToNextSteps: null.BoolFrom(input.AppliesToNextSteps),
}
noteToCreate.CreatedBy = appcontext.Principal(ctx).ID()
// ideally, we'd create the admin note and any links to recommendations in a single transaction, but we don't currently have that capability
// see Note [Database calls from resolvers aren't atomic]
// create the admin note itself (and get the result, with the generated ID)
createdNote, err := store.CreateTRBAdminNote(ctx, ¬eToCreate)
if err != nil {
return nil, err
}
// create links to recommendations referenced the by the admin note (if any are present)
if len(input.RecommendationIDs) > 0 {
_, err = store.CreateTRBAdminNoteTRBRecommendationLinks(ctx, input.TrbRequestID, createdNote.ID, input.RecommendationIDs)
if err != nil {
return nil, err
}
}
return createdNote, nil
}
// GetTRBAdminNoteByID retrieves a single admin note by its ID
func GetTRBAdminNoteByID(ctx context.Context, store *storage.Store, id uuid.UUID) (*models.TRBAdminNote, error) {
note, err := store.GetTRBAdminNoteByID(ctx, id)
if err != nil {
return nil, err
}
if note == nil {
return nil, &apperrors.ResourceNotFoundError{
Err: err,
Resource: models.TRBAdminNote{},
}
}
return note, nil
}
// GetTRBAdminNotesByTRBRequestID retrieves a list of admin notes associated with a TRB request
func GetTRBAdminNotesByTRBRequestID(ctx context.Context, store *storage.Store, trbRequestID uuid.UUID) ([]*models.TRBAdminNote, error) {
notes, err := store.GetTRBAdminNotesByTRBRequestID(ctx, trbRequestID)
if err != nil {
return nil, err
}
return notes, nil
}
// GetTRBAdminNoteCategorySpecificData returns the category-specific data for TRB admin notes that can be loaded from the database;
// fields that require querying other data sources (such as documents' Status and URL fields, which require querying S3) are handled by other resolvers if they're requested
func GetTRBAdminNoteCategorySpecificData(ctx context.Context, store *storage.Store, note *models.TRBAdminNote) (model.TRBAdminNoteCategorySpecificData, error) {
switch note.Category {
case models.TRBAdminNoteCategoryGeneralRequest:
return model.TRBAdminNoteGeneralRequestCategoryData{
PlaceholderField: nil,
}, nil
case models.TRBAdminNoteCategoryInitialRequestForm:
return model.TRBAdminNoteInitialRequestFormCategoryData{
AppliesToBasicRequestDetails: note.AppliesToBasicRequestDetails.Bool,
AppliesToSubjectAreas: note.AppliesToSubjectAreas.Bool,
AppliesToAttendees: note.AppliesToAttendees.Bool,
}, nil
case models.TRBAdminNoteCategorySupportingDocuments:
documents, err := store.GetTRBRequestDocumentsByAdminNoteID(ctx, note.ID)
if err != nil {
return nil, err
}
return model.TRBAdminNoteSupportingDocumentsCategoryData{
Documents: documents,
}, nil
case models.TRBAdminNoteCategoryConsultSession:
return model.TRBAdminNoteConsultSessionCategoryData{
PlaceholderField: nil,
}, nil
case models.TRBAdminNoteCategoryAdviceLetter:
recommendations, err := store.GetTRBRecommendationsByAdminNoteID(ctx, note.ID)
if err != nil {
return nil, err
}
return model.TRBAdminNoteAdviceLetterCategoryData{
AppliesToMeetingSummary: note.AppliesToMeetingSummary.Bool,
AppliesToNextSteps: note.AppliesToNextSteps.Bool,
Recommendations: recommendations,
}, nil
}
// this should never happen, all five categories should be handled, but in case it does, error and alert on it
return nil, apperrors.NewInvalidEnumError(fmt.Errorf("admin note has an unrecognized category"), note.Category, "TRBAdminNoteCategory")
}
// SetTRBAdminNoteArchived sets whether a TRB admin note is archived (soft-deleted)
func SetTRBAdminNoteArchived(ctx context.Context, store *storage.Store, id uuid.UUID, isArchived bool) (*models.TRBAdminNote, error) {
updatedNote, err := store.SetTRBAdminNoteArchived(ctx, id, isArchived, appcontext.Principal(ctx).ID())
if err != nil {
return nil, err
}
return updatedNote, nil
}