Add webhook calls ui
Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
@@ -18,11 +18,13 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"maps"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -39,6 +41,83 @@ type (
|
||||
WebhookCalls []*WebhookCall
|
||||
)
|
||||
|
||||
func (w WebhookCall) CursorKey(orderBy WebhookCallOrderField) page.CursorKey {
|
||||
switch orderBy {
|
||||
case WebhookCallOrderFieldCreatedAt:
|
||||
return page.NewCursorKey(w.ID, w.CreatedAt)
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
|
||||
}
|
||||
|
||||
func (w *WebhookCalls) LoadByConfigurationID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
webhookConfigurationID gid.GID,
|
||||
cursor *page.Cursor[WebhookCallOrderField],
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
webhook_event_id,
|
||||
webhook_configuration_id,
|
||||
endpoint_url,
|
||||
status,
|
||||
response,
|
||||
created_at
|
||||
FROM
|
||||
webhook_calls
|
||||
WHERE
|
||||
%s
|
||||
AND webhook_configuration_id = @webhook_configuration_id
|
||||
AND %s
|
||||
`
|
||||
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
|
||||
|
||||
args := pgx.NamedArgs{"webhook_configuration_id": webhookConfigurationID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
maps.Copy(args, cursor.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query webhook calls: %w", err)
|
||||
}
|
||||
|
||||
calls, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[WebhookCall])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect webhook calls: %w", err)
|
||||
}
|
||||
|
||||
*w = calls
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *WebhookCalls) CountByConfigurationID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
webhookConfigurationID gid.GID,
|
||||
) (int, error) {
|
||||
q := `
|
||||
SELECT COUNT(*)
|
||||
FROM webhook_calls
|
||||
WHERE %s
|
||||
AND webhook_configuration_id = @webhook_configuration_id
|
||||
`
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"webhook_configuration_id": webhookConfigurationID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
var count int
|
||||
if err := conn.QueryRow(ctx, q, args).Scan(&count); err != nil {
|
||||
return 0, fmt.Errorf("cannot count webhook calls: %w", err)
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (w *WebhookCall) Insert(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
|
||||
55
pkg/coredata/webhook_call_order_field.go
Normal file
55
pkg/coredata/webhook_call_order_field.go
Normal file
@@ -0,0 +1,55 @@
|
||||
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package coredata
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type (
|
||||
WebhookCallOrderField string
|
||||
)
|
||||
|
||||
const (
|
||||
WebhookCallOrderFieldCreatedAt WebhookCallOrderField = "CREATED_AT"
|
||||
)
|
||||
|
||||
func (p WebhookCallOrderField) Column() string {
|
||||
return string(p)
|
||||
}
|
||||
|
||||
func (p WebhookCallOrderField) String() string {
|
||||
return string(p)
|
||||
}
|
||||
|
||||
func (p WebhookCallOrderField) IsValid() bool {
|
||||
switch p {
|
||||
case WebhookCallOrderFieldCreatedAt:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (p WebhookCallOrderField) MarshalText() ([]byte, error) {
|
||||
return []byte(p.String()), nil
|
||||
}
|
||||
|
||||
func (p *WebhookCallOrderField) UnmarshalText(text []byte) error {
|
||||
*p = WebhookCallOrderField(text)
|
||||
if !p.IsValid() {
|
||||
return fmt.Errorf("%s is not a valid WebhookCallOrderField", string(text))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user