2022-07-06 10:38:21 +05:30
package arangodb
import (
2022-07-09 11:44:14 +05:30
"context"
"fmt"
2022-07-08 19:09:23 +05:30
"time"
2022-07-09 11:44:14 +05:30
"github.com/arangodb/go-driver"
arangoDriver "github.com/arangodb/go-driver"
2022-07-06 10:38:21 +05:30
"github.com/authorizerdev/authorizer/server/db/models"
"github.com/authorizerdev/authorizer/server/graph/model"
2022-07-08 19:09:23 +05:30
"github.com/google/uuid"
2022-07-06 10:38:21 +05:30
)
// AddWebhook to add webhook
2022-07-10 21:49:33 +05:30
func ( p * provider ) AddWebhook ( ctx context . Context , webhook models . Webhook ) ( * model . Webhook , error ) {
2022-07-08 19:09:23 +05:30
if webhook . ID == "" {
webhook . ID = uuid . New ( ) . String ( )
}
webhook . Key = webhook . ID
webhook . CreatedAt = time . Now ( ) . Unix ( )
webhook . UpdatedAt = time . Now ( ) . Unix ( )
2022-07-10 21:49:33 +05:30
webhookCollection , _ := p . db . Collection ( ctx , models . Collections . Webhook )
_ , err := webhookCollection . CreateDocument ( ctx , webhook )
2022-07-09 11:44:14 +05:30
if err != nil {
2022-07-10 21:49:33 +05:30
return nil , err
2022-07-09 11:44:14 +05:30
}
2022-07-10 21:49:33 +05:30
return webhook . AsAPIWebhook ( ) , nil
2022-07-06 10:38:21 +05:30
}
// UpdateWebhook to update webhook
2022-07-10 21:49:33 +05:30
func ( p * provider ) UpdateWebhook ( ctx context . Context , webhook models . Webhook ) ( * model . Webhook , error ) {
2022-07-08 19:09:23 +05:30
webhook . UpdatedAt = time . Now ( ) . Unix ( )
2022-07-10 21:49:33 +05:30
webhookCollection , _ := p . db . Collection ( ctx , models . Collections . Webhook )
meta , err := webhookCollection . UpdateDocument ( ctx , webhook . Key , webhook )
2022-07-09 11:44:14 +05:30
if err != nil {
2022-07-10 21:49:33 +05:30
return nil , err
2022-07-09 11:44:14 +05:30
}
webhook . Key = meta . Key
webhook . ID = meta . ID . String ( )
2022-07-10 21:49:33 +05:30
return webhook . AsAPIWebhook ( ) , nil
2022-07-06 10:38:21 +05:30
}
// ListWebhooks to list webhook
2022-07-10 21:49:33 +05:30
func ( p * provider ) ListWebhook ( ctx context . Context , pagination model . Pagination ) ( * model . Webhooks , error ) {
2022-07-09 11:44:14 +05:30
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 )
2022-07-10 21:49:33 +05:30
sctx := driver . WithQueryFullCount ( ctx )
cursor , err := p . db . Query ( sctx , query , nil )
2022-07-09 11:44:14 +05:30
if err != nil {
return nil , err
}
defer cursor . Close ( )
paginationClone := pagination
paginationClone . Total = cursor . Statistics ( ) . FullCount ( )
for {
var webhook models . Webhook
2022-07-10 21:49:33 +05:30
meta , err := cursor . ReadDocument ( ctx , & webhook )
2022-07-09 11:44:14 +05:30
if arangoDriver . IsNoMoreDocuments ( err ) {
break
} else if err != nil {
return nil , err
}
if meta . Key != "" {
webhooks = append ( webhooks , webhook . AsAPIWebhook ( ) )
}
}
return & model . Webhooks {
Pagination : & paginationClone ,
Webhooks : webhooks ,
} , nil
2022-07-06 10:38:21 +05:30
}
// GetWebhookByID to get webhook by id
2022-07-10 21:49:33 +05:30
func ( p * provider ) GetWebhookByID ( ctx context . Context , webhookID string ) ( * model . Webhook , error ) {
2022-07-09 11:44:14 +05:30
var webhook models . Webhook
2022-07-12 11:48:42 +05:30
query := fmt . Sprintf ( "FOR d in %s FILTER d._key == @webhook_id RETURN d" , models . Collections . Webhook )
2022-07-09 11:44:14 +05:30
bindVars := map [ string ] interface { } {
"webhook_id" : webhookID ,
}
2022-07-10 21:49:33 +05:30
cursor , err := p . db . Query ( ctx , query , bindVars )
2022-07-09 11:44:14 +05:30
if err != nil {
2022-07-10 21:49:33 +05:30
return nil , err
2022-07-09 11:44:14 +05:30
}
defer cursor . Close ( )
for {
if ! cursor . HasMore ( ) {
if webhook . Key == "" {
2022-07-10 21:49:33 +05:30
return nil , fmt . Errorf ( "webhook not found" )
2022-07-09 11:44:14 +05:30
}
break
}
2022-07-10 21:49:33 +05:30
_ , err := cursor . ReadDocument ( ctx , & webhook )
2022-07-09 11:44:14 +05:30
if err != nil {
2022-07-10 21:49:33 +05:30
return nil , err
2022-07-09 11:44:14 +05:30
}
}
2022-07-10 21:49:33 +05:30
return webhook . AsAPIWebhook ( ) , nil
2022-07-06 10:38:21 +05:30
}
2022-07-09 11:21:32 +05:30
// GetWebhookByEventName to get webhook by event_name
2022-07-10 21:49:33 +05:30
func ( p * provider ) GetWebhookByEventName ( ctx context . Context , eventName string ) ( * model . Webhook , error ) {
2022-07-09 11:44:14 +05:30
var webhook models . Webhook
query := fmt . Sprintf ( "FOR d in %s FILTER d.event_name == @event_name RETURN d" , models . Collections . Webhook )
bindVars := map [ string ] interface { } {
"event_name" : eventName ,
}
2022-07-10 21:49:33 +05:30
cursor , err := p . db . Query ( ctx , query , bindVars )
2022-07-09 11:44:14 +05:30
if err != nil {
2022-07-10 21:49:33 +05:30
return nil , err
2022-07-09 11:44:14 +05:30
}
defer cursor . Close ( )
for {
if ! cursor . HasMore ( ) {
if webhook . Key == "" {
2022-07-10 21:49:33 +05:30
return nil , fmt . Errorf ( "webhook not found" )
2022-07-09 11:44:14 +05:30
}
break
}
2022-07-10 21:49:33 +05:30
_ , err := cursor . ReadDocument ( ctx , & webhook )
2022-07-09 11:44:14 +05:30
if err != nil {
2022-07-10 21:49:33 +05:30
return nil , err
2022-07-09 11:44:14 +05:30
}
}
2022-07-10 21:49:33 +05:30
return webhook . AsAPIWebhook ( ) , nil
2022-07-06 10:38:21 +05:30
}
// DeleteWebhook to delete webhook
2022-07-10 21:49:33 +05:30
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 11:44:14 +05:30
if err != nil {
return err
}
2022-07-11 19:40:54 +05:30
2022-07-12 11:48:42 +05:30
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 19:40:54 +05:30
bindVars := map [ string ] interface { } {
2022-07-12 11:48:42 +05:30
"webhook_id" : webhook . ID ,
2022-07-11 19:40:54 +05:30
}
cursor , err := p . db . Query ( ctx , query , bindVars )
if err != nil {
return err
}
defer cursor . Close ( )
2022-07-06 10:38:21 +05:30
return nil
}