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

@@ -1,5 +1,5 @@
/**
* @generated SignedSource<<cc57c5db0d6374e4941bb0d4f1fb73c2>>
* @generated SignedSource<<18c0948adc229a75b1d980e64730b423>>
* @lightSyntaxTransform
* @nogrep
*/
@@ -20,10 +20,10 @@ export type WebhooksSettingsPageQuery$data = {
readonly webhookConfigurations: {
readonly edges: ReadonlyArray<{
readonly node: {
readonly calls: {
readonly endpointUrl: string;
readonly events: {
readonly totalCount: number;
};
readonly endpointUrl: string;
readonly id: string;
readonly selectedEvents: ReadonlyArray<WebhookEventType>;
};
@@ -110,9 +110,9 @@ v4 = [
"value": 0
}
],
"concreteType": "WebhookCallConnection",
"concreteType": "WebhookEventConnection",
"kind": "LinkedField",
"name": "calls",
"name": "events",
"plural": false,
"selections": [
{
@@ -123,7 +123,7 @@ v4 = [
"storageKey": null
}
],
"storageKey": "calls(first:0)"
"storageKey": "events(first:0)"
},
(v2/*: any*/)
],
@@ -265,7 +265,7 @@ return {
]
},
"params": {
"cacheID": "97b7c454586fe958e456fa7929b46686",
"cacheID": "b5ca3cf4d05d2cb81c744e760433cb94",
"id": null,
"metadata": {
"connection": [
@@ -282,11 +282,11 @@ return {
},
"name": "WebhooksSettingsPageQuery",
"operationKind": "query",
"text": "query WebhooksSettingsPageQuery(\n $organizationId: ID!\n) {\n organization: node(id: $organizationId) {\n __typename\n ... on Organization {\n id\n webhookConfigurations(first: 50) {\n edges {\n node {\n id\n endpointUrl\n selectedEvents\n calls(first: 0) {\n totalCount\n }\n __typename\n }\n cursor\n }\n pageInfo {\n endCursor\n hasNextPage\n }\n }\n }\n id\n }\n}\n"
"text": "query WebhooksSettingsPageQuery(\n $organizationId: ID!\n) {\n organization: node(id: $organizationId) {\n __typename\n ... on Organization {\n id\n webhookConfigurations(first: 50) {\n edges {\n node {\n id\n endpointUrl\n selectedEvents\n events(first: 0) {\n totalCount\n }\n __typename\n }\n cursor\n }\n pageInfo {\n endCursor\n hasNextPage\n }\n }\n }\n id\n }\n}\n"
}
};
})();
(node as any).hash = "58012f55644dae31fe5e18c2c1e193a7";
(node as any).hash = "bac1812903d6f495d15bb0e70ce5ed48";
export default node;

View File

