| 375 | } |
| 376 | |
| 377 | pub async fn get_count(filters: &Filters) -> Result<i64, Error> { |
| 378 | let mut q = application::table.select(dsl::count_star()).into_boxed(); |
| 379 | |
| 380 | // Filter by user permissions |
| 381 | if let Some(user_id) = &filters.user_id { |
| 382 | q = q.filter(dsl::exists( |
| 383 | user::table |
| 384 | .filter( |
| 385 | user::id |
| 386 | .eq(fields::Uuid::from(user_id)) |
| 387 | .and(user::is_active.eq(true)), |
| 388 | ) |
| 389 | .filter( |
| 390 | user::is_admin.eq(true).or(dsl::exists( |
| 391 | tenant_user::table |
| 392 | .filter( |
| 393 | tenant_user::user_id |
| 394 | .eq(user::id) |
| 395 | .and(tenant_user::tenant_id.eq(application::tenant_id)), |
| 396 | ) |
| 397 | .filter( |
| 398 | tenant_user::is_admin |
| 399 | .eq(true) |
| 400 | .or(tenant_user::is_device_admin.eq(true)) |
| 401 | .or(dsl::exists( |
| 402 | tenant_user_application::table.filter( |
| 403 | tenant_user_application::user_id.eq(user::id).and( |
| 404 | tenant_user_application::application_id |
| 405 | .eq(application::id), |
| 406 | ), |
| 407 | ), |
| 408 | )), |
| 409 | ), |
| 410 | )), |
| 411 | ), |
| 412 | )); |
| 413 | } |
| 414 | |
| 415 | if let Some(tenant_id) = &filters.tenant_id { |
| 416 | q = q.filter(application::tenant_id.eq(fields::Uuid::from(tenant_id))); |
| 417 | } |
| 418 | |
| 419 | if let Some(search) = &filters.search { |
| 420 | #[cfg(feature = "postgres")] |
| 421 | { |
| 422 | q = q.filter(application::name.ilike(format!("%{}%", search))); |
| 423 | } |
| 424 | #[cfg(feature = "sqlite")] |
| 425 | { |
| 426 | q = q.filter(application::name.like(format!("%{}%", search))); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | Ok(q.first(&mut get_async_db_conn().await?).await?) |
| 431 | } |
| 432 | |
| 433 | pub async fn list( |
| 434 | limit: i64, |