Skip to content

Commit

Permalink
refactor: paginate list-payouts cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
thevaibhav-dixit committed May 6, 2024
1 parent 40dd2fa commit a2dd889
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 14 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions proto/api/bria.proto
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,8 @@ message SubmitPayoutResponse {

message ListPayoutsRequest {
string wallet_name = 1;
optional uint64 page = 2;
optional uint64 page_size = 3;
}

message BriaWalletDestination {
Expand Down
8 changes: 7 additions & 1 deletion src/api/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,9 +667,15 @@ impl BriaService for Bria {

let key = extract_api_token(&request)?;
let profile = self.app.authenticate(key).await?;
let request = request.into_inner();
let ListPayoutsRequest {
wallet_name,
page,
page_size,
} = request;
let payouts = self
.app
.list_payouts(&profile, request.into_inner().wallet_name)
.list_payouts(&profile, wallet_name, page, page_size)
.await?;

let payout_messages: Vec<proto::Payout> =
Expand Down
4 changes: 3 additions & 1 deletion src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,14 +944,16 @@ impl App {
&self,
profile: &Profile,
wallet_name: String,
page: Option<u64>,
page_size: Option<u64>,
) -> Result<Vec<PayoutWithInclusionEstimate>, ApplicationError> {
let wallet = self
.wallets
.find_by_name(profile.account_id, wallet_name)
.await?;
let payouts = self
.payouts
.list_for_wallet(profile.account_id, wallet.id)
.list_for_wallet(profile.account_id, wallet.id, page, page_size)
.await?;

Ok(self
Expand Down
9 changes: 8 additions & 1 deletion src/cli/api_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,16 @@ impl ApiClient {
output_json(response)
}

pub async fn list_payouts(&self, wallet: String) -> anyhow::Result<()> {
pub async fn list_payouts(
&self,
wallet: String,
page: Option<u64>,
page_size: Option<u64>,
) -> anyhow::Result<()> {
let request = tonic::Request::new(proto::ListPayoutsRequest {
wallet_name: wallet,
page,
page_size,
});
let response = self
.connect()
Expand Down
8 changes: 7 additions & 1 deletion src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,10 @@ enum Command {
api_key: String,
#[clap(short, long)]
wallet: String,
#[clap(short, long)]
page: Option<u64>,
#[clap(short = 's', long = "page-size")]
page_size: Option<u64>,
},
/// Find Payout By external id or payout_id
GetPayout {
Expand Down Expand Up @@ -959,9 +963,11 @@ pub async fn run() -> anyhow::Result<()> {
url,
api_key,
wallet,
page,
page_size,
} => {
let client = api_client(cli.bria_home, url, api_key);
client.list_payouts(wallet).await?;
client.list_payouts(wallet, page, page_size).await?;
}
Command::GetPayout {
url,
Expand Down
23 changes: 16 additions & 7 deletions src/payout/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,25 @@ impl Payouts {
&self,
account_id: AccountId,
wallet_id: WalletId,
page: Option<u64>,
page_size: Option<u64>,
) -> Result<Vec<Payout>, PayoutError> {
let page = page.unwrap_or(1);
let page_size = page_size.unwrap_or(100) * 2;
let offset = (page - 1) * page_size;

let rows = sqlx::query!(
r#"
SELECT b.*, e.sequence, e.event
FROM bria_payouts b
JOIN bria_payout_events e ON b.id = e.id
WHERE b.account_id = $1 AND b.wallet_id = $2
ORDER BY b.created_at, b.id, e.sequence FOR UPDATE"#,
Uuid::from(account_id),
Uuid::from(wallet_id)
SELECT b.*, e.sequence, e.event
FROM bria_payouts b
JOIN bria_payout_events e ON b.id = e.id
WHERE b.account_id = $1 AND b.wallet_id = $2
ORDER BY b.created_at, b.id, e.sequence
LIMIT $3 OFFSET $4 FOR UPDATE"#,
account_id as AccountId,
wallet_id as WalletId,
page_size as i64,
offset as i64,
)
.fetch_all(&self.pool)
.await?;
Expand Down

0 comments on commit a2dd889

Please sign in to comment.