Skip to content

Commit

Permalink
feat: get canceled payment status for disbursement and receivers metr…
Browse files Browse the repository at this point in the history
…ics (#82)

What
Get canceled payments for disbursement and receiver metrics

Why
statistics metrics on FE
  • Loading branch information
ceciliaromao authored Nov 2, 2023
1 parent bf528d0 commit c92b934
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 3 deletions.
3 changes: 3 additions & 0 deletions internal/data/disbursements.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type DisbursementStats struct {
TotalPayments int `json:"total_payments" db:"total_payments"`
SuccessfulPayments int `json:"total_payments_sent" db:"total_payments_sent"`
FailedPayments int `json:"total_payments_failed" db:"total_payments_failed"`
CanceledPayments int `json:"total_payments_canceled" db:"total_payments_canceled"`
RemainingPayments int `json:"total_payments_remaining" db:"total_payments_remaining"`
AmountDisbursed string `json:"amount_disbursed" db:"amount_disbursed"`
TotalAmount string `json:"total_amount" db:"total_amount"`
Expand Down Expand Up @@ -232,6 +233,7 @@ func (d *DisbursementModel) populateStatistics(ctx context.Context, disbursement
count(*) as total_payments,
sum(case when status = 'SUCCESS' then 1 else 0 end) as total_payments_sent,
sum(case when status = 'FAILED' then 1 else 0 end) as total_payments_failed,
sum(case when status = 'CANCELED' then 1 else 0 end) as total_payments_canceled,
sum(case when status IN ('DRAFT', 'READY', 'PENDING', 'PAUSED') then 1 else 0 end) as total_payments_remaining,
ROUND(SUM(CASE WHEN status = 'SUCCESS' THEN amount ELSE 0 END), 2) as amount_disbursed,
ROUND(SUM(amount), 2) as total_amount,
Expand Down Expand Up @@ -259,6 +261,7 @@ func (d *DisbursementModel) populateStatistics(ctx context.Context, disbursement
&stats.TotalPayments,
&stats.SuccessfulPayments,
&stats.FailedPayments,
&stats.CanceledPayments,
&stats.RemainingPayments,
&stats.AmountDisbursed,
&stats.TotalAmount,
Expand Down
14 changes: 11 additions & 3 deletions internal/data/disbursements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,15 +391,23 @@ func Test_DisbursementModelGetAll(t *testing.T) {
Amount: "020.50",
Status: FailedPaymentStatus,
})
CreatePaymentFixture(t, ctx, dbConnectionPool, &paymentModel, &Payment{
ReceiverWallet: receiverWallet,
Disbursement: expectedDisbursement,
Asset: *asset,
Amount: "020.50",
Status: CanceledPaymentStatus,
})

expectedStats := &DisbursementStats{}
expectedStats.TotalPayments = 3
expectedStats.TotalPayments = 4
expectedStats.SuccessfulPayments = 1
expectedStats.FailedPayments = 1
expectedStats.CanceledPayments = 1
expectedStats.RemainingPayments = 1
expectedStats.TotalAmount = "270.55"
expectedStats.TotalAmount = "291.05"
expectedStats.AmountDisbursed = "100.00"
expectedStats.AverageAmount = "90.18"
expectedStats.AverageAmount = "72.76"

expectedDisbursement.DisbursementStats = expectedStats

Expand Down
7 changes: 7 additions & 0 deletions internal/data/receivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type ReceiverStats struct {
TotalPayments string `json:"total_payments,omitempty" db:"total_payments"`
SuccessfulPayments string `json:"successful_payments,omitempty" db:"successful_payments"`
FailedPayments string `json:"failed_payments,omitempty" db:"failed_payments"`
CanceledPayments string `json:"canceled_payments,omitempty" db:"canceled_payments"`
RemainingPayments string `json:"remaining_payments,omitempty" db:"remaining_payments"`
RegisteredWallets string `json:"registered_wallets,omitempty" db:"registered_wallets"`
ReceivedAmounts ReceivedAmounts `json:"received_amounts,omitempty" db:"received_amounts"`
Expand Down Expand Up @@ -117,6 +118,7 @@ func (r *ReceiverModel) Get(ctx context.Context, sqlExec db.SQLExecuter, id stri
COUNT(p) as total_payments,
COUNT(p) FILTER(WHERE p.status = 'SUCCESS') as successful_payments,
COUNT(p) FILTER(WHERE p.status = 'FAILED') as failed_payments,
COUNT(p) FILTER(WHERE p.status = 'CANCELED') as canceled_payments,
COUNT(p) FILTER(WHERE p.status IN ('DRAFT', 'READY', 'PENDING', 'PAUSED')) as remaining_payments,
a.code as asset_code,
a.issuer as asset_issuer,
Expand All @@ -132,6 +134,7 @@ func (r *ReceiverModel) Get(ctx context.Context, sqlExec db.SQLExecuter, id stri
SUM(rs.total_payments) as total_payments,
SUM(rs.successful_payments) as successful_payments,
SUM(rs.failed_payments) as failed_payments,
SUM(rs.canceled_payments) as canceled_payments,
SUM(rs.remaining_payments) as remaining_payments,
jsonb_agg(jsonb_build_object('asset_code', rs.asset_code, 'asset_issuer', rs.asset_issuer, 'received_amount', rs.received_amount::text)) as received_amounts
FROM receiver_stats rs
Expand All @@ -147,6 +150,7 @@ func (r *ReceiverModel) Get(ctx context.Context, sqlExec db.SQLExecuter, id stri
COALESCE(total_payments, 0) as total_payments,
COALESCE(successful_payments, 0) as successful_payments,
COALESCE(rs.failed_payments, '0') as failed_payments,
COALESCE(rs.canceled_payments, '0') as canceled_payments,
COALESCE(rs.remaining_payments, '0') as remaining_payments,
rs.received_amounts,
COALESCE(rw.registered_wallets, 0) as registered_wallets
Expand Down Expand Up @@ -206,6 +210,7 @@ func (r *ReceiverModel) GetAll(ctx context.Context, sqlExec db.SQLExecuter, quer
COUNT(p) as total_payments,
COUNT(p) FILTER(WHERE p.status = 'SUCCESS') as successful_payments,
COUNT(p) FILTER(WHERE p.status = 'FAILED') as failed_payments,
COUNT(p) FILTER(WHERE p.status = 'CANCELED') as canceled_payments,
COUNT(p) FILTER(WHERE p.status IN ('DRAFT', 'READY', 'PENDING', 'PAUSED')) as remaining_payments,
a.code as asset_code,
a.issuer as asset_issuer,
Expand All @@ -221,6 +226,7 @@ func (r *ReceiverModel) GetAll(ctx context.Context, sqlExec db.SQLExecuter, quer
SUM(rs.total_payments) as total_payments,
SUM(rs.successful_payments) as successful_payments,
SUM(rs.failed_payments) as failed_payments,
SUM(rs.canceled_payments) as canceled_payments,
SUM(rs.remaining_payments) as remaining_payments,
jsonb_agg(jsonb_build_object('asset_code', rs.asset_code, 'asset_issuer', rs.asset_issuer, 'received_amount', rs.received_amount::text)) as received_amounts
FROM receiver_stats rs
Expand All @@ -236,6 +242,7 @@ func (r *ReceiverModel) GetAll(ctx context.Context, sqlExec db.SQLExecuter, quer
COALESCE(total_payments, 0) as total_payments,
COALESCE(successful_payments, 0) as successful_payments,
COALESCE(rs.failed_payments, '0') as failed_payments,
COALESCE(rs.canceled_payments, '0') as canceled_payments,
COALESCE(rs.remaining_payments, '0') as remaining_payments,
rs.received_amounts,
COALESCE(rrwc.registered_wallets, 0) as registered_wallets
Expand Down
15 changes: 15 additions & 0 deletions internal/data/receivers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func Test_ReceiversModelGet(t *testing.T) {
TotalPayments: "0",
SuccessfulPayments: "0",
FailedPayments: "0",
CanceledPayments: "0",
RemainingPayments: "0",
RegisteredWallets: "0",
ReceivedAmounts: nil,
Expand Down Expand Up @@ -139,6 +140,7 @@ func Test_ReceiversModelGet(t *testing.T) {
TotalPayments: "1",
SuccessfulPayments: "0",
FailedPayments: "0",
CanceledPayments: "0",
RemainingPayments: "1",
RegisteredWallets: "0",
ReceivedAmounts: []Amount{
Expand Down Expand Up @@ -195,6 +197,7 @@ func Test_ReceiversModelGet(t *testing.T) {
TotalPayments: "2",
SuccessfulPayments: "1",
FailedPayments: "0",
CanceledPayments: "0",
RemainingPayments: "1",
RegisteredWallets: "0",
ReceivedAmounts: []Amount{
Expand Down Expand Up @@ -254,6 +257,7 @@ func Test_ReceiversModelGet(t *testing.T) {
TotalPayments: "2",
SuccessfulPayments: "1",
FailedPayments: "0",
CanceledPayments: "0",
RemainingPayments: "1",
RegisteredWallets: "0",
ReceivedAmounts: []Amount{
Expand Down Expand Up @@ -312,6 +316,7 @@ func Test_ReceiversModelGet(t *testing.T) {
TotalPayments: "1",
SuccessfulPayments: "0",
FailedPayments: "0",
CanceledPayments: "0",
RemainingPayments: "1",
RegisteredWallets: "0",
ReceivedAmounts: []Amount{
Expand Down Expand Up @@ -503,6 +508,7 @@ func Test_ReceiversModelGetAll(t *testing.T) {
"total_payments": "0",
"successful_payments": "0",
"failed_payments": "0",
"canceled_payments": "0",
"remaining_payments": "0",
"registered_wallets":"0"
},
Expand All @@ -516,6 +522,7 @@ func Test_ReceiversModelGetAll(t *testing.T) {
"total_payments": "0",
"successful_payments": "0",
"failed_payments": "0",
"canceled_payments": "0",
"remaining_payments": "0",
"registered_wallets":"1"
}
Expand Down Expand Up @@ -559,6 +566,7 @@ func Test_ReceiversModelGetAll(t *testing.T) {
"total_payments": "0",
"successful_payments": "0",
"failed_payments": "0",
"canceled_payments": "0",
"remaining_payments": "0",
"registered_wallets":"0"
}
Expand Down Expand Up @@ -601,6 +609,7 @@ func Test_ReceiversModelGetAll(t *testing.T) {
"total_payments": "0",
"successful_payments": "0",
"failed_payments": "0",
"canceled_payments": "0",
"remaining_payments": "0",
"registered_wallets":"1"
}
Expand Down Expand Up @@ -643,6 +652,7 @@ func Test_ReceiversModelGetAll(t *testing.T) {
"total_payments": "0",
"successful_payments": "0",
"failed_payments": "0",
"canceled_payments": "0",
"remaining_payments": "0",
"registered_wallets":"0"
}
Expand Down Expand Up @@ -680,6 +690,7 @@ func Test_ReceiversModelGetAll(t *testing.T) {
"total_payments": "0",
"successful_payments": "0",
"failed_payments": "0",
"canceled_payments": "0",
"remaining_payments": "0",
"registered_wallets":"0"
}
Expand Down Expand Up @@ -717,6 +728,7 @@ func Test_ReceiversModelGetAll(t *testing.T) {
"total_payments": "0",
"successful_payments": "0",
"failed_payments": "0",
"canceled_payments": "0",
"remaining_payments": "0",
"registered_wallets":"1"
}
Expand Down Expand Up @@ -758,6 +770,7 @@ func Test_ReceiversModelGetAll(t *testing.T) {
"total_payments": "0",
"successful_payments": "0",
"failed_payments": "0",
"canceled_payments": "0",
"remaining_payments": "0",
"registered_wallets":"0"
}
Expand Down Expand Up @@ -795,6 +808,7 @@ func Test_ReceiversModelGetAll(t *testing.T) {
"total_payments": "0",
"successful_payments": "0",
"failed_payments": "0",
"canceled_payments": "0",
"remaining_payments": "0",
"registered_wallets":"0"
},
Expand All @@ -808,6 +822,7 @@ func Test_ReceiversModelGetAll(t *testing.T) {
"total_payments": "0",
"successful_payments": "0",
"failed_payments": "0",
"canceled_payments": "0",
"remaining_payments": "0",
"registered_wallets":"1"
}
Expand Down
4 changes: 4 additions & 0 deletions internal/data/receivers_wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type ReceiverWalletStats struct {
TotalPayments string `json:"total_payments,omitempty" db:"total_payments"`
PaymentsReceived string `json:"payments_received,omitempty" db:"payments_received"`
FailedPayments string `json:"failed_payments,omitempty" db:"failed_payments"`
CanceledPayments string `json:"canceled_payments,omitempty" db:"canceled_payments"`
RemainingPayments string `json:"remaining_payments,omitempty" db:"remaining_payments"`
ReceivedAmounts ReceivedAmounts `json:"received_amounts,omitempty" db:"received_amounts"`
// TotalInvitationSMSResentAttempts holds how many times were resent the Invitation SMS to the receiver
Expand Down Expand Up @@ -133,6 +134,7 @@ func (rw *ReceiverWalletModel) GetWithReceiverIds(ctx context.Context, sqlExec d
COUNT(p) as total_payments,
COUNT(p) FILTER(WHERE p.status = 'SUCCESS') as payments_received,
COUNT(p) FILTER(WHERE p.status = 'FAILED') as failed_payments,
COUNT(p) FILTER(WHERE p.status = 'CANCELED') as canceled_payments,
COUNT(p) FILTER(WHERE p.status IN ('DRAFT', 'READY', 'PENDING', 'PAUSED')) as remaining_payments,
a.code as asset_code,
a.issuer as asset_issuer,
Expand All @@ -148,6 +150,7 @@ func (rw *ReceiverWalletModel) GetWithReceiverIds(ctx context.Context, sqlExec d
SUM(rws.total_payments) as total_payments,
SUM(rws.payments_received) as payments_received,
SUM(rws.failed_payments) as failed_payments,
SUM(rws.canceled_payments) as canceled_payments,
SUM(rws.remaining_payments) as remaining_payments,
jsonb_agg(jsonb_build_object('asset_code', rws.asset_code, 'asset_issuer', rws.asset_issuer, 'received_amount', rws.received_amount::text)) as received_amounts
FROM receiver_wallets_stats rws
Expand Down Expand Up @@ -180,6 +183,7 @@ func (rw *ReceiverWalletModel) GetWithReceiverIds(ctx context.Context, sqlExec d
COALESCE(rws.total_payments, '0') as total_payments,
COALESCE(rws.payments_received, '0') as payments_received,
COALESCE(rws.failed_payments, '0') as failed_payments,
COALESCE(rws.canceled_payments, '0') as canceled_payments,
COALESCE(rws.remaining_payments, '0') as remaining_payments,
rws.received_amounts,
rwm.invited_at as invited_at,
Expand Down
5 changes: 5 additions & 0 deletions internal/data/receivers_wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func Test_ReceiversWalletModelGetWithReceiverId(t *testing.T) {
TotalPayments: "0",
PaymentsReceived: "0",
FailedPayments: "0",
CanceledPayments: "0",
RemainingPayments: "0",
ReceivedAmounts: nil,
},
Expand Down Expand Up @@ -207,6 +208,7 @@ func Test_ReceiversWalletModelGetWithReceiverId(t *testing.T) {
TotalPayments: "2",
PaymentsReceived: "1",
FailedPayments: "0",
CanceledPayments: "0",
RemainingPayments: "1",
ReceivedAmounts: []Amount{
{
Expand Down Expand Up @@ -312,6 +314,7 @@ func Test_ReceiversWalletModelGetWithReceiverId(t *testing.T) {
TotalPayments: "2",
PaymentsReceived: "1",
FailedPayments: "0",
CanceledPayments: "0",
RemainingPayments: "1",
ReceivedAmounts: []Amount{
{
Expand Down Expand Up @@ -345,6 +348,7 @@ func Test_ReceiversWalletModelGetWithReceiverId(t *testing.T) {
TotalPayments: "1",
PaymentsReceived: "0",
FailedPayments: "0",
CanceledPayments: "0",
RemainingPayments: "1",
ReceivedAmounts: []Amount{
{
Expand Down Expand Up @@ -425,6 +429,7 @@ func Test_ReceiversWalletModelGetWithReceiverId(t *testing.T) {
TotalPayments: "0",
PaymentsReceived: "0",
FailedPayments: "0",
CanceledPayments: "0",
RemainingPayments: "0",
ReceivedAmounts: nil,
},
Expand Down
Loading

0 comments on commit c92b934

Please sign in to comment.