diff --git a/backend/src/controllers/mo_controller.rs b/backend/src/controllers/mo_controller.rs
index c02d6254..ce5a1489 100644
--- a/backend/src/controllers/mo_controller.rs
+++ b/backend/src/controllers/mo_controller.rs
@@ -1,8 +1,17 @@
+use axum::extract::Query;
+use serde::Deserialize;
+#[derive(Deserialize, Default)]
+#[serde(default)]
+pub struct MoQuery {
+ pub org_id: Option,
+ pub service_id: Option,
+}
use crate::dto::top_polluting_site_dto::TopPollutingSite;
use crate::green_score::calculate_green_score;
use crate::service::advice_service::AdviceService;
use crate::service::equivalent_service::EquivalentService;
use crate::service::organisation_service::OrganisationService;
+use crate::service::service_service::ServiceService;
use crate::models::equivalent::Equivalent;
use axum::extract::State;
use axum::Json;
@@ -25,17 +34,33 @@ pub struct MyOrganizationResponse {
pub weekly_consumption: Vec,
pub monthly_consumption: Vec,
pub top_polluting_sites: Vec,
+ pub is_admin: bool,
+ pub services: Option>,
}
-pub async fn mo(State(pool): State, AuthenticatedUser(user_full): AuthenticatedUser) -> Result, AppError> {
+pub async fn mo(
+ State(pool): State,
+ Query(query): Query,
+ AuthenticatedUser(user_full): AuthenticatedUser
+) -> Result, AppError> {
- let organization = user_full.organisation.first().ok_or(AppError::NotFound("User is not in an organization".to_string()))?;
+ let organization = if let Some(oid) = query.org_id {
+ user_full.organisation.iter().find(|o| o.id == oid).cloned().ok_or(AppError::NotFound("User is not in the specified organization".to_string()))?
+ } else {
+ user_full.organisation.first().cloned().ok_or(AppError::NotFound("User is not in an organization".to_string()))?
+ };
let organization_id = organization.id;
let user_id = user_full.user.id;
- let organization_informations = OrganisationService::organization_informations(&pool, organization_id, user_id).await
+ let service_id = if organization.est_admin {
+ query.service_id
+ } else {
+ user_full.service.map(|s| s.id)
+ };
+
+ let organization_informations = OrganisationService::organization_informations(&pool, organization_id, user_id, service_id).await
.map_err(AppError::from)?;
let advices: Vec = vec![
@@ -48,15 +73,23 @@ pub async fn mo(State(pool): State, AuthenticatedUser(user_full): Aut
let equivalents = EquivalentService::equivalent(&pool, Some(user_id), 2, organization_informations.total_consumption).await
.ok();
- let daily_consumption = OrganisationService::get_daily_organization_consumption(&pool, organization_id).await
+ let daily_consumption = OrganisationService::get_daily_organization_consumption(&pool, organization_id, service_id).await
.map_err(AppError::from)?;
- let weekly_consumption = OrganisationService::get_weekly_organization_consumption(&pool, organization_id).await
+ let weekly_consumption = OrganisationService::get_weekly_organization_consumption(&pool, organization_id, service_id).await
.map_err(AppError::from)?;
- let monthly_consumption = OrganisationService::get_monthly_organization_consumption(&pool, organization_id).await
+ let monthly_consumption = OrganisationService::get_monthly_organization_consumption(&pool, organization_id, service_id).await
.map_err(AppError::from)?;
- let top_polluting_sites = OrganisationService::get_top5_polluting_sites_by_organization(&pool, organization_id).await
+ let top_polluting_sites = OrganisationService::get_top5_polluting_sites_by_organization(&pool, organization_id, service_id).await
.map_err(AppError::from)?;
+ let is_admin = organization.est_admin;
+
+ let services = if is_admin {
+ ServiceService::get_organisation_services(&pool, organization_id).await.ok()
+ } else {
+ None
+ };
+
Ok(Json(MyOrganizationResponse {
success: true,
mo_infos: Some(organization_informations),
@@ -67,6 +100,8 @@ pub async fn mo(State(pool): State, AuthenticatedUser(user_full): Aut
daily_consumption,
weekly_consumption,
monthly_consumption,
- top_polluting_sites
+ top_polluting_sites,
+ is_admin,
+ services,
}))
}
\ No newline at end of file
diff --git a/backend/src/repository/monitored_website_repository.rs b/backend/src/repository/monitored_website_repository.rs
index 3144a32d..74dff5f2 100644
--- a/backend/src/repository/monitored_website_repository.rs
+++ b/backend/src/repository/monitored_website_repository.rs
@@ -52,22 +52,32 @@ impl MonitoredWebsiteRepository {
pub async fn get_top5_polluting_sites_by_organization(
pool: &MySqlPool,
- org_id: i64
+ org_id: i64, service_id: Option
) -> Result, Error> {
- let results = sqlx::query_as::<_, TopPollutingSite>(
+ let mut query = sqlx::QueryBuilder::new(
"SELECT
mw.url_domain,
SUM(mw.carbon_footprint) as total_footprint
FROM monitored_website mw
JOIN user u
ON u.id = mw.user_id
- WHERE u.organisation_id = ?
- AND mw.url_domain IS NOT NULL
+ JOIN organisation_user ou
+ ON ou.user_id = u.id
+ WHERE ou.organisation_id = "
+ );
+ query.push_bind(org_id);
+
+ if let Some(sid) = service_id {
+ query.push(" AND u.service_id = ");
+ query.push_bind(sid);
+ }
+
+ query.push(" AND mw.url_domain IS NOT NULL
GROUP BY mw.url_domain
ORDER BY total_footprint
- DESC LIMIT 5"
- )
- .bind(org_id)
+ DESC LIMIT 5");
+
+ let results = query.build_query_as::()
.fetch_all(pool)
.await?;
Ok(results)
@@ -96,9 +106,9 @@ impl MonitoredWebsiteRepository {
pub async fn average_daily_carbon_footprint_for_organization(
pool: &MySqlPool,
- org_id: i64
+ org_id: i64, service_id: Option
) -> f64 {
- sqlx::query_as::<_, (f64,)>(
+ let mut query = sqlx::QueryBuilder::new(
"SELECT ROUND(
COALESCE(
SUM(mw.carbon_footprint) / NULLIF(DATEDIFF(CURDATE(), MIN(DATE(mw.creation_date))) + 1, 0)
@@ -106,9 +116,17 @@ impl MonitoredWebsiteRepository {
, 2) AS average_daily_carbon_footprint
FROM monitored_website mw
JOIN user u ON u.id = mw.user_id
- WHERE u.organisation_id = ?"
- )
- .bind(org_id)
+ JOIN organisation_user ou ON ou.user_id = u.id
+ WHERE ou.organisation_id = "
+ );
+ query.push_bind(org_id);
+
+ if let Some(sid) = service_id {
+ query.push(" AND u.service_id = ");
+ query.push_bind(sid);
+ }
+
+ query.build_query_as::<(f64,)>()
.fetch_one(pool)
.await
.map(|(val,)| val)
@@ -117,16 +135,25 @@ impl MonitoredWebsiteRepository {
pub async fn total_organization_consumption(
pool: &MySqlPool,
- org_id: i64
+ org_id: i64, service_id: Option
) -> Result
{/if}
+
+ {#if userOrgs.length > 1}
+
+
+
+
+ {/if}
+ {#if isAdmin}
+
+
+
+
+ {:else if memberServiceName}
+
+ {$t('account.service.name_label')}:
+ {memberServiceName}
+
+ {/if}
+
{#if !noDatas}