120 lines
4.4 KiB
Rust
120 lines
4.4 KiB
Rust
use crate::entity::{client, project, time_entry};
|
|
use crate::toggl_api::types::{Client, Project, ReportRow, TimeEntry};
|
|
use sea_orm::sea_query::OnConflict;
|
|
use sea_orm::{NotSet, Set};
|
|
|
|
impl ReportRow {
|
|
pub fn to_time_entries(&self, workspace_id: i64) -> Vec<TimeEntry> {
|
|
self.time_entries
|
|
.iter()
|
|
.map(|inner| TimeEntry {
|
|
id: inner.id as i64,
|
|
description: self.description.clone(),
|
|
project_id: self.project_id.map(|id| id as i64),
|
|
task_id: self.task_id.map(|id| id as i64),
|
|
billable: self.billable,
|
|
start: inner.start,
|
|
stop: Some(inner.stop),
|
|
at: inner.at,
|
|
server_deleted_at: None,
|
|
tags: vec![], // TODO: tags on report row import, need to track in separate table
|
|
workspace_id,
|
|
duration: inner.seconds as i64,
|
|
tag_ids: self.tag_ids.iter().map(|ids| *ids as i64).collect(),
|
|
user_id: self.user_id as i64,
|
|
})
|
|
.collect()
|
|
}
|
|
}
|
|
|
|
impl TimeEntry {
|
|
pub(crate) fn as_model(&self) -> time_entry::ActiveModel {
|
|
time_entry::ActiveModel {
|
|
id: NotSet,
|
|
toggl_id: Set(self.id),
|
|
description: Set(self.description.clone()),
|
|
project_id: Set(self.project_id),
|
|
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 {
|
|
pub fn as_model(&self) -> client::ActiveModel {
|
|
client::ActiveModel {
|
|
id: Set(self.id),
|
|
name: Set(self.name.clone()),
|
|
archived: Set(self.archived),
|
|
workspace_id: Set(self.wid),
|
|
at: Set(self.at.clone().fixed_offset()),
|
|
server_deleted_at: Set(self.server_deleted_at.map(|dt| dt.fixed_offset())),
|
|
}
|
|
}
|
|
|
|
pub fn grafting_conflict_statement() -> OnConflict {
|
|
OnConflict::column(client::Column::Id)
|
|
.update_columns(vec![
|
|
client::Column::Name,
|
|
client::Column::Archived,
|
|
client::Column::WorkspaceId,
|
|
client::Column::At,
|
|
client::Column::ServerDeletedAt,
|
|
])
|
|
.to_owned()
|
|
}
|
|
}
|
|
|
|
impl Project {
|
|
pub fn as_model(&self) -> project::ActiveModel {
|
|
project::ActiveModel {
|
|
id: NotSet,
|
|
toggl_id: Set(self.id as i64),
|
|
name: Set(self.name.clone()),
|
|
active: Set(self.active),
|
|
client_id: Set(self.client_id.map(|id| id as i32)),
|
|
workspace_id: Set(self.workspace_id as i64),
|
|
raw_json: Set(serde_json::to_value(self).unwrap()),
|
|
color: Set(self.color.clone()),
|
|
server_created_at: Set(self.created_at.clone().fixed_offset()),
|
|
server_updated_at: Set(self.at.clone().fixed_offset()),
|
|
server_deleted_at: Set(self.server_deleted_at.map(|dt| dt.fixed_offset())),
|
|
}
|
|
}
|
|
|
|
pub fn grafting_conflict_statement() -> OnConflict {
|
|
OnConflict::column(project::Column::TogglId)
|
|
.update_columns(vec![
|
|
project::Column::Name,
|
|
project::Column::Active,
|
|
project::Column::ClientId,
|
|
project::Column::WorkspaceId,
|
|
project::Column::RawJson,
|
|
project::Column::Color,
|
|
project::Column::ServerCreatedAt,
|
|
project::Column::ServerUpdatedAt,
|
|
project::Column::ServerDeletedAt,
|
|
])
|
|
.to_owned()
|
|
}
|
|
}
|