diff --git a/src/grist.rs b/src/grist.rs index cf22d91..e32f180 100644 --- a/src/grist.rs +++ b/src/grist.rs @@ -4,6 +4,7 @@ use chrono::{DateTime, FixedOffset}; use clap::ValueEnum; use clap::builder::PossibleValue; use grist_client::apis::configuration::Configuration; +use grist_client::apis::data_api::delete_rows; use grist_client::apis::records_api::{add_records, list_records}; use grist_client::models::{ RecordsList, RecordsListRecordsInner, RecordsWithoutId, RecordsWithoutIdRecordsInner, @@ -284,12 +285,14 @@ impl Display for Shift { #[derive(Clone, Debug)] pub struct Inscription { + pub id: u64, pub shift_id: u64, pub person_name: String, } impl Inscription { - pub fn new(shift_id: u64, person_name: String) -> Self { + pub fn new(id: u64, shift_id: u64, person_name: String) -> Self { Self { + id: id, shift_id: shift_id, person_name: person_name, } @@ -398,6 +401,29 @@ pub async fn get_inscriptions() -> Res { get_records(SHIFTS_DOC, INSCRIPTIONS_TABLE, None).await } +pub async fn get_specific_inscription( + shift_id: u64, + user_matrix_id: String, +) -> Option { + let inscriptions = get_inscriptions().await.ok()?.records; + inscriptions + .into_iter() + .find(|inscription| { + get_number(&inscription, "Shift").expect("failed to get shift column") == shift_id + && get_string(&inscription, "Personne").expect("failed to get personne column") + == user_matrix_id + }) + .map(|record| { + Inscription::new( + record.id, + get_number(&record, "Shift").expect("can't get shift column"), + get_string(&record, "Personne") + .expect("can't get personne column") + .into(), + ) + }) +} + pub async fn get_inscription(index: u64) -> Res { let record_list = get_inscriptions().await?; let record = record_list @@ -407,6 +433,7 @@ pub async fn get_inscription(index: u64) -> Res { .ok_or(anyhow!("can't find inscription"))? .clone(); Ok(Inscription::new( + record.id, get_number(&record, "Shift")?, get_string(&record, "Personne")?.into(), )) @@ -461,24 +488,35 @@ pub async fn list_shifts() -> Res> { } // this is weird that we use both wordings, inscription and (un)subscribe -pub async fn subscribe(id: u64, user_id: String) -> Res<()> { - let inscriptions = get_inscriptions().await?.records; - if let Some(_) = inscriptions.into_iter().find(|inscription| { - get_number(&inscription, "Shift").expect("failed to get shift column") == id - && get_string(&inscription, "Personne").expect("failed to get personne column") - == user_id - }) { +pub async fn subscribe(shift_id: u64, user_matrix_id: String) -> Res<()> { + if get_specific_inscription(shift_id, user_matrix_id.clone()) + .await + .is_some() + { return Ok(()); } + let fields = vec![ - ("Shift", serde_json::to_value(id)?), - ("Personne", serde_json::to_value(user_id)?), + ("Shift", serde_json::to_value(shift_id)?), + ("Personne", serde_json::to_value(user_matrix_id)?), ]; add_record(SHIFTS_DOC, INSCRIPTIONS_TABLE, fields).await?; Ok(()) } -pub async fn unsubscribe(id: u64, user_id: String) -> Res<()> { +pub async fn unsubscribe(shift_id: u64, user_matrix_id: String) -> Res<()> { + if let Some(inscription) = get_specific_inscription(shift_id, user_matrix_id).await { + let conf = conf::get(); + let api_key = conf.grist_api_key(); + let grist_conf = Configuration::new(API_URL.into(), Some(api_key.into())); + delete_rows( + &grist_conf, + SHIFTS_DOC, + INSCRIPTIONS_TABLE, + vec![inscription.id as i32], + ) + .await?; + } Ok(()) }