(
tu: TenantUser,
device_profiles: &[TenantUserDeviceProfile],
applications: &[TenantUserApplication],
)
| 409 | } |
| 410 | |
| 411 | pub async fn update_user( |
| 412 | tu: TenantUser, |
| 413 | device_profiles: &[TenantUserDeviceProfile], |
| 414 | applications: &[TenantUserApplication], |
| 415 | ) -> Result<TenantUser, Error> { |
| 416 | let mut c = get_async_db_conn().await?; |
| 417 | let tu: TenantUser = c |
| 418 | .transaction::<TenantUser, Error, _>(async |c| { |
| 419 | let tu: TenantUser = diesel::update( |
| 420 | tenant_user::dsl::tenant_user |
| 421 | .filter(tenant_user::dsl::tenant_id.eq(&tu.tenant_id)) |
| 422 | .filter(tenant_user::dsl::user_id.eq(&tu.user_id)), |
| 423 | ) |
| 424 | .set(&tu) |
| 425 | .get_result(c) |
| 426 | .await |
| 427 | .map_err(|e| Error::from_diesel(e, tu.user_id.to_string()))?; |
| 428 | |
| 429 | // delete device-profile links |
| 430 | diesel::delete( |
| 431 | tenant_user_device_profile::table.filter( |
| 432 | tenant_user_device_profile::user_id.eq(&tu.user_id).and( |
| 433 | tenant_user_device_profile::device_profile_id.eq_any( |
| 434 | device_profile::table |
| 435 | .filter(device_profile::tenant_id.eq(&tu.tenant_id)) |
| 436 | .select(device_profile::id), |
| 437 | ), |
| 438 | ), |
| 439 | ), |
| 440 | ) |
| 441 | .execute(c) |
| 442 | .await |
| 443 | .map_err(|e| Error::from_diesel(e, tu.user_id.to_string()))?; |
| 444 | |
| 445 | // delete application links |
| 446 | diesel::delete( |
| 447 | tenant_user_application::table.filter( |
| 448 | tenant_user_application::user_id.eq(&tu.user_id).and( |
| 449 | tenant_user_application::application_id.eq_any( |
| 450 | application::table |
| 451 | .filter(application::tenant_id.eq(&tu.tenant_id)) |
| 452 | .select(application::id), |
| 453 | ), |
| 454 | ), |
| 455 | ), |
| 456 | ) |
| 457 | .execute(c) |
| 458 | .await |
| 459 | .map_err(|e| Error::from_diesel(e, tu.user_id.to_string()))?; |
| 460 | |
| 461 | // re-create device-profiles |
| 462 | for dp in device_profiles { |
| 463 | // make sure dp exists under same tenant |
| 464 | let _: storage::device_profile::DeviceProfile = device_profile::table |
| 465 | .filter( |
| 466 | device_profile::id |
| 467 | .eq(dp.device_profile_id) |
| 468 | .and(device_profile::tenant_id.eq(&tu.tenant_id)), |
no test coverage detected