Make description field optional and allow for a fixed lookback
This commit is contained in:
parent
3f17c45297
commit
3f2bbdd229
1
migrations/20240727173725_make_description_optional.sql
Normal file
1
migrations/20240727173725_make_description_optional.sql
Normal file
@ -0,0 +1 @@
|
||||
alter table time_entries alter column description drop not null;
|
||||
17
src/main.rs
17
src/main.rs
@ -1,4 +1,4 @@
|
||||
use chrono::{TimeDelta, Utc};
|
||||
use chrono::{DateTime, TimeDelta, Utc};
|
||||
use sqlx::{Connection, PgConnection};
|
||||
use toggl::TogglApi;
|
||||
|
||||
@ -51,18 +51,27 @@ impl Worker {
|
||||
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> {
|
||||
let result = sqlx::query!("select max(updated_at) as last_updated_at from time_entries")
|
||||
.fetch_optional(&mut self.db)
|
||||
.await
|
||||
.expect("Could not fetch max updated_at from time_entries");
|
||||
|
||||
let existing_ids = self.get_ids().await?;
|
||||
|
||||
let fetch_since = result
|
||||
.and_then(|record| record.last_updated_at)
|
||||
.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
|
||||
.get_time_entries_for_user_modified_since(fetch_since).await?;
|
||||
|
||||
@ -308,7 +317,7 @@ async fn main() {
|
||||
toggl_api: api,
|
||||
};
|
||||
|
||||
worker.update(TimeDelta::days(90))
|
||||
worker.fetch_changed_since(TimeDelta::days(90))
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
@ -265,7 +265,7 @@ pub mod types {
|
||||
#[serde(rename = "at")]
|
||||
pub updated_at: DateTime<Utc>,
|
||||
|
||||
pub description: String,
|
||||
pub description: Option<String>,
|
||||
|
||||
#[serde(default)]
|
||||
pub tags: Vec<String>,
|
||||
@ -397,7 +397,7 @@ pub mod types {
|
||||
pub project_id: Option<u64>,
|
||||
pub task_id: Option<u64>,
|
||||
pub billable: bool,
|
||||
pub description: String,
|
||||
pub description: Option<String>,
|
||||
pub tag_ids: Vec<u64>,
|
||||
pub billable_amount_in_cents: Option<u64>,
|
||||
pub hourly_rate_in_cents: Option<u64>,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user