Make description field optional and allow for a fixed lookback

This commit is contained in:
Joshua Coles 2024-07-27 18:39:31 +01:00
parent 3f17c45297
commit 3f2bbdd229
3 changed files with 16 additions and 6 deletions

View File

@ -0,0 +1 @@
alter table time_entries alter column description drop not null;

View File

@ -1,4 +1,4 @@
use chrono::{TimeDelta, Utc}; use chrono::{DateTime, TimeDelta, Utc};
use sqlx::{Connection, PgConnection}; use sqlx::{Connection, PgConnection};
use toggl::TogglApi; use toggl::TogglApi;
@ -51,18 +51,27 @@ impl Worker {
tag_ids: tag_ids.iter().map(|row| row.id as u64).collect(), tag_ids: tag_ids.iter().map(|row| row.id as u64).collect(),
}) })
} }
async fn fetch_changed_since(&mut self, look_back: TimeDelta) -> Result<(), AppError> {
self.update_time_entries(Utc::now() - look_back).await
}
async fn update(&mut self, default_look_back: TimeDelta) -> Result<(), AppError> { async fn update(&mut self, default_look_back: TimeDelta) -> Result<(), AppError> {
let result = sqlx::query!("select max(updated_at) as last_updated_at from time_entries") let result = sqlx::query!("select max(updated_at) as last_updated_at from time_entries")
.fetch_optional(&mut self.db) .fetch_optional(&mut self.db)
.await .await
.expect("Could not fetch max updated_at from time_entries"); .expect("Could not fetch max updated_at from time_entries");
let existing_ids = self.get_ids().await?;
let fetch_since = result let fetch_since = result
.and_then(|record| record.last_updated_at) .and_then(|record| record.last_updated_at)
.unwrap_or_else(|| Utc::now() - default_look_back); .unwrap_or_else(|| Utc::now() - default_look_back);
self.update_time_entries(fetch_since).await
}
async fn update_time_entries(&mut self, fetch_since: DateTime<Utc>) -> Result<(), AppError> {
let existing_ids = self.get_ids().await?;
let time_entries = self.toggl_api let time_entries = self.toggl_api
.get_time_entries_for_user_modified_since(fetch_since).await?; .get_time_entries_for_user_modified_since(fetch_since).await?;
@ -308,7 +317,7 @@ async fn main() {
toggl_api: api, toggl_api: api,
}; };
worker.update(TimeDelta::days(90)) worker.fetch_changed_since(TimeDelta::days(90))
.await .await
.unwrap(); .unwrap();
} }

View File

@ -265,7 +265,7 @@ pub mod types {
#[serde(rename = "at")] #[serde(rename = "at")]
pub updated_at: DateTime<Utc>, pub updated_at: DateTime<Utc>,
pub description: String, pub description: Option<String>,
#[serde(default)] #[serde(default)]
pub tags: Vec<String>, pub tags: Vec<String>,
@ -397,7 +397,7 @@ pub mod types {
pub project_id: Option<u64>, pub project_id: Option<u64>,
pub task_id: Option<u64>, pub task_id: Option<u64>,
pub billable: bool, pub billable: bool,
pub description: String, pub description: Option<String>,
pub tag_ids: Vec<u64>, pub tag_ids: Vec<u64>,
pub billable_amount_in_cents: Option<u64>, pub billable_amount_in_cents: Option<u64>,
pub hourly_rate_in_cents: Option<u64>, pub hourly_rate_in_cents: Option<u64>,