add unsubscribe command
This commit is contained in:
parent
578f28f51a
commit
a208f352bc
1 changed files with 49 additions and 11 deletions
60
src/grist.rs
60
src/grist.rs
|
|
@ -4,6 +4,7 @@ use chrono::{DateTime, FixedOffset};
|
||||||
use clap::ValueEnum;
|
use clap::ValueEnum;
|
||||||
use clap::builder::PossibleValue;
|
use clap::builder::PossibleValue;
|
||||||
use grist_client::apis::configuration::Configuration;
|
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::apis::records_api::{add_records, list_records};
|
||||||
use grist_client::models::{
|
use grist_client::models::{
|
||||||
RecordsList, RecordsListRecordsInner, RecordsWithoutId, RecordsWithoutIdRecordsInner,
|
RecordsList, RecordsListRecordsInner, RecordsWithoutId, RecordsWithoutIdRecordsInner,
|
||||||
|
|
@ -284,12 +285,14 @@ impl Display for Shift {
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Inscription {
|
pub struct Inscription {
|
||||||
|
pub id: u64,
|
||||||
pub shift_id: u64,
|
pub shift_id: u64,
|
||||||
pub person_name: String,
|
pub person_name: String,
|
||||||
}
|
}
|
||||||
impl Inscription {
|
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 {
|
Self {
|
||||||
|
id: id,
|
||||||
shift_id: shift_id,
|
shift_id: shift_id,
|
||||||
person_name: person_name,
|
person_name: person_name,
|
||||||
}
|
}
|
||||||
|
|
@ -398,6 +401,29 @@ pub async fn get_inscriptions() -> Res<RecordsList> {
|
||||||
get_records(SHIFTS_DOC, INSCRIPTIONS_TABLE, None).await
|
get_records(SHIFTS_DOC, INSCRIPTIONS_TABLE, None).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_specific_inscription(
|
||||||
|
shift_id: u64,
|
||||||
|
user_matrix_id: String,
|
||||||
|
) -> Option<Inscription> {
|
||||||
|
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<Inscription> {
|
pub async fn get_inscription(index: u64) -> Res<Inscription> {
|
||||||
let record_list = get_inscriptions().await?;
|
let record_list = get_inscriptions().await?;
|
||||||
let record = record_list
|
let record = record_list
|
||||||
|
|
@ -407,6 +433,7 @@ pub async fn get_inscription(index: u64) -> Res<Inscription> {
|
||||||
.ok_or(anyhow!("can't find inscription"))?
|
.ok_or(anyhow!("can't find inscription"))?
|
||||||
.clone();
|
.clone();
|
||||||
Ok(Inscription::new(
|
Ok(Inscription::new(
|
||||||
|
record.id,
|
||||||
get_number(&record, "Shift")?,
|
get_number(&record, "Shift")?,
|
||||||
get_string(&record, "Personne")?.into(),
|
get_string(&record, "Personne")?.into(),
|
||||||
))
|
))
|
||||||
|
|
@ -461,24 +488,35 @@ pub async fn list_shifts() -> Res<Vec<Shift>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// this is weird that we use both wordings, inscription and (un)subscribe
|
// this is weird that we use both wordings, inscription and (un)subscribe
|
||||||
pub async fn subscribe(id: u64, user_id: String) -> Res<()> {
|
pub async fn subscribe(shift_id: u64, user_matrix_id: String) -> Res<()> {
|
||||||
let inscriptions = get_inscriptions().await?.records;
|
if get_specific_inscription(shift_id, user_matrix_id.clone())
|
||||||
if let Some(_) = inscriptions.into_iter().find(|inscription| {
|
.await
|
||||||
get_number(&inscription, "Shift").expect("failed to get shift column") == id
|
.is_some()
|
||||||
&& get_string(&inscription, "Personne").expect("failed to get personne column")
|
{
|
||||||
== user_id
|
|
||||||
}) {
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let fields = vec![
|
let fields = vec![
|
||||||
("Shift", serde_json::to_value(id)?),
|
("Shift", serde_json::to_value(shift_id)?),
|
||||||
("Personne", serde_json::to_value(user_id)?),
|
("Personne", serde_json::to_value(user_matrix_id)?),
|
||||||
];
|
];
|
||||||
add_record(SHIFTS_DOC, INSCRIPTIONS_TABLE, fields).await?;
|
add_record(SHIFTS_DOC, INSCRIPTIONS_TABLE, fields).await?;
|
||||||
|
|
||||||
Ok(())
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue