From 57a1cedde6b64970a140879abaa9895095715707 Mon Sep 17 00:00:00 2001 From: Albin Antony Date: Thu, 26 Oct 2023 12:47:19 +0530 Subject: [PATCH] Fix #368 After updating webhooks, the edited value is not reflecting in the list webhook api --- .../handler/webhook/config_create_webhook.go | 2 +- .../config_list_recent_webhook_deliveries.go | 2 +- .../handler/webhook/config_list_webhooks.go | 4 +-- .../webhook/config_read_webhook_delivery.go | 2 +- ...redeliver_webhook_payload_by_deliveryid.go | 2 +- .../handler/webhook/config_update_webhook.go | 2 +- src/v2/webhook/db.go | 33 +++++++++++-------- src/v2/webhook/webhook.go | 2 +- 8 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/v2/handler/webhook/config_create_webhook.go b/src/v2/handler/webhook/config_create_webhook.go index 9064be2..8b4d707 100644 --- a/src/v2/handler/webhook/config_create_webhook.go +++ b/src/v2/handler/webhook/config_create_webhook.go @@ -137,7 +137,7 @@ func ConfigCreateWebhook(w http.ResponseWriter, r *http.Request) { } var newWebhook wh.Webhook - newWebhook.ID = primitive.NewObjectID().Hex() + newWebhook.ID = primitive.NewObjectID() newWebhook = updateWebhookFromAddWebhookRequestBody(webhookReq, newWebhook) newWebhook.OrganisationId = organisationId newWebhook.IsDeleted = false diff --git a/src/v2/handler/webhook/config_list_recent_webhook_deliveries.go b/src/v2/handler/webhook/config_list_recent_webhook_deliveries.go index 6a86f74..107a923 100644 --- a/src/v2/handler/webhook/config_list_recent_webhook_deliveries.go +++ b/src/v2/handler/webhook/config_list_recent_webhook_deliveries.go @@ -64,7 +64,7 @@ func ConfigListRecentWebhookDeliveries(w http.ResponseWriter, r *http.Request) { } // Get all recent webhook deliveries - recentWebhookDeliveries, err := wh.GetAllDeliveryByWebhookId(webhook.ID) + recentWebhookDeliveries, err := wh.GetAllDeliveryByWebhookId(webhook.ID.Hex()) if err != nil { m := fmt.Sprintf("Failed to fetch recent payload deliveries for webhook:%v for organisation: %v", webhookId, organisationId) common.HandleError(w, http.StatusInternalServerError, m, err) diff --git a/src/v2/handler/webhook/config_list_webhooks.go b/src/v2/handler/webhook/config_list_webhooks.go index dda119d..feecca8 100644 --- a/src/v2/handler/webhook/config_list_webhooks.go +++ b/src/v2/handler/webhook/config_list_webhooks.go @@ -65,7 +65,7 @@ func ConfigListWebhooks(w http.ResponseWriter, r *http.Request) { // Fetching the last delivery to the webhook and retrieving the delivery status isLastDeliverySuccess := false - lastDelivery, err := wh.GetLastWebhookDelivery(webhook.ID) + lastDelivery, err := wh.GetLastWebhookDelivery(webhook.ID.Hex()) if err != nil { // There is no payload delivery yet ! isLastDeliverySuccess = true @@ -80,7 +80,7 @@ func ConfigListWebhooks(w http.ResponseWriter, r *http.Request) { } updatedWebhook := WebhookWithLastDeliveryStatus{ - ID: webhook.ID, + ID: webhook.ID.Hex(), OrganisationId: webhook.OrganisationId, PayloadURL: webhook.PayloadURL, ContentType: webhook.ContentType, diff --git a/src/v2/handler/webhook/config_read_webhook_delivery.go b/src/v2/handler/webhook/config_read_webhook_delivery.go index 4b7b362..7ed4e90 100644 --- a/src/v2/handler/webhook/config_read_webhook_delivery.go +++ b/src/v2/handler/webhook/config_read_webhook_delivery.go @@ -47,7 +47,7 @@ func ConfigReadRecentWebhookDelivery(w http.ResponseWriter, r *http.Request) { } // Get the webhook delivery by ID - webhookDelivery, err := wh.GetWebhookDeliveryByID(webhook.ID, deliveryId) + webhookDelivery, err := wh.GetWebhookDeliveryByID(webhook.ID.Hex(), 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/src/v2/handler/webhook/config_redeliver_webhook_payload_by_deliveryid.go b/src/v2/handler/webhook/config_redeliver_webhook_payload_by_deliveryid.go index 2b02958..c5b5fc9 100644 --- a/src/v2/handler/webhook/config_redeliver_webhook_payload_by_deliveryid.go +++ b/src/v2/handler/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 := wh.GetWebhookDeliveryByID(webhook.ID.Hex(), 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/src/v2/handler/webhook/config_update_webhook.go b/src/v2/handler/webhook/config_update_webhook.go index 3bdfb98..fe73349 100644 --- a/src/v2/handler/webhook/config_update_webhook.go +++ b/src/v2/handler/webhook/config_update_webhook.go @@ -30,7 +30,7 @@ func validateUpdatewebhookRequestBody(webhookReq updateWebhookReq, currentWebhoo // Check if webhook with provided payload URL already exists tempWebhook, err := webhhokRepo.GetWebhookByPayloadURL(webhookReq.Webhook.PayloadURL) if err == nil { - if tempWebhook.ID != webhookId { + if tempWebhook.ID.Hex() != webhookId { return errors.New("webhook with provided payload URL already exists") } } diff --git a/src/v2/webhook/db.go b/src/v2/webhook/db.go index f56433d..92dcd90 100644 --- a/src/v2/webhook/db.go +++ b/src/v2/webhook/db.go @@ -13,16 +13,16 @@ import ( // Webhook Defines the structure for an organisation webhook type Webhook struct { - ID string `json:"id" bson:"_id,omitempty"` // Webhook ID - OrganisationId string `json:"orgId" bson:"orgid"` // Organisation ID - PayloadURL string `json:"payloadUrl" valid:"required"` // Webhook payload URL - ContentType string `json:"contentType" valid:"required"` // Webhook payload content type for e.g application/json - SubscribedEvents []string `json:"subscribedEvents" valid:"required"` // Events subscribed for e.g. user.data.delete - Disabled bool `json:"disabled"` // Disabled or not - SecretKey string `json:"secretKey" valid:"required"` // For calculating SHA256 HMAC to verify data integrity and authenticity - SkipSSLVerification bool `json:"skipSslVerification"` // Skip SSL certificate verification or not (expiry is checked) - TimeStamp string `json:"timestamp" valid:"required"` // UTC timestamp - IsDeleted bool `json:"-"` + ID primitive.ObjectID `json:"id" bson:"_id,omitempty"` // Webhook ID + OrganisationId string `json:"orgId" bson:"orgid"` // Organisation ID + PayloadURL string `json:"payloadUrl" valid:"required"` // Webhook payload URL + ContentType string `json:"contentType" valid:"required"` // Webhook payload content type for e.g application/json + SubscribedEvents []string `json:"subscribedEvents" valid:"required"` // Events subscribed for e.g. user.data.delete + Disabled bool `json:"disabled"` // Disabled or not + SecretKey string `json:"secretKey" valid:"required"` // For calculating SHA256 HMAC to verify data integrity and authenticity + SkipSSLVerification bool `json:"skipSslVerification"` // Skip SSL certificate verification or not (expiry is checked) + TimeStamp string `json:"timestamp" valid:"required"` // UTC timestamp + IsDeleted bool `json:"-"` } // WebhookDelivery Details of payload delivery to webhook endpoint @@ -227,11 +227,16 @@ func (whRepo *WebhookRepository) GetWebhookCountByPayloadURL(payloadURL string) } // GetByOrgID Gets a webhook by organisation ID and webhook ID -func (whRepo *WebhookRepository) GetByOrgID(webhookId string) (Webhook, error) { +func (whRepo *WebhookRepository) GetByOrgID(webhookID string) (Webhook, error) { + var result Webhook + webhookId, err := primitive.ObjectIDFromHex(webhookID) + if err != nil { + return result, err + } + filter := common.CombineFilters(whRepo.DefaultFilter, bson.M{"_id": webhookId}) - var result Webhook - err := webhookCollection().FindOne(context.TODO(), filter).Decode(&result) + err = webhookCollection().FindOne(context.TODO(), filter).Decode(&result) return result, err } @@ -247,7 +252,7 @@ func (whRepo *WebhookRepository) GetWebhookByPayloadURL(payloadURL string) (resu // UpdateWebhook Updates a webhook for an organization func (whRepo *WebhookRepository) UpdateWebhook(webhook Webhook) (Webhook, error) { - filter := common.CombineFilters(whRepo.DefaultFilter, bson.M{"_id": webhook.ID}) + filter := bson.M{"_id": webhook.ID, "orgid": webhook.OrganisationId, "isdeleted": false} update := bson.M{"$set": webhook} _, err := webhookCollection().UpdateOne(context.TODO(), filter, update) diff --git a/src/v2/webhook/webhook.go b/src/v2/webhook/webhook.go index 663bfaf..07e5622 100644 --- a/src/v2/webhook/webhook.go +++ b/src/v2/webhook/webhook.go @@ -227,7 +227,7 @@ func TriggerWebhooks(webhookEventData WebhookEventData, webhookEventType string) for _, toBeProcessedWebhook := range toBeProcessedWebhooks { // Constructing webhook payload we := WebhookEvent{ - WebhookID: toBeProcessedWebhook.ID, + WebhookID: toBeProcessedWebhook.ID.Hex(), Timestamp: strconv.FormatInt(time.Now().UTC().Unix(), 10), Data: webhookEventData, Type: webhookEventType,