Skip to main content

taskfast_cli/cmd/
payment.rs

1// SPDX-License-Identifier: MIT
2//! `taskfast payment` — task-scoped escrow state and agent-wide earnings.
3//!
4//! `get <task_id>` → `GET /tasks/{id}/payment` (escrow breakdown for a task).
5//! `list` → `GET /agents/me/payments` (paginated earnings ledger with
6//! optional status + date-range filters, used to reconcile on-chain events).
7
8use clap::{Parser, Subcommand, ValueEnum};
9use serde_json::json;
10use uuid::Uuid;
11
12use super::{CmdError, CmdResult, Ctx};
13use crate::envelope::Envelope;
14
15use taskfast_client::api::types::ListAgentPaymentsStatus;
16use taskfast_client::map_api_error;
17
18#[derive(Debug, Subcommand)]
19pub enum Command {
20    /// Get the escrow breakdown for a specific task.
21    Get(GetArgs),
22    /// List the caller's payment history (earnings ledger).
23    List(ListArgs),
24}
25
26#[derive(Debug, Parser)]
27pub struct GetArgs {
28    pub task_id: String,
29}
30
31#[derive(Debug, Parser)]
32pub struct ListArgs {
33    #[arg(long)]
34    pub status: Option<PaymentStatusFilter>,
35
36    /// ISO-8601 date (inclusive) lower bound.
37    #[arg(long)]
38    pub from: Option<chrono::NaiveDate>,
39
40    /// ISO-8601 date (inclusive) upper bound.
41    #[arg(long)]
42    pub to: Option<chrono::NaiveDate>,
43
44    #[arg(long)]
45    pub cursor: Option<String>,
46
47    /// Max items per page. Defaults to 50 — earnings ledger pulls
48    /// typically scan a wider window than active worker queues.
49    #[arg(long, default_value_t = 50)]
50    pub limit: i64,
51}
52
53/// clap-friendly mirror of `ListAgentPaymentsStatus`.
54#[derive(Debug, Clone, Copy, ValueEnum)]
55pub enum PaymentStatusFilter {
56    Pending,
57    Disbursed,
58    Held,
59    Failed,
60}
61
62impl From<PaymentStatusFilter> for ListAgentPaymentsStatus {
63    fn from(s: PaymentStatusFilter) -> Self {
64        match s {
65            PaymentStatusFilter::Pending => Self::Pending,
66            PaymentStatusFilter::Disbursed => Self::Disbursed,
67            PaymentStatusFilter::Held => Self::Held,
68            PaymentStatusFilter::Failed => Self::Failed,
69        }
70    }
71}
72
73pub async fn run(ctx: &Ctx, cmd: Command) -> CmdResult {
74    match cmd {
75        Command::Get(a) => get(ctx, a).await,
76        Command::List(a) => list(ctx, a).await,
77    }
78}
79
80async fn get(ctx: &Ctx, args: GetArgs) -> CmdResult {
81    let task_id = Uuid::parse_str(&args.task_id)
82        .map_err(|e| CmdError::Usage(format!("task id must be a UUID: {e}")))?;
83    let client = ctx.client()?;
84    let resp = match client.inner().get_task_payment(&task_id).await {
85        Ok(v) => v.into_inner(),
86        Err(e) => return Err(map_api_error(e).await.into()),
87    };
88    Ok(Envelope::success(
89        ctx.environment,
90        ctx.dry_run,
91        json!({
92            "task_id": task_id.to_string(),
93            "payment": resp,
94        }),
95    ))
96}
97
98async fn list(ctx: &Ctx, args: ListArgs) -> CmdResult {
99    let client = ctx.client()?;
100    let resp = match client
101        .inner()
102        .list_agent_payments(
103            args.cursor.as_deref(),
104            args.from.as_ref(),
105            taskfast_client::page_limit(args.limit),
106            args.status.map(Into::into),
107            args.to.as_ref(),
108        )
109        .await
110    {
111        Ok(v) => v.into_inner(),
112        Err(e) => return Err(map_api_error(e).await.into()),
113    };
114    Ok(Envelope::success(
115        ctx.environment,
116        ctx.dry_run,
117        json!({
118            "payments": resp.data,
119            "summary": resp.summary,
120            "meta": resp.meta,
121        }),
122    ))
123}