Files
fgueryandQuentin David a77f1fabef fix(EN-549): remove config and connector dependencies (#622)
* chore: rename RunNextTasks workflow

* feat: added a trimmed down type for connector only containing the id

* feat: make runNextTask use connectorIdOnly

* feat: added isScheduledForDeletion to plugin interface

* feat: add scheduledForDeletion at plugin level

* feat: refactor manager.Load to take a connector instead of parts of it

* feat: saves the connector's scheduledFOrDeletion into the plugin

* feat: use plugin.IsScheduledForDeletion instead of loading connector

* fix: make e2e tests less flaky

* fix: remove config from most request types

* feat: (WIP) remove config from runNextTask

* fix: remove unneeded method definitions in each plugin

* fix: simplify RunNextFlow as anyway we need to change the parameters

* fix: add a workflow compatible with previous version for runNextTasks

* fix(EN-577): added back RunSendEvents as a deprecated workflow

* fix(EN-577): added back RunUpdatePaymentInitiationFromPayment as a deprecated workflow

* fix: fix linting directives

* fix: replace old usage with refactored one

* fix: added back missing deprecated activities

* fix: ensure fetch_next_balance works with v3.0 code

* fix: simplify a bit the versionning etc

* fix: quick refactor before applying solution elsewhere

* fix: add back RunSendEvents and RunNextTask child workflows where necessary for backward compatibility

* fix: add back missing activity, seems like it's mandatory?

* fix: run payment initiation update as child workflow when necessary

* fix: missing activy call in test, refine some todos

* fix: lint

* fix: ensure we always reload plugins into the manager -- so exposing isScheduledForDeletion

* fix: ensure we stop storing clear config in temporal

* fix: make store config support plain text config

* fix: some comments

* fix: change runSendEvent workflow to be match what we had in 3.0

* feat: enable worker versionning to avoid workflow from new version to get executed on old code

* fix: lint

* fix: revert worker versionning

* fix: minor code review comments

* fix: add context to Encrypt & Decrypt methods

* fix: improve lint, add couple tests

* fix: use atomic to handle isScheduledForDeletion

* fix: make create_events work on migration

* fix: avoid returning channel; just send what is needed in runSendEventAsChildWorkflow and runNextTaskAsChildWorkflow

* fix: remove extra call to installWorkflow on start plugin

* fix: typo in error log line

* fix: simplify errChan initialization

* fix: clarify feature flag names
2026-01-19 14:35:36 +01:00

161 lines
4.9 KiB
Go

package activities
import (
"context"
"fmt"
"time"
"github.com/formancehq/payments/internal/models"
"github.com/google/uuid"
"go.temporal.io/sdk/workflow"
)
type SendEventsPayment struct {
Payment models.Payment
Adjustment models.PaymentAdjustment
}
type SendEventsRequest struct {
ConnectorID *models.ConnectorID
IdempotencyKey string
At time.Time
Account *models.Account
Balance *models.Balance
BankAccount *models.BankAccount
Payment *SendEventsPayment
PaymentDeleted *models.PaymentID
ConnectorReset *models.ConnectorID
PoolsCreation *models.Pool
PoolsDeletion *uuid.UUID
PaymentInitiation *models.PaymentInitiation
PaymentInitiationAdjustment *models.PaymentInitiationAdjustment
PaymentInitiationRelatedPayment *models.PaymentInitiationRelatedPayments
UserPendingDisconnect *models.UserConnectionPendingDisconnect
UserDisconnected *models.UserDisconnected
UserConnectionDisconnected *models.UserConnectionDisconnected
UserConnectionReconnected *models.UserConnectionReconnected
UserLinkStatus *models.UserLinkSessionFinished
UserConnectionDataSynced *models.UserConnectionDataSynced
Task *models.Task
}
// SendEvents
// Deprecated: from 3.1 we're using outbox pattern, this is kept for backward compatibility
func (a Activities) SendEvents(ctx context.Context, req SendEventsRequest) error {
eventID := models.EventID{
EventIdempotencyKey: req.IdempotencyKey,
ConnectorID: req.ConnectorID,
}
isExisting, err := a.storage.EventsSentExists(ctx, eventID)
if err != nil {
return temporalStorageError(err)
}
if isExisting {
// event was already sent; nothing to do
return nil
}
// event was not sent yet
if err := a.sendEvents(ctx, req); err != nil {
return err
}
if err := a.storage.EventsSentUpsert(ctx, models.EventSent{
ID: eventID,
ConnectorID: req.ConnectorID,
SentAt: req.At,
}); err != nil {
return temporalStorageError(err)
}
return nil
}
func (a Activities) sendEvents(ctx context.Context, req SendEventsRequest) error {
if req.Account != nil {
return a.events.Publish(ctx, a.events.NewEventSavedAccounts(*req.Account))
}
if req.Balance != nil {
return a.events.Publish(ctx, a.events.NewEventSavedBalances(*req.Balance))
}
if req.BankAccount != nil {
ba, err := a.events.NewEventSavedBankAccounts(*req.BankAccount)
if err != nil {
return fmt.Errorf("failed to send bank account: %w", err)
}
return a.events.Publish(ctx, ba)
}
if req.Payment != nil {
return a.events.Publish(ctx, a.events.NewEventSavedPayments(req.Payment.Payment, req.Payment.Adjustment))
}
if req.PaymentDeleted != nil {
return a.events.Publish(ctx, a.events.NewEventPaymentDeleted(*req.PaymentDeleted))
}
if req.ConnectorReset != nil {
return a.events.Publish(ctx, a.events.NewEventResetConnector(*req.ConnectorReset, req.At))
}
if req.PoolsCreation != nil {
return a.events.Publish(ctx, a.events.NewEventSavedPool(*req.PoolsCreation))
}
if req.PoolsDeletion != nil {
return a.events.Publish(ctx, a.events.NewEventDeletePool(*req.PoolsDeletion))
}
if req.PaymentInitiation != nil {
return a.events.Publish(ctx, a.events.NewEventSavedPaymentInitiation(*req.PaymentInitiation))
}
if req.PaymentInitiationAdjustment != nil {
return a.events.Publish(ctx, a.events.NewEventSavedPaymentInitiationAdjustment(*req.PaymentInitiationAdjustment))
}
if req.PaymentInitiationRelatedPayment != nil {
return a.events.Publish(ctx, a.events.NewEventSavedPaymentInitiationRelatedPayment(*req.PaymentInitiationRelatedPayment))
}
if req.UserPendingDisconnect != nil {
return a.events.Publish(ctx, a.events.NewEventOpenBankingUserPendingDisconnect(*req.UserPendingDisconnect))
}
if req.UserDisconnected != nil {
return a.events.Publish(ctx, a.events.NewEventOpenBankingUserDisconnected(*req.UserDisconnected))
}
if req.UserConnectionDisconnected != nil {
return a.events.Publish(ctx, a.events.NewEventOpenBankingUserConnectionDisconnected(*req.UserConnectionDisconnected))
}
if req.UserConnectionReconnected != nil {
return a.events.Publish(ctx, a.events.NewEventOpenBankingUserConnectionReconnected(*req.UserConnectionReconnected))
}
if req.UserLinkStatus != nil {
return a.events.Publish(ctx, a.events.NewEventOpenBankingUserLinkStatus(*req.UserLinkStatus))
}
if req.UserConnectionDataSynced != nil {
return a.events.Publish(ctx, a.events.NewEventOpenBankingUserConnectionDataSynced(*req.UserConnectionDataSynced))
}
if req.Task != nil {
return a.events.Publish(ctx, a.events.NewEventUpdatedTask(*req.Task))
}
return nil
}
var SendEventsActivity = Activities{}.SendEvents
func SendEvents(ctx workflow.Context, req SendEventsRequest) error {
return executeActivity(ctx, SendEventsActivity, nil, req)
}