taskfast_cli/cmd/
review.rs1use clap::{Parser, Subcommand};
9use serde_json::json;
10use uuid::Uuid;
11
12use super::{CmdError, CmdResult, Ctx};
13use crate::envelope::Envelope;
14
15use taskfast_client::api::types::{ReviewCreateRequest, ReviewCreateRequestComment};
16use taskfast_client::map_api_error;
17
18#[derive(Debug, Subcommand)]
19pub enum Command {
20 Create(CreateArgs),
22 List(ListArgs),
24}
25
26#[derive(Debug, Parser)]
27pub struct CreateArgs {
28 pub task_id: String,
29
30 #[arg(long)]
32 pub reviewee_id: String,
33
34 #[arg(long)]
36 pub rating: i64,
37
38 #[arg(long)]
40 pub comment: String,
41}
42
43#[derive(Debug, Parser)]
44pub struct ListArgs {
45 #[arg(long, conflicts_with = "agent")]
47 pub task: Option<String>,
48
49 #[arg(long)]
51 pub agent: Option<String>,
52
53 #[arg(long)]
54 pub cursor: Option<String>,
55
56 #[arg(long)]
57 pub limit: Option<i64>,
58}
59
60pub async fn run(ctx: &Ctx, cmd: Command) -> CmdResult {
61 match cmd {
62 Command::Create(a) => create(ctx, a).await,
63 Command::List(a) => list(ctx, a).await,
64 }
65}
66
67async fn create(ctx: &Ctx, args: CreateArgs) -> CmdResult {
68 let task_id = Uuid::parse_str(&args.task_id)
69 .map_err(|e| CmdError::Usage(format!("task id must be a UUID: {e}")))?;
70 let reviewee_id = Uuid::parse_str(&args.reviewee_id)
71 .map_err(|e| CmdError::Usage(format!("--reviewee-id must be a UUID: {e}")))?;
72 if !(1..=5).contains(&args.rating) {
73 return Err(CmdError::Usage("--rating must be an integer 1..=5".into()));
74 }
75 if args.comment.trim().is_empty() {
76 return Err(CmdError::Usage("--comment must not be empty".into()));
77 }
78 let comment = ReviewCreateRequestComment::try_from(args.comment.as_str())
79 .map_err(|e| CmdError::Usage(format!("--comment rejected by schema: {e}")))?;
80
81 if ctx.dry_run {
82 return Ok(Envelope::success(
83 ctx.environment,
84 ctx.dry_run,
85 json!({
86 "action": "would_create_review",
87 "task_id": task_id.to_string(),
88 "reviewee_id": reviewee_id.to_string(),
89 "rating": args.rating,
90 "comment": args.comment,
91 }),
92 ));
93 }
94
95 let body = ReviewCreateRequest {
96 comment,
97 rating: std::num::NonZeroU64::new(args.rating as u64)
100 .expect("rating validated to 1..=5 above"),
101 reviewee_id,
102 };
103 let client = ctx.client()?;
104 let resp = match client.inner().create_review(&task_id, &body).await {
105 Ok(v) => v.into_inner(),
106 Err(e) => return Err(map_api_error(e).await.into()),
107 };
108 Ok(Envelope::success(
109 ctx.environment,
110 ctx.dry_run,
111 json!({ "review": resp }),
112 ))
113}
114
115async fn list(ctx: &Ctx, args: ListArgs) -> CmdResult {
116 let client = ctx.client()?;
118 match (args.task.as_deref(), args.agent.as_deref()) {
119 (Some(t), None) => {
120 let task_id = Uuid::parse_str(t)
121 .map_err(|e| CmdError::Usage(format!("--task must be a UUID: {e}")))?;
122 let resp = match client
123 .inner()
124 .list_task_reviews(
125 &task_id,
126 args.cursor.as_deref(),
127 args.limit.and_then(taskfast_client::page_limit),
128 )
129 .await
130 {
131 Ok(v) => v.into_inner(),
132 Err(e) => return Err(map_api_error(e).await.into()),
133 };
134 Ok(Envelope::success(
135 ctx.environment,
136 ctx.dry_run,
137 json!({
138 "task_id": task_id.to_string(),
139 "reviews": resp.data,
140 "meta": resp.meta,
141 }),
142 ))
143 }
144 (None, Some(a)) => {
145 let agent_id = Uuid::parse_str(a)
146 .map_err(|e| CmdError::Usage(format!("--agent must be a UUID: {e}")))?;
147 let resp = match client
148 .inner()
149 .list_agent_reviews(
150 &agent_id,
151 args.cursor.as_deref(),
152 args.limit.and_then(taskfast_client::page_limit),
153 )
154 .await
155 {
156 Ok(v) => v.into_inner(),
157 Err(e) => return Err(map_api_error(e).await.into()),
158 };
159 Ok(Envelope::success(
160 ctx.environment,
161 ctx.dry_run,
162 json!({
163 "agent_id": agent_id.to_string(),
164 "reviews": resp.data,
165 "meta": resp.meta,
166 }),
167 ))
168 }
169 (None, None) => Err(CmdError::Usage(
170 "review list requires --task <id> or --agent <id>".into(),
171 )),
172 (Some(_), Some(_)) => unreachable!("clap conflicts_with enforces this"),
173 }
174}