diff --git a/src/args.rs b/src/args.rs index 13d2ea3..d40696f 100644 --- a/src/args.rs +++ b/src/args.rs @@ -39,6 +39,9 @@ pub(crate) enum PeopleCommands { #[arg(short, long)] vcard_file: PathBuf, }, + + /// Lists the people without date of birth + MissingDateOfBirths {}, } #[derive(Subcommand)] diff --git a/src/commands/missing_date_of_birth.rs b/src/commands/missing_date_of_birth.rs new file mode 100644 index 0000000..94fd825 --- /dev/null +++ b/src/commands/missing_date_of_birth.rs @@ -0,0 +1,19 @@ +use crate::{utils::people::fetch_all_contacts, Client}; +use anyhow::Result; +use log::*; + +pub async fn missing_date_of_birth(client: &Client) -> Result<()> { + let contacts = fetch_all_contacts(client).await?; + let mut filtered_contacts = contacts + .iter() + .filter(|c| c.birth_date.is_none() && !c.name.is_empty()) + .collect::>(); + + filtered_contacts.sort_by_key(|c| c.name.clone()); + + for c in filtered_contacts { + info!("{}", c.name); + } + + Ok(()) +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 6bca56d..5d5b0ac 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,2 +1,3 @@ +pub mod missing_date_of_birth; pub mod server_version; pub mod sync_date_of_birth; diff --git a/src/commands/sync_date_of_birth.rs b/src/commands/sync_date_of_birth.rs index d022627..7689814 100644 --- a/src/commands/sync_date_of_birth.rs +++ b/src/commands/sync_date_of_birth.rs @@ -11,6 +11,7 @@ use vcard4::{ use crate::{ types::{PersonResponseDto, PersonUpdateDto}, + utils::people::fetch_all_contacts, Client, }; @@ -118,20 +119,3 @@ async fn update_person_bday( Ok(()) } - -async fn fetch_all_contacts(client: &Client) -> Result> { - let mut all_people = Vec::new(); - let mut page_number = 1; - let mut has_next_page = true; - - while has_next_page { - let fetched = client - .get_all_people(Some(page_number.into()), None, Some(true)) - .await?; - all_people.extend(fetched.people.clone()); - has_next_page = fetched.has_next_page.expect("Missing has_next_page"); - page_number += 1; - } - - Ok(all_people) -} diff --git a/src/main.rs b/src/main.rs index f167507..f43deb0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,12 +4,14 @@ use crate::args::Commands; use anyhow::Result; use args::{PeopleCommands, ServerCommands}; use clap::Parser; +use commands::missing_date_of_birth::missing_date_of_birth; use commands::server_version::server_version; use commands::sync_date_of_birth::sync_date_of_birth; use reqwest::header; mod args; mod commands; +mod utils; #[tokio::main] async fn main() { @@ -21,6 +23,7 @@ async fn main() { let res = match &args.command { Commands::People { people_command } => match people_command { + PeopleCommands::MissingDateOfBirths {} => missing_date_of_birth(&client).await, PeopleCommands::SyncDateOfBirths { vcard_file } => { sync_date_of_birth(&client, vcard_file, args.dry_run).await } diff --git a/src/utils/mod.rs b/src/utils/mod.rs new file mode 100644 index 0000000..24eb2de --- /dev/null +++ b/src/utils/mod.rs @@ -0,0 +1 @@ +pub mod people; diff --git a/src/utils/people.rs b/src/utils/people.rs new file mode 100644 index 0000000..039e9e2 --- /dev/null +++ b/src/utils/people.rs @@ -0,0 +1,19 @@ +use crate::{types::PersonResponseDto, Client}; +use anyhow::Result; + +pub async fn fetch_all_contacts(client: &Client) -> Result> { + let mut all_people = Vec::new(); + let mut page_number = 1; + let mut has_next_page = true; + + while has_next_page { + let fetched = client + .get_all_people(Some(page_number.into()), None, Some(true)) + .await?; + all_people.extend(fetched.people.clone()); + has_next_page = fetched.has_next_page.expect("Missing has_next_page"); + page_number += 1; + } + + Ok(all_people) +}