| 431 | } |
| 432 | |
| 433 | pub async fn list( |
| 434 | limit: i64, |
| 435 | offset: i64, |
| 436 | filters: &Filters, |
| 437 | ) -> Result<Vec<ApplicationListItem>, Error> { |
| 438 | let mut q = application::dsl::application |
| 439 | .select(( |
| 440 | application::id, |
| 441 | application::created_at, |
| 442 | application::updated_at, |
| 443 | application::name, |
| 444 | application::description, |
| 445 | )) |
| 446 | .into_boxed(); |
| 447 | |
| 448 | // Filter by user permissions |
| 449 | if let Some(user_id) = &filters.user_id { |
| 450 | q = q.filter(dsl::exists( |
| 451 | user::table |
| 452 | .filter( |
| 453 | user::id |
| 454 | .eq(fields::Uuid::from(user_id)) |
| 455 | .and(user::is_active.eq(true)), |
| 456 | ) |
| 457 | .filter( |
| 458 | user::is_admin.eq(true).or(dsl::exists( |
| 459 | tenant_user::table |
| 460 | .filter( |
| 461 | tenant_user::user_id |
| 462 | .eq(user::id) |
| 463 | .and(tenant_user::tenant_id.eq(application::tenant_id)), |
| 464 | ) |
| 465 | .filter( |
| 466 | tenant_user::is_admin |
| 467 | .eq(true) |
| 468 | .or(tenant_user::is_device_admin.eq(true)) |
| 469 | .or(dsl::exists( |
| 470 | tenant_user_application::table.filter( |
| 471 | tenant_user_application::user_id.eq(user::id).and( |
| 472 | tenant_user_application::application_id |
| 473 | .eq(application::id), |
| 474 | ), |
| 475 | ), |
| 476 | )), |
| 477 | ), |
| 478 | )), |
| 479 | ), |
| 480 | )); |
| 481 | } |
| 482 | |
| 483 | if let Some(tenant_id) = &filters.tenant_id { |
| 484 | q = q.filter(application::dsl::tenant_id.eq(fields::Uuid::from(tenant_id))); |
| 485 | } |
| 486 | |
| 487 | if let Some(search) = &filters.search { |
| 488 | #[cfg(feature = "postgres")] |
| 489 | { |
| 490 | q = q.filter(application::dsl::name.ilike(format!("%{}%", search))); |