2022-07-06 05:08:21 +00:00
package arangodb
import (
2022-07-09 06:14:14 +00:00
"context"
"fmt"
2023-03-26 01:50:45 +00:00
"strings"
2022-07-08 13:39:23 +00:00
"time"
2023-03-26 01:50:45 +00:00
"github.com/arangodb/go-driver"
2022-07-09 06:14:14 +00:00
arangoDriver "github.com/arangodb/go-driver"
2022-07-06 05:08:21 +00:00
"github.com/authorizerdev/authorizer/server/db/models"
"github.com/authorizerdev/authorizer/server/graph/model"
2022-07-08 13:39:23 +00:00
"github.com/google/uuid"
2022-07-06 05:08:21 +00:00
)
// AddWebhook to add webhook
2023-07-31 11:12:11 +00:00
func ( p * provider ) AddWebhook ( ctx context . Context , webhook * models . Webhook ) ( * model . Webhook , error ) {
2022-07-08 13:39:23 +00:00
if webhook . ID == "" {
webhook . ID = uuid . New ( ) . String ( )
2022-08-02 08:42:36 +00:00
webhook . Key = webhook . ID
2022-07-08 13:39:23 +00:00
}
webhook . Key = webhook . ID
2023-03-26 01:50:45 +00:00
// Add timestamp to make event name unique for legacy version
webhook . EventName = fmt . Sprintf ( "%s-%d" , webhook . EventName , time . Now ( ) . Unix ( ) )
2022-07-08 13:39:23 +00:00
webhook . CreatedAt = time . Now ( ) . Unix ( )
webhook . UpdatedAt = time . Now ( ) . Unix ( )
2022-07-10 16:19:33 +00:00
webhookCollection , _ := p . db . Collection ( ctx , models . Collections . Webhook )
_ , err := webhookCollection . CreateDocument ( ctx , webhook )
2022-07-09 06:14:14 +00:00
if err != nil {
2022-07-10 16:19:33 +00:00
return nil , err
2022-07-09 06:14:14 +00:00
}
2022-07-10 16:19:33 +00:00
return webhook . AsAPIWebhook ( ) , nil
2022-07-06 05:08:21 +00:00
}
// UpdateWebhook to update webhook
2023-07-31 11:12:11 +00:00
func ( p * provider ) UpdateWebhook ( ctx context . Context , webhook * models . Webhook ) ( * model . Webhook , error ) {
2022-07-08 13:39:23 +00:00
webhook . UpdatedAt = time . Now ( ) . Unix ( )
2023-03-26 01:50:45 +00:00
// Event is changed
if ! strings . Contains ( webhook . EventName , "-" ) {
webhook . EventName = fmt . Sprintf ( "%s-%d" , webhook . EventName , time . Now ( ) . Unix ( ) )
}
2022-07-10 16:19:33 +00:00
webhookCollection , _ := p . db . Collection ( ctx , models . Collections . Webhook )
meta , err := webhookCollection . UpdateDocument ( ctx , webhook . Key , webhook )
2022-07-09 06:14:14 +00:00
if err != nil {
2022-07-10 16:19:33 +00:00
return nil , err
2022-07-09 06:14:14 +00:00
}
webhook . Key = meta . Key
webhook . ID = meta . ID . String ( )
2022-07-10 16:19:33 +00:00
return webhook . AsAPIWebhook ( ) , nil
2022-07-06 05:08:21 +00:00
}
// ListWebhooks to list webhook
2023-07-31 11:12:11 +00:00
func ( p * provider ) ListWebhook ( ctx context . Context , pagination * model . Pagination ) ( * model . Webhooks , error ) {
2022-07-09 06:14:14 +00:00
webhooks := [ ] * model . Webhook { }
query := fmt . Sprintf ( "FOR d in %s SORT d.created_at DESC LIMIT %d, %d RETURN d" , models . Collections . Webhook , pagination . Offset , pagination . Limit )
2023-02-06 12:44:19 +00:00
sctx := arangoDriver . WithQueryFullCount ( ctx )
2022-07-10 16:19:33 +00:00
cursor , err := p . db . Query ( sctx , query , nil )
2022-07-09 06:14:14 +00:00
if err != nil {
return nil , err
}
defer cursor . Close ( )
paginationClone := pagination
paginationClone . Total = cursor . Statistics ( ) . FullCount ( )
for {
2023-07-31 11:12:11 +00:00
var webhook * models . Webhook
2022-07-10 16:19:33 +00:00
meta , err := cursor . ReadDocument ( ctx , & webhook )
2022-07-09 06:14:14 +00:00
if arangoDriver . IsNoMoreDocuments ( err ) {
break
} else if err != nil {
return nil , err
}
if meta . Key != "" {
webhooks = append ( webhooks , webhook . AsAPIWebhook ( ) )
}
}
return & model . Webhooks {
2023-07-31 11:12:11 +00:00
Pagination : paginationClone ,
2022-07-09 06:14:14 +00:00
Webhooks : webhooks ,
} , nil
2022-07-06 05:08:21 +00:00
}
// GetWebhookByID to get webhook by id
2022-07-10 16:19:33 +00:00
func ( p * provider ) GetWebhookByID ( ctx context . Context , webhookID string ) ( * model . Webhook , error ) {
2023-07-31 11:12:11 +00:00
var webhook * models . Webhook
2022-07-12 06:18:42 +00:00
query := fmt . Sprintf ( "FOR d in %s FILTER d._key == @webhook_id RETURN d" , models . Collections . Webhook )
2022-07-09 06:14:14 +00:00
bindVars := map [ string ] interface { } {
"webhook_id" : webhookID ,
}
2022-07-10 16:19:33 +00:00
cursor , err := p . db . Query ( ctx , query , bindVars )
2022-07-09 06:14:14 +00:00
if err != nil {
2022-07-10 16:19:33 +00:00
return nil , err
2022-07-09 06:14:14 +00:00
}
defer cursor . Close ( )
for {
if ! cursor . HasMore ( ) {
2023-08-01 10:39:17 +00:00
if webhook == nil {
2022-07-10 16:19:33 +00:00
return nil , fmt . Errorf ( "webhook not found" )
2022-07-09 06:14:14 +00:00
}
break
}
2022-07-10 16:19:33 +00:00
_ , err := cursor . ReadDocument ( ctx , & webhook )
2022-07-09 06:14:14 +00:00
if err != nil {
2022-07-10 16:19:33 +00:00
return nil , err
2022-07-09 06:14:14 +00:00
}
}
2022-07-10 16:19:33 +00:00
return webhook . AsAPIWebhook ( ) , nil
2022-07-06 05:08:21 +00:00
}
2022-07-09 05:51:32 +00:00
// GetWebhookByEventName to get webhook by event_name
2023-03-26 01:50:45 +00:00
func ( p * provider ) GetWebhookByEventName ( ctx context . Context , eventName string ) ( [ ] * model . Webhook , error ) {
query := fmt . Sprintf ( "FOR d in %s FILTER d.event_name LIKE @event_name RETURN d" , models . Collections . Webhook )
2022-07-09 06:14:14 +00:00
bindVars := map [ string ] interface { } {
2023-03-26 01:50:45 +00:00
"event_name" : eventName + "%" ,
2022-07-09 06:14:14 +00:00
}
2022-07-10 16:19:33 +00:00
cursor , err := p . db . Query ( ctx , query , bindVars )
2022-07-09 06:14:14 +00:00
if err != nil {
2022-07-10 16:19:33 +00:00
return nil , err
2022-07-09 06:14:14 +00:00
}
defer cursor . Close ( )
2023-03-26 01:50:45 +00:00
webhooks := [ ] * model . Webhook { }
2022-07-09 06:14:14 +00:00
for {
2023-07-31 11:12:11 +00:00
var webhook * models . Webhook
2023-03-26 01:50:45 +00:00
if _ , err := cursor . ReadDocument ( ctx , & webhook ) ; driver . IsNoMoreDocuments ( err ) {
// We're done
2022-07-09 06:14:14 +00:00
break
2023-03-26 01:50:45 +00:00
} else if err != nil {
2022-07-10 16:19:33 +00:00
return nil , err
2022-07-09 06:14:14 +00:00
}
2023-03-26 01:50:45 +00:00
webhooks = append ( webhooks , webhook . AsAPIWebhook ( ) )
2022-07-09 06:14:14 +00:00
}
2023-03-26 01:50:45 +00:00
return webhooks , nil
2022-07-06 05:08:21 +00:00
}
// DeleteWebhook to delete webhook
2022-07-10 16:19:33 +00:00
func ( p * provider ) DeleteWebhook ( ctx context . Context , webhook * model . Webhook ) error {
webhookCollection , _ := p . db . Collection ( ctx , models . Collections . Webhook )
_ , err := webhookCollection . RemoveDocument ( ctx , webhook . ID )
2022-07-09 06:14:14 +00:00
if err != nil {
return err
}
2022-07-12 06:18:42 +00:00
query := fmt . Sprintf ( "FOR d IN %s FILTER d.webhook_id == @webhook_id REMOVE { _key: d._key } IN %s" , models . Collections . WebhookLog , models . Collections . WebhookLog )
2022-07-11 14:10:54 +00:00
bindVars := map [ string ] interface { } {
2022-07-12 06:18:42 +00:00
"webhook_id" : webhook . ID ,
2022-07-11 14:10:54 +00:00
}
cursor , err := p . db . Query ( ctx , query , bindVars )
if err != nil {
return err
}
defer cursor . Close ( )
2022-07-06 05:08:21 +00:00
return nil
}