Skip to content

Commit

Permalink
feat(payments/transferinitiations): store reference + list by referen…
Browse files Browse the repository at this point in the history
…ce (#1740)

Co-authored-by: Crimson <laouji@users.noreply.github.com>
  • Loading branch information
paul-nicolas and laouji authored Dec 2, 2024
1 parent 2a41dd3 commit e1b8eb0
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (s *Storage) GetTransferInitiation(ctx context.Context, id models.TransferI
var transferInitiation models.TransferInitiation

query := s.db.NewSelect().
Column("id", "connector_id", "created_at", "scheduled_at", "description", "type", "source_account_id", "destination_account_id", "provider", "initial_amount", "amount", "asset", "metadata").
Column("id", "reference", "connector_id", "created_at", "scheduled_at", "description", "type", "source_account_id", "destination_account_id", "provider", "initial_amount", "amount", "asset", "metadata").
Model(&transferInitiation).
Relation("RelatedAdjustments").
Where("id = ?", id)
Expand Down Expand Up @@ -68,7 +68,7 @@ func (s *Storage) ListTransferInitiations(ctx context.Context, q ListTransferIni
(*bunpaginate.OffsetPaginatedQuery[PaginatedQueryOptions[TransferInitiationQuery]])(&q),
func(query *bun.SelectQuery) *bun.SelectQuery {
query = query.
Column("id", "connector_id", "created_at", "scheduled_at", "description", "type", "source_account_id", "destination_account_id", "provider", "initial_amount", "amount", "asset", "metadata").
Column("id", "reference", "connector_id", "created_at", "scheduled_at", "description", "type", "source_account_id", "destination_account_id", "provider", "initial_amount", "amount", "asset", "metadata").
Relation("RelatedAdjustments")

if q.Options.QueryBuilder != nil {
Expand Down Expand Up @@ -105,6 +105,17 @@ func (s *Storage) transferInitiationQueryContext(qb query.Builder) (string, []an
default:
return "", nil, fmt.Errorf("unexpected type %T for column '%s'", accountID, key)
}
case key == "reference":
if operator != "$match" {
return "", nil, fmt.Errorf("'%s' column can only be used with $match", key)
}

switch reference := value.(type) {
case string:
return fmt.Sprintf("%s = ?", key), []any{reference}, nil
default:
return "", nil, fmt.Errorf("unexpected type %T for column %q", reference, key)
}
default:
return "", nil, fmt.Errorf("unknown key '%s' when building query", key)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func insertTransferInitiation(t *testing.T, store *Storage, connectorID models.C
Reference: "tf_1",
ConnectorID: connectorID,
},
Reference: "tf_1",
CreatedAt: time.Date(2023, 11, 14, 8, 0, 0, 0, time.UTC),
ScheduledAt: time.Date(2023, 11, 14, 8, 0, 0, 0, time.UTC),
Description: "test_1",
Expand Down Expand Up @@ -48,6 +49,7 @@ func insertTransferInitiation(t *testing.T, store *Storage, connectorID models.C
Reference: "tf_2",
ConnectorID: connectorID,
},
Reference: "tf_2",
CreatedAt: time.Date(2023, 11, 14, 9, 0, 0, 0, time.UTC),
ScheduledAt: time.Date(2023, 11, 14, 9, 0, 0, 0, time.UTC),
Description: "test_2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func (s *Service) CreateTransferInitiation(ctx context.Context, req *CreateTrans
createdAt := time.Now().UTC()
tf := &models.TransferInitiation{
ID: id,
Reference: req.Reference,
CreatedAt: createdAt,
ScheduledAt: req.ScheduledAt,
Description: req.Description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func TestCreateTransferInitiation(t *testing.T) {
Reference: "ref1",
ConnectorID: connectorDummyPay.ID,
},
Reference: "ref1",
ScheduledAt: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Description: "test",
DestinationAccountID: destinationAccountID,
Expand Down Expand Up @@ -99,6 +100,7 @@ func TestCreateTransferInitiation(t *testing.T) {
Reference: "ref1",
ConnectorID: connectorDummyPay.ID,
},
Reference: "ref1",
ScheduledAt: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
DestinationAccountID: destinationAccountID,
SourceAccountID: &sourceAccountID,
Expand Down Expand Up @@ -139,6 +141,7 @@ func TestCreateTransferInitiation(t *testing.T) {
Reference: "ref1",
ConnectorID: connectorDummyPay.ID,
},
Reference: "ref1",
ScheduledAt: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Description: "test",
DestinationAccountID: destinationAccountID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (s *Storage) CreateTransferInitiation(ctx context.Context, transferInitiati
}()

query := tx.NewInsert().
Column("id", "created_at", "scheduled_at", "description", "type", "destination_account_id", "provider", "connector_id", "initial_amount", "amount", "asset", "metadata").
Column("id", "reference", "created_at", "scheduled_at", "description", "type", "destination_account_id", "provider", "connector_id", "initial_amount", "amount", "asset", "metadata").
Model(transferInitiation)

if transferInitiation.SourceAccountID != nil {
Expand Down Expand Up @@ -53,7 +53,7 @@ func (s *Storage) ReadTransferInitiation(ctx context.Context, id models.Transfer
var transferInitiation models.TransferInitiation

query := s.db.NewSelect().
Column("id", "created_at", "scheduled_at", "description", "type", "source_account_id", "destination_account_id", "provider", "connector_id", "amount", "asset", "metadata").
Column("id", "reference", "created_at", "scheduled_at", "description", "type", "source_account_id", "destination_account_id", "provider", "connector_id", "amount", "asset", "metadata").
Model(&transferInitiation).
Relation("RelatedAdjustments").
Where("id = ?", id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func testCreateTransferInitiations(t *testing.T, store *storage.Storage) {
}
t1 = &models.TransferInitiation{
ID: t1ID,
Reference: "test1",
CreatedAt: t1T,
ScheduledAt: t1T,
Description: "test_description",
Expand All @@ -73,6 +74,7 @@ func testCreateTransferInitiations(t *testing.T, store *storage.Storage) {
}
t2 = &models.TransferInitiation{
ID: t2ID,
Reference: "test2",
CreatedAt: t2T,
ScheduledAt: t2T,
Description: "test_description2",
Expand Down Expand Up @@ -136,6 +138,7 @@ func testGetTransferInitiation(

func checkTransferInitiationsEqual(t *testing.T, t1, t2 *models.TransferInitiation, checkRelatedAdjusment bool) {
require.Equal(t, t1.ID, t2.ID)
require.Equal(t, t1.Reference, t2.Reference)
require.Equal(t, t1.CreatedAt.UTC(), t2.CreatedAt.UTC())
require.Equal(t, t1.ScheduledAt.UTC(), t2.ScheduledAt.UTC())
require.Equal(t, t1.Description, t2.Description)
Expand Down
1 change: 1 addition & 0 deletions components/payments/internal/models/transfer_initiation.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ type TransferInitiation struct {
// Filled when created in DB
ID TransferInitiationID `bun:",pk,nullzero"`

Reference string
CreatedAt time.Time `bun:",nullzero"`
ScheduledAt time.Time `bun:",nullzero"`
Description string
Expand Down
56 changes: 56 additions & 0 deletions components/payments/internal/storage/migrations_v1.x.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,11 @@ func registerMigrationsV1(ctx context.Context, migrator *migrations.Migrator) {
return fixExpandingChangelogs(ctx, tx)
},
},
migrations.Migration{
Up: func(tx bun.Tx) error {
return fixMissingReferenceTransferInitiation(ctx, tx)
},
},
)
}

Expand Down Expand Up @@ -1090,3 +1095,54 @@ func fixExpandingChangelogs(ctx context.Context, tx bun.Tx) error {

return nil
}

func fixMissingReferenceTransferInitiation(ctx context.Context, tx bun.Tx) error {
_, err := tx.Exec(`
ALTER TABLE transfers.transfer_initiation ADD COLUMN IF NOT EXISTS reference text;
`)
if err != nil {
return err
}

var createdAt time.Time
for {
var transferInitiations []models.TransferInitiation
query := tx.NewSelect().
Model(&transferInitiations).
Order("created_at ASC").
Limit(100)

if !createdAt.IsZero() {
query.Where("created_at > ?", createdAt)
}

err := query.Scan(ctx)
if err != nil {
return err
}

fmt.Println("Processing", len(transferInitiations), "transfer initiations")
if len(transferInitiations) == 0 {
break
}

for i := range transferInitiations {
if transferInitiations[i].Reference == "" {
transferInitiations[i].Reference = transferInitiations[i].ID.Reference
}
createdAt = transferInitiations[i].CreatedAt
}

fmt.Println("Updating", len(transferInitiations), "transfer initiations")
_, err = tx.NewInsert().
Model(&transferInitiations).
On("CONFLICT (id) DO UPDATE").
Set("reference = EXCLUDED.reference").
Exec(ctx)
if err != nil {
return err
}
}

return nil
}

0 comments on commit e1b8eb0

Please # to comment.