| 596 | } |
| 597 | |
| 598 | pub async fn delete_user(tenant_id: &Uuid, user_id: &Uuid) -> Result<(), Error> { |
| 599 | // delete device-profile admin references |
| 600 | diesel::delete( |
| 601 | tenant_user_device_profile::table |
| 602 | .filter(tenant_user_device_profile::user_id.eq(&fields::Uuid::from(user_id))) |
| 603 | .filter( |
| 604 | tenant_user_device_profile::device_profile_id.eq_any( |
| 605 | device_profile::table |
| 606 | .filter(device_profile::tenant_id.eq(&fields::Uuid::from(tenant_id))) |
| 607 | .select(device_profile::id), |
| 608 | ), |
| 609 | ), |
| 610 | ) |
| 611 | .execute(&mut get_async_db_conn().await?) |
| 612 | .await?; |
| 613 | |
| 614 | // delete application admin references |
| 615 | diesel::delete( |
| 616 | tenant_user_application::table |
| 617 | .filter(tenant_user_application::user_id.eq(&fields::Uuid::from(user_id))) |
| 618 | .filter( |
| 619 | tenant_user_application::application_id.eq_any( |
| 620 | application::table |
| 621 | .filter(application::tenant_id.eq(&fields::Uuid::from(tenant_id))) |
| 622 | .select(application::id), |
| 623 | ), |
| 624 | ), |
| 625 | ) |
| 626 | .execute(&mut get_async_db_conn().await?) |
| 627 | .await?; |
| 628 | |
| 629 | let ra = diesel::delete( |
| 630 | tenant_user::dsl::tenant_user |
| 631 | .filter(tenant_user::dsl::tenant_id.eq(&fields::Uuid::from(tenant_id))) |
| 632 | .filter(tenant_user::dsl::user_id.eq(&fields::Uuid::from(user_id))), |
| 633 | ) |
| 634 | .execute(&mut get_async_db_conn().await?) |
| 635 | .await?; |
| 636 | if ra == 0 { |
| 637 | return Err(Error::NotFound(user_id.to_string())); |
| 638 | } |
| 639 | info!( |
| 640 | tenant_id = %tenant_id, |
| 641 | user_id = %user_id, |
| 642 | "Tenant user deleted" |
| 643 | ); |
| 644 | Ok(()) |
| 645 | } |
| 646 | |
| 647 | pub async fn get_tenant_users_for_user(user_id: &Uuid) -> Result<Vec<TenantUser>, Error> { |
| 648 | let items = tenant_user::dsl::tenant_user |