Files
payments/internal/models/bank_accounts.go
fgueryandGitHub a63e3a20a8 feat(EN-214): create events via storage instead of manually calling event activity (#594)
* feat(EN-214): add outbox table migration

* feat(EN-214): add outbox models and storage layer

* feat(EN-214): implement outbox publisher activity

* feat(EN-214): implement outbox schedule activity

* feat(EN-214): add outbox publisher workflow

* feat(EN-214): migrate accounts to outbox pattern

* feat(EN-214): migrate balances to outbox pattern

* feat(EN-214): migrate payments to outbox pattern

* feat(EN-214): migrate bank accounts to outbox pattern

* feat(EN-214): migrate pools to outbox pattern

* feat(EN-214): migrate payment initiations to outbox pattern

* feat(EN-214): migrate PSU and connections to outbox pattern

* feat(EN-214): migrate connectors to outbox pattern

* feat(EN-214): remove legacy send_events workflow

* feat(EN-214): update workflows to use outbox pattern

* feat(EN-214): improve outbox idempotency and error handling

* feat(EN-214): update storage tasks and tests

* test(EN-214): update e2e tests for outbox pattern

* test(EN-214): clean up workflow tests

* chore(EN-214): update docker compose configuration

* feat(EN-214): add e2e run config in ide

* fix: add back docker-compose-dev run config

* chore: improve connector uninstall comments & remove connector id being passed

* fix: ensure events are not pushed if balances are not created

* fix: ensure err is not overwritten when using rollback

* fix: ensure we use v3 provider format

* chore: apply the nitpick comments from coderabbit

* chore: consistentely use e() error handling

* feat: optionally disable publisher schedule to make e2e test more consistent

* fix: improve error handling on uninstall connector

* avoid using custom payload and rely on existing interfaces; also avoid using different types for events

* fix: remove unecessary unmarshalling

* fix: ensure that rollbackOnTxError picks up errors on tx.Commit()

* fix: add extra validation on iban & account number in the test

* test: cover marshall/unmarshalling of event payload

* fix: increase test timeout

* try to make test more resiliant

* fix: wrong pointer use in loop

* fix: error formatting issue

* fix: move oubtox schedule creation out of an activity and do it in the worker itself

* feat: OutboxEventDeleteAndSend as batch

* change outbox_event to have id only instead of idempotency key

* make e2e test less brittle, save eventID as idempotency key in publication

* feat: make outbox_event keep an history

* feat(EN-214): added a schedule to cleanup outbox events

* test: make cleanup actually clean

* ignore linting rules for now

* feat: do not add psu metadata to event anymore as it's irrelevant

* fix: create local variable instead of relying on pointers, even if not 100% necessary

* fix: remove temporal schedule describe calls
2025-11-24 16:05:42 +00:00

154 lines
5.3 KiB
Go

package models
import (
"errors"
"fmt"
"strings"
"time"
"github.com/formancehq/go-libs/v3/pointer"
"github.com/google/uuid"
)
const (
bankAccountOwnerNamespace = formanceMetadataSpecNamespace + "owner/"
// Bank Account metadata
BankAccountOwnerAddressLine1MetadataKey = bankAccountOwnerNamespace + "addressLine1"
BankAccountOwnerAddressLine2MetadataKey = bankAccountOwnerNamespace + "addressLine2"
BankAccountOwnerStreetNameMetadataKey = bankAccountOwnerNamespace + "streetName"
BankAccountOwnerStreetNumberMetadataKey = bankAccountOwnerNamespace + "streetNumber"
BankAccountOwnerCityMetadataKey = bankAccountOwnerNamespace + "city"
BankAccountOwnerRegionMetadataKey = bankAccountOwnerNamespace + "region"
BankAccountOwnerPostalCodeMetadataKey = bankAccountOwnerNamespace + "postalCode"
BankAccountOwnerEmailMetadataKey = bankAccountOwnerNamespace + "email"
BankAccountOwnerPhoneNumberMetadataKey = bankAccountOwnerNamespace + "phoneNumber"
// Account metadata
AccountIBANMetadataKey = bankAccountOwnerNamespace + "iban"
AccountAccountNumberMetadataKey = bankAccountOwnerNamespace + "accountNumber"
AccountBankAccountNameMetadataKey = bankAccountOwnerNamespace + "name"
AccountBankAccountCountryMetadataKey = bankAccountOwnerNamespace + "country"
AccountSwiftBicCodeMetadataKey = bankAccountOwnerNamespace + "swiftBicCode"
)
type BankAccount struct {
ID uuid.UUID `json:"id"`
CreatedAt time.Time `json:"createdAt"`
Name string `json:"name"`
AccountNumber *string `json:"accountNumber"`
IBAN *string `json:"iban"`
SwiftBicCode *string `json:"swiftBicCode"`
Country *string `json:"country"`
Metadata map[string]string `json:"metadata"`
RelatedAccounts []BankAccountRelatedAccount `json:"relatedAccounts"`
}
type bankAccountIK struct {
ID uuid.UUID `json:"id"`
LastAccountID *AccountID `json:"lastAccountID,omitempty"`
MetadataHash string `json:"metadataHash,omitempty"`
}
func (b *BankAccount) IdempotencyKey() string {
ik := bankAccountIK{
ID: b.ID,
}
if len(b.RelatedAccounts) > 0 {
ik.LastAccountID = &b.RelatedAccounts[len(b.RelatedAccounts)-1].AccountID
}
if len(b.Metadata) > 0 {
//hash the metadata in ik.MetadataHash
ik.MetadataHash = IdempotencyKey(b.Metadata)
}
return IdempotencyKey(ik)
}
func (a *BankAccount) Obfuscate() error {
if a.IBAN != nil {
length := len(*a.IBAN)
if length < 8 {
return errors.New("IBAN is not valid")
}
*a.IBAN = (*a.IBAN)[:4] + strings.Repeat("*", length-8) + (*a.IBAN)[length-4:]
}
if a.AccountNumber != nil {
length := len(*a.AccountNumber)
if length < 5 {
return errors.New("Account number is not valid")
}
*a.AccountNumber = (*a.AccountNumber)[:2] + strings.Repeat("*", length-5) + (*a.AccountNumber)[length-3:]
}
return nil
}
func FillBankAccountDetailsToAccountMetadata(account *Account, bankAccount *BankAccount) {
if account.Metadata == nil {
account.Metadata = make(map[string]string)
}
account.Metadata[BankAccountOwnerAddressLine1MetadataKey] = bankAccount.Metadata[BankAccountOwnerAddressLine1MetadataKey]
account.Metadata[BankAccountOwnerAddressLine2MetadataKey] = bankAccount.Metadata[BankAccountOwnerAddressLine2MetadataKey]
account.Metadata[BankAccountOwnerCityMetadataKey] = bankAccount.Metadata[BankAccountOwnerCityMetadataKey]
account.Metadata[BankAccountOwnerRegionMetadataKey] = bankAccount.Metadata[BankAccountOwnerRegionMetadataKey]
account.Metadata[BankAccountOwnerPostalCodeMetadataKey] = bankAccount.Metadata[BankAccountOwnerPostalCodeMetadataKey]
if bankAccount.AccountNumber != nil {
account.Metadata[AccountAccountNumberMetadataKey] = *bankAccount.AccountNumber
}
if bankAccount.IBAN != nil {
account.Metadata[AccountIBANMetadataKey] = *bankAccount.IBAN
}
if bankAccount.SwiftBicCode != nil {
account.Metadata[AccountSwiftBicCodeMetadataKey] = *bankAccount.SwiftBicCode
}
if bankAccount.Country != nil {
account.Metadata[AccountBankAccountCountryMetadataKey] = *bankAccount.Country
}
account.Metadata[AccountBankAccountNameMetadataKey] = bankAccount.Name
}
func FillBankAccountMetadataWithPaymentServiceUserInfo(ba *BankAccount, psu *PaymentServiceUser) {
if psu.Address != nil {
var addressLine1 *string
switch {
case psu.Address.StreetNumber != nil && psu.Address.StreetName != nil:
addressLine1 = pointer.For(fmt.Sprintf("%s %s", *psu.Address.StreetNumber, *psu.Address.StreetName))
case psu.Address.StreetName != nil:
addressLine1 = psu.Address.StreetName
}
fillMetadata(ba.Metadata, BankAccountOwnerAddressLine1MetadataKey, addressLine1)
fillMetadata(ba.Metadata, BankAccountOwnerStreetNameMetadataKey, psu.Address.StreetName)
fillMetadata(ba.Metadata, BankAccountOwnerStreetNumberMetadataKey, psu.Address.StreetNumber)
fillMetadata(ba.Metadata, BankAccountOwnerCityMetadataKey, psu.Address.City)
fillMetadata(ba.Metadata, BankAccountOwnerRegionMetadataKey, psu.Address.Region)
fillMetadata(ba.Metadata, BankAccountOwnerPostalCodeMetadataKey, psu.Address.PostalCode)
}
if psu.ContactDetails != nil {
fillMetadata(ba.Metadata, BankAccountOwnerEmailMetadataKey, psu.ContactDetails.Email)
fillMetadata(ba.Metadata, BankAccountOwnerPhoneNumberMetadataKey, psu.ContactDetails.PhoneNumber)
}
}
func fillMetadata(metadata map[string]string, key string, value *string) {
if value != nil {
metadata[key] = *value
}
}