Skip to content

Commit

Permalink
fix paid amount in /bi/financials
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyBuisset committed Nov 20, 2024
1 parent 82a056b commit 4969c31
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import onlydust.com.marketplace.accounting.domain.model.Deposit;
import onlydust.com.marketplace.accounting.domain.model.Network;
import onlydust.com.marketplace.accounting.domain.model.Payment;
import onlydust.com.marketplace.accounting.domain.model.user.GithubUserId;
import onlydust.com.marketplace.api.contract.model.BiFinancialsStatsListResponse;
import onlydust.com.marketplace.api.contract.model.FinancialTransactionType;
Expand Down Expand Up @@ -174,23 +175,56 @@ void setUp() {
final var reward6 = at("2024-08-02T00:00:00Z", () -> rewardHelper.create(myProject.getId(), caller, recipientId, 1, ETH));
at("2024-08-03T00:00:00Z", () -> rewardHelper.cancel(myProject.getId(), caller, reward6));

// pay reward1
// pay reward1 directly
databaseHelper.executeQuery("""
insert into accounting.all_transactions (id, timestamp, currency_id, type, sponsor_id, program_id, project_id, reward_id, payment_id, amount)
values (:id, :timestamp, :currency_id, cast(:type as accounting.transaction_type), :sponsor_id, :program_id, :project_id, :reward_id, :payment_id, :amount)
values (:id, :timestamp, :currency_id, cast(:type as accounting.transaction_type), :sponsor_id, :program_id, :project_id, :reward_id, null, :amount)
""", Map.of(
"id", UUID.randomUUID(),
"timestamp", ZonedDateTime.parse("2024-08-15T00:00:00Z"),
"currency_id", USDC.value(),
"type", "TRANSFER",
"type", "BURN",
"sponsor_id", mySponsor.id().value(),
"program_id", myProgram.id().value(),
"project_id", myProject.getId().value(),
"reward_id", reward1.value(),
"payment_id", UUID.randomUUID(),
"amount", 400
));

// pay reward4 with bulk payment
final var paymentId = Payment.Id.random();
databaseHelper.executeQuery("""
insert into accounting.all_transactions (id, timestamp, currency_id, type, sponsor_id, program_id, project_id, reward_id, payment_id, amount)
values (:id, :timestamp, :currency_id, cast(:type as accounting.transaction_type), :sponsor_id, :program_id, :project_id, :reward_id, :payment_id, :amount)
""", Map.of(
"id", UUID.randomUUID(),
"timestamp", ZonedDateTime.parse("2024-08-15T00:00:00Z"),
"currency_id", USDC.value(),
"type", "TRANSFER",
"sponsor_id", mySponsor.id().value(),
"program_id", myProgram.id().value(),
"project_id", myProject.getId().value(),
"reward_id", reward4.value(),
"payment_id", paymentId.value(),
"amount", 100
));
databaseHelper.executeQuery("""
insert into accounting.all_transactions (id, timestamp, currency_id, type, sponsor_id, program_id, project_id, reward_id, payment_id, amount)
values (:id, :timestamp, :currency_id, cast(:type as accounting.transaction_type), :sponsor_id, :program_id, :project_id, :reward_id, :payment_id, :amount)
""", Map.of(
"id", UUID.randomUUID(),
"timestamp", ZonedDateTime.parse("2024-08-15T00:00:00Z"),
"currency_id", USDC.value(),
"type", "BURN",
"sponsor_id", mySponsor.id().value(),
"program_id", myProgram.id().value(),
"project_id", myProject.getId().value(),
"reward_id", reward4.value(),
"payment_id", paymentId.value(),
"amount", 100
));


projectFacadePort.refreshStats();
}

