Skip to content

Commit 48c809f

Browse files
committed
Ensure user exists before creating consent
1 parent 599118c commit 48c809f

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

auth/service/service/service.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,22 @@ func (s *Service) initializeRouter() error {
290290
if err := envconfig.Process("", &customerIOConfig); err != nil {
291291
return errors.Wrap(err, "unable to load customerio config")
292292
}
293-
customerIOClient, err := customerio.NewClient(customerIOConfig)
293+
customerIOClient, err := customerio.NewClient(customerIOConfig, s.Logger())
294294
if err != nil {
295295
return errors.Wrap(err, "unable to create customerio client")
296296
}
297297

298-
webhookProcessor, err := jotform.NewWebhookProcessor(jotformConfig, s.Logger(), s.consentService, customerIOClient)
298+
s.Logger().Debug("Initializing user client")
299+
usrClient, err := userClient.NewDefaultClient(userClient.Params{
300+
ConfigReporter: s.ConfigReporter(),
301+
Logger: s.Logger(),
302+
UserAgent: s.UserAgent(),
303+
})
304+
if err != nil {
305+
return errors.Wrap(err, "unable to create user client")
306+
}
307+
308+
webhookProcessor, err := jotform.NewWebhookProcessor(jotformConfig, s.Logger(), s.consentService, customerIOClient, usrClient)
299309
if err != nil {
300310
return errors.Wrap(err, "unable to create jotform webhook processor")
301311
}

oura/customerio/client.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package customerio
22

3+
import "github.com/tidepool-org/platform/log"
4+
35
const appAPIBaseURL = "https://api.customer.io"
46
const trackAPIBaseURL = "https://track.customer.io/api/"
57

@@ -9,6 +11,7 @@ type Client struct {
911
siteID string
1012
appAPIBaseURL string
1113
trackAPIBaseURL string
14+
logger log.Logger
1215
}
1316

1417
type Config struct {
@@ -18,12 +21,13 @@ type Config struct {
1821
SegmentID string `envconfig:"TIDEPOOL_CUSTOMERIO_SEGMENT_ID"`
1922
}
2023

21-
func NewClient(config Config) (*Client, error) {
24+
func NewClient(config Config, logger log.Logger) (*Client, error) {
2225
return &Client{
2326
appAPIKey: config.AppAPIKey,
2427
trackAPIKey: config.TrackAPIKey,
2528
siteID: config.SiteID,
2629
appAPIBaseURL: appAPIBaseURL,
2730
trackAPIBaseURL: trackAPIBaseURL,
31+
logger: logger,
2832
}, nil
2933
}

oura/customerio/customer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ func (c *Client) GetCustomer(ctx context.Context, cid string, typ IDType) (*Cust
7171
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.appAPIKey))
7272
req.Header.Set("Content-Type", "application/json")
7373

74+
c.logger.WithField("cid", cid).WithField("url", req.URL.String()).Debug("fetching customer")
75+
7476
resp, err := http.DefaultClient.Do(req)
7577
if err != nil {
7678
return nil, fmt.Errorf("failed to execute request: %w", err)

oura/jotform/webhook.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1819
const (
1920
EligibleField = "eligible"
2021
NameField = "name"
2122
DateOfBirthField = "dateOfBirth"
2223

23-
UserIDField = "participantId"
24-
ParticipantIDField = "userId"
24+
UserIDField = "userId"
25+
ParticipantIDField = "participantId"
2526
)
2627

2728
type WebhookProcessor struct {
@@ -32,14 +33,15 @@ type WebhookProcessor struct {
3233

3334
consentService consent.Service
3435
customerIOClient *customerio.Client
36+
userClient user.Client
3537
}
3638

3739
type 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

113125
func (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

Comments
 (0)