From 73b3e2cb96b732982c476d453b831f7f381d452b Mon Sep 17 00:00:00 2001 From: Joshua Coles Date: Sun, 3 Mar 2024 18:21:00 +0000 Subject: [PATCH] Update Datetime handling in API and DB layers Changed the handling of start, stop, and update timestamp fields in ReportRowInnerTimeEntry data structure from strings to DateTime objects in the API layer. Also updated the data persisting in DB layer to use these new datetime objects and introduced additional columns for server_updated_at, server_deleted_at and tags. --- src/db.rs | 31 ++++++++++++++++++++++++++++--- src/toggl_api/api_client.rs | 2 +- src/toggl_api/types.rs | 6 +++--- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/db.rs b/src/db.rs index 4e04711..f831f20 100644 --- a/src/db.rs +++ b/src/db.rs @@ -12,9 +12,13 @@ impl ReportRow { toggl_id: Set(inner.id as i64), description: Set(self.description.clone()), project_id: Set(self.project_id.map(|id| id as i64)), - start: Set(chrono::DateTime::parse_from_rfc3339(&inner.start).unwrap()), - stop: Set(chrono::DateTime::parse_from_rfc3339(&inner.stop).unwrap()), + start: Set(inner.start.fixed_offset()), + stop: Set(inner.stop.fixed_offset()), raw_json: Set(serde_json::to_value(inner).unwrap()), + server_deleted_at: NotSet, + server_updated_at: Set(inner.at.fixed_offset()), + // TODO: tags on report row import, need to track in separate table + tags: NotSet }) .collect() } @@ -27,6 +31,9 @@ impl ReportRow { time_entry::Column::Start, time_entry::Column::Stop, time_entry::Column::RawJson, + time_entry::Column::ServerUpdatedAt, + time_entry::Column::ServerDeletedAt, + // time_entry::Column::Tags, TODO add tags to conflict resolution when implemented ]) .to_owned() } @@ -39,11 +46,29 @@ impl TimeEntry { toggl_id: Set(self.id), description: Set(self.description.clone()), project_id: Set(self.project_id), - start: Set(self.start.clone().fixed_offset()), + start: Set(self.start.fixed_offset()), stop: Set(self.stop.unwrap().fixed_offset()), raw_json: Set(serde_json::to_value(self).unwrap()), + server_updated_at: Set(self.at.fixed_offset()), + server_deleted_at: Set(self.server_deleted_at.map(|dt| dt.fixed_offset())), + tags: Set(serde_json::to_value(&self.tags).unwrap()), } } + + pub fn grafting_conflict_statement() -> OnConflict { + OnConflict::column(time_entry::Column::TogglId) + .update_columns(vec![ + time_entry::Column::Description, + time_entry::Column::ProjectId, + time_entry::Column::Start, + time_entry::Column::Stop, + time_entry::Column::RawJson, + time_entry::Column::ServerUpdatedAt, + time_entry::Column::ServerDeletedAt, + time_entry::Column::Tags, + ]) + .to_owned() + } } impl Client { diff --git a/src/toggl_api/api_client.rs b/src/toggl_api/api_client.rs index c1ec070..0c11052 100644 --- a/src/toggl_api/api_client.rs +++ b/src/toggl_api/api_client.rs @@ -1,4 +1,4 @@ -use reqwest::{Client, RequestBuilder, Response, Url}; +use reqwest::{Client, RequestBuilder, Response}; use serde_json::Value; use std::collections::HashMap; use std::time::Duration; diff --git a/src/toggl_api/types.rs b/src/toggl_api/types.rs index d84bbc9..df56178 100644 --- a/src/toggl_api/types.rs +++ b/src/toggl_api/types.rs @@ -25,9 +25,9 @@ pub struct ReportRow { pub struct ReportRowInnerTimeEntry { pub id: u64, pub seconds: u32, - pub start: String, - pub stop: String, - pub at: String, + pub start: DateTime, + pub stop: DateTime, + pub at: DateTime, } #[derive(Debug, Clone, Serialize, Deserialize)]