Skip to content

Commit

Permalink
Fix #600 Redeliver webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
albinpa committed Dec 15, 2023
1 parent 3244c88 commit 82fa350
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func ConfigRedeliverWebhookPayloadByDeliveryID(w http.ResponseWriter, r *http.Re
}

// Validating the given delivery ID for a webhook
webhookDelivery, err := wh.GetWebhookDeliveryByID(webhook.ID, deliveryId)
webhookDelivery, err := webhook_dispatcher.GetWebhookDeliveryByID(webhook.ID, deliveryId)
if err != nil {
m := fmt.Sprintf("Failed to get delivery details by ID:%v for webhook:%v for organisation: %v", deliveryId, webhookId, organisationId)
common.HandleError(w, http.StatusBadRequest, m, err)
Expand Down
8 changes: 8 additions & 0 deletions internal/webhook_dispatcher/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,11 @@ func AddWebhookDelivery(webhookDelivery WebhookDelivery) (WebhookDelivery, error

return webhookDelivery, err
}

// GetWebhookDeliveryByID Gets payload delivery details by ID
func GetWebhookDeliveryByID(webhookID string, webhookDeliveryId string) (result WebhookDelivery, err error) {

err = webhookDeliveryCollection().FindOne(context.TODO(), bson.M{"webhookid": webhookID, "_id": webhookDeliveryId}).Decode(&result)

return result, err
}
34 changes: 21 additions & 13 deletions internal/webhook_dispatcher/webhookdispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,23 @@ import (

// WebhookEvent Webhook event wrapper
type WebhookEvent struct {
DeliveryID string `json:"deliveryID"` // Webhook delivery ID
WebhookID string `json:"webhookID"` // Webhook endpoint ID
Timestamp string `json:"timestamp"` // UTC timestamp of webhook triggered data time
Data interface{} `json:"data"` // Event data attribute
Type string `json:"type"` // Event type for e.g. data.delete.initiated
DeliveryID string `json:"deliveryID"` // Webhook delivery ID
WebhookID string `json:"webhookID"` // Webhook endpoint ID
Timestamp string `json:"timestamp"` // UTC timestamp of webhook triggered data time
Data ConsentRecordWebhookEvent `json:"data"` // Event data attribute
Type string `json:"type"` // Event type for e.g. data.delete.initiated
}

type ConsentRecordWebhookEvent struct {
ConsentRecordId string `json:"consentRecordId"`
DataAgreementId string `json:"dataAgreementId"`
DataAgreementRevisionId string `json:"dataAgreementRevisionId"`
DataAgreementRevisionHash string `json:"dataAgreementRevisionHash"`
IndividualId string `json:"individualId"`
OptIn bool `json:"optIn"`
State string `json:"state"`
SignatureId string `json:"signatureId"`
OrganisationId string `json:"organisationId"`
}

// Payload content type const
Expand Down Expand Up @@ -74,7 +86,7 @@ type WebhookDelivery struct {
UserID string // ID of user who triggered the webhook event
WebhookEventType string // Webhook event type for e.g. data.delete.initiated
RequestHeaders map[string][]string // HTTP headers posted to webhook endpoint
RequestPayload interface{} // JSON payload posted to webhook endpoint
RequestPayload WebhookEvent // JSON payload posted to webhook endpoint
ResponseHeaders map[string][]string // HTTP response headers received from webhook endpoint
ResponseBody string // HTTP response body received from webhook endpoint in bytes
ResponseStatusCode int // HTTP response status code
Expand Down Expand Up @@ -109,16 +121,12 @@ func ProcessWebhooks(webhookEventType string, value []byte) {
// Webhook event data attribute
// Converting data attribute to appropriate webhook event struct

webhookEventData, ok := webhookEvent.Data.(map[string]interface{})
if !ok {
log.Printf("Invalid incoming webhook recieved !")
return
}
webhookEventData := webhookEvent.Data

// Quick fix
// Retrieving user and organisation ID from webhook data attribute
userID := webhookEventData["individualId"].(string)
orgID := webhookEventData["organisationId"].(string)
userID := webhookEventData.IndividualId
orgID := webhookEventData.OrganisationId

log.Printf("Processing webhook:%s triggered by user:%s of org:%s for event:%s", webhookEvent.WebhookID, userID, orgID, webhookEventType)

Expand Down

0 comments on commit 82fa350

Please sign in to comment.