Remove status on webhook data
Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
@@ -18,25 +18,18 @@ CREATE TABLE webhook_configurations (
|
||||
updated_at TIMESTAMP WITH TIME ZONE NOT NULL
|
||||
);
|
||||
|
||||
CREATE TYPE webhook_data_status AS ENUM (
|
||||
'PENDING',
|
||||
'PROCESSING',
|
||||
'PROCESSED',
|
||||
'FAILED'
|
||||
);
|
||||
|
||||
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_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_event_status AS ENUM (
|
||||
'PENDING',
|
||||
'SUCCEEDED',
|
||||
'FAILED'
|
||||
);
|
||||
|
||||
@@ -29,13 +29,12 @@ import (
|
||||
|
||||
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"`
|
||||
ID gid.GID `db:"id"`
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
EventType WebhookEventType `db:"event_type"`
|
||||
Data json.RawMessage `db:"data"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
ProcessedAt *time.Time `db:"processed_at"`
|
||||
}
|
||||
|
||||
WebhookDataList []*WebhookData
|
||||
@@ -52,7 +51,6 @@ INSERT INTO webhook_data (
|
||||
tenant_id,
|
||||
organization_id,
|
||||
event_type,
|
||||
status,
|
||||
data,
|
||||
created_at
|
||||
)
|
||||
@@ -61,7 +59,6 @@ VALUES (
|
||||
@tenant_id,
|
||||
@organization_id,
|
||||
@event_type,
|
||||
@status,
|
||||
@data,
|
||||
@created_at
|
||||
)
|
||||
@@ -72,7 +69,6 @@ VALUES (
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"organization_id": w.OrganizationID,
|
||||
"event_type": w.EventType,
|
||||
"status": w.Status,
|
||||
"data": w.Data,
|
||||
"created_at": w.CreatedAt,
|
||||
}
|
||||
@@ -85,7 +81,7 @@ VALUES (
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *WebhookData) LoadNextPendingForUpdate(
|
||||
func (w *WebhookData) LoadNextUnprocessedForUpdate(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
) error {
|
||||
@@ -94,12 +90,11 @@ SELECT
|
||||
id,
|
||||
organization_id,
|
||||
event_type,
|
||||
status,
|
||||
data,
|
||||
created_at,
|
||||
processed_at
|
||||
FROM webhook_data
|
||||
WHERE status = 'PENDING'
|
||||
WHERE processed_at IS NULL
|
||||
ORDER BY created_at ASC
|
||||
LIMIT 1
|
||||
FOR UPDATE SKIP LOCKED
|
||||
@@ -107,7 +102,7 @@ FOR UPDATE SKIP LOCKED
|
||||
|
||||
rows, err := conn.Query(ctx, q)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query pending webhook data: %w", err)
|
||||
return fmt.Errorf("cannot query unprocessed webhook data: %w", err)
|
||||
}
|
||||
|
||||
data, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[WebhookData])
|
||||
@@ -123,16 +118,14 @@ FOR UPDATE SKIP LOCKED
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *WebhookData) UpdateStatus(
|
||||
func (w *WebhookData) UpdateProcessedAt(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
UPDATE webhook_data
|
||||
SET
|
||||
status = @status,
|
||||
processed_at = @processed_at
|
||||
SET processed_at = @processed_at
|
||||
WHERE %s
|
||||
AND id = @id
|
||||
`
|
||||
@@ -141,7 +134,6 @@ WHERE %s
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"id": w.ID,
|
||||
"status": w.Status.String(),
|
||||
"processed_at": w.ProcessedAt,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
@@ -1,68 +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 (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type WebhookDataStatus string
|
||||
|
||||
const (
|
||||
WebhookDataStatusPending WebhookDataStatus = "PENDING"
|
||||
WebhookDataStatusProcessing WebhookDataStatus = "PROCESSING"
|
||||
WebhookDataStatusProcessed WebhookDataStatus = "PROCESSED"
|
||||
WebhookDataStatusFailed WebhookDataStatus = "FAILED"
|
||||
)
|
||||
|
||||
func (s WebhookDataStatus) String() string {
|
||||
return string(s)
|
||||
}
|
||||
|
||||
func (s WebhookDataStatus) IsValid() bool {
|
||||
switch s {
|
||||
case WebhookDataStatusPending, WebhookDataStatusProcessing, WebhookDataStatusProcessed, WebhookDataStatusFailed:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s WebhookDataStatus) MarshalText() ([]byte, error) {
|
||||
return []byte(s.String()), nil
|
||||
}
|
||||
|
||||
func (s *WebhookDataStatus) UnmarshalText(text []byte) error {
|
||||
*s = WebhookDataStatus(text)
|
||||
if !s.IsValid() {
|
||||
return fmt.Errorf("%s is not a valid WebhookDataStatus", string(text))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *WebhookDataStatus) Scan(value any) error {
|
||||
switch v := value.(type) {
|
||||
case string:
|
||||
return s.UnmarshalText([]byte(v))
|
||||
case []byte:
|
||||
return s.UnmarshalText(v)
|
||||
default:
|
||||
return fmt.Errorf("unsupported type for WebhookDataStatus: %T", value)
|
||||
}
|
||||
}
|
||||
|
||||
func (s WebhookDataStatus) Value() (driver.Value, error) {
|
||||
return s.String(), nil
|
||||
}
|
||||
@@ -159,3 +159,38 @@ VALUES (
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *WebhookEvent) UpdateStatus(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
UPDATE webhook_events
|
||||
SET
|
||||
status = @status,
|
||||
response = @response
|
||||
WHERE %s
|
||||
AND id = @id
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"id": w.ID,
|
||||
"status": w.Status,
|
||||
"response": w.Response,
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
@@ -22,8 +22,9 @@ import (
|
||||
type WebhookEventStatus string
|
||||
|
||||
const (
|
||||
WebhookEventStatusSucceeded WebhookEventStatus = "SUCCEEDED"
|
||||
WebhookEventStatusFailed WebhookEventStatus = "FAILED"
|
||||
WebhookEventStatusPending WebhookEventStatus = "PENDING"
|
||||
WebhookEventStatusSucceeded WebhookEventStatus = "SUCCEEDED"
|
||||
WebhookEventStatusFailed WebhookEventStatus = "FAILED"
|
||||
)
|
||||
|
||||
func (s WebhookEventStatus) String() string {
|
||||
@@ -32,7 +33,7 @@ func (s WebhookEventStatus) String() string {
|
||||
|
||||
func (s WebhookEventStatus) IsValid() bool {
|
||||
switch s {
|
||||
case WebhookEventStatusSucceeded, WebhookEventStatusFailed:
|
||||
case WebhookEventStatusPending, WebhookEventStatusSucceeded, WebhookEventStatusFailed:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
|
||||
Reference in New Issue
Block a user