diff --git a/.sqlx/query-053634754e0b3678d61c7dd5f89a894bc5619fa2f94cdbcc6dfd59bcc3aa123f.json b/.sqlx/query-1c8be5c0021c10a1377ac2a03d9b3f8b7e3994e0d4edf0e36275619d11a233f3.json similarity index 88% rename from .sqlx/query-053634754e0b3678d61c7dd5f89a894bc5619fa2f94cdbcc6dfd59bcc3aa123f.json rename to .sqlx/query-1c8be5c0021c10a1377ac2a03d9b3f8b7e3994e0d4edf0e36275619d11a233f3.json index c5b009a..562b326 100644 --- a/.sqlx/query-053634754e0b3678d61c7dd5f89a894bc5619fa2f94cdbcc6dfd59bcc3aa123f.json +++ b/.sqlx/query-1c8be5c0021c10a1377ac2a03d9b3f8b7e3994e0d4edf0e36275619d11a233f3.json @@ -1,6 +1,6 @@ { "db_name": "SQLite", - "query": "SELECT id, sales_person_id, amount, category, description, date_time, created, deleted, update_version FROM extra_hours WHERE sales_person_id = ? AND CAST(strftime('%Y', date_time) AS INTEGER) = ? AND CAST(strftime('%m', date_time) AS INTEGER) <= ?", + "query": "SELECT id, sales_person_id, amount, category, description, date_time, created, deleted, update_version FROM extra_hours WHERE sales_person_id = ? AND CAST(strftime('%Y', date_time) AS INTEGER) = ? AND deleted IS NULL", "describe": { "columns": [ { @@ -50,7 +50,7 @@ } ], "parameters": { - "Right": 3 + "Right": 2 }, "nullable": [ false, @@ -64,5 +64,5 @@ false ] }, - "hash": "053634754e0b3678d61c7dd5f89a894bc5619fa2f94cdbcc6dfd59bcc3aa123f" + "hash": "1c8be5c0021c10a1377ac2a03d9b3f8b7e3994e0d4edf0e36275619d11a233f3" } diff --git a/.sqlx/query-77e028e8c559bdb0a725d6af0b75fc7048bb63735b359dc1365abbba68774a93.json b/.sqlx/query-77e028e8c559bdb0a725d6af0b75fc7048bb63735b359dc1365abbba68774a93.json new file mode 100644 index 0000000..feba609 --- /dev/null +++ b/.sqlx/query-77e028e8c559bdb0a725d6af0b75fc7048bb63735b359dc1365abbba68774a93.json @@ -0,0 +1,12 @@ +{ + "db_name": "SQLite", + "query": "UPDATE extra_hours SET deleted = ?, update_version = ?, update_process = ? WHERE id = ?", + "describe": { + "columns": [], + "parameters": { + "Right": 4 + }, + "nullable": [] + }, + "hash": "77e028e8c559bdb0a725d6af0b75fc7048bb63735b359dc1365abbba68774a93" +} diff --git a/.sqlx/query-a90b5c6c759c7d5dc7beb05c8e90963c7eec7158004bc002b5fd81cffd0e17e2.json b/.sqlx/query-a90b5c6c759c7d5dc7beb05c8e90963c7eec7158004bc002b5fd81cffd0e17e2.json new file mode 100644 index 0000000..b26757e --- /dev/null +++ b/.sqlx/query-a90b5c6c759c7d5dc7beb05c8e90963c7eec7158004bc002b5fd81cffd0e17e2.json @@ -0,0 +1,68 @@ +{ + "db_name": "SQLite", + "query": "SELECT id, sales_person_id, amount, category, description, date_time, created, deleted, update_version FROM extra_hours WHERE id = ? AND deleted IS NULL", + "describe": { + "columns": [ + { + "name": "id", + "ordinal": 0, + "type_info": "Blob" + }, + { + "name": "sales_person_id", + "ordinal": 1, + "type_info": "Blob" + }, + { + "name": "amount", + "ordinal": 2, + "type_info": "Float" + }, + { + "name": "category", + "ordinal": 3, + "type_info": "Text" + }, + { + "name": "description", + "ordinal": 4, + "type_info": "Text" + }, + { + "name": "date_time", + "ordinal": 5, + "type_info": "Text" + }, + { + "name": "created", + "ordinal": 6, + "type_info": "Text" + }, + { + "name": "deleted", + "ordinal": 7, + "type_info": "Text" + }, + { + "name": "update_version", + "ordinal": 8, + "type_info": "Blob" + } + ], + "parameters": { + "Right": 1 + }, + "nullable": [ + false, + false, + false, + false, + true, + false, + false, + true, + false + ] + }, + "hash": "a90b5c6c759c7d5dc7beb05c8e90963c7eec7158004bc002b5fd81cffd0e17e2" +} diff --git a/Cargo.lock b/Cargo.lock index b051355..58748e4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -59,7 +59,7 @@ checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc" [[package]] name = "app" -version = "0.1.1-dev" +version = "0.1.5" dependencies = [ "dao", "dao_impl", diff --git a/app/Cargo.toml b/app/Cargo.toml index a1283d5..23076cf 100644 --- a/app/Cargo.toml +++ b/app/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "app" -version = "0.1.1-dev" +version = "0.1.5" edition = "2021" resolver = "2" diff --git a/dao/src/extra_hours.rs b/dao/src/extra_hours.rs index 38a262f..3989318 100644 --- a/dao/src/extra_hours.rs +++ b/dao/src/extra_hours.rs @@ -28,11 +28,11 @@ pub struct ExtraHoursEntity { #[automock] #[async_trait] pub trait ExtraHoursDao { + async fn find_by_id(&self, id: Uuid) -> Result, crate::DaoError>; async fn find_by_sales_person_id_and_year( &self, sales_person_id: Uuid, year: u32, - until_week: u8, ) -> Result, crate::DaoError>; async fn create(&self, entity: &ExtraHoursEntity, process: &str) -> Result<(), crate::DaoError>; diff --git a/dao_impl/src/extra_hours.rs b/dao_impl/src/extra_hours.rs index 6fe22e9..11f0404 100644 --- a/dao_impl/src/extra_hours.rs +++ b/dao_impl/src/extra_hours.rs @@ -69,19 +69,31 @@ impl ExtraHoursDaoImpl { #[async_trait] impl ExtraHoursDao for ExtraHoursDaoImpl { + async fn find_by_id(&self, id: Uuid) -> Result, crate::DaoError> { + let id_vec = id.as_bytes().to_vec(); + Ok(query_as!( + ExtraHoursDb, + "SELECT id, sales_person_id, amount, category, description, date_time, created, deleted, update_version FROM extra_hours WHERE id = ? AND deleted IS NULL", + id_vec, + ).fetch_optional(self.pool.as_ref()) + .await + .map_db_error()? + .as_ref() + .map(ExtraHoursEntity::try_from) + .transpose()?) + } + async fn find_by_sales_person_id_and_year( &self, sales_person_id: Uuid, year: u32, - until_week: u8, ) -> Result, crate::DaoError> { let id_vec = sales_person_id.as_bytes().to_vec(); Ok(query_as!( ExtraHoursDb, - "SELECT id, sales_person_id, amount, category, description, date_time, created, deleted, update_version FROM extra_hours WHERE sales_person_id = ? AND CAST(strftime('%Y', date_time) AS INTEGER) = ? AND CAST(strftime('%m', date_time) AS INTEGER) <= ?", + "SELECT id, sales_person_id, amount, category, description, date_time, created, deleted, update_version FROM extra_hours WHERE sales_person_id = ? AND CAST(strftime('%Y', date_time) AS INTEGER) = ? AND deleted IS NULL", id_vec, year, - until_week, ).fetch_all(self.pool.as_ref()) .await .map_db_error()? @@ -130,10 +142,26 @@ impl ExtraHoursDao for ExtraHoursDaoImpl { } async fn update( &self, - _entity: &ExtraHoursEntity, - _process: &str, + entity: &ExtraHoursEntity, + process: &str, ) -> Result<(), crate::DaoError> { - unimplemented!() + let id_vec = entity.id.as_bytes().to_vec(); + let version_vec = entity.version.as_bytes().to_vec(); + let delete = entity + .deleted + .map(|date_time| date_time.format(&Iso8601::DATE_TIME)) + .transpose()?; + query!( + "UPDATE extra_hours SET deleted = ?, update_version = ?, update_process = ? WHERE id = ?", + delete, + version_vec, + process, + id_vec, + ) + .execute(self.pool.as_ref()) + .await + .map_db_error()?; + Ok(()) } async fn delete(&self, _id: Uuid, _process: &str) -> Result<(), crate::DaoError> { unimplemented!() diff --git a/rest/src/extra_hours.rs b/rest/src/extra_hours.rs index 82d06cb..d104c74 100644 --- a/rest/src/extra_hours.rs +++ b/rest/src/extra_hours.rs @@ -1,14 +1,63 @@ +use std::rc::Rc; + use axum::{ - body::Body, extract::State, response::Response, routing::post, Extension, Json, Router, + body::Body, + extract::{Path, Query, State}, + response::Response, + routing::{delete, get, post}, + Extension, Json, Router, }; use rest_types::ExtraHoursTO; +use serde::Deserialize; use service::extra_hours::ExtraHoursService; +use uuid::Uuid; use crate::{error_handler, Context, RestStateDef}; pub fn generate_route() -> Router { - Router::new().route("/", post(create_extra_hours::)) + Router::new() + .route("/", post(create_extra_hours::)) + .route("/:id", delete(delete_extra_hours::)) + .route( + "/by-sales-person/:id", + get(get_extra_hours_for_sales_person::), + ) +} + +#[derive(Clone, Debug, Deserialize)] +pub struct ExtraHoursForSalesPersonAttributes { + year: u32, + until_week: u8, +} + +pub async fn get_extra_hours_for_sales_person( + rest_state: State, + Extension(context): Extension, + query: Query, + Path(sales_person_id): Path, +) -> Response { + error_handler( + (async { + let extra_hours: Rc<[ExtraHoursTO]> = rest_state + .extra_hours_service() + .find_by_sales_person_id_and_year( + sales_person_id, + query.year, + query.until_week, + context.into(), + ) + .await? + .iter() + .map(ExtraHoursTO::from) + .collect(); + Ok(Response::builder() + .status(201) + .body(Body::new(serde_json::to_string(&extra_hours).unwrap())) + .unwrap()) + }) + .await, + ) } pub async fn create_extra_hours( @@ -25,10 +74,27 @@ pub async fn create_extra_hours( .await?, ); Ok(Response::builder() - .status(200) + .status(201) .body(Body::new(serde_json::to_string(&extra_hours).unwrap())) .unwrap()) }) .await, ) } + +pub async fn delete_extra_hours( + rest_state: State, + Extension(context): Extension, + Path(extra_hours_id): Path, +) -> Response { + error_handler( + (async { + rest_state + .extra_hours_service() + .delete(extra_hours_id, context.into()) + .await?; + Ok(Response::builder().status(204).body(Body::empty()).unwrap()) + }) + .await, + ) +} diff --git a/service/src/extra_hours.rs b/service/src/extra_hours.rs index 9ab6484..a419703 100644 --- a/service/src/extra_hours.rs +++ b/service/src/extra_hours.rs @@ -107,5 +107,5 @@ pub trait ExtraHoursService { &self, id: Uuid, context: Authentication, - ) -> Result; + ) -> Result<(), ServiceError>; } diff --git a/service_impl/src/extra_hours.rs b/service_impl/src/extra_hours.rs index 562dab6..1ab8b0d 100644 --- a/service_impl/src/extra_hours.rs +++ b/service_impl/src/extra_hours.rs @@ -4,7 +4,7 @@ use async_trait::async_trait; use dao::extra_hours; use service::{ extra_hours::ExtraHours, - permission::{Authentication, HR_PRIVILEGE}, + permission::{Authentication, HR_PRIVILEGE, SALES_PRIVILEGE}, ServiceError, }; use tokio::join; @@ -78,12 +78,29 @@ impl< async fn find_by_sales_person_id_and_year( &self, - _sales_person_id: Uuid, - _year: u32, - _until_week: u8, - _context: Authentication, + sales_person_id: Uuid, + year: u32, + until_week: u8, + context: Authentication, ) -> Result, ServiceError> { - unimplemented!() + let (hr_permission, sales_person_permission) = join!( + self.permission_service + .check_permission(HR_PRIVILEGE, context.clone()), + self.sales_person_service + .verify_user_is_sales_person(sales_person_id, context), + ); + hr_permission.or(sales_person_permission)?; + + let extra_hours_entities = self + .extra_hours_dao + .find_by_sales_person_id_and_year(sales_person_id, year) + .await?; + let extra_hours = extra_hours_entities + .iter() + .filter(|extra_hours| extra_hours.date_time.iso_week() <= until_week) + .map(ExtraHours::from) + .collect::>(); + Ok(extra_hours.into()) } async fn create( @@ -127,11 +144,40 @@ impl< ) -> Result { unimplemented!() } + async fn delete( &self, - _id: Uuid, - _context: Authentication, - ) -> Result { - unimplemented!() + extra_hours_id: Uuid, + context: Authentication, + ) -> Result<(), ServiceError> { + let (hr_permission, sales_person_permission) = join!( + self.permission_service + .check_permission(HR_PRIVILEGE, context.clone()), + self.permission_service + .check_permission(SALES_PRIVILEGE, context.clone()), + ); + hr_permission.or(sales_person_permission)?; + + let mut extra_hours_entity = self + .extra_hours_dao + .find_by_id(extra_hours_id) + .await? + .ok_or(ServiceError::EntityNotFound(extra_hours_id))?; + + let (hr_permission, user_permission) = join!( + self.permission_service + .check_permission(HR_PRIVILEGE, context.clone()), + self.sales_person_service + .verify_user_is_sales_person(extra_hours_entity.sales_person_id, context), + ); + hr_permission.or(user_permission)?; + + extra_hours_entity.deleted = Some(self.clock_service.date_time_now()); + + self.extra_hours_dao + .update(&extra_hours_entity, "extra_hours_service::delete") + .await?; + + Ok(()) } } diff --git a/service_impl/src/reporting.rs b/service_impl/src/reporting.rs index 14dd26c..6c4a065 100644 --- a/service_impl/src/reporting.rs +++ b/service_impl/src/reporting.rs @@ -178,9 +178,10 @@ where .sum(); let extra_hours = self .extra_hours_dao - .find_by_sales_person_id_and_year(paid_employee.id, year, until_week) + .find_by_sales_person_id_and_year(paid_employee.id, year) .await? .iter() + .filter(|eh| eh.date_time.iso_week() <= until_week) .map(|eh| eh.amount) .sum::(); let balance_hours = shiftplan_hours + extra_hours - planned_hours; @@ -221,7 +222,7 @@ where .await?; let extra_hours = self .extra_hours_dao - .find_by_sales_person_id_and_year(*sales_person_id, year, until_week) + .find_by_sales_person_id_and_year(*sales_person_id, year) .await?; let planned_hours: f32 = (1..=until_week) @@ -232,7 +233,11 @@ where }) .sum(); let shiftplan_hours = shiftplan_report.iter().map(|r| r.hours).sum::() as f32; - let overall_extra_hours = extra_hours.iter().map(|eh| eh.amount).sum::(); + let overall_extra_hours = extra_hours + .iter() + .filter(|eh| eh.date_time.iso_week() <= until_week) + .map(|eh| eh.amount) + .sum::(); let employee_report = EmployeeReport { sales_person: Arc::new(sales_person),