Change webhook table names

Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Sacha Al Himdani
2026-02-13 12:16:15 +01:00
parent 5a8c60a5aa
commit a1e726ec49
19 changed files with 1444 additions and 1436 deletions

View File

@@ -80,8 +80,8 @@ const (
TokenEntityType uint16 = 54
SCIMBridgeEntityType uint16 = 55
WebhookConfigurationEntityType uint16 = 56
WebhookEventEntityType uint16 = 57
WebhookCallEntityType uint16 = 58
WebhookDataEntityType uint16 = 57
WebhookEventEntityType uint16 = 58
)
func NewEntityFromID(id gid.GID) (any, bool) {
@@ -196,10 +196,10 @@ func NewEntityFromID(id gid.GID) (any, bool) {
return &SCIMBridge{ID: id}, true
case WebhookConfigurationEntityType:
return &WebhookConfiguration{ID: id}, true
case WebhookDataEntityType:
return &WebhookData{ID: id}, true
case WebhookEventEntityType:
return &WebhookEvent{ID: id}, true
case WebhookCallEntityType:
return &WebhookCall{ID: id}, true
default:
return nil, false
}

View File

@@ -18,35 +18,35 @@ CREATE TABLE webhook_configurations (
updated_at TIMESTAMP WITH TIME ZONE NOT NULL
);
CREATE TYPE webhook_event_status AS ENUM (
CREATE TYPE webhook_data_status AS ENUM (
'PENDING',
'PROCESSING',
'DELIVERED'
);
CREATE TABLE webhook_events (
CREATE TABLE webhook_data (
id TEXT PRIMARY KEY,
tenant_id TEXT NOT NULL,
organization_id TEXT NOT NULL REFERENCES organizations(id) ON UPDATE CASCADE ON DELETE CASCADE,
event_type webhook_event_type NOT NULL,
status webhook_event_status NOT NULL,
status webhook_data_status NOT NULL,
data JSONB NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
processed_at TIMESTAMP WITH TIME ZONE
);
CREATE TYPE webhook_call_status AS ENUM (
CREATE TYPE webhook_event_status AS ENUM (
'SUCCEEDED',
'FAILED'
);
CREATE TABLE webhook_calls (
CREATE TABLE webhook_events (
id TEXT PRIMARY KEY,
tenant_id TEXT NOT NULL,
webhook_event_id TEXT NOT NULL REFERENCES webhook_events(id) ON UPDATE CASCADE ON DELETE CASCADE,
webhook_data_id TEXT NOT NULL REFERENCES webhook_data(id) ON UPDATE CASCADE ON DELETE CASCADE,
webhook_configuration_id TEXT NOT NULL REFERENCES webhook_configurations(id) ON UPDATE CASCADE ON DELETE CASCADE,
endpoint_url TEXT NOT NULL,
status webhook_call_status NOT NULL,
status webhook_event_status NOT NULL,
response JSONB,
created_at TIMESTAMP WITH TIME ZONE NOT NULL
);

View File

@@ -1,166 +0,0 @@
// 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 (
"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 (
WebhookCall struct {
ID gid.GID `db:"id"`
WebhookEventID gid.GID `db:"webhook_event_id"`
WebhookConfigurationID gid.GID `db:"webhook_configuration_id"`
EndpointURL string `db:"endpoint_url"`
Status WebhookCallStatus `db:"status"`
Response json.RawMessage `db:"response"`
CreatedAt time.Time `db:"created_at"`
}
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,
scope Scoper,
) error {
q := `
INSERT INTO webhook_calls (
id,
tenant_id,
webhook_event_id,
webhook_configuration_id,
endpoint_url,
status,
response,
created_at
)
VALUES (
@id,
@tenant_id,
@webhook_event_id,
@webhook_configuration_id,
@endpoint_url,
@status,
@response,
@created_at
)
`
args := pgx.StrictNamedArgs{
"id": w.ID,
"tenant_id": scope.GetTenantID(),
"webhook_event_id": w.WebhookEventID,
"webhook_configuration_id": w.WebhookConfigurationID,
"endpoint_url": w.EndpointURL,
"status": w.Status,
"response": w.Response,
"created_at": w.CreatedAt,
}
_, err := conn.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot insert webhook call: %w", err)
}
return nil
}

View File

@@ -0,0 +1,159 @@
// 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 (
"context"
"encoding/json"
"errors"
"fmt"
"maps"
"time"
"github.com/jackc/pgx/v5"
"go.gearno.de/kit/pg"
"go.probo.inc/probo/pkg/gid"
)
type (
WebhookData struct {
ID gid.GID `db:"id"`
OrganizationID gid.GID `db:"organization_id"`
EventType WebhookEventType `db:"event_type"`
Status WebhookDataStatus `db:"status"`
Data json.RawMessage `db:"data"`
CreatedAt time.Time `db:"created_at"`
ProcessedAt *time.Time `db:"processed_at"`
}
WebhookDataList []*WebhookData
)
func (w *WebhookData) Insert(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
INSERT INTO webhook_data (
id,
tenant_id,
organization_id,
event_type,
status,
data,
created_at
)
VALUES (
@id,
@tenant_id,
@organization_id,
@event_type,
@status,
@data,
@created_at
)
`
args := pgx.StrictNamedArgs{
"id": w.ID,
"tenant_id": scope.GetTenantID(),
"organization_id": w.OrganizationID,
"event_type": w.EventType,
"status": w.Status,
"data": w.Data,
"created_at": w.CreatedAt,
}
_, err := conn.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot insert webhook data: %w", err)
}
return nil
}
func (w *WebhookData) LoadNextPendingForUpdate(
ctx context.Context,
conn pg.Conn,
) error {
q := `
SELECT
id,
organization_id,
event_type,
status,
data,
created_at,
processed_at
FROM webhook_data
WHERE status = 'PENDING'
ORDER BY created_at ASC
LIMIT 1
FOR UPDATE SKIP LOCKED
`
rows, err := conn.Query(ctx, q)
if err != nil {
return fmt.Errorf("cannot query pending webhook data: %w", err)
}
data, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[WebhookData])
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrResourceNotFound
}
return fmt.Errorf("cannot collect webhook data: %w", err)
}
*w = data
return nil
}
func (w *WebhookData) UpdateStatus(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
UPDATE webhook_data
SET
status = @status,
processed_at = @processed_at
WHERE %s
AND id = @id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"id": w.ID,
"status": w.Status.String(),
"processed_at": w.ProcessedAt,
}
maps.Copy(args, scope.SQLArguments())
result, err := conn.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot update webhook data: %w", err)
}
if result.RowsAffected() == 0 {
return ErrResourceNotFound
}
return nil
}

View File

@@ -19,46 +19,47 @@ import (
"fmt"
)
type WebhookCallStatus string
type WebhookDataStatus string
const (
WebhookCallStatusSucceeded WebhookCallStatus = "SUCCEEDED"
WebhookCallStatusFailed WebhookCallStatus = "FAILED"
WebhookDataStatusPending WebhookDataStatus = "PENDING"
WebhookDataStatusProcessing WebhookDataStatus = "PROCESSING"
WebhookDataStatusDelivered WebhookDataStatus = "DELIVERED"
)
func (s WebhookCallStatus) String() string {
func (s WebhookDataStatus) String() string {
return string(s)
}
func (s WebhookCallStatus) IsValid() bool {
func (s WebhookDataStatus) IsValid() bool {
switch s {
case WebhookCallStatusSucceeded, WebhookCallStatusFailed:
case WebhookDataStatusPending, WebhookDataStatusProcessing, WebhookDataStatusDelivered:
return true
}
return false
}
func (s WebhookCallStatus) MarshalText() ([]byte, error) {
func (s WebhookDataStatus) MarshalText() ([]byte, error) {
return []byte(s.String()), nil
}
func (s *WebhookCallStatus) UnmarshalText(text []byte) error {
*s = WebhookCallStatus(text)
func (s *WebhookDataStatus) UnmarshalText(text []byte) error {
*s = WebhookDataStatus(text)
if !s.IsValid() {
return fmt.Errorf("%s is not a valid WebhookCallStatus", string(text))
return fmt.Errorf("%s is not a valid WebhookDataStatus", string(text))
}
return nil
}
func (s *WebhookCallStatus) Scan(value any) error {
func (s *WebhookDataStatus) Scan(value any) error {
str, ok := value.(string)
if !ok {
return fmt.Errorf("unsupported type for WebhookCallStatus: %T", value)
return fmt.Errorf("unsupported type for WebhookDataStatus: %T", value)
}
return s.UnmarshalText([]byte(str))
}
func (s WebhookCallStatus) Value() (driver.Value, error) {
func (s WebhookDataStatus) Value() (driver.Value, error) {
return s.String(), nil
}

View File

@@ -17,7 +17,6 @@ package coredata
import (
"context"
"encoding/json"
"errors"
"fmt"
"maps"
"time"
@@ -25,23 +24,100 @@ import (
"github.com/jackc/pgx/v5"
"go.gearno.de/kit/pg"
"go.probo.inc/probo/pkg/gid"
"go.probo.inc/probo/pkg/page"
)
type (
WebhookEvent struct {
ID gid.GID `db:"id"`
OrganizationID gid.GID `db:"organization_id"`
EventType WebhookEventType `db:"event_type"`
Status WebhookEventStatus `db:"status"`
Data json.RawMessage `db:"data"`
CreatedAt time.Time `db:"created_at"`
ProcessedAt *time.Time `db:"processed_at"`
ID gid.GID `db:"id"`
WebhookDataID gid.GID `db:"webhook_data_id"`
WebhookConfigurationID gid.GID `db:"webhook_configuration_id"`
EndpointURL string `db:"endpoint_url"`
Status WebhookEventStatus `db:"status"`
Response json.RawMessage `db:"response"`
CreatedAt time.Time `db:"created_at"`
}
WebhookEvents []*WebhookEvent
)
func (w WebhookEvent) CursorKey(orderBy WebhookEventOrderField) page.CursorKey {
switch orderBy {
case WebhookEventOrderFieldCreatedAt:
return page.NewCursorKey(w.ID, w.CreatedAt)
}
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
}
func (w *WebhookEvents) LoadByConfigurationID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
webhookConfigurationID gid.GID,
cursor *page.Cursor[WebhookEventOrderField],
) error {
q := `
SELECT
id,
webhook_data_id,
webhook_configuration_id,
endpoint_url,
status,
response,
created_at
FROM
webhook_events
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 events: %w", err)
}
events, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[WebhookEvent])
if err != nil {
return fmt.Errorf("cannot collect webhook events: %w", err)
}
*w = events
return nil
}
func (w *WebhookEvents) CountByConfigurationID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
webhookConfigurationID gid.GID,
) (int, error) {
q := `
SELECT COUNT(*)
FROM webhook_events
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 events: %w", err)
}
return count, nil
}
func (w *WebhookEvent) Insert(
ctx context.Context,
conn pg.Conn,
@@ -51,31 +127,34 @@ func (w *WebhookEvent) Insert(
INSERT INTO webhook_events (
id,
tenant_id,
organization_id,
event_type,
webhook_data_id,
webhook_configuration_id,
endpoint_url,
status,
data,
response,
created_at
)
VALUES (
@id,
@tenant_id,
@organization_id,
@event_type,
@webhook_data_id,
@webhook_configuration_id,
@endpoint_url,
@status,
@data,
@response,
@created_at
)
`
args := pgx.StrictNamedArgs{
"id": w.ID,
"tenant_id": scope.GetTenantID(),
"organization_id": w.OrganizationID,
"event_type": w.EventType,
"status": w.Status,
"data": w.Data,
"created_at": w.CreatedAt,
"id": w.ID,
"tenant_id": scope.GetTenantID(),
"webhook_data_id": w.WebhookDataID,
"webhook_configuration_id": w.WebhookConfigurationID,
"endpoint_url": w.EndpointURL,
"status": w.Status,
"response": w.Response,
"created_at": w.CreatedAt,
}
_, err := conn.Exec(ctx, q, args)
@@ -85,76 +164,3 @@ VALUES (
return nil
}
func (w *WebhookEvent) LoadNextPendingForUpdate(
ctx context.Context,
conn pg.Conn,
) error {
q := `
SELECT
id,
organization_id,
event_type,
status,
data,
created_at,
processed_at
FROM webhook_events
WHERE status = 'PENDING'
ORDER BY created_at ASC
LIMIT 1
FOR UPDATE SKIP LOCKED
`
rows, err := conn.Query(ctx, q)
if err != nil {
return fmt.Errorf("cannot query pending webhook events: %w", err)
}
event, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[WebhookEvent])
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrResourceNotFound
}
return fmt.Errorf("cannot collect webhook event: %w", err)
}
*w = event
return nil
}
func (w *WebhookEvent) UpdateStatus(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
UPDATE webhook_events
SET
status = @status,
processed_at = @processed_at
WHERE %s
AND id = @id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"id": w.ID,
"status": w.Status.String(),
"processed_at": w.ProcessedAt,
}
maps.Copy(args, scope.SQLArguments())
result, err := conn.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot update webhook event: %w", err)
}
if result.RowsAffected() == 0 {
return ErrResourceNotFound
}
return nil
}

