Skip to content

Commit d0e99a7

Browse files
committed
Reorganize the oura releated code
1 parent 8af6266 commit d0e99a7

File tree

13 files changed

+154
-121
lines changed

13 files changed

+154
-121
lines changed

auth/service/service/service.go

Lines changed: 96 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import (
55
"net/http"
66
"time"
77

8+
"github.com/tidepool-org/platform/customerio"
89
"github.com/tidepool-org/platform/mailer"
9-
"github.com/tidepool-org/platform/oura/customerio"
1010
"github.com/tidepool-org/platform/oura/jotform"
1111
"github.com/tidepool-org/platform/oura/shopify"
12+
"github.com/tidepool-org/platform/user"
1213

1314
userClient "github.com/tidepool-org/platform/user/client"
1415

@@ -71,21 +72,24 @@ func (c *confirmationClientConfig) Load() error {
7172
type Service struct {
7273
*serviceService.Service
7374
domain string
75+
appValidator *appvalidate.Validator
76+
authClient *Client
7477
authStore *authStoreMongo.Store
75-
workStructuredStore *workStoreStructuredMongo.Store
78+
confirmationClient confirmationClient.ClientWithResponsesInterface
79+
customerIOClient *customerio.Client
7680
dataClient dataClient.Client
7781
dataSourceClient *dataSourceClient.Client
78-
confirmationClient confirmationClient.ClientWithResponsesInterface
79-
taskClient task.Client
80-
workClient *workService.Client
81-
providerFactory *providerFactory.Factory
82-
authClient *Client
83-
userEventsHandler events.Runner
8482
deviceCheck apple.DeviceCheck
85-
appValidator *appvalidate.Validator
8683
partnerSecrets *appvalidate.PartnerSecrets
84+
providerFactory *providerFactory.Factory
85+
shopifyClient shopify.Client
86+
taskClient task.Client
8787
twiistServiceAccountAuthorizer auth.ServiceAccountAuthorizer
88+
userEventsHandler events.Runner
89+
userClient user.Client
8890
consentService consent.Service
91+
workClient *workService.Client
92+
workStructuredStore *workStoreStructuredMongo.Store
8993
}
9094

9195
func New() *Service {
@@ -141,6 +145,9 @@ func (s *Service) Initialize(provider application.Provider) error {
141145
if err := s.initializeAuthClient(); err != nil {
142146
return err
143147
}
148+
if err := s.initializeUserClient(); err != nil {
149+
return err
150+
}
144151
if err := s.initializeConsentService(); err != nil {
145152
return err
146153
}
@@ -159,6 +166,12 @@ func (s *Service) Initialize(provider application.Provider) error {
159166
if err := s.initializeTwiistServiceAccountAuthorizer(); err != nil {
160167
return err
161168
}
169+
if err := s.initializeCustomerIOClient(); err != nil {
170+
return err
171+
}
172+
if err := s.initializeShopifyClient(); err != nil {
173+
return err
174+
}
162175
if err := s.initializeRouter(); err != nil {
163176
return err
164177
}
@@ -260,85 +273,119 @@ func (s *Service) terminateDomain() {
260273
}
261274
}
262275

263-
func (s *Service) initializeRouter() error {
264-
s.Logger().Debug("Creating api router")
265-
266-
apiRouter, err := authServiceApi.NewRouter(s)
276+
func (s *Service) initializeUserClient() error {
277+
s.Logger().Debug("Initializing user client")
278+
var err error
279+
s.userClient, err = userClient.NewDefaultClient(userClient.Params{
280+
ConfigReporter: s.ConfigReporter(),
281+
Logger: s.Logger(),
282+
UserAgent: s.UserAgent(),
283+
})
267284
if err != nil {
268-
return errors.Wrap(err, "unable to create api router")
285+
return errors.Wrap(err, "unable to create user client")
269286
}
287+
return nil
288+
}
270289

271-
s.Logger().Debug("Creating v1 router")
290+
func (s *Service) initializeShopifyClient() error {
291+
shopifyConfig := shopify.ClientConfig{}
292+
if err := envconfig.Process("", &shopifyConfig); err != nil {
293+
return errors.Wrap(err, "unable to load shopify config")
294+
}
272295

273-
v1Router, err := authServiceApiV1.NewRouter(s)
296+
var err error
297+
s.shopifyClient, err = shopifyClient.New(context.Background(), shopifyConfig)
274298
if err != nil {
275-
return errors.Wrap(err, "unable to create v1 router")
299+
return errors.Wrap(err, "unable to create shopify client")
276300
}
277301

278-
s.Logger().Debug("Creating consent router")
302+
return nil
303+
}
304+
func (s *Service) initializeCustomerIOClient() error {
305+
customerIOConfig := customerio.Config{}
306+
if err := envconfig.Process("", &customerIOConfig); err != nil {
307+
return errors.Wrap(err, "unable to load customerio config")
308+
}
279309

280-
consentV1Router, err := consentApiV1.NewRouter(s.consentService)
310+
var err error
311+
s.customerIOClient, err = customerio.NewClient(customerIOConfig, s.Logger())
281312
if err != nil {
282-
return errors.Wrap(err, "unable to create consent router")
313+
return errors.Wrap(err, "unable to create customerio client")
283314
}
284315

285-
s.Logger().Debug("Creating jotform router")
316+
return nil
317+
}
286318

319+
func (s *Service) createJotformRouter() (*jotformAPI.Router, error) {
287320
jotformConfig := jotform.Config{}
288321
if err := envconfig.Process("", &jotformConfig); err != nil {
289-
return errors.Wrap(err, "unable to load jotform config")
322+
return nil, errors.Wrap(err, "unable to load jotform config")
290323
}
291324

292-
customerIOConfig := customerio.Config{}
293-
if err := envconfig.Process("", &customerIOConfig); err != nil {
294-
return errors.Wrap(err, "unable to load customerio config")
325+
webhookProcessor, err := jotform.NewWebhookProcessor(jotformConfig, s.Logger(), s.consentService, s.customerIOClient, s.userClient, s.shopifyClient)
326+
if err != nil {
327+
return nil, errors.Wrap(err, "unable to create jotform webhook processor")
295328
}
296-
customerIOClient, err := customerio.NewClient(customerIOConfig, s.Logger())
329+
330+
jotformRouter, err := jotformAPI.NewRouter(webhookProcessor)
297331
if err != nil {
298-
return errors.Wrap(err, "unable to create customerio client")
332+
return nil, errors.Wrap(err, "unable to create jotform router")
299333
}
300334

301-
s.Logger().Debug("Initializing user client")
302-
usrClient, err := userClient.NewDefaultClient(userClient.Params{
303-
ConfigReporter: s.ConfigReporter(),
304-
Logger: s.Logger(),
305-
UserAgent: s.UserAgent(),
306-
})
335+
return jotformRouter, nil
336+
}
337+
338+
func (s *Service) createShopifyRouter() (*shopifyAPI.Router, error) {
339+
fulfillmentEventProcessor, err := shopify.NewFulfillmentEventProcessor(s.Logger(), s.customerIOClient, s.shopifyClient, s.AuthServiceClient())
307340
if err != nil {
308-
return errors.Wrap(err, "unable to create user client")
341+
return nil, errors.Wrap(err, "unable to create fulfillment event processor")
309342
}
310343

311-
shopifyConfig := shopify.ClientConfig{}
312-
if err := envconfig.Process("", &shopifyConfig); err != nil {
313-
return errors.Wrap(err, "unable to load shopify config")
344+
ordersCreateEventProcessor, err := shopify.NewOrdersCreateEventProcessor(s.Logger(), s.customerIOClient)
345+
if err != nil {
346+
return nil, errors.Wrap(err, "unable to create orders create event processor")
314347
}
315348

316-
shopifyClnt, err := shopifyClient.New(context.Background(), shopifyConfig)
349+
shopifyRouter, err := shopifyAPI.NewRouter(fulfillmentEventProcessor, ordersCreateEventProcessor)
317350
if err != nil {
318-
return errors.Wrap(err, "unable to create shopify client")
351+
return nil, errors.Wrap(err, "unable to create shopify router")
319352
}
320353

321-
webhookProcessor, err := jotform.NewWebhookProcessor(jotformConfig, s.Logger(), s.consentService, customerIOClient, usrClient, shopifyClnt)
354+
return shopifyRouter, nil
355+
}
356+
357+
func (s *Service) initializeRouter() error {
358+
s.Logger().Debug("Creating api router")
359+
360+
apiRouter, err := authServiceApi.NewRouter(s)
322361
if err != nil {
323-
return errors.Wrap(err, "unable to create jotform webhook processor")
362+
return errors.Wrap(err, "unable to create api router")
324363
}
325364

326-
jotformRouter, err := jotformAPI.NewRouter(webhookProcessor)
365+
s.Logger().Debug("Creating v1 router")
366+
367+
v1Router, err := authServiceApiV1.NewRouter(s)
327368
if err != nil {
328-
return errors.Wrap(err, "unable to create jotform router")
369+
return errors.Wrap(err, "unable to create v1 router")
329370
}
330371

331-
fulfillmentEventProcessor, err := shopify.NewFulfillmentEventProcessor(s.Logger(), customerIOClient, shopifyClnt, s.AuthServiceClient())
372+
s.Logger().Debug("Creating consent router")
373+
374+
consentV1Router, err := consentApiV1.NewRouter(s.consentService)
332375
if err != nil {
333-
return errors.Wrap(err, "unable to create fulfillment event processor")
376+
return errors.Wrap(err, "unable to create consent router")
334377
}
335378

336-
ordersCreateEventProcessor, err := shopify.NewOrdersCreateEventProcessor(s.Logger(), customerIOClient)
379+
s.Logger().Debug("Creating jotform router")
380+
381+
jotformRouter, err := s.createJotformRouter()
337382
if err != nil {
338-
return errors.Wrap(err, "unable to create orders create event processor")
383+
return errors.Wrap(err, "unable to create jotform router")
339384
}
340385

341-
shopifyRouter, err := shopifyAPI.NewRouter(fulfillmentEventProcessor, ordersCreateEventProcessor)
386+
s.Logger().Debug("Creating shopify router")
387+
388+
shopifyRouter, err := s.createShopifyRouter()
342389
if err != nil {
343390
return errors.Wrap(err, "unable to create shopify router")
344391
}
@@ -430,24 +477,14 @@ func (s *Service) initializeConsentService() error {
430477
return errors.Wrap(err, "unable to create bddp sharer")
431478
}
432479

433-
s.Logger().Debug("Initializing user client")
434-
usrClient, err := userClient.NewDefaultClient(userClient.Params{
435-
ConfigReporter: s.ConfigReporter(),
436-
Logger: s.Logger(),
437-
UserAgent: s.UserAgent(),
438-
})
439-
if err != nil {
440-
return errors.Wrap(err, "unable to create user client")
441-
}
442-
443480
s.Logger().Debug("Initializing mailer")
444481
mailr, err := mailer.Client()
445482
if err != nil {
446483
return errors.Wrap(err, "unable to create mailer")
447484
}
448485

449486
s.Logger().Debug("Initializing consent mailer")
450-
consentMailer, err := consentService.NewConsentMailer(mailr, usrClient, s.Logger())
487+
consentMailer, err := consentService.NewConsentMailer(mailr, s.userClient, s.Logger())
451488
if err != nil {
452489
return errors.Wrap(err, "unable to create consent mailer")
453490
}
Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@ package customerio
33
import "github.com/tidepool-org/platform/log"
44

55
type Client struct {
6-
appAPIBaseURL string
7-
appAPIKey string
8-
trackAPIBaseURL string
9-
trackAPIKey string
10-
siteID string
11-
logger log.Logger
6+
config Config
7+
logger log.Logger
128
}
139

1410
type Config struct {
@@ -22,11 +18,7 @@ type Config struct {
2218

2319
func NewClient(config Config, logger log.Logger) (*Client, error) {
2420
return &Client{
25-
appAPIKey: config.AppAPIKey,
26-
trackAPIKey: config.TrackAPIKey,
27-
siteID: config.SiteID,
28-
appAPIBaseURL: config.AppAPIBaseURL,
29-
trackAPIBaseURL: config.TrackAPIBaseURL,
30-
logger: logger,
21+
config: config,
22+
logger: logger,
3123
}, nil
3224
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ type errorResponse struct {
6161
}
6262

6363
func (c *Client) GetCustomer(ctx context.Context, cid string, typ IDType) (*Customer, error) {
64-
url := fmt.Sprintf("%s/v1/customers/%s/attributes", c.appAPIBaseURL, cid)
64+
url := fmt.Sprintf("%s/v1/customers/%s/attributes", c.config.AppAPIBaseURL, cid)
6565

6666
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
6767
if err != nil {
@@ -74,7 +74,7 @@ func (c *Client) GetCustomer(ctx context.Context, cid string, typ IDType) (*Cust
7474
req.URL.RawQuery = q.Encode()
7575

7676
// Add authorization header
77-
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.appAPIKey))
77+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.config.AppAPIKey))
7878
req.Header.Set("Content-Type", "application/json")
7979

8080
c.logger.WithField("cid", cid).WithField("url", req.URL.String()).Debug("fetching customer")
@@ -103,7 +103,7 @@ func (c *Client) GetCustomer(ctx context.Context, cid string, typ IDType) (*Cust
103103
}
104104

105105
func (c *Client) FindCustomers(ctx context.Context, filter map[string]any) (*FindCustomersResponse, error) {
106-
url := fmt.Sprintf("%s/v1/customers", c.appAPIBaseURL)
106+
url := fmt.Sprintf("%s/v1/customers", c.config.AppAPIBaseURL)
107107

108108
body, err := json.Marshal(filter)
109109
if err != nil {
@@ -115,7 +115,7 @@ func (c *Client) FindCustomers(ctx context.Context, filter map[string]any) (*Fin
115115
}
116116

117117
// Add authorization header
118-
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.appAPIKey))
118+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.config.AppAPIKey))
119119
req.Header.Set("Content-Type", "application/json")
120120

121121
c.logger.WithField("url", req.URL.String()).WithField("filter", filter).Debug("finding customer")
@@ -139,7 +139,7 @@ func (c *Client) FindCustomers(ctx context.Context, filter map[string]any) (*Fin
139139
}
140140

141141
func (c *Client) UpdateCustomer(ctx context.Context, customer Customer) error {
142-
url := fmt.Sprintf("%s/api/v2/entity", c.trackAPIBaseURL)
142+
url := fmt.Sprintf("%s/api/v2/entity", c.config.TrackAPIBaseURL)
143143

144144
// Prepare the request body
145145
reqBody := entityRequest{
@@ -161,7 +161,7 @@ func (c *Client) UpdateCustomer(ctx context.Context, customer Customer) error {
161161
}
162162

163163
// Add the authorization header (Basic Auth for Track API)
164-
req.SetBasicAuth(c.siteID, c.trackAPIKey)
164+
req.SetBasicAuth(c.config.SiteID, c.config.TrackAPIKey)
165165
req.Header.Set("Content-Type", "application/json")
166166

167167
resp, err := http.DefaultClient.Do(req)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Event struct {
1717
}
1818

1919
func (c *Client) SendEvent(ctx context.Context, cid string, event Event) error {
20-
url := fmt.Sprintf("%s/api/v1/customers/%s/events", c.trackAPIBaseURL, cid)
20+
url := fmt.Sprintf("%s/api/v1/customers/%s/events", c.config.TrackAPIBaseURL, cid)
2121

2222
jsonBody, err := json.Marshal(event)
2323
if err != nil {
@@ -30,7 +30,7 @@ func (c *Client) SendEvent(ctx context.Context, cid string, event Event) error {
3030
}
3131

3232
// Add the authorization header (Basic Auth for Track API)
33-
req.SetBasicAuth(c.siteID, c.trackAPIKey)
33+
req.SetBasicAuth(c.config.SiteID, c.config.TrackAPIKey)
3434
req.Header.Set("Content-Type", "application/json")
3535

3636
resp, err := http.DefaultClient.Do(req)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (c *Client) ListCustomersInSegment(ctx context.Context, segmentID string) (
2626
start := ""
2727

2828
for {
29-
url := fmt.Sprintf("%s/v1/segments/%s/membership", c.appAPIBaseURL, segmentID)
29+
url := fmt.Sprintf("%s/v1/segments/%s/membership", c.config.AppAPIBaseURL, segmentID)
3030

3131
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
3232
if err != nil {
@@ -41,7 +41,7 @@ func (c *Client) ListCustomersInSegment(ctx context.Context, segmentID string) (
4141
}
4242

4343
// Add authorization header
44-
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.appAPIKey))
44+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.config.AppAPIKey))
4545
req.Header.Set("Content-Type", "application/json")
4646

4747
resp, err := http.DefaultClient.Do(req)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package customerio
1+
package oura
22

33
const (
44
OuraEligibilitySurveyCompletedEventType = "oura_eligibility_survey_completed"

0 commit comments

Comments
 (0)