Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/dogmatiq/dogma"
"github.com/dogmatiq/example/domain"
"github.com/dogmatiq/example/integrations"
"github.com/dogmatiq/example/projections"
"github.com/dogmatiq/projectionkit/sqlprojection"
)
Expand All @@ -24,6 +25,8 @@ type App struct {
TransferProcess domain.TransferProcessHandler
WithdrawalProcess domain.WithdrawalProcessHandler

ThirdPartyBank integrations.ThirdPartyBankIntegrationHandler

ReadDB *sql.DB
AccountProjection projections.AccountProjectionHandler
CustomerProjection projections.CustomerProjectionHandler
Expand All @@ -44,6 +47,8 @@ func (a *App) Configure(c dogma.ApplicationConfigurer) {
dogma.ViaProcess(a.TransferProcess),
dogma.ViaProcess(a.WithdrawalProcess),

dogma.ViaIntegration(a.ThirdPartyBank),

dogma.ViaProjection(sqlprojection.New(a.ReadDB, sqlprojection.SQLiteDriver, &a.AccountProjection)),
dogma.ViaProjection(sqlprojection.New(a.ReadDB, sqlprojection.SQLiteDriver, &a.CustomerProjection)),
)
Expand Down
2 changes: 1 addition & 1 deletion domain/dailydebitlimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (d *dailyDebitLimit) wouldExceedLimit(amount int64) bool {
func (d *dailyDebitLimit) ApplyEvent(m dogma.Event) {
switch x := m.(type) {
case *events.DailyDebitLimitConsumed:
d.TotalDebitsForDay = x.Amount
d.TotalDebitsForDay = x.TotalDebitsForDay
}
}

Expand Down
2 changes: 1 addition & 1 deletion domain/deposit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Test_Deposit(t *testing.T) {
},
),
).
// verify that funds are availalbe
// verify that funds are available
Expect(
ExecuteCommand(
&commands.Withdraw{
Expand Down
26 changes: 21 additions & 5 deletions domain/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ func (t *transaction) StartTransfer(s dogma.AggregateCommandScope, m *commands.T
}

s.RecordEvent(&events.TransferStarted{
TransactionID: m.TransactionID,
FromAccountID: m.FromAccountID,
ToAccountID: m.ToAccountID,
Amount: m.Amount,
ScheduledTime: m.ScheduledTime,
TransactionID: m.TransactionID,
FromAccountID: m.FromAccountID,
ToAccountID: m.ToAccountID,
ToThirdPartyBank: m.ToThirdPartyBank,
Amount: m.Amount,
ScheduledTime: m.ScheduledTime,
})
}

Expand All @@ -105,6 +106,15 @@ func (t *transaction) DeclineTransfer(s dogma.AggregateCommandScope, m *commands
})
}

func (t *transaction) MarkTransferAsFailed(s dogma.AggregateCommandScope, m *commands.MarkTransferAsFailed) {
s.RecordEvent(&events.TransferFailed{
TransactionID: m.TransactionID,
FromAccountID: m.FromAccountID,
ToAccountID: m.ToAccountID,
Amount: m.Amount,
})
}

