Cargo clippy and cargo fmt

This commit is contained in:
Simon Goller 2024-05-09 15:00:50 +02:00
parent b0000c0117
commit ed609cf06c
22 changed files with 286 additions and 94 deletions

View file

@ -24,7 +24,13 @@ pub struct BookingEntity {
pub trait BookingDao { pub trait BookingDao {
async fn all(&self) -> Result<Arc<[BookingEntity]>, DaoError>; async fn all(&self) -> Result<Arc<[BookingEntity]>, DaoError>;
async fn find_by_id(&self, id: Uuid) -> Result<Option<BookingEntity>, DaoError>; async fn find_by_id(&self, id: Uuid) -> Result<Option<BookingEntity>, DaoError>;
async fn find_by_booking_data(&self, sales_person_id: Uuid, slot_id: Uuid, calendar_week: i32, year: u32) -> Result<Option<BookingEntity>, DaoError>; async fn find_by_booking_data(
&self,
sales_person_id: Uuid,
slot_id: Uuid,
calendar_week: i32,
year: u32,
) -> Result<Option<BookingEntity>, DaoError>;
async fn create(&self, entity: &BookingEntity, process: &str) -> Result<(), DaoError>; async fn create(&self, entity: &BookingEntity, process: &str) -> Result<(), DaoError>;
async fn update(&self, entity: &BookingEntity, process: &str) -> Result<(), DaoError>; async fn update(&self, entity: &BookingEntity, process: &str) -> Result<(), DaoError>;
} }

View file

@ -24,6 +24,11 @@ pub trait SalesPersonDao {
async fn create(&self, entity: &SalesPersonEntity, process: &str) -> Result<(), DaoError>; async fn create(&self, entity: &SalesPersonEntity, process: &str) -> Result<(), DaoError>;
async fn update(&self, entity: &SalesPersonEntity, process: &str) -> Result<(), DaoError>; async fn update(&self, entity: &SalesPersonEntity, process: &str) -> Result<(), DaoError>;
async fn get_assigned_user(&self, sales_person_id: Uuid) -> Result<Option<Arc<str>>, DaoError>; async fn get_assigned_user(&self, sales_person_id: Uuid) -> Result<Option<Arc<str>>, DaoError>;
async fn assign_to_user(&self, sales_person_id: Uuid, user_id: &str, process: &str) -> Result<(), DaoError>; async fn assign_to_user(
&self,
sales_person_id: Uuid,
user_id: &str,
process: &str,
) -> Result<(), DaoError>;
async fn discard_assigned_user(&self, sales_person_id: Uuid) -> Result<(), DaoError>; async fn discard_assigned_user(&self, sales_person_id: Uuid) -> Result<(), DaoError>;
} }

View file

