Skip to content

Commit

Permalink
Fix #368 After updating webhooks, the edited value is not reflecting …
Browse files Browse the repository at this point in the history
…in the list webhook api
  • Loading branch information
albinpa authored and georgepadayatti committed Oct 26, 2023
1 parent 2ea4a38 commit 57a1ced
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/v2/handler/webhook/config_create_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/v2/handler/webhook/config_list_webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/v2/handler/webhook/config_read_webhook_delivery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
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 := 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)
Expand Down
2 changes: 1 addition & 1 deletion src/v2/handler/webhook/config_update_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Expand Down
33 changes: 19 additions & 14 deletions src/v2/webhook/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/v2/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 57a1ced

Please sign in to comment.