Add endpoint to get sales_person for current user

This commit is contained in:
Simon Goller 2024-06-12 12:04:41 +02:00
parent 42ebce15e8
commit bd887cfd7b
12 changed files with 108 additions and 5 deletions

View file

@ -33,7 +33,7 @@ pub trait BookingDao {
) -> Result<Option<BookingEntity>, DaoError>; ) -> Result<Option<BookingEntity>, DaoError>;
async fn find_by_week( async fn find_by_week(
&self, &self,
calendar_week: i32, calendar_week: u8,
year: u32, year: u32,
) -> Result<Arc<[BookingEntity]>, DaoError>; ) -> Result<Arc<[BookingEntity]>, DaoError>;
async fn create(&self, entity: &BookingEntity, process: &str) -> Result<(), DaoError>; async fn create(&self, entity: &BookingEntity, process: &str) -> Result<(), DaoError>;

View file

@ -31,4 +31,8 @@ pub trait SalesPersonDao {
process: &str, process: &str,
) -> Result<(), DaoError>; ) -> 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>;
async fn find_sales_person_by_user_id(
&self,
user_id: &str,
) -> Result<Option<SalesPersonEntity>, DaoError>;
} }

View file

@ -108,7 +108,7 @@ impl BookingDao for BookingDaoImpl {
async fn find_by_week( async fn find_by_week(
&self, &self,
calendar_week: i32, calendar_week: u8,
year: u32, year: u32,
) -> Result<Arc<[BookingEntity]>, DaoError> { ) -> Result<Arc<[BookingEntity]>, DaoError> {
Ok(query_as!( Ok(query_as!(

View file

@ -149,4 +149,22 @@ impl SalesPersonDao for SalesPersonDaoImpl {
.map_db_error()? .map_db_error()?
.map(|result| result.user_id.into())) .map(|result| result.user_id.into()))
} }
async fn find_sales_person_by_user_id(
&self,
user_id: &str,
) -> Result<Option<SalesPersonEntity>, DaoError> {
Ok(query_as!(
SalesPersonDb,
"SELECT sp.id, sp.name, sp.inactive, sp.deleted, sp.update_version FROM sales_person sp JOIN sales_person_user spu ON sp.id = spu.sales_person_id WHERE spu.user_id = ?",
user_id
)
.fetch_optional(self.pool.as_ref())
.await
.map_db_error()?
.as_ref()
.map(SalesPersonEntity::try_from)
.transpose()?
)
}
} }

View file

@ -45,7 +45,7 @@ pub async fn get_all_bookings<RestState: RestStateDef>(
pub async fn get_by_week<RestState: RestStateDef>( pub async fn get_by_week<RestState: RestStateDef>(
rest_state: State<RestState>, rest_state: State<RestState>,
Extension(context): Extension<Context>, Extension(context): Extension<Context>,
Path((calendar_week, year)): Path<(i32, u32)>, Path((year, calendar_week)): Path<(u32, u8)>,
) -> Response { ) -> Response {
error_handler( error_handler(
(async { (async {

View file

@ -21,6 +21,7 @@ pub fn generate_route<RestState: RestStateDef>() -> Router<RestState> {
.route("/:id/user", get(get_sales_person_user::<RestState>)) .route("/:id/user", get(get_sales_person_user::<RestState>))
.route("/:id/user", post(set_sales_person_user::<RestState>)) .route("/:id/user", post(set_sales_person_user::<RestState>))
.route("/:id/user", delete(delete_sales_person_user::<RestState>)) .route("/:id/user", delete(delete_sales_person_user::<RestState>))
.route("/current", get(get_sales_person_current_user::<RestState>))
} }
pub async fn get_all_sales_persons<RestState: RestStateDef>( pub async fn get_all_sales_persons<RestState: RestStateDef>(
@ -184,3 +185,24 @@ pub async fn delete_sales_person_user<RestState: RestStateDef>(
.await, .await,
) )
} }
pub async fn get_sales_person_current_user<RestState: RestStateDef>(
rest_state: State<RestState>,
Extension(context): Extension<Context>,
) -> Response {
error_handler(
(async {
let sales_person = rest_state
.sales_person_service()
.get_sales_person_current_user(context.into())
.await?
.map(|sales_person| SalesPersonTO::from(&sales_person));
Ok(Response::builder()
.status(200)
.body(Body::new(serde_json::to_string(&sales_person).unwrap()))
.unwrap())
})
.await,
)
}

View file

@ -66,7 +66,7 @@ pub trait BookingService {
) -> Result<Booking, ServiceError>; ) -> Result<Booking, ServiceError>;
async fn get_for_week( async fn get_for_week(
&self, &self,
calendar_week: i32, calendar_week: u8,
year: u32, year: u32,
context: Authentication<Self::Context>, context: Authentication<Self::Context>,
) -> Result<Arc<[Booking]>, ServiceError>; ) -> Result<Arc<[Booking]>, ServiceError>;

View file

@ -65,6 +65,10 @@ impl<Context: Clone + Debug + PartialEq + Eq + Send + Sync + 'static> From<Conte
pub trait PermissionService { pub trait PermissionService {
type Context: Clone + PartialEq + Eq + Debug + Send + Sync + 'static; type Context: Clone + PartialEq + Eq + Debug + Send + Sync + 'static;
async fn current_user_id(
&self,
context: Authentication<Self::Context>,
) -> Result<Option<Arc<str>>, ServiceError>;
async fn check_permission( async fn check_permission(
&self, &self,
privilege: &str, privilege: &str,

View file

@ -84,4 +84,13 @@ pub trait SalesPersonService {
user_id: Option<Arc<str>>, user_id: Option<Arc<str>>,
context: Authentication<Self::Context>, context: Authentication<Self::Context>,
) -> Result<(), ServiceError>; ) -> Result<(), ServiceError>;
async fn get_sales_person_for_user(
&self,
user_id: Arc<str>,
context: Authentication<Self::Context>,
) -> Result<Option<SalesPerson>, ServiceError>;
async fn get_sales_person_current_user(
&self,
context: Authentication<Self::Context>,
) -> Result<Option<SalesPerson>, ServiceError>;
} }

View file

@ -125,7 +125,7 @@ where
async fn get_for_week( async fn get_for_week(
&self, &self,
calendar_week: i32, calendar_week: u8,
year: u32, year: u32,
context: Authentication<Self::Context>, context: Authentication<Self::Context>,
) -> Result<Arc<[Booking]>, ServiceError> { ) -> Result<Arc<[Booking]>, ServiceError> {

View file

@ -36,6 +36,18 @@ where
{ {
type Context = UserService::Context; type Context = UserService::Context;
async fn current_user_id(
&self,
context: Authentication<Self::Context>,
) -> Result<Option<Arc<str>>, ServiceError> {
match context {
Authentication::Full => Ok(None),
Authentication::Context(context) => {
let current_user = self.user_service.current_user(context).await?;
Ok(Some(current_user))
}
}
}
async fn check_permission( async fn check_permission(
&self, &self,
privilege: &str, privilege: &str,

View file

@ -232,4 +232,38 @@ where
} }
Ok(()) Ok(())
} }
async fn get_sales_person_for_user(
&self,
user_id: Arc<str>,
context: Authentication<Self::Context>,
) -> Result<Option<SalesPerson>, ServiceError> {
self.permission_service
.check_permission("hr", context)
.await?;
Ok(self
.sales_person_dao
.find_sales_person_by_user_id(&user_id)
.await?
.as_ref()
.map(SalesPerson::from))
}
async fn get_sales_person_current_user(
&self,
context: Authentication<Self::Context>,
) -> Result<Option<SalesPerson>, ServiceError> {
let current_user = if let Some(current_user) = self
.permission_service
.current_user_id(context.clone())
.await?
{
current_user
} else {
return Ok(None);
};
Ok(self
.get_sales_person_for_user(current_user, Authentication::Full)
.await?)
}
} }