@@ -3,8 +3,10 @@ package arangodb
33import (
44 "context"
55 "fmt"
6+ "strings"
67 "time"
78
9+ "github.com/arangodb/go-driver"
810 arangoDriver "github.com/arangodb/go-driver"
911 "github.com/authorizerdev/authorizer/server/db/models"
1012 "github.com/authorizerdev/authorizer/server/graph/model"
@@ -17,8 +19,9 @@ func (p *provider) AddWebhook(ctx context.Context, webhook models.Webhook) (*mod
1719 webhook .ID = uuid .New ().String ()
1820 webhook .Key = webhook .ID
1921 }
20-
2122 webhook .Key = webhook .ID
23+ // Add timestamp to make event name unique for legacy version
24+ webhook .EventName = fmt .Sprintf ("%s-%d" , webhook .EventName , time .Now ().Unix ())
2225 webhook .CreatedAt = time .Now ().Unix ()
2326 webhook .UpdatedAt = time .Now ().Unix ()
2427 webhookCollection , _ := p .db .Collection (ctx , models .Collections .Webhook )
@@ -32,12 +35,15 @@ func (p *provider) AddWebhook(ctx context.Context, webhook models.Webhook) (*mod
3235// UpdateWebhook to update webhook
3336func (p * provider ) UpdateWebhook (ctx context.Context , webhook models.Webhook ) (* model.Webhook , error ) {
3437 webhook .UpdatedAt = time .Now ().Unix ()
38+ // Event is changed
39+ if ! strings .Contains (webhook .EventName , "-" ) {
40+ webhook .EventName = fmt .Sprintf ("%s-%d" , webhook .EventName , time .Now ().Unix ())
41+ }
3542 webhookCollection , _ := p .db .Collection (ctx , models .Collections .Webhook )
3643 meta , err := webhookCollection .UpdateDocument (ctx , webhook .Key , webhook )
3744 if err != nil {
3845 return nil , err
3946 }
40-
4147 webhook .Key = meta .Key
4248 webhook .ID = meta .ID .String ()
4349 return webhook .AsAPIWebhook (), nil
@@ -55,10 +61,8 @@ func (p *provider) ListWebhook(ctx context.Context, pagination model.Pagination)
5561 return nil , err
5662 }
5763 defer cursor .Close ()
58-
5964 paginationClone := pagination
6065 paginationClone .Total = cursor .Statistics ().FullCount ()
61-
6266 for {
6367 var webhook models.Webhook
6468 meta , err := cursor .ReadDocument (ctx , & webhook )
@@ -87,13 +91,11 @@ func (p *provider) GetWebhookByID(ctx context.Context, webhookID string) (*model
8791 bindVars := map [string ]interface {}{
8892 "webhook_id" : webhookID ,
8993 }
90-
9194 cursor , err := p .db .Query (ctx , query , bindVars )
9295 if err != nil {
9396 return nil , err
9497 }
9598 defer cursor .Close ()
96-
9799 for {
98100 if ! cursor .HasMore () {
99101 if webhook .Key == "" {
@@ -110,32 +112,28 @@ func (p *provider) GetWebhookByID(ctx context.Context, webhookID string) (*model
110112}
111113
112114// GetWebhookByEventName to get webhook by event_name
113- func (p * provider ) GetWebhookByEventName (ctx context.Context , eventName string ) (* model.Webhook , error ) {
114- var webhook models.Webhook
115- query := fmt .Sprintf ("FOR d in %s FILTER d.event_name == @event_name RETURN d" , models .Collections .Webhook )
115+ func (p * provider ) GetWebhookByEventName (ctx context.Context , eventName string ) ([]* model.Webhook , error ) {
116+ query := fmt .Sprintf ("FOR d in %s FILTER d.event_name LIKE @event_name RETURN d" , models .Collections .Webhook )
116117 bindVars := map [string ]interface {}{
117- "event_name" : eventName ,
118+ "event_name" : eventName + "%" ,
118119 }
119-
120120 cursor , err := p .db .Query (ctx , query , bindVars )
121121 if err != nil {
122122 return nil , err
123123 }
124124 defer cursor .Close ()
125-
125+ webhooks := [] * model. Webhook {}
126126 for {
127- if ! cursor .HasMore () {
128- if webhook .Key == "" {
129- return nil , fmt .Errorf ("webhook not found" )
130- }
127+ var webhook models.Webhook
128+ if _ , err := cursor .ReadDocument (ctx , & webhook ); driver .IsNoMoreDocuments (err ) {
129+ // We're done
131130 break
132- }
133- _ , err := cursor .ReadDocument (ctx , & webhook )
134- if err != nil {
131+ } else if err != nil {
135132 return nil , err
136133 }
134+ webhooks = append (webhooks , webhook .AsAPIWebhook ())
137135 }
138- return webhook . AsAPIWebhook () , nil
136+ return webhooks , nil
139137}
140138
141139// DeleteWebhook to delete webhook
@@ -145,17 +143,14 @@ func (p *provider) DeleteWebhook(ctx context.Context, webhook *model.Webhook) er
145143 if err != nil {
146144 return err
147145 }
148-
149146 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 )
150147 bindVars := map [string ]interface {}{
151148 "webhook_id" : webhook .ID ,
152149 }
153-
154150 cursor , err := p .db .Query (ctx , query , bindVars )
155151 if err != nil {
156152 return err
157153 }
158154 defer cursor .Close ()
159-
160155 return nil
161156}
0 commit comments