1use crate::client::contact::{Contact, ContactClient};
2use crate::config::Config;
3use crate::error::YukiError;
4use crate::output::{OutputFormat, format_json, format_table, is_tty};
5
6fn contacts_to_rows(contacts: &[Contact]) -> Vec<Vec<String>> {
7 contacts
8 .iter()
9 .map(|c| {
10 vec![
11 c.id.clone(),
12 c.name.clone(),
13 c.contact_type.clone(),
14 c.country.clone(),
15 if c.is_supplier { "Yes" } else { "No" }.to_string(),
16 if c.is_customer { "Yes" } else { "No" }.to_string(),
17 ]
18 })
19 .collect()
20}
21
22pub async fn search(
23 config: &Config,
24 _admin: Option<&str>,
25 query: &str,
26 format: Option<&str>,
27) -> Result<(), YukiError> {
28 let mut client = ContactClient::new();
29 client.authenticate(&config.api_key).await?;
30 let contacts = client.search_contacts(query).await?;
31
32 let headers = vec![
33 "ID".into(),
34 "Name".into(),
35 "Type".into(),
36 "Country".into(),
37 "Supplier".into(),
38 "Customer".into(),
39 ];
40 let rows = contacts_to_rows(&contacts);
41
42 let fmt = OutputFormat::from_flag(format, is_tty());
43 match fmt {
44 OutputFormat::Table => println!("{}", format_table(&headers, &rows)),
45 OutputFormat::Json => println!("{}", format_json(&headers, &rows)),
46 }
47 Ok(())
48}
49
50pub async fn list(
51 config: &Config,
52 _admin: Option<&str>,
53 contact_type: Option<&str>,
54 format: Option<&str>,
55) -> Result<(), YukiError> {
56 let mut client = ContactClient::new();
57 client.authenticate(&config.api_key).await?;
58 let contacts = client
59 .get_suppliers_and_customers(contact_type.unwrap_or(""))
60 .await?;
61
62 let headers = vec![
63 "ID".into(),
64 "Name".into(),
65 "Type".into(),
66 "Country".into(),
67 "Supplier".into(),
68 "Customer".into(),
69 ];
70 let rows = contacts_to_rows(&contacts);
71
72 let fmt = OutputFormat::from_flag(format, is_tty());
73 match fmt {
74 OutputFormat::Table => println!("{}", format_table(&headers, &rows)),
75 OutputFormat::Json => println!("{}", format_json(&headers, &rows)),
76 }
77 Ok(())
78}