-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
195 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use super::task_op::select_or_create_task; | ||
use crate::msg::message::TimeSpent; | ||
use crate::rally::api; | ||
use crate::rally::models::time::UpdateValue; | ||
use crate::rally::models::ObjectModel; | ||
use crate::rally::models::SingleObjectModel; | ||
use crate::rally::models::Task; | ||
use crate::token::tokens::UserToken; | ||
use anyhow::Result; | ||
use chrono::prelude::*; | ||
use tracing::error; | ||
|
||
use crate::rally::models::TimeEntryItem; | ||
|
||
pub async fn add_time_entry_value( | ||
item: &TimeEntryItem, | ||
ut: &UserToken, | ||
task_date: DateTime<Utc>, | ||
tp: &TimeSpent, | ||
work_product: ObjectModel, | ||
) -> Result<(), anyhow::Error> { | ||
let update_value = create_update_value(item, ut, task_date, tp).await?; | ||
if update_value.object_id.is_none() { | ||
api::time::add_time_entry_value(ut, &work_product.get_project(), &update_value).await?; | ||
} else { | ||
api::time::update_time_entry_value(ut, &work_product.get_project(), &update_value).await?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub async fn create_update_value( | ||
item: &TimeEntryItem, | ||
ut: &UserToken, | ||
task_date: DateTime<Utc>, | ||
tp: &TimeSpent, | ||
) -> Result<UpdateValue, anyhow::Error> { | ||
let values = api::time::get_time_entry_values(ut, &item._ref).await?; | ||
let mut update_value = UpdateValue::new(task_date, tp.get_time_spent(), item._ref.clone()); | ||
values.iter().for_each(|i| { | ||
if i.DateVal.year() == task_date.year() | ||
&& i.DateVal.month() == task_date.month() | ||
&& i.DateVal.day() == task_date.day() | ||
{ | ||
update_value.set_object_id(i.ObjectID); | ||
update_value.add_hours(i.Hours); | ||
} | ||
}); | ||
Ok(update_value) | ||
} | ||
|
||
pub async fn get_uncompleted_task_and_entry_item( | ||
ut: &UserToken, | ||
tis: Vec<TimeEntryItem>, | ||
) -> Result<Option<(Task, TimeEntryItem)>> { | ||
for i in tis { | ||
if let SingleObjectModel::Task(t) = | ||
api::fetch_object::<SingleObjectModel>(ut, &i.Task._ref).await? | ||
{ | ||
if t.State != "Completed" { | ||
return Ok(Some((t, i))); | ||
} | ||
} | ||
} | ||
Ok(None) | ||
} | ||
/// get task and time entry item | ||
/// if there is a uncompleted task, return it | ||
/// if there is no uncompleted task, create a new task and time entry item | ||
/// if there is no task, return None | ||
pub async fn get_task_and_time_entry_item( | ||
ut: &UserToken, | ||
task_date: DateTime<Utc>, | ||
wp_id: &str, | ||
work_product: &ObjectModel, | ||
tp: &TimeSpent, | ||
) -> Result<(Option<Task>, Option<TimeEntryItem>)> { | ||
let tis = api::time::get_time_entry_items(ut, &task_date, wp_id).await?; | ||
let uncompleted_task_and_entry_item = get_uncompleted_task_and_entry_item(ut, tis).await?; | ||
if let Some((task, item)) = uncompleted_task_and_entry_item { | ||
return Ok((Some(task), Some(item))); | ||
} | ||
if let Some(task) = select_or_create_task(ut, work_product, tp).await? { | ||
let item = Some( | ||
api::time::create_time_entry_item( | ||
ut, | ||
&work_product.get_project(), | ||
work_product, | ||
&task_date, | ||
&task, | ||
) | ||
.await?, | ||
); | ||
return Ok((Some(task), item)); | ||
} | ||
error!("cannot find task end item for {}", wp_id); | ||
Ok((None, None)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use crate::msg::message::TimeSpent; | ||
use crate::rally::api; | ||
use crate::rally::api::task::create_task; | ||
use crate::rally::api::user::fetch_rally_user; | ||
use crate::rally::models::task::CreateTask; | ||
use crate::rally::models::{ObjectModel, Task, User}; | ||
use crate::token::tokens::UserToken; | ||
use anyhow::Result; | ||
use chrono::prelude::*; | ||
|
||
fn get_task_name(date: &DateTime<Utc>) -> String { | ||
let s = "Code review ".to_string(); | ||
let date = date.format("%Y-%m-%d").to_string(); | ||
s + &date | ||
} | ||
|
||
/// Select a task for the owner. If the owner has a task that is not completed, then | ||
/// return that task. Otherwise, create a new task for the owner. | ||
pub async fn select_or_create_task( | ||
ut: &UserToken, | ||
work_product: &ObjectModel, | ||
tp: &TimeSpent, | ||
) -> Result<Option<Task>> { | ||
let tasks = api::task::get_tasks(ut, work_product).await?; | ||
let owners = fetch_rally_user(ut, &ut.name).await?; | ||
for owner in owners.iter() { | ||
let t = select_task_for_owner(tasks, owner); | ||
if t.is_some() { | ||
return Ok(t); | ||
} | ||
|
||
let task_date: DateTime<Utc> = Utc::now(); | ||
let task_name = tp | ||
.task_name | ||
.clone() | ||
.unwrap_or_else(|| get_task_name(&task_date)); | ||
let ct = CreateTask::new( | ||
task_name, | ||
owner._ref.clone(), | ||
tp.get_time_spent(), | ||
work_product, | ||
); | ||
return Ok(Some(create_task(ut, &ct).await?)); | ||
} | ||
Ok(None) | ||
} | ||
|
||
pub fn select_task_for_owner(tasks: Vec<Task>, owner: &User) -> Option<Task> { | ||
for t in tasks { | ||
if t.State != "Completed" | ||
&& t.Owner | ||
.as_ref() | ||
.map(|o| o._refObjectUUID.as_deref()) | ||
.flatten() | ||
.map(|o| o == owner._refObjectUUID) | ||
.unwrap_or(false) | ||
{ | ||
return Some(t); | ||
} | ||
} | ||
None | ||
} |