From 00301011d5012727796d09029f1dbf086fdeac90 Mon Sep 17 00:00:00 2001 From: Albin Antony Date: Fri, 15 Dec 2023 15:16:13 +0530 Subject: [PATCH] Fix #600 Redeliver webhook --- ...redeliver_webhook_payload_by_deliveryid.go | 2 +- internal/webhook_dispatcher/db.go | 8 +++++ .../webhook_dispatcher/webhookdispatcher.go | 34 ++++++++++++------- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/internal/handler/v2/config/webhook/config_redeliver_webhook_payload_by_deliveryid.go b/internal/handler/v2/config/webhook/config_redeliver_webhook_payload_by_deliveryid.go index d019ea2..3de5783 100644 --- a/internal/handler/v2/config/webhook/config_redeliver_webhook_payload_by_deliveryid.go +++ b/internal/handler/v2/config/webhook/config_redeliver_webhook_payload_by_deliveryid.go @@ -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) diff --git a/internal/webhook_dispatcher/db.go b/internal/webhook_dispatcher/db.go index cee8ad9..8e3a4cc 100644 --- a/internal/webhook_dispatcher/db.go +++ b/internal/webhook_dispatcher/db.go @@ -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 +} diff --git a/internal/webhook_dispatcher/webhookdispatcher.go b/internal/webhook_dispatcher/webhookdispatcher.go index f60f565..6cc47b9 100644 --- a/internal/webhook_dispatcher/webhookdispatcher.go +++ b/internal/webhook_dispatcher/webhookdispatcher.go @@ -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 @@ -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 @@ -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)