@@ -13,15 +13,16 @@ import (
1313 "github.com/tidepool-org/platform/page"
1414 "github.com/tidepool-org/platform/pointer"
1515 "github.com/tidepool-org/platform/structure/validator"
16+ "github.com/tidepool-org/platform/user"
1617)
1718
1819const (
1920 EligibleField = "eligible"
2021 NameField = "name"
2122 DateOfBirthField = "dateOfBirth"
2223
23- UserIDField = "participantId "
24- ParticipantIDField = "userId "
24+ UserIDField = "userId "
25+ ParticipantIDField = "participantId "
2526)
2627
2728type WebhookProcessor struct {
@@ -32,14 +33,15 @@ type WebhookProcessor struct {
3233
3334 consentService consent.Service
3435 customerIOClient * customerio.Client
36+ userClient user.Client
3537}
3638
3739type Config struct {
3840 BaseURL string `envconfig:"TIDEPOOL_JOTFORM_BASE_URL"`
3941 APIKey string `envconfig:"TIDEPOOL_JOTFORM_API_KEY"`
4042}
4143
42- func NewWebhookProcessor (config Config , logger log.Logger , consentService consent.Service , customerIOClient * customerio.Client ) (* WebhookProcessor , error ) {
44+ func NewWebhookProcessor (config Config , logger log.Logger , consentService consent.Service , customerIOClient * customerio.Client , userClient user. Client ) (* WebhookProcessor , error ) {
4345 return & WebhookProcessor {
4446 apiKey : config .APIKey ,
4547 baseURL : config .BaseURL ,
@@ -48,6 +50,7 @@ func NewWebhookProcessor(config Config, logger log.Logger, consentService consen
4850
4951 consentService : consentService ,
5052 customerIOClient : customerIOClient ,
53+ userClient : userClient ,
5154 }, nil
5255}
5356
@@ -98,27 +101,33 @@ func (w *WebhookProcessor) validateUser(ctx context.Context, submissionID string
98101
99102 customer , err := w .customerIOClient .GetCustomer (ctx , userID , customerio .IDTypeUserID )
100103 if err != nil {
101- return "" , errors .Wrap (err , "unable to get customer" )
104+ return "" , errors .Wrapf (err , "unable to get customer with id %s" , userID )
102105 }
103106
104107 if customer == nil {
105- return "" , errors .New ("customer not found" )
108+ return "" , errors .Newf ("customer with id %s not found" , userID )
106109 }
107110 if customer .OuraParticipantID != participantID {
108- return "" , errors .New ("participant id mismatch" )
111+ return "" , errors .Newf ("participant id mismatch for user with id %s" , userID )
109112 }
113+
114+ usr , err := w .userClient .Get (ctx , userID )
115+ if err != nil {
116+ return "" , errors .Wrap (err , "unable to get user" )
117+ }
118+ if usr == nil {
119+ return "" , errors .New ("user not found" )
120+ }
121+
110122 return userID , nil
111123}
112124
113125func (w * WebhookProcessor ) ensureConsentRecordExists (ctx context.Context , userID string , submission * SubmissionResponse ) error {
114126 logger := w .logger .WithField ("submission" , submission .Content .ID )
115127
116- survey := OuraEligibilitySurvey {}
117- if dob , ok := submission .Content .Answers [DateOfBirthField ]; ok && dob .Answer () != "" {
118- survey .DateOfBirth = dob .Answer ()
119- }
120- if name , ok := submission .Content .Answers [NameField ]; ok && name .Answer () != "" {
121- survey .Name = name .Answer ()
128+ survey := OuraEligibilitySurvey {
129+ DateOfBirth : submission .Content .Answers .GetAnswerTextByName (DateOfBirthField ),
130+ Name : submission .Content .Answers .GetAnswerTextByName (NameField ),
122131 }
123132
124133 v := validator .New (w .logger )
0 commit comments