@@ -1,5 +1,5 @@
/**
* @generated SignedSource<<c53381e72c0d17cff70568af0bbbb6e5>>
* @generated SignedSource<<70389123cd52ffd010e2b8f0109aedcd>>
* @lightSyntaxTransform
* @nogrep
*/
@@ -9,22 +9,22 @@
// @ts-nocheck
import { ConcreteRequest } from 'relay-runtime';
export type WebhookCallStatus = "FAILED" | "SUCCEEDED";
export type WebhooksSettingsPage_callsQuery$variables = {
export type WebhookEventStatus = "FAILED" | "SUCCEEDED";
export type WebhooksSettingsPage_eventsQuery$variables = {
after?: string | null | undefined;
first?: number | null | undefined;
webhookConfigurationId: string;
};
export type WebhooksSettingsPage_callsQuery$data = {
export type WebhooksSettingsPage_eventsQuery$data = {
readonly node: {
readonly calls?: {
readonly events?: {
readonly edges: ReadonlyArray<{
readonly node: {
readonly createdAt: string;
readonly endpointUrl: string;
readonly id: string;
readonly response: string | null | undefined;
readonly status: WebhookCallStatus;
readonly status: WebhookEventStatus;
};
}>;
readonly pageInfo: {
@@ -35,9 +35,9 @@ export type WebhooksSettingsPage_callsQuery$data = {
};
};
};
export type WebhooksSettingsPage_callsQuery = {
response: WebhooksSettingsPage_callsQuery$data;
variables: WebhooksSettingsPage_callsQuery$variables;
export type WebhooksSettingsPage_eventsQuery = {
response: WebhooksSettingsPage_eventsQuery$data;
variables: WebhooksSettingsPage_eventsQuery$variables;
};
const node: ConcreteRequest = (function(){
@@ -87,9 +87,9 @@ v5 = {
"variableName": "first"
}
],
"concreteType": "WebhookCallConnection",
"concreteType": "WebhookEventConnection",
"kind": "LinkedField",
"name": "calls",
"name": "events",
"plural": false,
"selections": [
{
@@ -127,7 +127,7 @@ v5 = {
{
"alias": null,
"args": null,
"concreteType": "WebhookCallEdge",
"concreteType": "WebhookEventEdge",
"kind": "LinkedField",
"name": "edges",
"plural": true,
@@ -135,7 +135,7 @@ v5 = {
{
"alias": null,
"args": null,
"concreteType": "WebhookCall",
"concreteType": "WebhookEvent",
"kind": "LinkedField",
"name": "node",
"plural": false,
@@ -191,7 +191,7 @@ return {
],
"kind": "Fragment",
"metadata": null,
"name": "WebhooksSettingsPage_callsQuery",
"name": "WebhooksSettingsPage_eventsQuery",
"selections": [
{
"alias": null,
@@ -217,7 +217,7 @@ return {
(v0/*: any*/)
],
"kind": "Operation",
"name": "WebhooksSettingsPage_callsQuery",
"name": "WebhooksSettingsPage_eventsQuery",
"selections": [
{
"alias": null,
@@ -242,16 +242,16 @@ return {
]
},
"params": {
"cacheID": "9371a8918daebbb2a660b02649ba0241",
"cacheID": "507b568e987ce78e58b171810a60bce8",
"id": null,
"metadata": {},
"name": "WebhooksSettingsPage_callsQuery",
"name": "WebhooksSettingsPage_eventsQuery",
"operationKind": "query",
"text": "query WebhooksSettingsPage_callsQuery(\n $webhookConfigurationId: ID!\n $first: Int\n $after: CursorKey\n) {\n node(id: $webhookConfigurationId) {\n __typename\n ... on WebhookConfiguration {\n calls(first: $first, after: $after) {\n totalCount\n pageInfo {\n hasNextPage\n endCursor\n }\n edges {\n node {\n id\n status\n endpointUrl\n createdAt\n response\n }\n }\n }\n }\n id\n }\n}\n"
"text": "query WebhooksSettingsPage_eventsQuery(\n $webhookConfigurationId: ID!\n $first: Int\n $after: CursorKey\n) {\n node(id: $webhookConfigurationId) {\n __typename\n ... on WebhookConfiguration {\n events(first: $first, after: $after) {\n totalCount\n pageInfo {\n hasNextPage\n endCursor\n }\n edges {\n node {\n id\n status\n endpointUrl\n createdAt\n response\n }\n }\n }\n }\n id\n }\n}\n"
}
};
})();
(node as any).hash = "de18874117b6272fdf2e22871adc4a5d";
(node as any).hash = "2e528e16c34c33138858f92215af2ab4";
export default node;

View File

@@ -23,9 +23,9 @@ import { type PreloadedQuery, usePreloadedQuery, useRelayEnvironment } from "rea
import { ConnectionHandler, fetchQuery, graphql } from "relay-runtime";
import { z } from "zod";
import type { WebhooksSettingsPage_callsQuery } from "#/__generated__/core/WebhooksSettingsPage_callsQuery.graphql";
import type { WebhooksSettingsPage_createMutation } from "#/__generated__/core/WebhooksSettingsPage_createMutation.graphql";
import type { WebhooksSettingsPage_deleteMutation } from "#/__generated__/core/WebhooksSettingsPage_deleteMutation.graphql";
import type { WebhooksSettingsPage_eventsQuery } from "#/__generated__/core/WebhooksSettingsPage_eventsQuery.graphql";
import type { WebhooksSettingsPage_signingSecretQuery } from "#/__generated__/core/WebhooksSettingsPage_signingSecretQuery.graphql";
import type { WebhooksSettingsPage_updateMutation } from "#/__generated__/core/WebhooksSettingsPage_updateMutation.graphql";
import type { WebhooksSettingsPageQuery } from "#/__generated__/core/WebhooksSettingsPageQuery.graphql";
@@ -45,7 +45,7 @@ export const webhooksSettingsPageQuery = graphql`
id
endpointUrl
selectedEvents
calls(first: 0) {
events(first: 0) {
totalCount
}
}
@@ -98,15 +98,15 @@ const signingSecretQuery = graphql`
}
`;
const webhookCallsQuery = graphql`
query WebhooksSettingsPage_callsQuery(
const webhookEventsQuery = graphql`
query WebhooksSettingsPage_eventsQuery(
$webhookConfigurationId: ID!
$first: Int
$after: CursorKey
) {
node(id: $webhookConfigurationId) {
... on WebhookConfiguration {
calls(first: $first, after: $after) {
events(first: $first, after: $after) {
totalCount
pageInfo {
hasNextPage
@@ -285,7 +285,7 @@ function WebhookFormDialog({
);
}
function CallStatusBadge({ status }: { status: string }) {
function EventStatusBadge({ status }: { status: string }) {
const { __ } = useTranslate();
if (status === "SUCCEEDED") {
return <Badge variant="success" size="sm">{__("Succeeded")}</Badge>;
@@ -297,7 +297,7 @@ function formatDate(iso: string) {
return new Date(iso).toLocaleString();
}
function WebhookCallsDialog({
function WebhookEventsDialog({
webhookConfigurationId,
endpointUrl,
onClose,
@@ -307,10 +307,11 @@ function WebhookCallsDialog({
onClose: () => void;
}) {
const { __ } = useTranslate();
const { toast } = useToast();
const environment = useRelayEnvironment();
const dialogRef = useDialogRef();
type CallNode = NonNullable<WebhooksSettingsPage_callsQuery["response"]["node"]["calls"]>["edges"][number]["node"];
const [calls, setCalls] = useState<CallNode[]>([]);
type EventNode = NonNullable<WebhooksSettingsPage_eventsQuery["response"]["node"]["events"]>["edges"][number]["node"];
const [events, setEvents] = useState<EventNode[]>([]);
const [loading, setLoading] = useState(true);
const [hasNextPage, setHasNextPage] = useState(false);
const [endCursor, setEndCursor] = useState<string | null>(null);
@@ -318,13 +319,13 @@ function WebhookCallsDialog({
const PAGE_SIZE = 20;
const loadCalls = useCallback(
const loadEvents = useCallback(
async (after?: string | null) => {
setLoading(true);
try {
const data = await fetchQuery<WebhooksSettingsPage_callsQuery>(
const data = await fetchQuery<WebhooksSettingsPage_eventsQuery>(
environment,
webhookCallsQuery,
webhookEventsQuery,
{
webhookConfigurationId,
first: PAGE_SIZE,
@@ -332,30 +333,36 @@ function WebhookCallsDialog({
},
).toPromise();
const connection = data?.node?.calls;
const connection = data?.node?.events;
if (connection) {
const newCalls = connection.edges.map(e => e.node);
setCalls(prev => after ? [...prev, ...newCalls] : newCalls);
const newEvents = connection.edges.map(e => e.node);
setEvents(prev => after ? [...prev, ...newEvents] : newEvents);
setHasNextPage(connection.pageInfo.hasNextPage);
setEndCursor(connection.pageInfo.endCursor ?? null);
setTotalCount(connection.totalCount);
}
} catch {
toast({
title: __("Error"),
description: __("Failed to load webhook events."),
variant: "error",
});
} finally {
setLoading(false);
}
},
[environment, webhookConfigurationId],
[environment, webhookConfigurationId, toast, __],
);
useEffect(() => {
void loadCalls();
void loadEvents();
dialogRef.current?.open();
}, []); // eslint-disable-line react-hooks/exhaustive-deps
}, [loadEvents]);
return (
<Dialog
ref={dialogRef}
title={__("Webhook Calls")}
title={__("Webhook Events")}
className="max-w-2xl"
onClose={onClose}
>
@@ -368,29 +375,29 @@ function WebhookCallsDialog({
</span>
)}
</p>
{calls.length === 0 && !loading
{events.length === 0 && !loading
? (
<p className="text-sm text-txt-tertiary text-center py-8">
{__("No webhook calls recorded yet.")}
{__("No webhook events recorded yet.")}
</p>
)
: (
<div className="space-y-2">
{calls.map(call => (
{events.map(event => (
<div
key={call.id}
key={event.id}
className="border border-border-solid rounded-md p-3 space-y-1"
>
<div className="flex items-center justify-between">
<CallStatusBadge status={call.status} />
<EventStatusBadge status={event.status} />
<span className="text-xs text-txt-tertiary">
{formatDate(call.createdAt)}
{formatDate(event.createdAt)}
</span>
</div>
<p className="text-xs font-mono text-txt-secondary truncate">
{call.endpointUrl}
{event.endpointUrl}
</p>
{call.response && (
{event.response && (
<details className="text-xs">
<summary className="cursor-pointer text-txt-link hover:underline">
{__("Response")}
@@ -398,9 +405,9 @@ function WebhookCallsDialog({
<pre className="mt-1 bg-subtle p-2 rounded text-xs overflow-auto max-h-48 whitespace-pre-wrap break-all">
{(() => {
try {
return JSON.stringify(JSON.parse(call.response), null, 2);
return JSON.stringify(JSON.parse(event.response), null, 2);
} catch {
return call.response;
return event.response;
}
})()}
</pre>
@@ -420,7 +427,7 @@ function WebhookCallsDialog({
<DialogFooter>
<Button
variant="secondary"
onClick={() => void loadCalls(endCursor)}
onClick={() => void loadEvents(endCursor)}
>
{__("Load more")}
</Button>
@@ -441,7 +448,7 @@ export function WebhooksSettingsPage(props: {
const [deletingId, setDeletingId] = useState<string | null>(null);
const [revealedSecrets, setRevealedSecrets] = useState<Record<string, string>>({});
const [loadingSecrets, setLoadingSecrets] = useState<Set<string>>(new Set());
const [viewingCallsId, setViewingCallsId] = useState<string | null>(null);
const [viewingEventsId, setViewingEventsId] = useState<string | null>(null);
const fetchSigningSecret = useCallback(
async (webhookConfigurationId: string): Promise<string | null> => {
@@ -543,6 +550,9 @@ export function WebhooksSettingsPage(props: {
);
const webhooks = organization.webhookConfigurations?.edges ?? [];
const viewingEventsWebhook = viewingEventsId
? webhooks.find(e => e.node.id === viewingEventsId)?.node ?? null
: null;
const connectionId = ConnectionHandler.getConnectionID(
organization.id,
@@ -683,9 +693,9 @@ export function WebhooksSettingsPage(props: {
<div className="flex items-center gap-1 shrink-0">
<Button
variant="secondary"
onClick={() => setViewingCallsId(webhook.id)}
onClick={() => setViewingEventsId(webhook.id)}
>
{`${__("Calls")} (${webhook.calls.totalCount})`}
{`${__("Events")} (${webhook.events.totalCount})`}
</Button>
<WebhookFormDialog
mode="edit"
@@ -740,26 +750,22 @@ export function WebhooksSettingsPage(props: {
variant="danger"
onClick={() => deletingId && handleDelete(deletingId)}
disabled={isDeleting}
icon={isDeleting ? Spinner : IconTrashCan}
icon={isDeleting ? undefined : IconTrashCan}
>
{isDeleting
? __("Deleting...")
? <><Spinner size={16} /> {__("Deleting...")}</>
: __("Delete")}
</Button>
</DialogFooter>
</Dialog>
{viewingCallsId && (() => {
const webhook = webhooks.find(e => e.node.id === viewingCallsId);
if (!webhook) return null;
return (
<WebhookCallsDialog
webhookConfigurationId={viewingCallsId}
endpointUrl={webhook.node.endpointUrl}
onClose={() => setViewingCallsId(null)}
/>
);
})()}
{viewingEventsWebhook && viewingEventsId && (
<WebhookEventsDialog
webhookConfigurationId={viewingEventsId}
endpointUrl={viewingEventsWebhook.endpointUrl}
onClose={() => setViewingEventsId(null)}
/>
)}
</div>
);
}

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

View File

@@ -261,18 +261,18 @@ func (s WebhookConfigurationService) GetSigningSecret(
return wc.DecryptSigningSecret(s.svc.encryptionKey)
}
func (s WebhookConfigurationService) ListCallsForConfigurationID(
func (s WebhookConfigurationService) ListEventsForConfigurationID(
ctx context.Context,
webhookConfigurationID gid.GID,
cursor *page.Cursor[coredata.WebhookCallOrderField],
) (*page.Page[*coredata.WebhookCall, coredata.WebhookCallOrderField], error) {
var calls coredata.WebhookCalls
cursor *page.Cursor[coredata.WebhookEventOrderField],
) (*page.Page[*coredata.WebhookEvent, coredata.WebhookEventOrderField], error) {
var events coredata.WebhookEvents
err := s.svc.pg.WithConn(
ctx,
func(conn pg.Conn) error {
if err := calls.LoadByConfigurationID(ctx, conn, s.svc.scope, webhookConfigurationID, cursor); err != nil {
return fmt.Errorf("cannot load webhook calls: %w", err)
if err := events.LoadByConfigurationID(ctx, conn, s.svc.scope, webhookConfigurationID, cursor); err != nil {
return fmt.Errorf("cannot load webhook events: %w", err)
}
return nil
@@ -283,10 +283,10 @@ func (s WebhookConfigurationService) ListCallsForConfigurationID(
return nil, err
}
return page.NewPage(calls, cursor), nil
return page.NewPage(events, cursor), nil
}
func (s WebhookConfigurationService) CountCallsForConfigurationID(
func (s WebhookConfigurationService) CountEventsForConfigurationID(
ctx context.Context,
webhookConfigurationID gid.GID,
) (int, error) {
@@ -295,11 +295,11 @@ func (s WebhookConfigurationService) CountCallsForConfigurationID(
err := s.svc.pg.WithConn(
ctx,
func(conn pg.Conn) (err error) {
calls := &coredata.WebhookCalls{}
count, err = calls.CountByConfigurationID(ctx, conn, s.svc.scope, webhookConfigurationID)
events := &coredata.WebhookEvents{}
count, err = events.CountByConfigurationID(ctx, conn, s.svc.scope, webhookConfigurationID)
if err != nil {
return fmt.Errorf("cannot count webhook calls: %w", err)
return fmt.Errorf("cannot count webhook events: %w", err)
}
return nil

View File

@@ -2293,13 +2293,13 @@ type WebhookConfiguration implements Node {
createdAt: Datetime!
updatedAt: Datetime!
calls(
events(
first: Int
after: CursorKey
last: Int
before: CursorKey
orderBy: WebhookCallOrder
): WebhookCallConnection! @goField(forceResolver: true)
orderBy: WebhookEventOrder
): WebhookEventConnection! @goField(forceResolver: true)
permission(action: String!): Boolean! @goField(forceResolver: true)
}
@@ -2318,54 +2318,54 @@ type WebhookConfigurationEdge {
node: WebhookConfiguration!
}
enum WebhookCallStatus
@goModel(model: "go.probo.inc/probo/pkg/coredata.WebhookCallStatus") {
enum WebhookEventStatus
@goModel(model: "go.probo.inc/probo/pkg/coredata.WebhookEventStatus") {
SUCCEEDED
@goEnum(value: "go.probo.inc/probo/pkg/coredata.WebhookCallStatusSucceeded")
@goEnum(value: "go.probo.inc/probo/pkg/coredata.WebhookEventStatusSucceeded")
FAILED
@goEnum(value: "go.probo.inc/probo/pkg/coredata.WebhookCallStatusFailed")
@goEnum(value: "go.probo.inc/probo/pkg/coredata.WebhookEventStatusFailed")
}
enum WebhookCallOrderField
enum WebhookEventOrderField
@goModel(
model: "go.probo.inc/probo/pkg/coredata.WebhookCallOrderField"
model: "go.probo.inc/probo/pkg/coredata.WebhookEventOrderField"
) {
CREATED_AT
@goEnum(
value: "go.probo.inc/probo/pkg/coredata.WebhookCallOrderFieldCreatedAt"
value: "go.probo.inc/probo/pkg/coredata.WebhookEventOrderFieldCreatedAt"
)
}
input WebhookCallOrder
input WebhookEventOrder
@goModel(
model: "go.probo.inc/probo/pkg/server/api/console/v1/types.WebhookCallOrderBy"
model: "go.probo.inc/probo/pkg/server/api/console/v1/types.WebhookEventOrderBy"
) {
field: WebhookCallOrderField!
field: WebhookEventOrderField!
direction: OrderDirection!
}
type WebhookCall implements Node {
type WebhookEvent implements Node {
id: ID!
webhookEventId: ID!
webhookDataId: ID!
webhookConfigurationId: ID!
endpointUrl: String!
status: WebhookCallStatus!
status: WebhookEventStatus!
response: String
createdAt: Datetime!
}
type WebhookCallConnection
type WebhookEventConnection
@goModel(
model: "go.probo.inc/probo/pkg/server/api/console/v1/types.WebhookCallConnection"
model: "go.probo.inc/probo/pkg/server/api/console/v1/types.WebhookEventConnection"
) {
edges: [WebhookCallEdge!]!
edges: [WebhookEventEdge!]!
pageInfo: PageInfo!
totalCount: Int! @goField(forceResolver: true)
}
type WebhookCallEdge {
type WebhookEventEdge {
cursor: CursorKey!
node: WebhookCall!
node: WebhookEvent!
}
type StateOfApplicability implements Node {

File diff suppressed because it is too large Load Diff

View File

@@ -2623,24 +2623,6 @@ type Viewer struct {
SignableDocument *SignableDocument `json:"signableDocument,omitempty"`
}
type WebhookCall struct {
ID gid.GID `json:"id"`
WebhookEventID gid.GID `json:"webhookEventId"`
WebhookConfigurationID gid.GID `json:"webhookConfigurationId"`
EndpointURL string `json:"endpointUrl"`
Status coredata.WebhookCallStatus `json:"status"`
Response *string `json:"response,omitempty"`
CreatedAt time.Time `json:"createdAt"`
}
func (WebhookCall) IsNode() {}
func (this WebhookCall) GetID() gid.GID { return this.ID }
type WebhookCallEdge struct {
Cursor page.CursorKey `json:"cursor"`
Node *WebhookCall `json:"node"`
}
type WebhookConfiguration struct {
ID gid.GID `json:"id"`
Organization *Organization `json:"organization,omitempty"`
@@ -2649,7 +2631,7 @@ type WebhookConfiguration struct {
SelectedEvents []coredata.WebhookEventType `json:"selectedEvents"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
Calls *WebhookCallConnection `json:"calls"`
Events *WebhookEventConnection `json:"events"`
Permission bool `json:"permission"`
}
@@ -2660,3 +2642,21 @@ type WebhookConfigurationEdge struct {
Cursor page.CursorKey `json:"cursor"`
Node *WebhookConfiguration `json:"node"`
}
type WebhookEvent struct {
ID gid.GID `json:"id"`
WebhookDataID gid.GID `json:"webhookDataId"`
WebhookConfigurationID gid.GID `json:"webhookConfigurationId"`
EndpointURL string `json:"endpointUrl"`
Status coredata.WebhookEventStatus `json:"status"`
Response *string `json:"response,omitempty"`
CreatedAt time.Time `json:"createdAt"`
}
func (WebhookEvent) IsNode() {}
func (this WebhookEvent) GetID() gid.GID { return this.ID }
type WebhookEventEdge struct {
Cursor page.CursorKey `json:"cursor"`
Node *WebhookEvent `json:"node"`
}

View File

@@ -21,11 +21,11 @@ import (
)
type (
WebhookCallOrderBy OrderBy[coredata.WebhookCallOrderField]
WebhookEventOrderBy OrderBy[coredata.WebhookEventOrderField]
WebhookCallConnection struct {
WebhookEventConnection struct {
TotalCount int
Edges []*WebhookCallEdge
Edges []*WebhookEventEdge
PageInfo PageInfo
Resolver any
@@ -33,18 +33,18 @@ type (
}
)
func NewWebhookCallConnection(
p *page.Page[*coredata.WebhookCall, coredata.WebhookCallOrderField],
func NewWebhookEventConnection(
p *page.Page[*coredata.WebhookEvent, coredata.WebhookEventOrderField],
parentType any,
parentID gid.GID,
) *WebhookCallConnection {
var edges = make([]*WebhookCallEdge, len(p.Data))
) *WebhookEventConnection {
var edges = make([]*WebhookEventEdge, len(p.Data))
for i := range edges {
edges[i] = NewWebhookCallEdge(p.Data[i], p.Cursor.OrderBy.Field)
edges[i] = NewWebhookEventEdge(p.Data[i], p.Cursor.OrderBy.Field)
}
return &WebhookCallConnection{
return &WebhookEventConnection{
Edges: edges,
PageInfo: *NewPageInfo(p),
@@ -53,27 +53,27 @@ func NewWebhookCallConnection(
}
}
func NewWebhookCallEdge(wc *coredata.WebhookCall, orderBy coredata.WebhookCallOrderField) *WebhookCallEdge {
return &WebhookCallEdge{
Cursor: wc.CursorKey(orderBy),
Node: NewWebhookCall(wc),
func NewWebhookEventEdge(we *coredata.WebhookEvent, orderBy coredata.WebhookEventOrderField) *WebhookEventEdge {
return &WebhookEventEdge{
Cursor: we.CursorKey(orderBy),
Node: NewWebhookEvent(we),
}
}
func NewWebhookCall(wc *coredata.WebhookCall) *WebhookCall {
func NewWebhookEvent(we *coredata.WebhookEvent) *WebhookEvent {
var response *string
if len(wc.Response) > 0 {
s := string(wc.Response)
if len(we.Response) > 0 {
s := string(we.Response)
response = &s
}
return &WebhookCall{
ID: wc.ID,
WebhookEventID: wc.WebhookEventID,
WebhookConfigurationID: wc.WebhookConfigurationID,
EndpointURL: wc.EndpointURL,
Status: wc.Status,
return &WebhookEvent{
ID: we.ID,
WebhookDataID: we.WebhookDataID,
WebhookConfigurationID: we.WebhookConfigurationID,
EndpointURL: we.EndpointURL,
Status: we.Status,
Response: response,
CreatedAt: wc.CreatedAt,
CreatedAt: we.CreatedAt,
}
}

View File

@@ -8559,23 +8559,6 @@ func (r *viewerResolver) SignableDocument(ctx context.Context, obj *types.Viewer
}, nil
}
// TotalCount is the resolver for the totalCount field.
func (r *webhookCallConnectionResolver) TotalCount(ctx context.Context, obj *types.WebhookCallConnection) (int, error) {
if err := r.authorize(ctx, obj.ParentID, probo.ActionWebhookConfigurationGet); err != nil {
return 0, err
}
prb := r.ProboService(ctx, obj.ParentID.TenantID())
count, err := prb.WebhookConfigurations.CountCallsForConfigurationID(ctx, obj.ParentID)
if err != nil {
r.logger.ErrorCtx(ctx, "cannot count webhook calls", log.Error(err))
return 0, gqlutils.Internal(ctx)
}
return count, nil
}
// Organization is the resolver for the organization field.
func (r *webhookConfigurationResolver) Organization(ctx context.Context, obj *types.WebhookConfiguration) (*types.Organization, error) {
if err := r.authorize(ctx, obj.ID, probo.ActionOrganizationGet); err != nil {
@@ -8599,6 +8582,10 @@ func (r *webhookConfigurationResolver) Organization(ctx context.Context, obj *ty
// SigningSecret is the resolver for the signingSecret field.
func (r *webhookConfigurationResolver) SigningSecret(ctx context.Context, obj *types.WebhookConfiguration) (string, error) {
if err := r.authorize(ctx, obj.ID, probo.ActionWebhookConfigurationUpdate); err != nil {
return "", err
}
prb := r.ProboService(ctx, obj.ID.TenantID())
signingSecret, err := prb.WebhookConfigurations.GetSigningSecret(ctx, obj.ID)
@@ -8610,20 +8597,20 @@ func (r *webhookConfigurationResolver) SigningSecret(ctx context.Context, obj *t
return signingSecret, nil
}
// Calls is the resolver for the calls field.
func (r *webhookConfigurationResolver) Calls(ctx context.Context, obj *types.WebhookConfiguration, first *int, after *page.CursorKey, last *int, before *page.CursorKey, orderBy *types.WebhookCallOrderBy) (*types.WebhookCallConnection, error) {
// Events is the resolver for the events field.
func (r *webhookConfigurationResolver) Events(ctx context.Context, obj *types.WebhookConfiguration, first *int, after *page.CursorKey, last *int, before *page.CursorKey, orderBy *types.WebhookEventOrderBy) (*types.WebhookEventConnection, error) {
if err := r.authorize(ctx, obj.ID, probo.ActionWebhookConfigurationGet); err != nil {
return nil, err
}
prb := r.ProboService(ctx, obj.ID.TenantID())
pageOrderBy := page.OrderBy[coredata.WebhookCallOrderField]{
Field: coredata.WebhookCallOrderFieldCreatedAt,
pageOrderBy := page.OrderBy[coredata.WebhookEventOrderField]{
Field: coredata.WebhookEventOrderFieldCreatedAt,
Direction: page.OrderDirectionDesc,
}
if orderBy != nil {
pageOrderBy = page.OrderBy[coredata.WebhookCallOrderField]{
pageOrderBy = page.OrderBy[coredata.WebhookEventOrderField]{
Field: orderBy.Field,
Direction: orderBy.Direction,
}
@@ -8631,13 +8618,13 @@ func (r *webhookConfigurationResolver) Calls(ctx context.Context, obj *types.Web
cursor := types.NewCursor(first, after, last, before, pageOrderBy)
page, err := prb.WebhookConfigurations.ListCallsForConfigurationID(ctx, obj.ID, cursor)
page, err := prb.WebhookConfigurations.ListEventsForConfigurationID(ctx, obj.ID, cursor)
if err != nil {
r.logger.ErrorCtx(ctx, "cannot list webhook calls", log.Error(err))
r.logger.ErrorCtx(ctx, "cannot list webhook events", log.Error(err))
return nil, gqlutils.Internal(ctx)
}
return types.NewWebhookCallConnection(page, r, obj.ID), nil
return types.NewWebhookEventConnection(page, r, obj.ID), nil
}
// Permission is the resolver for the permission field.
@@ -8667,6 +8654,23 @@ func (r *webhookConfigurationConnectionResolver) TotalCount(ctx context.Context,
return 0, gqlutils.Internal(ctx)
}
// TotalCount is the resolver for the totalCount field.
func (r *webhookEventConnectionResolver) TotalCount(ctx context.Context, obj *types.WebhookEventConnection) (int, error) {
if err := r.authorize(ctx, obj.ParentID, probo.ActionWebhookConfigurationGet); err != nil {
return 0, err
}
prb := r.ProboService(ctx, obj.ParentID.TenantID())
count, err := prb.WebhookConfigurations.CountEventsForConfigurationID(ctx, obj.ParentID)
if err != nil {
r.logger.ErrorCtx(ctx, "cannot count webhook events", log.Error(err))
return 0, gqlutils.Internal(ctx)
}
return count, nil
}
// ApplicabilityStatement returns schema.ApplicabilityStatementResolver implementation.
func (r *Resolver) ApplicabilityStatement() schema.ApplicabilityStatementResolver {
return &applicabilityStatementResolver{r}
@@ -8969,11 +8973,6 @@ func (r *Resolver) VendorService() schema.VendorServiceResolver { return &vendor
// Viewer returns schema.ViewerResolver implementation.
func (r *Resolver) Viewer() schema.ViewerResolver { return &viewerResolver{r} }
// WebhookCallConnection returns schema.WebhookCallConnectionResolver implementation.
func (r *Resolver) WebhookCallConnection() schema.WebhookCallConnectionResolver {
return &webhookCallConnectionResolver{r}
}
// WebhookConfiguration returns schema.WebhookConfigurationResolver implementation.
func (r *Resolver) WebhookConfiguration() schema.WebhookConfigurationResolver {
return &webhookConfigurationResolver{r}
@@ -8984,6 +8983,11 @@ func (r *Resolver) WebhookConfigurationConnection() schema.WebhookConfigurationC
return &webhookConfigurationConnectionResolver{r}
}
// WebhookEventConnection returns schema.WebhookEventConnectionResolver implementation.
func (r *Resolver) WebhookEventConnection() schema.WebhookEventConnectionResolver {
return &webhookEventConnectionResolver{r}
}
type applicabilityStatementResolver struct{ *Resolver }
type applicabilityStatementConnectionResolver struct{ *Resolver }
type assetResolver struct{ *Resolver }
@@ -9056,6 +9060,6 @@ type vendorDataPrivacyAgreementResolver struct{ *Resolver }
type vendorRiskAssessmentResolver struct{ *Resolver }
type vendorServiceResolver struct{ *Resolver }
type viewerResolver struct{ *Resolver }
type webhookCallConnectionResolver struct{ *Resolver }
type webhookConfigurationResolver struct{ *Resolver }
type webhookConfigurationConnectionResolver struct{ *Resolver }
type webhookEventConnectionResolver struct{ *Resolver }

View File

@@ -48,17 +48,17 @@ func InsertEvent(
return fmt.Errorf("cannot marshal webhook event data: %w", err)
}
event := &coredata.WebhookEvent{
ID: gid.New(scope.GetTenantID(), coredata.WebhookEventEntityType),
webhookData := &coredata.WebhookData{
ID: gid.New(scope.GetTenantID(), coredata.WebhookDataEntityType),
OrganizationID: organizationID,
EventType: eventType,
Status: coredata.WebhookEventStatusPending,
Status: coredata.WebhookDataStatusPending,
Data: raw,
CreatedAt: time.Now(),
}
if err = event.Insert(ctx, conn, scope); err != nil {
return fmt.Errorf("cannot insert webhook event: %w", err)
if err = webhookData.Insert(ctx, conn, scope); err != nil {
return fmt.Errorf("cannot insert webhook data: %w", err)
}
return nil

View File

@@ -91,7 +91,7 @@ func (s *Sender) Run(ctx context.Context) error {
func (s *Sender) processEvents(ctx context.Context) error {
for {
event, err := s.claimNextEvent(ctx)
webhookData, err := s.claimNextWebhookData(ctx)
if err != nil {
if errors.Is(err, coredata.ErrResourceNotFound) {
return nil
@@ -99,24 +99,24 @@ func (s *Sender) processEvents(ctx context.Context) error {
return err
}
s.processEvent(ctx, event)
s.processWebhookData(ctx, webhookData)
}
}
func (s *Sender) claimNextEvent(ctx context.Context) (*coredata.WebhookEvent, error) {
var event coredata.WebhookEvent
func (s *Sender) claimNextWebhookData(ctx context.Context) (*coredata.WebhookData, error) {
var webhookData coredata.WebhookData
err := s.pg.WithTx(ctx, func(tx pg.Conn) error {
if err := event.LoadNextPendingForUpdate(ctx, tx); err != nil {
if err := webhookData.LoadNextPendingForUpdate(ctx, tx); err != nil {
return err
}
scope := coredata.NewScopeFromObjectID(event.ID)
scope := coredata.NewScopeFromObjectID(webhookData.ID)
event.Status = coredata.WebhookEventStatusProcessing
webhookData.Status = coredata.WebhookDataStatusProcessing
if err := event.UpdateStatus(ctx, tx, scope); err != nil {
return fmt.Errorf("cannot update webhook event to processing: %w", err)
if err := webhookData.UpdateStatus(ctx, tx, scope); err != nil {
return fmt.Errorf("cannot update webhook data to processing: %w", err)
}
return nil
@@ -126,77 +126,77 @@ func (s *Sender) claimNextEvent(ctx context.Context) (*coredata.WebhookEvent, er
return nil, err
}
return &event, nil
return &webhookData, nil
}
func (s *Sender) processEvent(ctx context.Context, event *coredata.WebhookEvent) {
scope := coredata.NewScopeFromObjectID(event.ID)
func (s *Sender) processWebhookData(ctx context.Context, webhookData *coredata.WebhookData) {
scope := coredata.NewScopeFromObjectID(webhookData.ID)
var configs coredata.WebhookConfigurations
err := s.pg.WithConn(ctx, func(conn pg.Conn) error {
return configs.LoadMatchingByOrganizationIDAndEventType(
ctx, conn, scope, event.OrganizationID, event.EventType,
ctx, conn, scope, webhookData.OrganizationID, webhookData.EventType,
)
})
if err != nil {
s.logger.ErrorCtx(ctx, "cannot load matching webhook configurations",
log.Error(err),
log.String("event_id", event.ID.String()),
log.String("webhook_data_id", webhookData.ID.String()),
)
return
}
for _, config := range configs {
s.deliverToConfiguration(ctx, event, config, scope)
s.deliverToConfiguration(ctx, webhookData, config, scope)
}
now := time.Now()
event.Status = coredata.WebhookEventStatusDelivered
event.ProcessedAt = &now
webhookData.Status = coredata.WebhookDataStatusDelivered
webhookData.ProcessedAt = &now
err = s.pg.WithConn(ctx, func(conn pg.Conn) error {
return event.UpdateStatus(ctx, conn, scope)
return webhookData.UpdateStatus(ctx, conn, scope)
})
if err != nil {
s.logger.ErrorCtx(ctx, "cannot update webhook event to delivered",
s.logger.ErrorCtx(ctx, "cannot update webhook data to delivered",
log.Error(err),
log.String("event_id", event.ID.String()),
log.String("webhook_data_id", webhookData.ID.String()),
)
}
}
func (s *Sender) deliverToConfiguration(
ctx context.Context,
event *coredata.WebhookEvent,
webhookData *coredata.WebhookData,
config *coredata.WebhookConfiguration,
scope coredata.Scoper,
) {
callID := gid.New(event.ID.TenantID(), coredata.WebhookCallEntityType)
eventID := gid.New(webhookData.ID.TenantID(), coredata.WebhookEventEntityType)
signingSecret, err := s.getSigningSecret(config.ID.String(), config.EncryptedSigningSecret)
if err != nil {
s.logger.ErrorCtx(ctx, "cannot get signing secret",
log.Error(err),
log.String("event_id", event.ID.String()),
log.String("webhook_data_id", webhookData.ID.String()),
log.String("configuration_id", config.ID.String()),
)
s.recordCall(ctx, callID, event, config, scope, coredata.WebhookCallStatusFailed, nil)
s.recordEvent(ctx, eventID, webhookData, config, scope, coredata.WebhookEventStatusFailed, nil)
return
}
response, sendErr := s.doHTTPCall(ctx, callID, config.EndpointURL, event, config.ID, signingSecret)
response, sendErr := s.doHTTPCall(ctx, eventID, config.EndpointURL, webhookData, config.ID, signingSecret)
callStatus := coredata.WebhookCallStatusSucceeded
eventStatus := coredata.WebhookEventStatusSucceeded
if sendErr != nil {
callStatus = coredata.WebhookCallStatusFailed
s.logger.ErrorCtx(ctx, "error delivering webhook event",
eventStatus = coredata.WebhookEventStatusFailed
s.logger.ErrorCtx(ctx, "error delivering webhook",
log.Error(sendErr),
log.String("event_id", event.ID.String()),
log.String("webhook_data_id", webhookData.ID.String()),
log.String("endpoint_url", config.EndpointURL),
)
}
s.recordCall(ctx, callID, event, config, scope, callStatus, response)
s.recordEvent(ctx, eventID, webhookData, config, scope, eventStatus, response)
}
func (s *Sender) getSigningSecret(webhookConfigurationID string, encryptedSigningSecret []byte) (string, error) {
@@ -217,20 +217,19 @@ func (s *Sender) getSigningSecret(webhookConfigurationID string, encryptedSignin
func (s *Sender) doHTTPCall(
ctx context.Context,
callID gid.GID,
eventID gid.GID,
endpointURL string,
event *coredata.WebhookEvent,
webhookData *coredata.WebhookData,
configurationID gid.GID,
signingSecret string,
) (json.RawMessage, error) {
payload := map[string]any{
"eventId": event.ID.String(),
"callId": callID.String(),
"eventId": eventID.String(),
"configurationId": configurationID.String(),
"organizationId": event.OrganizationID.String(),
"eventType": event.EventType.String(),
"createdAt": event.CreatedAt,
"data": event.Data,
"organizationId": webhookData.OrganizationID.String(),
"eventType": webhookData.EventType.String(),
"createdAt": webhookData.CreatedAt,
"data": webhookData.Data,
}
body, err := json.Marshal(payload)
@@ -250,7 +249,7 @@ func (s *Sender) doHTTPCall(
signature := computeSignature(signingSecret, timestamp, body)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Probo-Webhook-Event", event.EventType.String())
req.Header.Set("X-Probo-Webhook-Event", webhookData.EventType.String())
req.Header.Set("X-Probo-Webhook-Timestamp", timestamp)
req.Header.Set("X-Probo-Webhook-Signature", signature)
@@ -275,18 +274,18 @@ func (s *Sender) doHTTPCall(
}
}
func (s *Sender) recordCall(
func (s *Sender) recordEvent(
ctx context.Context,
callID gid.GID,
event *coredata.WebhookEvent,
eventID gid.GID,
webhookData *coredata.WebhookData,
config *coredata.WebhookConfiguration,
scope coredata.Scoper,
status coredata.WebhookCallStatus,
status coredata.WebhookEventStatus,
response json.RawMessage,
) {
call := coredata.WebhookCall{
ID: callID,
WebhookEventID: event.ID,
event := coredata.WebhookEvent{
ID: eventID,
WebhookDataID: webhookData.ID,
WebhookConfigurationID: config.ID,
EndpointURL: config.EndpointURL,
Status: status,
@@ -295,12 +294,12 @@ func (s *Sender) recordCall(
}
err := s.pg.WithConn(ctx, func(conn pg.Conn) error {
return call.Insert(ctx, conn, scope)
return event.Insert(ctx, conn, scope)
})
if err != nil {
s.logger.ErrorCtx(ctx, "cannot insert webhook call",
s.logger.ErrorCtx(ctx, "cannot insert webhook event",
log.Error(err),
log.String("event_id", event.ID.String()),
log.String("webhook_data_id", webhookData.ID.String()),
log.String("configuration_id", config.ID.String()),
)
}