func (t *transaction) ApplyEvent(m dogma.Event) {
switch m.(type) {
case *events.DepositStarted:
Expand Down Expand Up @@ -141,6 +151,7 @@ func (TransactionHandler) Configure(c dogma.AggregateConfigurer) {
dogma.HandlesCommand[*commands.Transfer](),
dogma.HandlesCommand[*commands.ApproveTransfer](),
dogma.HandlesCommand[*commands.DeclineTransfer](),
dogma.HandlesCommand[*commands.MarkTransferAsFailed](),
dogma.RecordsEvent[*events.DepositStarted](),
dogma.RecordsEvent[*events.DepositApproved](),
dogma.RecordsEvent[*events.WithdrawalStarted](),
Expand All @@ -149,6 +160,7 @@ func (TransactionHandler) Configure(c dogma.AggregateConfigurer) {
dogma.RecordsEvent[*events.TransferStarted](),
dogma.RecordsEvent[*events.TransferApproved](),
dogma.RecordsEvent[*events.TransferDeclined](),
dogma.RecordsEvent[*events.TransferFailed](),
)
}

Expand All @@ -172,6 +184,8 @@ func (TransactionHandler) RouteCommandToInstance(m dogma.Command) string {
return x.TransactionID
case *commands.DeclineTransfer:
return x.TransactionID
case *commands.MarkTransferAsFailed:
return x.TransactionID
default:
panic(dogma.UnexpectedMessage)
}
Expand Down Expand Up @@ -203,6 +217,8 @@ func (TransactionHandler) HandleCommand(
t.ApproveTransfer(s, x)
case *commands.DeclineTransfer:
t.DeclineTransfer(s, x)
case *commands.MarkTransferAsFailed:
t.MarkTransferAsFailed(s, x)
default:
panic(dogma.UnexpectedMessage)
}
Expand Down
69 changes: 56 additions & 13 deletions domain/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ func init() {

// transfer is the process root for a funds transfer.
type transferProcess struct {
FromAccountID string
ToAccountID string
Amount int64
DeclineReason messages.DebitFailureReason
FromAccountID string
ToAccountID string
ToThirdPartyBank bool
Amount int64
DeclineReason messages.DebitFailureReason
}

// MarshalBinary returns the transferProcess encoded as binary data.
Expand Down Expand Up @@ -54,13 +55,18 @@ func (TransferProcessHandler) Configure(c dogma.ProcessConfigurer) {
dogma.HandlesEvent[*events.DailyDebitLimitConsumed](),
dogma.HandlesEvent[*events.DailyDebitLimitExceeded](),
dogma.HandlesEvent[*events.AccountCredited](),
dogma.HandlesEvent[*events.ThirdPartyAccountCredited](),
dogma.HandlesEvent[*events.ThirdPartyAccountCreditFailed](),
dogma.HandlesEvent[*events.TransferApproved](),
dogma.HandlesEvent[*events.TransferDeclined](),
dogma.HandlesEvent[*events.TransferFailed](),
dogma.ExecutesCommand[*commands.DebitAccount](),
dogma.ExecutesCommand[*commands.ConsumeDailyDebitLimit](),
dogma.ExecutesCommand[*commands.CreditAccount](),
dogma.ExecutesCommand[*commands.CreditThirdPartyAccount](),
dogma.ExecutesCommand[*commands.ApproveTransfer](),
dogma.ExecutesCommand[*commands.DeclineTransfer](),
dogma.ExecutesCommand[*commands.MarkTransferAsFailed](),
dogma.SchedulesTimeout[*TransferReadyToProceed](),
)
}
Expand All @@ -84,10 +90,16 @@ func (TransferProcessHandler) RouteEventToInstance(
return x.TransactionID, x.DebitType == messages.Transfer, nil
case *events.AccountCredited:
return x.TransactionID, x.TransactionType == messages.Transfer, nil
case *events.ThirdPartyAccountCredited:
return x.TransactionID, true, nil
case *events.ThirdPartyAccountCreditFailed:
return x.TransactionID, true, nil
case *events.TransferApproved:
return x.TransactionID, true, nil
case *events.TransferDeclined:
return x.TransactionID, true, nil
case *events.TransferFailed:
return x.TransactionID, true, nil
default:
panic(dogma.UnexpectedMessage)
}
Expand All @@ -106,6 +118,7 @@ func (TransferProcessHandler) HandleEvent(
case *events.TransferStarted:
t.FromAccountID = x.FromAccountID
t.ToAccountID = x.ToAccountID
t.ToThirdPartyBank = x.ToThirdPartyBank
t.Amount = x.Amount

s.ScheduleTimeout(
Expand Down Expand Up @@ -134,13 +147,20 @@ func (TransferProcessHandler) HandleEvent(
})

case *events.DailyDebitLimitConsumed:
// continue transfer
s.ExecuteCommand(&commands.CreditAccount{
TransactionID: x.TransactionID,
AccountID: t.ToAccountID,
TransactionType: messages.Transfer,
Amount: x.Amount,
})
if t.ToThirdPartyBank {
s.ExecuteCommand(&commands.CreditThirdPartyAccount{
TransactionID: x.TransactionID,
AccountID: t.ToAccountID,
Amount: x.Amount,
})
} else {
s.ExecuteCommand(&commands.CreditAccount{
TransactionID: x.TransactionID,
AccountID: t.ToAccountID,
TransactionType: messages.Transfer,
Amount: x.Amount,
})
}

case *events.DailyDebitLimitExceeded:
t.DeclineReason = messages.DailyDebitLimitExceeded
Expand All @@ -163,7 +183,7 @@ func (TransferProcessHandler) HandleEvent(
Amount: x.Amount,
})
} else {
// it was a compensating credit to undo the transfer (failure)
// it was a compensating credit to undo the transfer (business rejection)
s.ExecuteCommand(&commands.DeclineTransfer{
TransactionID: x.TransactionID,
FromAccountID: t.FromAccountID,
Expand All @@ -173,7 +193,30 @@ func (TransferProcessHandler) HandleEvent(
})
}

case *events.TransferApproved, *events.TransferDeclined:
case *events.ThirdPartyAccountCredited:
s.ExecuteCommand(&commands.ApproveTransfer{
TransactionID: x.TransactionID,
FromAccountID: t.FromAccountID,
ToAccountID: t.ToAccountID,
Amount: t.Amount,
})

case *events.ThirdPartyAccountCreditFailed:
s.ExecuteCommand(&commands.MarkTransferAsFailed{
TransactionID: x.TransactionID,
FromAccountID: t.FromAccountID,
ToAccountID: t.ToAccountID,
Amount: t.Amount,
})

s.ExecuteCommand(&commands.CreditAccount{
TransactionID: x.TransactionID,
AccountID: t.FromAccountID,
TransactionType: messages.Transfer,
Amount: t.Amount,
})

case *events.TransferApproved, *events.TransferDeclined, *events.TransferFailed:
s.End()

default:
Expand Down
Loading
Loading