(
user_id: Option<Uuid>,
tenant_id: Option<Uuid>,
application_id: Option<Uuid>,
)
| 266 | } |
| 267 | |
| 268 | pub async fn get_deployment_count( |
| 269 | user_id: Option<Uuid>, |
| 270 | tenant_id: Option<Uuid>, |
| 271 | application_id: Option<Uuid>, |
| 272 | ) -> Result<i64, Error> { |
| 273 | let mut q = fuota_deployment::table |
| 274 | .inner_join(application::table) |
| 275 | .select(dsl::count_star()) |
| 276 | .into_boxed(); |
| 277 | |
| 278 | // filter by user permissions |
| 279 | if let Some(user_id) = user_id { |
| 280 | q = q.filter(dsl::exists( |
| 281 | user::table |
| 282 | .filter( |
| 283 | user::id |
| 284 | .eq(fields::Uuid::from(user_id)) |
| 285 | .and(user::is_active.eq(true)), |
| 286 | ) |
| 287 | .filter( |
| 288 | user::is_admin.eq(true).or(dsl::exists( |
| 289 | tenant_user::table |
| 290 | .filter( |
| 291 | tenant_user::user_id |
| 292 | .eq(user::id) |
| 293 | .and(tenant_user::tenant_id.eq(application::id)), |
| 294 | ) |
| 295 | .filter( |
| 296 | tenant_user::is_admin |
| 297 | .eq(true) |
| 298 | .or(tenant_user::is_device_admin.eq(true)) |
| 299 | .or(dsl::exists( |
| 300 | tenant_user_application::table.filter( |
| 301 | tenant_user_application::user_id.eq(user::id).and( |
| 302 | tenant_user_application::application_id |
| 303 | .eq(application::id), |
| 304 | ), |
| 305 | ), |
| 306 | )), |
| 307 | ), |
| 308 | )), |
| 309 | ), |
| 310 | )); |
| 311 | } |
| 312 | |
| 313 | if let Some(tenant_id) = tenant_id { |
| 314 | q = q.filter(application::tenant_id.eq(fields::Uuid::from(tenant_id))); |
| 315 | } |
| 316 | |
| 317 | if let Some(application_id) = application_id { |
| 318 | q = q.filter(fuota_deployment::application_id.eq(fields::Uuid::from(application_id))); |
| 319 | } |
| 320 | |
| 321 | q.first(&mut get_async_db_conn().await?) |
| 322 | .await |
| 323 | .map_err(|e| Error::from_diesel(e, "".into())) |
| 324 | } |
| 325 |
no test coverage detected