PoC of refactoring actions
This commit is contained in:
parent
6ee741fd5f
commit
e630997ff5
11 changed files with 158 additions and 56 deletions
|
@ -1,12 +1,16 @@
|
|||
use crate::{context::Context, utils::people::fetch_all_contacts};
|
||||
use crate::{
|
||||
actions::{action::Action, fetch_all_persons::FetchAllPersons},
|
||||
context::Context,
|
||||
};
|
||||
use color_eyre::eyre::Result;
|
||||
use log::*;
|
||||
|
||||
pub async fn missing_date_of_birth(ctx: Context) -> Result<()> {
|
||||
let contacts = fetch_all_contacts(&ctx.client).await?;
|
||||
let fetch_action = FetchAllPersons::new(());
|
||||
let contacts = fetch_action.execute(&ctx).await?;
|
||||
let mut filtered_contacts = contacts
|
||||
.iter()
|
||||
.filter(|c| c.birth_date.is_none() && !c.name.is_empty())
|
||||
.filter(|c| c.date_of_birth.is_none() && !c.name.is_empty())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
filtered_contacts.sort_by_key(|c| c.name.clone());
|
||||
|
|
|
@ -1,30 +1,37 @@
|
|||
use std::{fs, path::PathBuf};
|
||||
|
||||
use chrono::{Datelike, NaiveDate};
|
||||
use color_eyre::eyre::{ContextCompat, Result};
|
||||
use color_eyre::eyre::{Context as _, ContextCompat, Result};
|
||||
use log::*;
|
||||
use uuid::Uuid;
|
||||
use vcard4::{
|
||||
parse_loose,
|
||||
property::{DateAndOrTime, DateTimeOrTextProperty},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
actions::{
|
||||
action::Action,
|
||||
fetch_all_persons::FetchAllPersons,
|
||||
update_person_date_of_birth::{UpdatePersonDateOfBirth, UpdatePersonDateOfBirthArgs},
|
||||
},
|
||||
context::Context,
|
||||
types::{PersonResponseDto, PersonUpdateDto},
|
||||
utils::people::fetch_all_contacts,
|
||||
Client,
|
||||
};
|
||||
|
||||
pub async fn sync_date_of_birth(ctx: Context, vcard_file: &PathBuf) -> Result<()> {
|
||||
let server_contacts = fetch_all_contacts(&ctx.client).await?;
|
||||
let fetch_action = FetchAllPersons::new(());
|
||||
let server_contacts = fetch_action.execute(&ctx).await?;
|
||||
|
||||
let filtered_contacts: Vec<_> = server_contacts
|
||||
.iter()
|
||||
.filter(|p| !p.name.is_empty())
|
||||
.collect();
|
||||
|
||||
let contacts = fs::read_to_string(vcard_file)?;
|
||||
let contacts = fs::read_to_string(vcard_file).wrap_err_with(|| {
|
||||
format!(
|
||||
"Unable to read vcard file at {}",
|
||||
vcard_file.to_string_lossy()
|
||||
)
|
||||
})?;
|
||||
let cards = parse_loose(contacts)?;
|
||||
|
||||
let cards_with_bday: Vec<_> = cards.iter().filter(|c| c.bday.is_some()).collect();
|
||||
|
@ -46,13 +53,21 @@ pub async fn sync_date_of_birth(ctx: Context, vcard_file: &PathBuf) -> Result<()
|
|||
Some(c) => match card.bday.as_ref().unwrap() {
|
||||
DateTimeOrTextProperty::DateTime(dt) => {
|
||||
let bday = vcard_date_to_naive_date(dt.value.first().unwrap().clone())?;
|
||||
if c.birth_date.as_ref().map_or(false, |bdate| bday == *bdate) {
|
||||
if c.date_of_birth
|
||||
.as_ref()
|
||||
.map_or(false, |bdate| bday == *bdate)
|
||||
{
|
||||
debug!(
|
||||
"{} already has the proper date of birth, skipping",
|
||||
formatted_name
|
||||
);
|
||||
} else if bday.year() > 0 {
|
||||
update_person_bday(c, bday, &ctx.client, ctx.dry_run).await?;
|
||||
UpdatePersonDateOfBirth::new(UpdatePersonDateOfBirthArgs {
|
||||
person: (*c).clone(),
|
||||
date_of_birth: bday,
|
||||
})
|
||||
.execute(&ctx)
|
||||
.await?;
|
||||
updated_dobs += 1;
|
||||
} else {
|
||||
debug!(
|
||||
|
@ -90,27 +105,3 @@ fn vcard_date_to_naive_date(src: DateAndOrTime) -> Result<NaiveDate> {
|
|||
DateAndOrTime::Time(_time) => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
async fn update_person_bday(
|
||||
person: &PersonResponseDto,
|
||||
bday: NaiveDate,
|
||||
client: &Client,
|
||||
dry_run: bool,
|
||||
) -> Result<()> {
|
||||
let update = PersonUpdateDto {
|
||||
feature_face_asset_id: None,
|
||||
is_hidden: None,
|
||||
name: None,
|
||||
birth_date: Some(bday),
|
||||
};
|
||||
|
||||
info!("Updating {}: {:?}", person.name, update);
|
||||
|
||||
if !dry_run {
|
||||
client
|
||||
.update_person(&Uuid::parse_str(&person.id)?, &update)
|
||||
.await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue