mirror of
https://github.com/hanzo-fi/payments.git
synced 2026-10-30 20:31:20 +00:00
* Set webhook ID as payment adjustment ID when adjustment created via webhook * Fetch Column payments oldest to newest * Ensure that webhook secrets are fetched when containers restart * Regenerate plugin code for tests * Add paging to fetch webhook request * Add WorkerPool unit test to increase test coverage * Guard against unhandled webhooks in Column plugin initiation * Rename Timeline.LatestID to Timeline.LastSeenID for clarity * Add metadata column to webhooks_configs table * Persist webhook_configs.metadata * Persist Column webhook secrets as metadata in webhooks_configs * Remove PSPWebhookConfig from InstallResponse * Stop passing context to initWebhookConfig() * Don't pass context to RegisterPlugin func * Don't pass context to plugins.RegisterPlugin
106 lines
2.8 KiB
Go
106 lines
2.8 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/formancehq/payments/internal/models"
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
type webhookConfig struct {
|
|
bun.BaseModel `bun:"table:webhooks_configs"`
|
|
|
|
// Mandatory fields
|
|
Name string `bun:"name,pk,type:text,notnull"`
|
|
ConnectorID models.ConnectorID `bun:"connector_id,pk,type:character varying,notnull"`
|
|
URLPath string `bun:"url_path,type:text,notnull"`
|
|
|
|
// Optional fields with default
|
|
// c.f. https://bun.uptrace.dev/guide/models.html#default
|
|
Metadata map[string]string `bun:"metadata,type:jsonb,nullzero,notnull,default:'{}'"`
|
|
}
|
|
|
|
func (s *store) WebhooksConfigsUpsert(ctx context.Context, webhooksConfigs []models.WebhookConfig) error {
|
|
toInsert := fromWebhooksConfigsModels(webhooksConfigs)
|
|
|
|
_, err := s.db.NewInsert().
|
|
Model(&toInsert).
|
|
On("CONFLICT (name, connector_id) DO NOTHING").
|
|
Exec(ctx)
|
|
if err != nil {
|
|
return e("upsert webhook config", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *store) WebhooksConfigsGet(ctx context.Context, name string, connectorID models.ConnectorID) (*models.WebhookConfig, error) {
|
|
var webhookConfig webhookConfig
|
|
err := s.db.NewSelect().
|
|
Model(&webhookConfig).
|
|
Where("name = ? AND connector_id = ?", name, connectorID).
|
|
Scan(ctx)
|
|
if err != nil {
|
|
return nil, e("get webhook config", err)
|
|
}
|
|
|
|
return toWebhookConfigModel(webhookConfig), nil
|
|
}
|
|
|
|
func (s *store) WebhooksConfigsGetFromConnectorID(ctx context.Context, connectorID models.ConnectorID) ([]models.WebhookConfig, error) {
|
|
var webhookConfigs []webhookConfig
|
|
err := s.db.NewSelect().
|
|
Model(&webhookConfigs).
|
|
Where("connector_id = ?", connectorID).
|
|
Scan(ctx)
|
|
if err != nil {
|
|
return nil, e("get webhook configs", err)
|
|
}
|
|
|
|
res := make([]models.WebhookConfig, 0, len(webhookConfigs))
|
|
for _, webhookConfig := range webhookConfigs {
|
|
res = append(res, *toWebhookConfigModel(webhookConfig))
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
func (s *store) WebhooksConfigsDeleteFromConnectorID(ctx context.Context, connectorID models.ConnectorID) error {
|
|
_, err := s.db.NewDelete().
|
|
Model((*webhookConfig)(nil)).
|
|
Where("connector_id = ?", connectorID).
|
|
Exec(ctx)
|
|
if err != nil {
|
|
return e("delete webhook config", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func fromWebhookConfigModels(from models.WebhookConfig) webhookConfig {
|
|
return webhookConfig{
|
|
Name: from.Name,
|
|
ConnectorID: from.ConnectorID,
|
|
URLPath: from.URLPath,
|
|
Metadata: from.Metadata,
|
|
}
|
|
}
|
|
|
|
func fromWebhooksConfigsModels(from []models.WebhookConfig) []webhookConfig {
|
|
to := make([]webhookConfig, 0, len(from))
|
|
for _, webhookConfig := range from {
|
|
to = append(to, fromWebhookConfigModels(webhookConfig))
|
|
}
|
|
|
|
return to
|
|
}
|
|
|
|
func toWebhookConfigModel(from webhookConfig) *models.WebhookConfig {
|
|
return &models.WebhookConfig{
|
|
Name: from.Name,
|
|
ConnectorID: from.ConnectorID,
|
|
URLPath: from.URLPath,
|
|
Metadata: from.Metadata,
|
|
}
|
|
}
|