fix: queries for webhooks + improve tests
This commit is contained in:
@@ -34,18 +34,12 @@ func (w *Webhook) AsAPIWebhook() *model.Webhook {
|
||||
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.EventDescription == "" {
|
||||
w.EventDescription = strings.Join(strings.Split(w.EventName, "."), " ")
|
||||
}
|
||||
return &model.Webhook{
|
||||
ID: id,
|
||||
// Title: refs.NewStringRef(w.EventDescription),
|
||||
ID: id,
|
||||
EventName: refs.NewStringRef(w.EventName),
|
||||
Endpoint: refs.NewStringRef(w.EndPoint),
|
||||
Headers: headersMap,
|
||||
|
@@ -20,9 +20,6 @@ func (p *provider) AddWebhook(ctx context.Context, webhook models.Webhook) (*mod
|
||||
webhook.Key = webhook.ID
|
||||
}
|
||||
webhook.Key = webhook.ID
|
||||
if webhook.EventDescription == "" {
|
||||
webhook.EventDescription = strings.Join(strings.Split(webhook.EventName, "."), " ")
|
||||
}
|
||||
// Add timestamp to make event name unique for legacy version
|
||||
webhook.EventName = fmt.Sprintf("%s-%d", webhook.EventName, time.Now().Unix())
|
||||
webhook.CreatedAt = time.Now().Unix()
|
||||
|
@@ -207,6 +207,13 @@ func NewProvider() (*provider, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// add event_description to webhook table
|
||||
webhookAlterQuery := fmt.Sprintf(`ALTER TABLE %s.%s ADD (event_description text);`, KeySpace, models.Collections.Webhook)
|
||||
err = session.Query(webhookAlterQuery).Exec()
|
||||
if err != nil {
|
||||
log.Debug("Failed to alter table as column exists: ", err)
|
||||
// continue
|
||||
}
|
||||
|
||||
webhookLogCollectionQuery := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s.%s (id text, http_status bigint, response text, request text, webhook_id text,updated_at bigint, created_at bigint, PRIMARY KEY (id))", KeySpace, models.Collections.WebhookLog)
|
||||
err = session.Query(webhookLogCollectionQuery).Exec()
|
||||
|
@@ -22,9 +22,6 @@ func (p *provider) AddWebhook(ctx context.Context, webhook models.Webhook) (*mod
|
||||
webhook.Key = webhook.ID
|
||||
webhook.CreatedAt = time.Now().Unix()
|
||||
webhook.UpdatedAt = time.Now().Unix()
|
||||
if webhook.EventDescription == "" {
|
||||
webhook.EventDescription = strings.Join(strings.Split(webhook.EventName, "."), " ")
|
||||
}
|
||||
// Add timestamp to make event name unique for legacy version
|
||||
webhook.EventName = fmt.Sprintf("%s-%d", webhook.EventName, time.Now().Unix())
|
||||
insertQuery := fmt.Sprintf("INSERT INTO %s (id, event_description, event_name, endpoint, headers, enabled, created_at, updated_at) VALUES ('%s', '%s', '%s', '%s', '%s', %t, %d, %d)", KeySpace+"."+models.Collections.Webhook, webhook.ID, webhook.EventDescription, webhook.EventName, webhook.EndPoint, webhook.Headers, webhook.Enabled, webhook.CreatedAt, webhook.UpdatedAt)
|
||||
@@ -129,7 +126,7 @@ func (p *provider) GetWebhookByID(ctx context.Context, webhookID string) (*model
|
||||
|
||||
// GetWebhookByEventName to get webhook by event_name
|
||||
func (p *provider) GetWebhookByEventName(ctx context.Context, eventName string) ([]*model.Webhook, error) {
|
||||
query := fmt.Sprintf(`SELECT id, event_description, event_name, endpoint, headers, enabled, created_at, updated_at FROM %s WHERE event_name LIKE '%s' ALLOW FILTERING`, KeySpace+"."+models.Collections.Webhook, eventName+"%s")
|
||||
query := fmt.Sprintf(`SELECT id, event_description, event_name, endpoint, headers, enabled, created_at, updated_at FROM %s WHERE event_name LIKE '%s' ALLOW FILTERING`, KeySpace+"."+models.Collections.Webhook, eventName+"%")
|
||||
scanner := p.db.Query(query).Iter().Scanner()
|
||||
webhooks := []*model.Webhook{}
|
||||
for scanner.Next() {
|
||||
|
@@ -22,9 +22,6 @@ func (p *provider) AddWebhook(ctx context.Context, webhook models.Webhook) (*mod
|
||||
webhook.Key = webhook.ID
|
||||
webhook.CreatedAt = time.Now().Unix()
|
||||
webhook.UpdatedAt = time.Now().Unix()
|
||||
if webhook.EventDescription == "" {
|
||||
webhook.EventDescription = strings.Join(strings.Split(webhook.EventName, "."), " ")
|
||||
}
|
||||
// Add timestamp to make event name unique for legacy version
|
||||
webhook.EventName = fmt.Sprintf("%s-%d", webhook.EventName, time.Now().Unix())
|
||||
insertOpt := gocb.InsertOptions{
|
||||
@@ -90,8 +87,6 @@ func (p *provider) ListWebhook(ctx context.Context, pagination model.Pagination)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if err := queryResult.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for queryResult.Next() {
|
||||
var webhook models.Webhook
|
||||
@@ -101,6 +96,9 @@ func (p *provider) ListWebhook(ctx context.Context, pagination model.Pagination)
|
||||
}
|
||||
webhooks = append(webhooks, webhook.AsAPIWebhook())
|
||||
}
|
||||
if err := queryResult.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &model.Webhooks{
|
||||
Pagination: &paginationClone,
|
||||
Webhooks: webhooks,
|
||||
@@ -131,8 +129,8 @@ func (p *provider) GetWebhookByID(ctx context.Context, webhookID string) (*model
|
||||
// GetWebhookByEventName to get webhook by event_name
|
||||
func (p *provider) GetWebhookByEventName(ctx context.Context, eventName string) ([]*model.Webhook, error) {
|
||||
params := make(map[string]interface{}, 1)
|
||||
params["event_name"] = eventName + "%"
|
||||
query := fmt.Sprintf(`SELECT _id, event_description, event_name, endpoint, headers, enabled, created_at, updated_at FROM %s.%s WHERE event_name LIKE $event_name`, p.scopeName, models.Collections.Webhook)
|
||||
// params["event_name"] = eventName + "%"
|
||||
query := fmt.Sprintf(`SELECT _id, event_description, event_name, endpoint, headers, enabled, created_at, updated_at FROM %s.%s WHERE event_name LIKE '%s'`, p.scopeName, models.Collections.Webhook, eventName+"%")
|
||||
queryResult, err := p.db.Query(query, &gocb.QueryOptions{
|
||||
Context: ctx,
|
||||
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
|
||||
@@ -140,8 +138,6 @@ func (p *provider) GetWebhookByEventName(ctx context.Context, eventName string)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if err := queryResult.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
webhooks := []*model.Webhook{}
|
||||
for queryResult.Next() {
|
||||
@@ -152,12 +148,14 @@ func (p *provider) GetWebhookByEventName(ctx context.Context, eventName string)
|
||||
}
|
||||
webhooks = append(webhooks, webhook.AsAPIWebhook())
|
||||
}
|
||||
if err := queryResult.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return webhooks, nil
|
||||
}
|
||||
|
||||
// DeleteWebhook to delete webhook
|
||||
func (p *provider) DeleteWebhook(ctx context.Context, webhook *model.Webhook) error {
|
||||
|
||||
params := make(map[string]interface{}, 1)
|
||||
params["webhook_id"] = webhook.ID
|
||||
removeOpt := gocb.RemoveOptions{
|
||||
|
@@ -23,9 +23,6 @@ func (p *provider) AddWebhook(ctx context.Context, webhook models.Webhook) (*mod
|
||||
webhook.Key = webhook.ID
|
||||
webhook.CreatedAt = time.Now().Unix()
|
||||
webhook.UpdatedAt = time.Now().Unix()
|
||||
if webhook.EventDescription == "" {
|
||||
webhook.EventDescription = strings.Join(strings.Split(webhook.EventName, "."), " ")
|
||||
}
|
||||
// Add timestamp to make event name unique for legacy version
|
||||
webhook.EventName = fmt.Sprintf("%s-%d", webhook.EventName, time.Now().Unix())
|
||||
err := collection.Put(webhook).RunWithContext(ctx)
|
||||
|
@@ -21,9 +21,6 @@ func (p *provider) AddWebhook(ctx context.Context, webhook models.Webhook) (*mod
|
||||
webhook.Key = webhook.ID
|
||||
webhook.CreatedAt = time.Now().Unix()
|
||||
webhook.UpdatedAt = time.Now().Unix()
|
||||
if webhook.EventDescription == "" {
|
||||
webhook.EventDescription = strings.Join(strings.Split(webhook.EventName, "."), " ")
|
||||
}
|
||||
// Add timestamp to make event name unique for legacy version
|
||||
webhook.EventName = fmt.Sprintf("%s-%d", webhook.EventName, time.Now().Unix())
|
||||
webhookCollection := p.db.Collection(models.Collections.Webhook, options.Collection())
|
||||
@@ -99,9 +96,9 @@ func (p *provider) GetWebhookByEventName(ctx context.Context, eventName string)
|
||||
webhookCollection := p.db.Collection(models.Collections.Webhook, options.Collection())
|
||||
opts := options.Find()
|
||||
opts.SetSort(bson.M{"created_at": -1})
|
||||
cursor, err := webhookCollection.Find(ctx, bson.M{
|
||||
"event_name": fmt.Sprintf("'^%s'", eventName),
|
||||
}, opts)
|
||||
cursor, err := webhookCollection.Find(ctx, bson.M{"event_name": bson.M{
|
||||
"$regex": fmt.Sprintf("^%s", eventName),
|
||||
}}, opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@@ -19,9 +19,6 @@ func (p *provider) AddWebhook(ctx context.Context, webhook models.Webhook) (*mod
|
||||
webhook.Key = webhook.ID
|
||||
webhook.CreatedAt = time.Now().Unix()
|
||||
webhook.UpdatedAt = time.Now().Unix()
|
||||
if webhook.EventDescription == "" {
|
||||
webhook.EventDescription = strings.Join(strings.Split(webhook.EventName, "."), " ")
|
||||
}
|
||||
// Add timestamp to make event name unique for legacy version
|
||||
webhook.EventName = fmt.Sprintf("%s-%d", webhook.EventName, time.Now().Unix())
|
||||
return webhook.AsAPIWebhook(), nil
|
||||
|
@@ -19,9 +19,6 @@ func (p *provider) AddWebhook(ctx context.Context, webhook models.Webhook) (*mod
|
||||
webhook.Key = webhook.ID
|
||||
webhook.CreatedAt = time.Now().Unix()
|
||||
webhook.UpdatedAt = time.Now().Unix()
|
||||
if webhook.EventDescription == "" {
|
||||
webhook.EventDescription = strings.Join(strings.Split(webhook.EventName, "."), " ")
|
||||
}
|
||||
// Add timestamp to make event name unique for legacy version
|
||||
webhook.EventName = fmt.Sprintf("%s-%d", webhook.EventName, time.Now().Unix())
|
||||
res := p.db.Create(&webhook)
|
||||
|
Reference in New Issue
Block a user