Expand All @@ -213,7 +247,6 @@ void should_get_recipient_bi_financial_stats() {
.expectStatus()
.isOk()
.expectBody()
.consumeWith(System.out::println)
.json("""
{
"totalDeposited": {
Expand Down Expand Up @@ -276,25 +309,25 @@ void should_get_recipient_bi_financial_stats() {
]
},
"totalPaid": {
"totalUsdEquivalent": 404.00,
"totalUsdEquivalent": 505.00,
"totalPerCurrency": [
{
"amount": 400,
"prettyAmount": 400,
"amount": 500,
"prettyAmount": 500,
"currency": {
"id": "562bbf65-8a71-4d30-ad63-520c0d68ba27",
"code": "USDC",
"name": "USD Coin",
"logoUrl": "https://s2.coinmarketcap.com/static/img/coins/64x64/3408.png",
"decimals": 6
},
"usdEquivalent": 404.00,
"usdEquivalent": 505.00,
"usdConversionRate": 1.010001,
"ratio": 100
}
]
},
"transactionCount": 7
"transactionCount": 8
}
""")
.jsonPath("$.stats[?(@.date == '2024-07-01')]").value(jsonObjectEquals("""
Expand Down Expand Up @@ -730,7 +763,7 @@ void should_get_recipient_bi_financial_stats() {
]
},
"totalPaid": {
"totalUsdEquivalent": 404.00,
"totalUsdEquivalent": 505.00,
"totalPerCurrency": [
{
"amount": 0,
Expand All @@ -747,16 +780,16 @@ void should_get_recipient_bi_financial_stats() {
"ratio": null
},
{
"amount": 400,
"prettyAmount": 400,
"amount": 500,
"prettyAmount": 500,
"currency": {
"id": "562bbf65-8a71-4d30-ad63-520c0d68ba27",
"code": "USDC",
"name": "USD Coin",
"logoUrl": "https://s2.coinmarketcap.com/static/img/coins/64x64/3408.png",
"decimals": 6
},
"usdEquivalent": 404.00,
"usdEquivalent": 505.00,
"usdConversionRate": 1.010001,
"ratio": 100
},
Expand All @@ -776,7 +809,7 @@ void should_get_recipient_bi_financial_stats() {
}
]
},
"transactionCount": 3
"transactionCount": 4
}
"""))
.jsonPath("$.stats[?(@.date == '2024-09-01')]").value(jsonObjectEquals("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ left join lateral (select #group_by#
coalesce(sum(tx.amount) filter ( where type = 'TRANSFER' and tx.reward_id is not null and tx.payment_id is null ), 0)
- coalesce(sum(tx.amount) filter ( where type = 'REFUND' and tx.reward_id is not null and tx.payment_id is null ), 0) as total_rewarded,
coalesce(sum(tx.amount) filter ( where type = 'TRANSFER' and tx.payment_id is not null ), 0)
- coalesce(sum(tx.amount) filter ( where type = 'REFUND' and tx.payment_id is not null ), 0) as total_paid,
coalesce(sum(tx.amount) filter ( where type = 'BURN' and tx.reward_id is not null ), 0) as total_paid,
count(tx.id) as transaction_count
from accounting.all_transactions tx
Expand All @@ -55,15 +54,15 @@ left join lateral (select #group_by#
where #group_by# = :id
and tx.timestamp >= d.date
and tx.timestamp < d.date + interval '1 month'
and tx.type in ('DEPOSIT', 'WITHDRAW', 'TRANSFER', 'REFUND')
and tx.type in ('DEPOSIT', 'WITHDRAW', 'TRANSFER', 'REFUND', 'BURN')
and (
('DEPOSITED' in (:types) and tx.type = 'DEPOSIT' and tx.deposit_status != 'DRAFT') or
('ALLOCATED' in (:types) and tx.type = 'TRANSFER' and tx.program_id is not null and tx.project_id is null) or
('UNALLOCATED' in (:types) and tx.type = 'REFUND' and tx.program_id is not null and tx.project_id is null) or
('GRANTED' in (:types) and tx.type = 'TRANSFER' and tx.project_id is not null and tx.reward_id is null) or
('UNGRANTED' in (:types) and tx.type = 'REFUND' and tx.project_id is not null and tx.reward_id is null) or
('REWARDED' in (:types) and tx.type = 'TRANSFER' and tx.reward_id is not null and tx.payment_id is null) or
('PAID' in (:types) and tx.type = 'TRANSFER' and tx.payment_id is not null)
('PAID' in (:types) and tx.type = 'BURN' and tx.reward_id is not null)
)
and (cast(:search as text) is null or (concat(s.name, ' ', pgm.name, ' ', p.name, ' ', rr.login) ilike '%' || :search || '%'))
and (cast(:searchProjectsAndRecipients as text) is null or (concat(p.name, ' ', rr.login) ilike '%' || :searchProjectsAndRecipients || '%'))
Expand Down

0 comments on commit 4969c31

Please sign in to comment.