diff --git a/.sqlx/query-1adc3ff219a135b40566e2a0a94ff507b51f6a71163a404e12b2cab65376e187.json b/.sqlx/query-1adc3ff219a135b40566e2a0a94ff507b51f6a71163a404e12b2cab65376e187.json new file mode 100644 index 0000000..3dcbebe --- /dev/null +++ b/.sqlx/query-1adc3ff219a135b40566e2a0a94ff507b51f6a71163a404e12b2cab65376e187.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 = ?", + "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": "1adc3ff219a135b40566e2a0a94ff507b51f6a71163a404e12b2cab65376e187" +} diff --git a/dao/src/extra_hours.rs b/dao/src/extra_hours.rs index fa397f3..3989318 100644 --- a/dao/src/extra_hours.rs +++ b/dao/src/extra_hours.rs @@ -28,6 +28,7 @@ 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, diff --git a/dao_impl/src/extra_hours.rs b/dao_impl/src/extra_hours.rs index 73d5330..02601d0 100644 --- a/dao_impl/src/extra_hours.rs +++ b/dao_impl/src/extra_hours.rs @@ -69,6 +69,20 @@ 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 = ?", + 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, diff --git a/rest/src/extra_hours.rs b/rest/src/extra_hours.rs index 82d06cb..c5b4dd3 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::{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", post(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..462efac 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,32 @@ 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))?; + + self.sales_person_service + .verify_user_is_sales_person(extra_hours_entity.sales_person_id, context) + .await?; + + extra_hours_entity.deleted = Some(self.clock_service.date_time_now()); + + Ok(()) } }