Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(payments): Atlar connector: Add more bank details to Accounts (internal) and Payments #1607

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ func ExternalAccountFromAtlarData(
}, nil
}

func ExtractAccountMetadata(account *atlar_models.Account) metadata.Metadata {
func ExtractAccountMetadata(account *atlar_models.Account, bank *atlar_models.ThirdParty) metadata.Metadata {
result := metadata.Metadata{}
result = result.Merge(ComputeAccountMetadataBool("fictive", account.Fictive))
result = result.Merge(ComputeAccountMetadata("bank/id", account.Bank.ID))
result = result.Merge(ComputeAccountMetadata("bank/name", account.Bank.Name))
result = result.Merge(ComputeAccountMetadata("bank/id", bank.ID))
result = result.Merge(ComputeAccountMetadata("bank/name", bank.Name))
result = result.Merge(ComputeAccountMetadata("bank/bic", account.Bank.Bic))
result = result.Merge(IdentifiersToMetadata(account.Identifiers))
result = result.Merge(ComputeAccountMetadata("alias", account.Alias))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ import (
"github.com/get-momo/atlar-v1-go-client/client/accounts"
)

func (c *Client) GetV1AccountsID(ctx context.Context, id string) (*accounts.GetV1AccountsIDOK, error) {
f := connectors.ClientMetrics(ctx, "atlar", "list_accounts")
now := time.Now()
defer f(ctx, now)

accountsParams := accounts.GetV1AccountsIDParams{
Context: ctx,
ID: id,
}

return c.client.Accounts.GetV1AccountsID(&accountsParams)
}

func (c *Client) GetV1Accounts(ctx context.Context, token string, pageSize int64) (*accounts.GetV1AccountsOK, error) {
f := connectors.ClientMetrics(ctx, "atlar", "list_accounts")
now := time.Now()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package client

import (
"context"
"time"

"github.com/formancehq/payments/cmd/connectors/internal/connectors"
"github.com/get-momo/atlar-v1-go-client/client/third_parties"
)

func (c *Client) GetV1BetaThirdPartiesID(ctx context.Context, id string) (*third_parties.GetV1betaThirdPartiesIDOK, error) {
f := connectors.ClientMetrics(ctx, "atlar", "list_third_parties")
now := time.Now()
defer f(ctx, now)

params := third_parties.GetV1betaThirdPartiesIDParams{
Context: ctx,
ID: id,
}

return c.client.ThirdParties.GetV1betaThirdPartiesID(&params)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import "github.com/formancehq/payments/cmd/connectors/internal/connectors/curren
var (
supportedCurrenciesWithDecimal = map[string]int{
"EUR": currency.ISO4217Currencies["EUR"], // Euro
"DKK": currency.ISO4217Currencies["DKK"],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need the DKK currency, because there is only one bank for the EUR currency in the Atlar default sandbox. After asking them for a second EUR bank, they asked us if we could use a DKK accounts for testing multi banking.

}
)
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func FetchAccountsTask(config Config, client *client.Client) task.Task {

token = pagedAccounts.Payload.NextToken

if err := ingestAccountsBatch(ctx, connectorID, ingester, pagedAccounts); err != nil {
if err := ingestAccountsBatch(ctx, connectorID, taskID, ingester, pagedAccounts, client); err != nil {
otel.RecordError(span, err)
return err
}
Expand Down Expand Up @@ -107,9 +107,19 @@ func FetchAccountsTask(config Config, client *client.Client) task.Task {
func ingestAccountsBatch(
ctx context.Context,
connectorID models.ConnectorID,
taskID models.TaskID,
ingester ingestion.Ingester,
pagedAccounts *accounts.GetV1AccountsOK,
client *client.Client,
) error {
ctx, span := connectors.StartSpan(
ctx,
"atlar.taskFetchAccounts.ingestAccountsBatch",
attribute.String("connectorID", connectorID.String()),
attribute.String("taskID", taskID.String()),
)
defer span.End()

accountsBatch := ingestion.AccountBatch{}
balanceBatch := ingestion.BalanceBatch{}

Expand All @@ -124,6 +134,14 @@ func ingestAccountsBatch(
return fmt.Errorf("failed to parse opening date: %w", err)
}

requestCtx, cancel := contextutil.DetachedWithTimeout(ctx, 30*time.Second)
defer cancel()
thirdPartyResponse, err := client.GetV1BetaThirdPartiesID(requestCtx, account.ThirdPartyID)
if err != nil {
otel.RecordError(span, err)
return err
}

accountsBatch = append(accountsBatch, &models.Account{
ID: models.AccountID{
Reference: *account.ID,
Expand All @@ -135,7 +153,7 @@ func ingestAccountsBatch(
DefaultAsset: currency.FormatAsset(supportedCurrenciesWithDecimal, account.Currency),
AccountName: account.Name,
Type: models.AccountTypeInternal,
Metadata: ExtractAccountMetadata(account),
Metadata: ExtractAccountMetadata(account, thirdPartyResponse.Payload),
RawData: raw,
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func FetchTransactionsTask(config Config, client *client.Client) task.Task {

token = pagedTransactions.Payload.NextToken

if err := ingestPaymentsBatch(ctx, connectorID, ingester, pagedTransactions); err != nil {
if err := ingestPaymentsBatch(ctx, connectorID, taskID, ingester, client, pagedTransactions); err != nil {
otel.RecordError(span, err)
return err
}
Expand All @@ -66,13 +66,15 @@ func FetchTransactionsTask(config Config, client *client.Client) task.Task {
func ingestPaymentsBatch(
ctx context.Context,
connectorID models.ConnectorID,
taskID models.TaskID,
ingester ingestion.Ingester,
client *client.Client,
pagedTransactions *transactions.GetV1TransactionsOK,
) error {
batch := ingestion.PaymentBatch{}

for _, item := range pagedTransactions.Payload.Items {
batchElement, err := atlarTransactionToPaymentBatchElement(connectorID, item)
batchElement, err := atlarTransactionToPaymentBatchElement(ctx, connectorID, taskID, item, client)
if err != nil {
return err
}
Expand All @@ -91,9 +93,20 @@ func ingestPaymentsBatch(
}

func atlarTransactionToPaymentBatchElement(
ctx context.Context,
connectorID models.ConnectorID,
taskID models.TaskID,
transaction *atlar_models.Transaction,
client *client.Client,
) (*ingestion.PaymentBatchElement, error) {
ctx, span := connectors.StartSpan(
ctx,
"atlar.atlarTransactionToPaymentBatchElement",
attribute.String("connectorID", connectorID.String()),
attribute.String("taskID", taskID.String()),
)
defer span.End()
Comment on lines +102 to +108
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope creating instead of passing a span is the correct thing to do here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep it's good, it's gonna be linked to the parent span thanks to the StartSpan function, so LGTM


if _, ok := supportedCurrenciesWithDecimal[*transaction.Amount.Currency]; !ok {
// Discard transactions with unsupported currencies
return nil, nil
Expand All @@ -117,6 +130,22 @@ func atlarTransactionToPaymentBatchElement(
return nil, err
}

requestCtx, cancel := contextutil.DetachedWithTimeout(ctx, 30*time.Second)
defer cancel()
accountResponse, err := client.GetV1AccountsID(requestCtx, *transaction.Account.ID)
if err != nil {
otel.RecordError(span, err)
return nil, err
}

requestCtx, cancel = contextutil.DetachedWithTimeout(ctx, 30*time.Second)
defer cancel()
thirdPartyResponse, err := client.GetV1BetaThirdPartiesID(requestCtx, *&accountResponse.Payload.ThirdPartyID)
if err != nil {
otel.RecordError(span, err)
return nil, err
}

paymentId := models.PaymentID{
PaymentReference: models.PaymentReference{
Reference: transaction.ID,
Expand All @@ -137,7 +166,7 @@ func atlarTransactionToPaymentBatchElement(
Amount: amount,
InitialAmount: amount,
Asset: currency.FormatAsset(supportedCurrenciesWithDecimal, *transaction.Amount.Currency),
Metadata: ExtractPaymentMetadata(paymentId, transaction),
Metadata: ExtractPaymentMetadata(paymentId, transaction, accountResponse.Payload, thirdPartyResponse.Payload),
RawData: raw,
},
}
Expand Down Expand Up @@ -197,7 +226,7 @@ func determinePaymentScheme(item *atlar_models.Transaction) models.PaymentScheme
return models.PaymentSchemeSepa
}

func ExtractPaymentMetadata(paymentId models.PaymentID, transaction *atlar_models.Transaction) []*models.PaymentMetadata {
func ExtractPaymentMetadata(paymentId models.PaymentID, transaction *atlar_models.Transaction, account *atlar_models.Account, bank *atlar_models.ThirdParty) []*models.PaymentMetadata {
result := []*models.PaymentMetadata{}
if transaction.Date != "" {
result = append(result, ComputePaymentMetadata(paymentId, "date", transaction.Date))
Expand All @@ -207,6 +236,9 @@ func ExtractPaymentMetadata(paymentId models.PaymentID, transaction *atlar_model
}
result = append(result, ComputePaymentMetadata(paymentId, "remittanceInformation/type", *transaction.RemittanceInformation.Type))
result = append(result, ComputePaymentMetadata(paymentId, "remittanceInformation/value", *transaction.RemittanceInformation.Value))
result = append(result, ComputePaymentMetadata(paymentId, "bank/id", bank.ID))
result = append(result, ComputePaymentMetadata(paymentId, "bank/name", bank.Name))
result = append(result, ComputePaymentMetadata(paymentId, "bank/bic", account.Bank.Bic))
result = append(result, ComputePaymentMetadata(paymentId, "btc/domain", transaction.Characteristics.BankTransactionCode.Domain))
result = append(result, ComputePaymentMetadata(paymentId, "btc/family", transaction.Characteristics.BankTransactionCode.Family))
result = append(result, ComputePaymentMetadata(paymentId, "btc/subfamily", transaction.Characteristics.BankTransactionCode.Subfamily))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ func UpdatePaymentStatusTask(
err = ingestAtlarTransaction(ctx,
ingester,
connectorID,
taskID,
client,
getCreditTransferResponse.Payload.Reconciliation.BookedTransactionID,
)
Expand Down Expand Up @@ -356,16 +357,19 @@ func ingestAtlarTransaction(
ctx context.Context,
ingester ingestion.Ingester,
connectorID models.ConnectorID,
taskID models.TaskID,
client *client.Client,
transactionId string,
) error {

transactionResponse, err := client.GetV1TransactionsID(ctx, transactionId)
requestCtx, cancel := contextutil.DetachedWithTimeout(ctx, 30*time.Second)
defer cancel()
transactionResponse, err := client.GetV1TransactionsID(requestCtx, transactionId)
if err != nil {
return err
}

batchElement, err := atlarTransactionToPaymentBatchElement(connectorID, transactionResponse.Payload)
batchElement, err := atlarTransactionToPaymentBatchElement(ctx, connectorID, taskID, transactionResponse.Payload, client)
if err != nil {
return err
}
Expand Down
Loading