2022-07-06 05:08:21 +00:00
|
|
|
package mongodb
|
|
|
|
|
|
|
|
import (
|
2022-07-10 16:19:33 +00:00
|
|
|
"context"
|
2022-07-08 13:39:23 +00:00
|
|
|
"time"
|
|
|
|
|
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"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
2022-07-06 05:08:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// AddWebhookLog to add webhook log
|
2023-07-31 11:12:11 +00:00
|
|
|
func (p *provider) AddWebhookLog(ctx context.Context, webhookLog *models.WebhookLog) (*model.WebhookLog, error) {
|
2022-07-08 13:39:23 +00:00
|
|
|
if webhookLog.ID == "" {
|
|
|
|
webhookLog.ID = uuid.New().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
webhookLog.Key = webhookLog.ID
|
|
|
|
webhookLog.CreatedAt = time.Now().Unix()
|
|
|
|
webhookLog.UpdatedAt = time.Now().Unix()
|
|
|
|
|
|
|
|
webhookLogCollection := p.db.Collection(models.Collections.WebhookLog, options.Collection())
|
2022-07-10 16:19:33 +00:00
|
|
|
_, err := webhookLogCollection.InsertOne(ctx, webhookLog)
|
2022-07-08 13:39:23 +00:00
|
|
|
if err != nil {
|
2022-07-10 16:19:33 +00:00
|
|
|
return nil, err
|
2022-07-08 13:39:23 +00:00
|
|
|
}
|
2022-07-10 16:19:33 +00:00
|
|
|
return webhookLog.AsAPIWebhookLog(), nil
|
2022-07-06 05:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListWebhookLogs to list webhook logs
|
2023-07-31 11:12:11 +00:00
|
|
|
func (p *provider) ListWebhookLogs(ctx context.Context, pagination *model.Pagination, webhookID string) (*model.WebhookLogs, error) {
|
2022-07-08 13:39:23 +00:00
|
|
|
webhookLogs := []*model.WebhookLog{}
|
|
|
|
opts := options.Find()
|
|
|
|
opts.SetLimit(pagination.Limit)
|
|
|
|
opts.SetSkip(pagination.Offset)
|
|
|
|
opts.SetSort(bson.M{"created_at": -1})
|
|
|
|
|
|
|
|
paginationClone := pagination
|
|
|
|
query := bson.M{}
|
|
|
|
|
|
|
|
if webhookID != "" {
|
|
|
|
query = bson.M{"webhook_id": webhookID}
|
|
|
|
}
|
|
|
|
|
|
|
|
webhookLogCollection := p.db.Collection(models.Collections.WebhookLog, options.Collection())
|
2022-07-10 16:19:33 +00:00
|
|
|
count, err := webhookLogCollection.CountDocuments(ctx, query, options.Count())
|
2022-07-08 13:39:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
paginationClone.Total = count
|
|
|
|
|
2022-07-10 16:19:33 +00:00
|
|
|
cursor, err := webhookLogCollection.Find(ctx, query, opts)
|
2022-07-08 13:39:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-10 16:19:33 +00:00
|
|
|
defer cursor.Close(ctx)
|
2022-07-08 13:39:23 +00:00
|
|
|
|
2022-07-10 16:19:33 +00:00
|
|
|
for cursor.Next(ctx) {
|
2023-07-31 11:12:11 +00:00
|
|
|
var webhookLog *models.WebhookLog
|
2022-07-08 13:39:23 +00:00
|
|
|
err := cursor.Decode(&webhookLog)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
webhookLogs = append(webhookLogs, webhookLog.AsAPIWebhookLog())
|
|
|
|
}
|
|
|
|
|
|
|
|
return &model.WebhookLogs{
|
2023-07-31 11:12:11 +00:00
|
|
|
Pagination: paginationClone,
|
2022-07-08 13:39:23 +00:00
|
|
|
WebhookLogs: webhookLogs,
|
|
|
|
}, nil
|
2022-07-06 05:08:21 +00:00
|
|
|
}
|