2022-07-04 17:07:13 +00:00
|
|
|
package models
|
|
|
|
|
2022-07-10 16:19:33 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-07-12 06:18:42 +00:00
|
|
|
"strings"
|
2022-07-10 16:19:33 +00:00
|
|
|
|
|
|
|
"github.com/authorizerdev/authorizer/server/graph/model"
|
2022-07-15 16:41:08 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/refs"
|
2022-07-10 16:19:33 +00:00
|
|
|
)
|
2022-07-09 05:51:32 +00:00
|
|
|
|
2022-07-04 17:07:13 +00:00
|
|
|
// Note: any change here should be reflected in providers/casandra/provider.go as it does not have model support in collection creation
|
|
|
|
|
|
|
|
// Webhook model for db
|
|
|
|
type Webhook struct {
|
2022-10-02 19:38:12 +00:00
|
|
|
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"`
|
2022-10-05 10:02:32 +00:00
|
|
|
EventName string `gorm:"unique" json:"event_name" bson:"event_name" cql:"event_name" dynamo:"event_name" index:"event_name,hash"`
|
2022-10-02 19:38:12 +00:00
|
|
|
EndPoint string `gorm:"type:text" json:"endpoint" bson:"endpoint" cql:"endpoint" dynamo:"endpoint"`
|
|
|
|
Headers string `gorm:"type:text" json:"headers" bson:"headers" cql:"headers" dynamo:"headers"`
|
|
|
|
Enabled bool `json:"enabled" bson:"enabled" cql:"enabled" dynamo:"enabled"`
|
|
|
|
CreatedAt int64 `json:"created_at" bson:"created_at" cql:"created_at" dynamo:"created_at"`
|
|
|
|
UpdatedAt int64 `json:"updated_at" bson:"updated_at" cql:"updated_at" dynamo:"updated_at"`
|
2022-07-04 17:07:13 +00:00
|
|
|
}
|
2022-07-09 05:51:32 +00:00
|
|
|
|
2022-07-15 04:42:24 +00:00
|
|
|
// AsAPIWebhook to return webhook as graphql response object
|
2022-07-09 05:51:32 +00:00
|
|
|
func (w *Webhook) AsAPIWebhook() *model.Webhook {
|
2022-07-10 16:19:33 +00:00
|
|
|
headersMap := make(map[string]interface{})
|
|
|
|
json.Unmarshal([]byte(w.Headers), &headersMap)
|
2022-07-12 06:18:42 +00:00
|
|
|
|
|
|
|
id := w.ID
|
|
|
|
if strings.Contains(id, Collections.Webhook+"/") {
|
|
|
|
id = strings.TrimPrefix(id, Collections.Webhook+"/")
|
|
|
|
}
|
2022-07-15 16:41:08 +00:00
|
|
|
|
2022-07-09 05:51:32 +00:00
|
|
|
return &model.Webhook{
|
2022-07-12 06:18:42 +00:00
|
|
|
ID: id,
|
2022-07-15 16:41:08 +00:00
|
|
|
EventName: refs.NewStringRef(w.EventName),
|
|
|
|
Endpoint: refs.NewStringRef(w.EndPoint),
|
2022-07-10 16:19:33 +00:00
|
|
|
Headers: headersMap,
|
2022-07-15 16:41:08 +00:00
|
|
|
Enabled: refs.NewBoolRef(w.Enabled),
|
|
|
|
CreatedAt: refs.NewInt64Ref(w.CreatedAt),
|
|
|
|
UpdatedAt: refs.NewInt64Ref(w.UpdatedAt),
|
2022-07-09 05:51:32 +00:00
|
|
|
}
|
|
|
|
}
|