fix: allow multiple hooks for same event

This commit is contained in:
Lakhan Samani
2023-03-26 07:20:45 +05:30
parent f324976801
commit deaf1e2ff7
15 changed files with 385 additions and 273 deletions

View File

@@ -10,11 +10,15 @@ import (
// Note: any change here should be reflected in providers/casandra/provider.go as it does not have model support in collection creation
// Event name has been kept unique as per initial design. But later on decided that we can have
// multiple hooks for same event so will be in a pattern `event_name-TIMESTAMP`
// Webhook model for db
type Webhook struct {
Key string `json:"_key,omitempty" bson:"_key,omitempty" cql:"_key,omitempty" dynamo:"key,omitempty"` // for arangodb
ID string `gorm:"primaryKey;type:char(36)" json:"_id" bson:"_id" cql:"id" dynamo:"id,hash"`
EventName string `gorm:"unique" json:"event_name" bson:"event_name" cql:"event_name" dynamo:"event_name" index:"event_name,hash"`
Title string `json:"title" bson:"title" cql:"title" dynamo:"title"`
EndPoint string `json:"endpoint" bson:"endpoint" cql:"endpoint" dynamo:"endpoint"`
Headers string `json:"headers" bson:"headers" cql:"headers" dynamo:"headers"`
Enabled bool `json:"enabled" bson:"enabled" cql:"enabled" dynamo:"enabled"`
@@ -26,14 +30,22 @@ type Webhook struct {
func (w *Webhook) AsAPIWebhook() *model.Webhook {
headersMap := make(map[string]interface{})
json.Unmarshal([]byte(w.Headers), &headersMap)
id := w.ID
if strings.Contains(id, Collections.Webhook+"/") {
id = strings.TrimPrefix(id, Collections.Webhook+"/")
}
// If event name contains timestamp trim that part
if strings.Contains(w.EventName, "-") {
splitData := strings.Split(w.EventName, "-")
w.EventName = splitData[0]
}
// set default title to event name without dot(.)
if w.Title == "" {
w.Title = strings.Join(strings.Split(w.EventName, "."), " ")
}
return &model.Webhook{
ID: id,
Title: refs.NewStringRef(w.Title),
EventName: refs.NewStringRef(w.EventName),
Endpoint: refs.NewStringRef(w.EndPoint),
Headers: headersMap,