@ -81,7 +81,13 @@ impl BookingDao for BookingDaoImpl {
.transpose()?) .transpose()?)
} }
async fn find_by_booking_data(&self, sales_person_id: Uuid, slot_id: Uuid, calendar_week: i32, year: u32) -> Result<Option<BookingEntity>, DaoError> { async fn find_by_booking_data(
&self,
sales_person_id: Uuid,
slot_id: Uuid,
calendar_week: i32,
year: u32,
) -> Result<Option<BookingEntity>, DaoError> {
let sales_person_id_vec = sales_person_id.as_bytes().to_vec(); let sales_person_id_vec = sales_person_id.as_bytes().to_vec();
let slot_id_vec = slot_id.as_bytes().to_vec(); let slot_id_vec = slot_id.as_bytes().to_vec();
Ok(query_as!( Ok(query_as!(
@ -100,13 +106,17 @@ impl BookingDao for BookingDaoImpl {
.transpose()?) .transpose()?)
} }
async fn create(&self, entity: &BookingEntity, process: &str) -> Result<(), DaoError> { async fn create(&self, entity: &BookingEntity, process: &str) -> Result<(), DaoError> {
let id_vec = entity.id.as_bytes().to_vec(); let id_vec = entity.id.as_bytes().to_vec();
let sales_person_id_vec = entity.sales_person_id.as_bytes().to_vec(); let sales_person_id_vec = entity.sales_person_id.as_bytes().to_vec();
let slot_id_vec = entity.slot_id.as_bytes().to_vec(); let slot_id_vec = entity.slot_id.as_bytes().to_vec();
let created = entity.created.format(&Iso8601::DATE_TIME).map_db_error()?; let created = entity.created.format(&Iso8601::DATE_TIME).map_db_error()?;
let deleted = entity.deleted.as_ref().map(|deleted| deleted.format(&Iso8601::DATE_TIME)).transpose().map_db_error()?; let deleted = entity
.deleted
.as_ref()
.map(|deleted| deleted.format(&Iso8601::DATE_TIME))
.transpose()
.map_db_error()?;
let version_vec = entity.version.as_bytes().to_vec(); let version_vec = entity.version.as_bytes().to_vec();
query!("INSERT INTO booking (id, sales_person_id, slot_id, calendar_week, year, created, deleted, update_version, update_process) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", query!("INSERT INTO booking (id, sales_person_id, slot_id, calendar_week, year, created, deleted, update_version, update_process) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
id_vec, sales_person_id_vec, slot_id_vec, entity.calendar_week, entity.year, created, deleted, version_vec, process id_vec, sales_person_id_vec, slot_id_vec, entity.calendar_week, entity.year, created, deleted, version_vec, process
@ -116,7 +126,12 @@ impl BookingDao for BookingDaoImpl {
async fn update(&self, entity: &BookingEntity, process: &str) -> Result<(), DaoError> { async fn update(&self, entity: &BookingEntity, process: &str) -> Result<(), DaoError> {
let id_vec = entity.id.as_bytes().to_vec(); let id_vec = entity.id.as_bytes().to_vec();
let version_vec = entity.version.as_bytes().to_vec(); let version_vec = entity.version.as_bytes().to_vec();
let deleted = entity.deleted.as_ref().map(|deleted| deleted.format(&Iso8601::DATE_TIME)).transpose().map_db_error()?; let deleted = entity
.deleted
.as_ref()
.map(|deleted| deleted.format(&Iso8601::DATE_TIME))
.transpose()
.map_db_error()?;
query!( query!(
"UPDATE booking SET deleted = ?, update_version = ?, update_process = ? WHERE id = ?", "UPDATE booking SET deleted = ?, update_version = ?, update_process = ? WHERE id = ?",
deleted, deleted,

View file

@ -92,10 +92,7 @@ impl dao::PermissionDao for PermissionDaoImpl {
Ok(()) Ok(())
} }
async fn find_user(&self, username: &str) -> Result<Option<dao::UserEntity>, DaoError> { async fn find_user(&self, username: &str) -> Result<Option<dao::UserEntity>, DaoError> {
let result = query!( let result = query!(r"SELECT name FROM user WHERE name = ?", username)
r"SELECT name FROM user WHERE name = ?",
username
)
.fetch_optional(self.pool.as_ref()) .fetch_optional(self.pool.as_ref())
.await .await
.map_db_error()?; .map_db_error()?;

View file

@ -112,7 +112,12 @@ impl SalesPersonDao for SalesPersonDaoImpl {
Ok(()) Ok(())
} }
async fn assign_to_user(&self, sales_person_id: Uuid, user_id: &str, process: &str) -> Result<(), DaoError> { async fn assign_to_user(
&self,
sales_person_id: Uuid,
user_id: &str,
process: &str,
) -> Result<(), DaoError> {
let sales_person_id = sales_person_id.as_bytes().to_vec(); let sales_person_id = sales_person_id.as_bytes().to_vec();
query!("INSERT INTO sales_person_user (user_id, sales_person_id, update_process) VALUES (?, ?, ?)", user_id, sales_person_id, process) query!("INSERT INTO sales_person_user (user_id, sales_person_id, update_process) VALUES (?, ?, ?)", user_id, sales_person_id, process)
.execute(self.pool.as_ref()) .execute(self.pool.as_ref())
@ -123,7 +128,10 @@ impl SalesPersonDao for SalesPersonDaoImpl {
async fn discard_assigned_user(&self, sales_person_id: Uuid) -> Result<(), DaoError> { async fn discard_assigned_user(&self, sales_person_id: Uuid) -> Result<(), DaoError> {
let sales_person_id = sales_person_id.as_bytes().to_vec(); let sales_person_id = sales_person_id.as_bytes().to_vec();
query!("DELETE FROM sales_person_user WHERE sales_person_id = ?", sales_person_id) query!(
"DELETE FROM sales_person_user WHERE sales_person_id = ?",
sales_person_id
)
.execute(self.pool.as_ref()) .execute(self.pool.as_ref())
.await .await
.map_db_error()?; .map_db_error()?;

View file

@ -90,7 +90,10 @@ pub async fn get_booking<RestState: RestStateDef>(
) -> Response { ) -> Response {
error_handler( error_handler(
(async { (async {
let booking = rest_state.booking_service().get(booking_id, ().into()).await?; let booking = rest_state
.booking_service()
.get(booking_id, ().into())
.await?;
Ok(Response::builder() Ok(Response::builder()
.status(200) .status(200)
.body(Body::new( .body(Body::new(
@ -129,7 +132,10 @@ pub async fn delete_booking<RestState: RestStateDef>(
) -> Response { ) -> Response {
error_handler( error_handler(
(async { (async {
rest_state.booking_service().delete(booking_id, ().into()).await?; rest_state
.booking_service()
.delete(booking_id, ().into())
.await?;
Ok(Response::builder().status(200).body(Body::empty()).unwrap()) Ok(Response::builder().status(200).body(Body::empty()).unwrap())
}) })
.await, .await,

View file

@ -162,7 +162,6 @@ pub async fn delete_sales_person<RestState: RestStateDef>(
) )
} }
pub async fn get_sales_person_user<RestState: RestStateDef>( pub async fn get_sales_person_user<RestState: RestStateDef>(
rest_state: State<RestState>, rest_state: State<RestState>,
Path(sales_person_id): Path<Uuid>, Path(sales_person_id): Path<Uuid>,

View file

@ -127,7 +127,12 @@ pub async fn get_slot<RestState: RestStateDef>(
) -> Response { ) -> Response {
error_handler( error_handler(
(async { (async {
let slot = SlotTO::from(&rest_state.slot_service().get_slot(&slot_id, ().into()).await?); let slot = SlotTO::from(
&rest_state
.slot_service()
.get_slot(&slot_id, ().into())
.await?,
);
Ok(Response::builder() Ok(Response::builder()
.status(200) .status(200)
.body(Body::new(serde_json::to_string(&slot).unwrap())) .body(Body::new(serde_json::to_string(&slot).unwrap()))

View file

@ -1,12 +1,12 @@
use std::sync::Arc;
use std::fmt::Debug; use std::fmt::Debug;
use std::sync::Arc;
use async_trait::async_trait; use async_trait::async_trait;
use time::PrimitiveDateTime; use time::PrimitiveDateTime;
use uuid::Uuid; use uuid::Uuid;
use crate::ServiceError;
use crate::permission::Authentication; use crate::permission::Authentication;
use crate::ServiceError;
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct Booking { pub struct Booking {
@ -55,12 +55,23 @@ impl TryFrom<&Booking> for dao::booking::BookingEntity {
pub trait BookingService { pub trait BookingService {
type Context: Clone + PartialEq + Eq + Debug + Send + Sync; type Context: Clone + PartialEq + Eq + Debug + Send + Sync;
async fn get_all(&self, context: Authentication<Self::Context>) -> Result<Arc<[Booking]>, ServiceError>; async fn get_all(
async fn get(&self, id: Uuid, context: Authentication<Self::Context>) -> Result<Booking, ServiceError>; &self,
context: Authentication<Self::Context>,
) -> Result<Arc<[Booking]>, ServiceError>;
async fn get(
&self,
id: Uuid,
context: Authentication<Self::Context>,
) -> Result<Booking, ServiceError>;
async fn create( async fn create(
&self, &self,
booking: &Booking, booking: &Booking,
context: Authentication<Self::Context>, context: Authentication<Self::Context>,
) -> Result<Booking, ServiceError>; ) -> Result<Booking, ServiceError>;
async fn delete(&self, id: Uuid, context: Authentication<Self::Context>) -> Result<(), ServiceError>; async fn delete(
&self,
id: Uuid,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError>;
} }

View file

@ -1,5 +1,5 @@
use std::sync::Arc;
use std::fmt::Debug; use std::fmt::Debug;
use std::sync::Arc;
use async_trait::async_trait; use async_trait::async_trait;
use mockall::automock; use mockall::automock;
@ -47,7 +47,9 @@ pub enum Authentication<Context: Clone + PartialEq + Eq + Send + Sync + Debug +
Full, Full,
Context(Context), Context(Context),
} }
impl<Context: Clone + Debug + PartialEq + Eq + Send + Sync + 'static> From<Context> for Authentication<Context> { impl<Context: Clone + Debug + PartialEq + Eq + Send + Sync + 'static> From<Context>
for Authentication<Context>
{
fn from(context: Context) -> Self { fn from(context: Context) -> Self {
Self::Context(context) Self::Context(context)
} }
@ -64,14 +66,40 @@ pub trait PermissionService {
context: Authentication<Self::Context>, context: Authentication<Self::Context>,
) -> Result<(), ServiceError>; ) -> Result<(), ServiceError>;
async fn create_user(&self, user: &str, context: Authentication<Self::Context>) -> Result<(), ServiceError>; async fn create_user(
async fn user_exists(&self, user: &str, context: Authentication<Self::Context>) -> Result<bool, ServiceError>; &self,
async fn delete_user(&self, user: &str, context: Authentication<Self::Context>) -> Result<(), ServiceError>; user: &str,
async fn get_all_users(&self, context: Authentication<Self::Context>) -> Result<Arc<[User]>, ServiceError>; context: Authentication<Self::Context>,
) -> Result<(), ServiceError>;
async fn user_exists(
&self,
user: &str,
context: Authentication<Self::Context>,
) -> Result<bool, ServiceError>;
async fn delete_user(
&self,
user: &str,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError>;
async fn get_all_users(
&self,
context: Authentication<Self::Context>,
) -> Result<Arc<[User]>, ServiceError>;
async fn create_role(&self, role: &str, context: Authentication<Self::Context>) -> Result<(), ServiceError>; async fn create_role(
async fn delete_role(&self, role: &str, context: Authentication<Self::Context>) -> Result<(), ServiceError>; &self,
async fn get_all_roles(&self, context: Authentication<Self::Context>) -> Result<Arc<[Role]>, ServiceError>; role: &str,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError>;
async fn delete_role(
&self,
role: &str,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError>;
async fn get_all_roles(
&self,
context: Authentication<Self::Context>,
) -> Result<Arc<[Role]>, ServiceError>;
async fn create_privilege( async fn create_privilege(
&self, &self,

View file

@ -1,12 +1,12 @@
use std::sync::Arc;
use std::fmt::Debug; use std::fmt::Debug;
use std::sync::Arc;
use async_trait::async_trait; use async_trait::async_trait;
use mockall::automock; use mockall::automock;
use uuid::Uuid; use uuid::Uuid;
use crate::ServiceError;
use crate::permission::Authentication; use crate::permission::Authentication;
use crate::ServiceError;
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub struct SalesPerson { pub struct SalesPerson {
@ -44,9 +44,20 @@ impl From<&SalesPerson> for dao::sales_person::SalesPersonEntity {
pub trait SalesPersonService { pub trait SalesPersonService {
type Context: Clone + Debug + PartialEq + Eq + Send + Sync + 'static; type Context: Clone + Debug + PartialEq + Eq + Send + Sync + 'static;
async fn get_all(&self, context: Authentication<Self::Context>) -> Result<Arc<[SalesPerson]>, ServiceError>; async fn get_all(
async fn get(&self, id: Uuid, context: Authentication<Self::Context>) -> Result<SalesPerson, ServiceError>; &self,
async fn exists(&self, id: Uuid, context: Authentication<Self::Context>) -> Result<bool, ServiceError>; context: Authentication<Self::Context>,
) -> Result<Arc<[SalesPerson]>, ServiceError>;
async fn get(
&self,
id: Uuid,
context: Authentication<Self::Context>,
) -> Result<SalesPerson, ServiceError>;
async fn exists(
&self,
id: Uuid,
context: Authentication<Self::Context>,
) -> Result<bool, ServiceError>;
async fn create( async fn create(
&self, &self,
item: &SalesPerson, item: &SalesPerson,
@ -57,7 +68,11 @@ pub trait SalesPersonService {
item: &SalesPerson, item: &SalesPerson,
context: Authentication<Self::Context>, context: Authentication<Self::Context>,
) -> Result<SalesPerson, ServiceError>; ) -> Result<SalesPerson, ServiceError>;
async fn delete(&self, id: Uuid, context: Authentication<Self::Context>) -> Result<(), ServiceError>; async fn delete(
&self,
id: Uuid,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError>;
async fn get_assigned_user( async fn get_assigned_user(
&self, &self,
sales_person_id: Uuid, sales_person_id: Uuid,

View file

@ -1,11 +1,11 @@
use async_trait::async_trait; use async_trait::async_trait;
use mockall::automock; use mockall::automock;
use std::fmt::Debug;
use std::sync::Arc; use std::sync::Arc;
use uuid::Uuid; use uuid::Uuid;
use std::fmt::Debug;
use crate::ServiceError;
use crate::permission::Authentication; use crate::permission::Authentication;
use crate::ServiceError;
#[derive(Debug, PartialEq, Eq, Clone, Copy)] #[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum DayOfWeek { pub enum DayOfWeek {
@ -89,10 +89,33 @@ impl From<&Slot> for dao::slot::SlotEntity {
pub trait SlotService { pub trait SlotService {
type Context: Clone + Debug + PartialEq + Eq + Send + Sync + 'static; type Context: Clone + Debug + PartialEq + Eq + Send + Sync + 'static;
async fn get_slots(&self, context: Authentication<Self::Context>) -> Result<Arc<[Slot]>, ServiceError>; async fn get_slots(
async fn get_slot(&self, id: &Uuid, context: Authentication<Self::Context>) -> Result<Slot, ServiceError>; &self,
async fn exists(&self, id: Uuid, context: Authentication<Self::Context>) -> Result<bool, ServiceError>; context: Authentication<Self::Context>,
async fn create_slot(&self, slot: &Slot, context: Authentication<Self::Context>) -> Result<Slot, ServiceError>; ) -> Result<Arc<[Slot]>, ServiceError>;
async fn delete_slot(&self, id: &Uuid, context: Authentication<Self::Context>) -> Result<(), ServiceError>; async fn get_slot(
async fn update_slot(&self, slot: &Slot, context: Authentication<Self::Context>) -> Result<(), ServiceError>; &self,
id: &Uuid,
context: Authentication<Self::Context>,
) -> Result<Slot, ServiceError>;
async fn exists(
&self,
id: Uuid,
context: Authentication<Self::Context>,
) -> Result<bool, ServiceError>;
async fn create_slot(
&self,
slot: &Slot,
context: Authentication<Self::Context>,
) -> Result<Slot, ServiceError>;
async fn delete_slot(
&self,
id: &Uuid,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError>;
async fn update_slot(
&self,
slot: &Slot,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError>;
} }

View file

@ -1,5 +1,5 @@
use std::sync::Arc;
use std::fmt::Debug; use std::fmt::Debug;
use std::sync::Arc;
use async_trait::async_trait; use async_trait::async_trait;
use mockall::automock; use mockall::automock;

View file

@ -1,8 +1,8 @@
use async_trait::async_trait; use async_trait::async_trait;
use service::{ use service::{
booking::{Booking, BookingService}, booking::{Booking, BookingService},
ServiceError, ValidationFailureItem,
permission::Authentication, permission::Authentication,
ServiceError, ValidationFailureItem,
}; };
use std::sync::Arc; use std::sync::Arc;
use uuid::Uuid; use uuid::Uuid;
@ -90,7 +90,10 @@ where
{ {
type Context = PermissionService::Context; type Context = PermissionService::Context;
async fn get_all(&self, context: Authentication<Self::Context>) -> Result<Arc<[Booking]>, ServiceError> { async fn get_all(
&self,
context: Authentication<Self::Context>,
) -> Result<Arc<[Booking]>, ServiceError> {
self.permission_service self.permission_service
.check_permission("hr", context) .check_permission("hr", context)
.await?; .await?;
@ -103,7 +106,11 @@ where
.collect()) .collect())
} }
async fn get(&self, id: Uuid, context: Authentication<Self::Context>) -> Result<Booking, ServiceError> { async fn get(
&self,
id: Uuid,
context: Authentication<Self::Context>,
) -> Result<Booking, ServiceError> {
self.permission_service self.permission_service
.check_permission("hr", context) .check_permission("hr", context)
.await?; .await?;
@ -149,22 +156,20 @@ where
if booking.calendar_week > 53 { if booking.calendar_week > 53 {
validation.push(ValidationFailureItem::InvalidValue("calendar_week".into())); validation.push(ValidationFailureItem::InvalidValue("calendar_week".into()));
} }
if self if !self
.sales_person_service .sales_person_service
.exists(booking.sales_person_id, context.clone()) .exists(booking.sales_person_id, context.clone())
.await? .await?
== false
{ {
validation.push(ValidationFailureItem::IdDoesNotExist( validation.push(ValidationFailureItem::IdDoesNotExist(
"sales_person_id".into(), "sales_person_id".into(),
booking.sales_person_id, booking.sales_person_id,
)); ));
} }
if self if !self
.slot_service .slot_service
.exists(booking.slot_id, context.clone()) .exists(booking.slot_id, context.clone())
.await? .await?
== false
{ {
validation.push(ValidationFailureItem::IdDoesNotExist( validation.push(ValidationFailureItem::IdDoesNotExist(
"slot_id".into(), "slot_id".into(),
@ -178,7 +183,10 @@ where
booking.slot_id, booking.slot_id,
booking.calendar_week, booking.calendar_week,
booking.year, booking.year,
).await?.is_some() { )
.await?
.is_some()
{
validation.push(ValidationFailureItem::Duplicate); validation.push(ValidationFailureItem::Duplicate);
} }
@ -202,7 +210,11 @@ where
Ok(new_booking) Ok(new_booking)
} }
async fn delete(&self, id: Uuid, context: Authentication<Self::Context>) -> Result<(), ServiceError> { async fn delete(
&self,
id: Uuid,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError> {
self.permission_service self.permission_service
.check_permission("hr", context) .check_permission("hr", context)
.await?; .await?;

View file

@ -1,8 +1,8 @@
use std::sync::Arc; use std::sync::Arc;
use async_trait::async_trait; use async_trait::async_trait;
use service::ServiceError;
use service::permission::Authentication; use service::permission::Authentication;
use service::ServiceError;
pub struct PermissionServiceImpl<PermissionDao, UserService> pub struct PermissionServiceImpl<PermissionDao, UserService>
where where
@ -82,9 +82,17 @@ where
Ok(()) Ok(())
} }
async fn user_exists(&self, user: &str, context: Authentication<Self::Context>) -> Result<bool, ServiceError> { async fn user_exists(
&self,
user: &str,
context: Authentication<Self::Context>,
) -> Result<bool, ServiceError> {
self.check_permission("hr", context).await?; self.check_permission("hr", context).await?;
Ok(self.permission_dao.find_user(user).await.map(|x| x.is_some())?) Ok(self
.permission_dao
.find_user(user)
.await
.map(|x| x.is_some())?)
} }
async fn get_all_users( async fn get_all_users(

View file

@ -2,7 +2,9 @@ use std::sync::Arc;
use async_trait::async_trait; use async_trait::async_trait;
use dao::sales_person::SalesPersonEntity; use dao::sales_person::SalesPersonEntity;
use service::{permission::Authentication, sales_person::SalesPerson, ServiceError, ValidationFailureItem}; use service::{
permission::Authentication, sales_person::SalesPerson, ServiceError, ValidationFailureItem,
};
use uuid::Uuid; use uuid::Uuid;
pub struct SalesPersonServiceImpl<SalesPersonDao, PermissionService, ClockService, UuidService> pub struct SalesPersonServiceImpl<SalesPersonDao, PermissionService, ClockService, UuidService>
@ -86,7 +88,11 @@ where
.ok_or(ServiceError::EntityNotFound(id)) .ok_or(ServiceError::EntityNotFound(id))
} }
async fn exists(&self, id: Uuid, _context: Authentication<Self::Context>) -> Result<bool, ServiceError> { async fn exists(
&self,
id: Uuid,
_context: Authentication<Self::Context>,
) -> Result<bool, ServiceError> {
Ok(self Ok(self
.sales_person_dao .sales_person_dao
.find_by_id(id) .find_by_id(id)
@ -172,7 +178,11 @@ where
Ok(sales_person) Ok(sales_person)
} }
async fn delete(&self, id: Uuid, context: Authentication<Self::Context>) -> Result<(), ServiceError> { async fn delete(
&self,
id: Uuid,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError> {
self.permission_service self.permission_service
.check_permission("hr", context) .check_permission("hr", context)
.await?; .await?;
@ -197,7 +207,10 @@ where
self.permission_service self.permission_service
.check_permission("hr", context) .check_permission("hr", context)
.await?; .await?;
Ok(self.sales_person_dao.get_assigned_user(sales_person_id).await?) Ok(self
.sales_person_dao
.get_assigned_user(sales_person_id)
.await?)
} }
async fn set_user( async fn set_user(
@ -216,7 +229,6 @@ where
self.sales_person_dao self.sales_person_dao
.assign_to_user(sales_person_id, user.as_ref(), SALES_PERSON_SERVICE_PROCESS) .assign_to_user(sales_person_id, user.as_ref(), SALES_PERSON_SERVICE_PROCESS)
.await?; .await?;
} }
Ok(()) Ok(())
} }

View file

@ -60,7 +60,10 @@ where
{ {
type Context = PermissionService::Context; type Context = PermissionService::Context;
async fn get_slots(&self, context: Authentication<Self::Context>) -> Result<Arc<[Slot]>, ServiceError> { async fn get_slots(
&self,
context: Authentication<Self::Context>,
) -> Result<Arc<[Slot]>, ServiceError> {
let (hr_permission, sales_permission) = join!( let (hr_permission, sales_permission) = join!(
self.permission_service self.permission_service
.check_permission("hr", context.clone()), .check_permission("hr", context.clone()),
@ -76,7 +79,11 @@ where
.map(Slot::from) .map(Slot::from)
.collect()) .collect())
} }
async fn get_slot(&self, id: &Uuid, context: Authentication<Self::Context>) -> Result<Slot, ServiceError> { async fn get_slot(
&self,
id: &Uuid,
context: Authentication<Self::Context>,
) -> Result<Slot, ServiceError> {
let (hr_permission, sales_permission) = join!( let (hr_permission, sales_permission) = join!(
self.permission_service self.permission_service
.check_permission("hr", context.clone()), .check_permission("hr", context.clone()),
@ -92,11 +99,19 @@ where
Ok(slot) Ok(slot)
} }
async fn exists(&self, id: Uuid, _context: Authentication<Self::Context>) -> Result<bool, ServiceError> { async fn exists(
&self,
id: Uuid,
_context: Authentication<Self::Context>,
) -> Result<bool, ServiceError> {
Ok(self.slot_dao.get_slot(&id).await.map(|s| s.is_some())?) Ok(self.slot_dao.get_slot(&id).await.map(|s| s.is_some())?)
} }
async fn create_slot(&self, slot: &Slot, context: Authentication<Self::Context>) -> Result<Slot, ServiceError> { async fn create_slot(
&self,
slot: &Slot,
context: Authentication<Self::Context>,
) -> Result<Slot, ServiceError> {
self.permission_service self.permission_service
.check_permission("hr", context.clone()) .check_permission("hr", context.clone())
.await?; .await?;
@ -137,7 +152,11 @@ where
Ok(slot) Ok(slot)
} }
async fn delete_slot(&self, id: &Uuid, context: Authentication<Self::Context>) -> Result<(), ServiceError> { async fn delete_slot(
&self,
id: &Uuid,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError> {
self.permission_service self.permission_service
.check_permission("hr", context) .check_permission("hr", context)
.await?; .await?;
@ -152,7 +171,11 @@ where
.await?; .await?;
Ok(()) Ok(())
} }
async fn update_slot(&self, slot: &Slot, context: Authentication<Self::Context>) -> Result<(), ServiceError> { async fn update_slot(
&self,
slot: &Slot,
context: Authentication<Self::Context>,
) -> Result<(), ServiceError> {
self.permission_service self.permission_service
.check_permission("hr", context) .check_permission("hr", context)
.await?; .await?;

View file

@ -2,14 +2,16 @@ use crate::test::error_test::*;
use dao::booking::{BookingEntity, MockBookingDao}; use dao::booking::{BookingEntity, MockBookingDao};
use mockall::predicate::eq; use mockall::predicate::eq;
use service::{ use service::{
booking::Booking, clock::MockClockService, sales_person::MockSalesPersonService, slot::MockSlotService, uuid_service::MockUuidService, MockPermissionService, ValidationFailureItem booking::Booking, clock::MockClockService, sales_person::MockSalesPersonService,
slot::MockSlotService, uuid_service::MockUuidService, MockPermissionService,
ValidationFailureItem,
}; };
use time::{Date, Month, PrimitiveDateTime, Time}; use time::{Date, Month, PrimitiveDateTime, Time};
use uuid::{uuid, Uuid}; use uuid::{uuid, Uuid};
use super::error_test::NoneTypeExt;
use crate::booking::BookingServiceImpl; use crate::booking::BookingServiceImpl;
use service::booking::BookingService; use service::booking::BookingService;
use super::error_test::NoneTypeExt;
pub fn default_id() -> Uuid { pub fn default_id() -> Uuid {
uuid!("CEA260A0-112B-4970-936C-F7E529955BD0") uuid!("CEA260A0-112B-4970-936C-F7E529955BD0")
@ -94,7 +96,9 @@ impl BookingServiceDependencies {
pub fn build_dependencies(permission: bool, role: &'static str) -> BookingServiceDependencies { pub fn build_dependencies(permission: bool, role: &'static str) -> BookingServiceDependencies {
let mut booking_dao = MockBookingDao::new(); let mut booking_dao = MockBookingDao::new();
booking_dao.expect_find_by_booking_data().returning(|_, _, _, _| Ok(None)); booking_dao
.expect_find_by_booking_data()
.returning(|_, _, _, _| Ok(None));
let mut permission_service = MockPermissionService::new(); let mut permission_service = MockPermissionService::new();
permission_service permission_service
.expect_check_permission() .expect_check_permission()
@ -357,7 +361,12 @@ async fn test_create_booking_data_already_exists() {
deps.booking_dao.checkpoint(); deps.booking_dao.checkpoint();
deps.booking_dao deps.booking_dao
.expect_find_by_booking_data() .expect_find_by_booking_data()
.with(eq(default_sales_person_id()), eq(default_slot_id()), eq(3), eq(2024)) .with(
eq(default_sales_person_id()),
eq(default_slot_id()),
eq(3),
eq(2024),
)
.returning(|_, _, _, _| Ok(Some(default_booking_entity()))); .returning(|_, _, _, _| Ok(Some(default_booking_entity())));
let service = deps.build_service(); let service = deps.build_service();
let result = service let result = service
@ -371,12 +380,7 @@ async fn test_create_booking_data_already_exists() {
().auth(), ().auth(),
) )
.await; .await;
test_validation_error( test_validation_error(&result, &ValidationFailureItem::Duplicate, 1);
&result,
&ValidationFailureItem::Duplicate,
1,
);
} }
#[tokio::test] #[tokio::test]

View file

@ -122,5 +122,4 @@ impl NoneTypeExt for () {
fn auth(&self) -> Authentication<()> { fn auth(&self) -> Authentication<()> {
Authentication::Context(()) Authentication::Context(())
} }
} }

View file

@ -30,7 +30,9 @@ async fn test_check_permission() {
let permission_service = let permission_service =
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service)); PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
let result = permission_service.check_permission("hello", ().auth()).await; let result = permission_service
.check_permission("hello", ().auth())
.await;
result.expect("Expected successful authorization"); result.expect("Expected successful authorization");
} }
@ -40,7 +42,9 @@ async fn test_check_permission_denied() {
let permission_service = let permission_service =
PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service)); PermissionServiceImpl::new(Arc::new(permission_dao), Arc::new(user_service));
let result = permission_service.check_permission("hello", ().auth()).await; let result = permission_service
.check_permission("hello", ().auth())
.await;
test_forbidden(&result); test_forbidden(&result);
} }

View file

@ -527,7 +527,10 @@ async fn test_exists() {
.with(eq(default_id())) .with(eq(default_id()))
.returning(|_| Ok(Some(default_sales_person_entity()))); .returning(|_| Ok(Some(default_sales_person_entity())));
let sales_person_service = dependencies.build_service(); let sales_person_service = dependencies.build_service();
let result = sales_person_service.exists(default_id(), ().auth()).await.unwrap(); let result = sales_person_service
.exists(default_id(), ().auth())
.await
.unwrap();
assert!(result); assert!(result);
let mut dependencies = build_dependencies(true, "hr"); let mut dependencies = build_dependencies(true, "hr");
@ -537,6 +540,9 @@ async fn test_exists() {
.expect_find_by_id() .expect_find_by_id()
.with(eq(default_id())) .with(eq(default_id()))
.returning(|_| Ok(None)); .returning(|_| Ok(None));
let result = sales_person_service.exists(default_id(), ().auth()).await.unwrap(); let result = sales_person_service
.exists(default_id(), ().auth())
.await
.unwrap();
assert_eq!(false, !result); assert_eq!(false, !result);
} }

View file

@ -257,7 +257,9 @@ async fn test_create_slot() {
async fn test_create_slot_no_permission() { async fn test_create_slot_no_permission() {
let dependencies = build_dependencies(false, "hr"); let dependencies = build_dependencies(false, "hr");
let slot_service = dependencies.build_service(); let slot_service = dependencies.build_service();
let result = slot_service.create_slot(&generate_default_slot(), ().auth()).await; let result = slot_service
.create_slot(&generate_default_slot(), ().auth())
.await;
test_forbidden(&result); test_forbidden(&result);
} }
@ -568,7 +570,9 @@ async fn test_delete_slot_not_found() {
async fn test_update_slot_no_permission() { async fn test_update_slot_no_permission() {
let dependencies = build_dependencies(false, "hr"); let dependencies = build_dependencies(false, "hr");
let slot_service = dependencies.build_service(); let slot_service = dependencies.build_service();
let result = slot_service.update_slot(&generate_default_slot(), ().auth()).await; let result = slot_service
.update_slot(&generate_default_slot(), ().auth())
.await;
test_forbidden(&result); test_forbidden(&result);
} }
@ -582,7 +586,9 @@ async fn test_update_slot_not_found() {
.times(1) .times(1)
.returning(|_| Ok(None)); .returning(|_| Ok(None));
let slot_service = dependencies.build_service(); let slot_service = dependencies.build_service();
let result = slot_service.update_slot(&generate_default_slot(), ().auth()).await; let result = slot_service
.update_slot(&generate_default_slot(), ().auth())
.await;
test_not_found(&result, &default_id()); test_not_found(&result, &default_id());
} }