/
trb_advice_letter.go
189 lines (159 loc) · 5.32 KB
/
trb_advice_letter.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
package resolvers
import (
"context"
"errors"
"fmt"
"github.com/google/uuid"
"golang.org/x/sync/errgroup"
"github.com/cmsgov/easi-app/pkg/appcontext"
"github.com/cmsgov/easi-app/pkg/email"
"github.com/cmsgov/easi-app/pkg/models"
"github.com/cmsgov/easi-app/pkg/storage"
)
// GetTRBAdviceLetterByTRBRequestID fetches a TRB advice letter record by its associated request's ID.
func GetTRBAdviceLetterByTRBRequestID(ctx context.Context, store *storage.Store, id uuid.UUID) (*models.TRBAdviceLetter, error) {
return store.GetTRBAdviceLetterByTRBRequestID(ctx, id)
}
// CreateTRBAdviceLetter creates an advice letter for a TRB request, in the "In Progress" status, when the advice letter is ready to be worked on.
func CreateTRBAdviceLetter(ctx context.Context, store *storage.Store, trbRequestID uuid.UUID) (*models.TRBAdviceLetter, error) {
letter, err := store.CreateTRBAdviceLetter(ctx, appcontext.Principal(ctx).ID(), trbRequestID)
if err != nil {
return nil, err
}
return letter, nil
}
// UpdateTRBAdviceLetter handles general updates to a TRB advice letter
func UpdateTRBAdviceLetter(ctx context.Context, store *storage.Store, input map[string]interface{}) (*models.TRBAdviceLetter, error) {
idIface, idFound := input["trbRequestId"]
if !idFound {
return nil, errors.New("missing required property trbRequestId")
}
id, ok := idIface.(uuid.UUID)
if !ok {
return nil, fmt.Errorf("unable to convert incoming trbRequestId to uuid when updating TRB advice letter: %v", idIface)
}
letter, err := store.GetTRBAdviceLetterByTRBRequestID(ctx, id)
if err != nil {
return nil, err
}
err = ApplyChangesAndMetaData(input, letter, appcontext.Principal(ctx))
if err != nil {
return nil, err
}
updatedLetter, err := store.UpdateTRBAdviceLetter(ctx, letter)
if err != nil {
return nil, err
}
return updatedLetter, err
}
// RequestReviewForTRBAdviceLetter sets a TRB advice letter as ready for review and notifies the given recipients.
func RequestReviewForTRBAdviceLetter(
ctx context.Context,
store *storage.Store,
emailClient *email.Client,
fetchUserInfo func(context.Context, string) (*models.UserInfo, error),
id uuid.UUID,
) (*models.TRBAdviceLetter, error) {
letter, err := store.UpdateTRBAdviceLetterStatus(ctx, id, models.TRBAdviceLetterStatusReadyForReview)
if err != nil {
return nil, err
}
trb, err := store.GetTRBRequestByID(ctx, letter.TRBRequestID)
if err != nil {
return nil, err
}
var leadName string
if trb.TRBLead != nil {
leadInfo, err2 := fetchUserInfo(ctx, *trb.TRBLead)
if err2 != nil {
return nil, err2
}
leadName = leadInfo.DisplayName
}
emailInput := email.SendTRBAdviceLetterInternalReviewEmailInput{
TRBRequestID: trb.ID,
TRBRequestName: trb.GetName(),
TRBLeadName: leadName,
}
// Email client can be nil when this is called from tests - the email client itself tests this
// separately in the email package test
if emailClient != nil {
err = emailClient.SendTRBAdviceLetterInternalReviewEmail(ctx, emailInput)
if err != nil {
return nil, err
}
}
return letter, nil
}
// SendTRBAdviceLetter sends a TRB advice letter, setting its DateSent field, and (TODO) notifies the given recipients.
func SendTRBAdviceLetter(ctx context.Context,
store *storage.Store,
id uuid.UUID,
emailClient *email.Client,
fetchUserInfo func(context.Context, string) (*models.UserInfo, error),
fetchUserInfos func(context.Context, []string) ([]*models.UserInfo, error),
copyTRBMailbox bool,
notifyEUAIDs []string,
) (*models.TRBAdviceLetter, error) {
// Fetch user info for each EUA ID we want to notify
notifyUserInfos, err := fetchUserInfos(ctx, notifyEUAIDs)
if err != nil {
return nil, err
}
letter, err := store.UpdateTRBAdviceLetterStatus(ctx, id, models.TRBAdviceLetterStatusCompleted)
if err != nil {
return nil, err
}
trbID := letter.TRBRequestID
// Query the TRB request, form, attendees in parallel
errGroup := new(errgroup.Group)
// Query the TRB request
var trb *models.TRBRequest
var errTRB error
errGroup.Go(func() error {
trb, errTRB = store.GetTRBRequestByID(ctx, trbID)
return errTRB
})
// Query the TRB form
var form *models.TRBRequestForm
var errForm error
errGroup.Go(func() error {
form, errForm = store.GetTRBRequestFormByTRBRequestID(ctx, trbID)
return errForm
})
if errG := errGroup.Wait(); errG != nil {
return nil, errG
}
requester, err := fetchUserInfo(ctx, trb.CreatedBy)
if err != nil {
return nil, err
}
recipientEmails := make([]models.EmailAddress, 0, len(notifyUserInfos))
for _, recipientInfo := range notifyUserInfos {
recipientEmails = append(recipientEmails, recipientInfo.Email)
}
var component string
if form.Component != nil {
component = *form.Component
}
emailInput := email.SendTRBAdviceLetterSubmittedEmailInput{
TRBRequestID: trb.ID,
RequestName: trb.GetName(),
RequestType: string(trb.Type),
RequesterName: requester.DisplayName,
Component: component,
SubmissionDate: letter.ModifiedAt,
ConsultDate: trb.ConsultMeetingTime,
CopyTRBMailbox: copyTRBMailbox,
Recipients: recipientEmails,
}
// Email client can be nil when this is called from tests - the email client itself tests this
// separately in the email package test
if emailClient != nil {
err = emailClient.SendTRBAdviceLetterSubmittedEmail(ctx, emailInput)
if err != nil {
return nil, err
}
}
return letter, nil
}