From 4969c3155d6308ab12f871f62850159d967ac606 Mon Sep 17 00:00:00 2001 From: Anthony Buisset Date: Wed, 20 Nov 2024 10:37:12 +0100 Subject: [PATCH] fix paid amount in /bi/financials --- .../api/it/api/ReadBiFinancialStatsApiIT.java | 63 ++++++++++++++----- ...BiFinancialMonthlyStatsReadRepository.java | 7 +-- 2 files changed, 51 insertions(+), 19 deletions(-) diff --git a/bootstrap/src/test/java/onlydust/com/marketplace/api/it/api/ReadBiFinancialStatsApiIT.java b/bootstrap/src/test/java/onlydust/com/marketplace/api/it/api/ReadBiFinancialStatsApiIT.java index fcb38ab02..7d84e0485 100644 --- a/bootstrap/src/test/java/onlydust/com/marketplace/api/it/api/ReadBiFinancialStatsApiIT.java +++ b/bootstrap/src/test/java/onlydust/com/marketplace/api/it/api/ReadBiFinancialStatsApiIT.java @@ -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; @@ -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(); } @@ -213,7 +247,6 @@ void should_get_recipient_bi_financial_stats() { .expectStatus() .isOk() .expectBody() - .consumeWith(System.out::println) .json(""" { "totalDeposited": { @@ -276,11 +309,11 @@ 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", @@ -288,13 +321,13 @@ void should_get_recipient_bi_financial_stats() { "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(""" @@ -730,7 +763,7 @@ void should_get_recipient_bi_financial_stats() { ] }, "totalPaid": { - "totalUsdEquivalent": 404.00, + "totalUsdEquivalent": 505.00, "totalPerCurrency": [ { "amount": 0, @@ -747,8 +780,8 @@ 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", @@ -756,7 +789,7 @@ void should_get_recipient_bi_financial_stats() { "logoUrl": "https://s2.coinmarketcap.com/static/img/coins/64x64/3408.png", "decimals": 6 }, - "usdEquivalent": 404.00, + "usdEquivalent": 505.00, "usdConversionRate": 1.010001, "ratio": 100 }, @@ -776,7 +809,7 @@ void should_get_recipient_bi_financial_stats() { } ] }, - "transactionCount": 3 + "transactionCount": 4 } """)) .jsonPath("$.stats[?(@.date == '2024-09-01')]").value(jsonObjectEquals(""" diff --git a/read-api/src/main/java/onlydust/com/marketplace/api/read/repositories/BiFinancialMonthlyStatsReadRepository.java b/read-api/src/main/java/onlydust/com/marketplace/api/read/repositories/BiFinancialMonthlyStatsReadRepository.java index 75bd3cf0a..558008ec7 100644 --- a/read-api/src/main/java/onlydust/com/marketplace/api/read/repositories/BiFinancialMonthlyStatsReadRepository.java +++ b/read-api/src/main/java/onlydust/com/marketplace/api/read/repositories/BiFinancialMonthlyStatsReadRepository.java @@ -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 @@ -55,7 +54,7 @@ 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 @@ -63,7 +62,7 @@ and tx.type in ('DEPOSIT', 'WITHDRAW', 'TRANSFER', 'REFUND') ('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 || '%'))