-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
74 lines (62 loc) · 2.52 KB
/
handler.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
package notification
import (
"context"
"fmt"
nutsClient "github.com/nuts-foundation/nuts-demo-ehr/nuts/client"
"github.com/monarko/fhirgo/STU3/resources"
"github.com/nuts-foundation/nuts-demo-ehr/domain/fhir"
"github.com/nuts-foundation/nuts-demo-ehr/domain/transfer"
"github.com/nuts-foundation/nuts-demo-ehr/domain/transfer/receiver"
"github.com/nuts-foundation/nuts-demo-ehr/nuts/registry"
)
type Notification struct {
TaskID string
SenderID string
CustomerID string
}
type Handler interface {
Handle(ctx context.Context, notification Notification) error
}
type handler struct {
nutsClient *nutsClient.HTTPClient
localFHIRClientFactory fhir.Factory
transferService receiver.TransferService
registry registry.OrganizationRegistry
}
func NewHandler(
nutsClient *nutsClient.HTTPClient,
localFHIRClientFactory fhir.Factory,
transferReceiverService receiver.TransferService,
registry registry.OrganizationRegistry,
) Handler {
return &handler{
nutsClient: nutsClient,
localFHIRClientFactory: localFHIRClientFactory,
transferService: transferReceiverService,
registry: registry,
}
}
// Handle handles an incoming notification about an updated Task for one of its customers.
func (service *handler) Handle(ctx context.Context, notification Notification) error {
fhirServer, err := service.registry.GetCompoundServiceEndpoint(ctx, notification.SenderID, transfer.ServiceName, "fhir")
if err != nil {
return fmt.Errorf("error while looking up custodian's FHIR server (did=%s): %w", notification.SenderID, err)
}
authServer, err := service.registry.GetCompoundServiceEndpoint(ctx, notification.SenderID, transfer.ServiceName, "authServerURL")
if err != nil {
return fmt.Errorf("error while looking up custodian's Auth server (did=%s): %w", notification.SenderID, err)
}
taskPath := fmt.Sprintf("/Task/%s", notification.TaskID)
accessToken, err := service.nutsClient.RequestServiceAccessToken(ctx, notification.CustomerID, authServer, transfer.SenderServiceScope)
if err != nil {
return err
}
task := &resources.Task{}
client := fhir.NewFactory(fhir.WithURL(fhirServer), fhir.WithAuthToken(accessToken))
// FIXME: add query params to filter on the owner so to only process the customer addressed in the notification
err = client().ReadOne(ctx, taskPath, &task)
if err != nil {
return err
}
return service.transferService.CreateOrUpdate(ctx, fhir.FromCodePtr(task.Status), notification.CustomerID, notification.SenderID, fhir.FromIDPtr(task.ID))
}