Allow multiple reminder dates for a single chat group

This commit is contained in:
Manuel Vögele
2024-01-17 17:02:20 +01:00
parent bc2f647243
commit a4479e6a9d
8 changed files with 160 additions and 73 deletions

View File

@@ -1,33 +1,47 @@
use chrono::{DateTime, TimeZone, Utc};
use diesel::Queryable;
use diesel::{
associations::{Associations, Identifiable},
Queryable, Selectable,
};
use crate::appointment::Appointment;
use crate::schema::{chat, reminder};
#[derive(Queryable)]
#[derive(Queryable, Selectable, Identifiable)]
#[diesel(table_name = chat)]
pub struct DbChat {
_id: i32,
id: i32,
telegram_id: i64,
calendar: String,
next_appointment_start: Option<i64>,
next_appointment_end: Option<i64>,
last_reminder: Option<i64>,
pinned_message: Option<i32>,
pinned_message_id: Option<i32>,
locale: Option<String>,
remind_days_ahead: i64,
}
#[derive(Queryable, Selectable, Identifiable, Associations)]
#[diesel(belongs_to(DbChat, foreign_key=chat_id))]
#[diesel(table_name = reminder)]
pub struct DbReminder {
id: i32,
chat_id: i32,
days_ahead: i64,
}
pub struct ChatInfo<Tz: TimeZone> {
pub db_id: i32,
pub id: i64,
pub calendar: String,
pub next_appointment: Option<Appointment<Tz>>,
pub last_reminder: Option<DateTime<Tz>>,
pub pinned_message_id: Option<i32>,
pub locale: String,
pub remind_days_ahead: u64,
pub remind_days_ahead: Vec<u64>,
}
impl From<DbChat> for ChatInfo<Utc> {
fn from(db_chat: DbChat) -> Self {
impl ChatInfo<Utc> {
pub fn from_db(db_chat: DbChat, db_reminders: Vec<DbReminder>) -> Self {
let next_appointment = db_chat
.next_appointment_start
// Join appointments into single option
@@ -43,14 +57,18 @@ impl From<DbChat> for ChatInfo<Utc> {
let locale = db_chat.locale.unwrap_or("de".into());
let remind_days_ahead = db_chat.remind_days_ahead.try_into().unwrap_or(0);
let remind_days_ahead = db_reminders
.into_iter()
.map(|reminder| reminder.days_ahead.try_into().unwrap_or(0))
.collect();
ChatInfo {
db_id: db_chat.id,
id: db_chat.telegram_id,
calendar: db_chat.calendar,
next_appointment: next_appointment,
last_reminder,
pinned_message_id: db_chat.pinned_message,
pinned_message_id: db_chat.pinned_message_id,
locale,
remind_days_ahead,
}