View File

@@ -19,37 +19,37 @@ import (
)
type (
WebhookCallOrderField string
WebhookEventOrderField string
)
const (
WebhookCallOrderFieldCreatedAt WebhookCallOrderField = "CREATED_AT"
WebhookEventOrderFieldCreatedAt WebhookEventOrderField = "CREATED_AT"
)
func (p WebhookCallOrderField) Column() string {
func (p WebhookEventOrderField) Column() string {
return string(p)
}
func (p WebhookCallOrderField) String() string {
func (p WebhookEventOrderField) String() string {
return string(p)
}
func (p WebhookCallOrderField) IsValid() bool {
func (p WebhookEventOrderField) IsValid() bool {
switch p {
case WebhookCallOrderFieldCreatedAt:
case WebhookEventOrderFieldCreatedAt:
return true
}
return false
}
func (p WebhookCallOrderField) MarshalText() ([]byte, error) {
func (p WebhookEventOrderField) MarshalText() ([]byte, error) {
return []byte(p.String()), nil
}
func (p *WebhookCallOrderField) UnmarshalText(text []byte) error {
*p = WebhookCallOrderField(text)
func (p *WebhookEventOrderField) UnmarshalText(text []byte) error {
*p = WebhookEventOrderField(text)
if !p.IsValid() {
return fmt.Errorf("%s is not a valid WebhookCallOrderField", string(text))
return fmt.Errorf("%s is not a valid WebhookEventOrderField", string(text))
}
return nil
}

View File

@@ -22,9 +22,8 @@ import (
type WebhookEventStatus string
const (
WebhookEventStatusPending WebhookEventStatus = "PENDING"
WebhookEventStatusProcessing WebhookEventStatus = "PROCESSING"
WebhookEventStatusDelivered WebhookEventStatus = "DELIVERED"
WebhookEventStatusSucceeded WebhookEventStatus = "SUCCEEDED"
WebhookEventStatusFailed WebhookEventStatus = "FAILED"
)
func (s WebhookEventStatus) String() string {
@@ -33,7 +32,7 @@ func (s WebhookEventStatus) String() string {
func (s WebhookEventStatus) IsValid() bool {
switch s {
case WebhookEventStatusPending, WebhookEventStatusProcessing, WebhookEventStatusDelivered:
case WebhookEventStatusSucceeded, WebhookEventStatusFailed:
return true
}
return false