Skip to main content

bitwarden_member_administration/
organization_user_bulk_response.rs

1use bitwarden_api_api::models::OrganizationUserBulkResponseModel;
2use bitwarden_core::{MissingFieldError, require};
3use bitwarden_organizations::OrganizationUserId;
4use serde::{Deserialize, Serialize};
5
6/// The outcome of a bulk member operation for one organization member.
7#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
8#[serde(rename_all = "camelCase")]
9#[bitwarden_ffi::wasm_record]
10pub struct OrganizationUserBulkResponse {
11    /// The organization membership this outcome refers to.
12    pub id: OrganizationUserId,
13    /// Why the operation failed for this member. Absent when it succeeded.
14    pub error: Option<String>,
15}
16
17impl TryFrom<OrganizationUserBulkResponseModel> for OrganizationUserBulkResponse {
18    type Error = MissingFieldError;
19
20    fn try_from(model: OrganizationUserBulkResponseModel) -> Result<Self, Self::Error> {
21        Ok(Self {
22            id: OrganizationUserId::new(require!(model.id)),
23            // The server reports success as an empty error string rather than omitting it.
24            error: model.error.filter(|error| !error.is_empty()),
25        })
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    const MEMBER_A: &str = "1c4d9d5a-0000-4000-8000-00000000000a";
34
35    /// Builds the row the server emits for one member. Success is an empty error string.
36    fn row(id: Option<&str>, error: &str) -> OrganizationUserBulkResponseModel {
37        OrganizationUserBulkResponseModel {
38            object: Some("organizationUserBulkResponseModel".to_owned()),
39            id: id.map(|id| id.parse().unwrap()),
40            error: Some(error.to_owned()),
41        }
42    }
43
44    #[test]
45    fn empty_error_means_success() {
46        let response = OrganizationUserBulkResponse::try_from(row(Some(MEMBER_A), "")).unwrap();
47
48        assert_eq!(response.id, MEMBER_A.parse().unwrap());
49        assert_eq!(response.error, None);
50    }
51
52    #[test]
53    fn keeps_a_member_error() {
54        let response =
55            OrganizationUserBulkResponse::try_from(row(Some(MEMBER_A), "User is not staged."))
56                .unwrap();
57
58        assert_eq!(response.error, Some("User is not staged.".to_owned()));
59    }
60
61    #[test]
62    fn fails_when_a_row_has_no_id() {
63        assert!(OrganizationUserBulkResponse::try_from(row(None, "")).is_err());
64    }
65}