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