/
trb_ready_for_consult.go
88 lines (74 loc) · 2.5 KB
/
trb_ready_for_consult.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
package email
import (
"bytes"
"context"
"errors"
"fmt"
"html/template"
"path"
"github.com/google/uuid"
"github.com/cmsgov/easi-app/pkg/apperrors"
"github.com/cmsgov/easi-app/pkg/models"
)
type readyForConsultEmailParameters struct {
RequestName string
RequesterName string
Feedback template.HTML // This allows unescaped HTML //TODO! make sure to sanitize it! // https://pkg.go.dev/html/template
TRBRequestLink string
TRBAdminRequestLink string
TRBInboxAddress string
}
func (c Client) trbReadyForConsultEmailBody(requestID uuid.UUID, requestName string, requesterName string, feedback models.HTML) (string, error) {
requestTaskListPath := path.Join("trb", "task-list", requestID.String())
requestAdminViewPath := path.Join("trb", requestID.String(), "request")
//TODO, should we use template.HTML earlier instead of converting here? We can use that instead of models.HTML potentially
data := readyForConsultEmailParameters{
RequestName: requestName,
RequesterName: requesterName,
Feedback: feedback.ToTemplate(),
TRBRequestLink: c.urlFromPath(requestTaskListPath),
TRBAdminRequestLink: c.urlFromPath(requestAdminViewPath),
TRBInboxAddress: c.config.TRBEmail.String(),
}
var b bytes.Buffer
if c.templates.trbReadyForConsult == nil {
return "", errors.New("TRB Ready for Consult template is nil")
}
// c.templates.trbReadyForConsult
err := c.templates.trbReadyForConsult.Execute(&b, data)
if err != nil {
return "", err
}
return b.String(), nil
}
// SendTRBReadyForConsultNotification notifies user-selected recipients that a TRB request is ready for its consult session
func (c Client) SendTRBReadyForConsultNotification(
ctx context.Context,
recipients []models.EmailAddress,
copyTRBMailbox bool,
requestID uuid.UUID,
requestName string,
requesterName string,
feedback models.HTML, //TODO make this accept rich text
) error {
subject := fmt.Sprintf("%v is ready for a consult session", requestName)
body, err := c.trbReadyForConsultEmailBody(requestID, requestName, requesterName, feedback)
if err != nil {
return &apperrors.NotificationError{Err: err, DestinationType: apperrors.DestinationTypeEmail}
}
allRecipients := recipients
if copyTRBMailbox {
allRecipients = append(allRecipients, c.config.TRBEmail)
}
err = c.sender.Send(
ctx,
allRecipients,
nil,
subject,
body,
)
if err != nil {
return &apperrors.NotificationError{Err: err, DestinationType: apperrors.DestinationTypeEmail}
}
return nil
}