1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use async_trait::async_trait;
use serde::Serialize;
use thiserror::Error;
use crate::organizations::OrganizationId;
use crate::roles::RoleSlug;
use crate::user_management::{Invitation, Locale, UserId, UserManagement};
use crate::{ResponseExt, WorkOsError, WorkOsResult};
/// The parameters for [`SendInvitation`].
#[derive(Debug, Serialize)]
pub struct SendInvitationParams<'a> {
/// The email address of the recipient.
pub email: &'a str,
/// The ID of the organization that the recipient will join.
pub organization_id: Option<&'a OrganizationId>,
/// How many days the invitations will be valid for.
pub expires_in_days: Option<u8>,
/// The ID of the user who invites the recipient.
///
/// The invitation email will mention the name of this user.
pub inviter_user_id: Option<&'a UserId>,
/// The role that the recipient will receive when they join the organization in the invitation.
pub role_slug: Option<&'a RoleSlug>,
/// The locale to use when rendering the invitation email.
pub locale: Option<&'a Locale>,
}
/// An error returned from [`SendInvitation`].
#[derive(Debug, Error)]
pub enum SendInvitationError {}
impl From<SendInvitationError> for WorkOsError<SendInvitationError> {
fn from(err: SendInvitationError) -> Self {
Self::Operation(err)
}
}
/// [WorkOS Docs: Send an invitation](https://workos.com/docs/reference/user-management/invitation/send)
#[async_trait]
pub trait SendInvitation {
/// Sends an invitation email to the recipient.
///
/// [WorkOS Docs: Send an invitation](https://workos.com/docs/reference/user-management/invitation/send)
///
/// # Examples
///
/// ```
/// # use workos::WorkOsResult;
/// # use workos::user_management::*;
/// use workos::{ApiKey, WorkOs};
///
/// # async fn run() -> WorkOsResult<(), SendInvitationError> {
/// let workos = WorkOs::new(&ApiKey::from("sk_example_123456789"));
///
/// let invitation = workos
/// .user_management()
/// .send_invitation(&SendInvitationParams {
/// email: "marcelina@example.com",
/// organization_id: None,
/// expires_in_days: None,
/// inviter_user_id: None,
/// role_slug: None,
/// locale: None,
/// })
/// .await?;
/// # Ok(())
/// # }
/// ```
async fn send_invitation(
&self,
params: &SendInvitationParams<'_>,
) -> WorkOsResult<Invitation, SendInvitationError>;
}
#[async_trait]
impl SendInvitation for UserManagement<'_> {
async fn send_invitation(
&self,
params: &SendInvitationParams<'_>,
) -> WorkOsResult<Invitation, SendInvitationError> {
let url = self
.workos
.base_url()
.join("/user_management/invitations")?;
let invitation = self
.workos
.client()
.post(url)
.bearer_auth(self.workos.key())
.json(¶ms)
.send()
.await?
.handle_unauthorized_or_generic_error()
.await?
.json::<Invitation>()
.await?;
Ok(invitation)
}
}
#[cfg(test)]
mod test {
use serde_json::json;
use tokio;
use crate::user_management::InvitationId;
use crate::{ApiKey, WorkOs};
use super::*;
#[tokio::test]
async fn it_calls_the_send_invitation_endpoint() {
let mut server = mockito::Server::new_async().await;
let workos = WorkOs::builder(&ApiKey::from("sk_example_123456789"))
.base_url(&server.url())
.unwrap()
.build();
server
.mock("POST", "/user_management/invitations")
.match_header("Authorization", "Bearer sk_example_123456789")
.with_status(201)
.with_body(
json!({
"object": "invitation",
"id": "invitation_01E4ZCR3C56J083X43JQXF3JK5",
"email": "marcelina.davis@example.com",
"state": "pending",
"accepted_at": null,
"revoked_at": null,
"expires_at": "2021-07-01T19:07:33.155Z",
"token": "Z1uX3RbwcIl5fIGJJJCXXisdI",
"accept_invitation_url": "https://your-app.com/invite?invitation_token=Z1uX3RbwcIl5fIGJJJCXXisdI",
"organization_id": "org_01E4ZCR3C56J083X43JQXF3JK5",
"inviter_user_id": "user_01HYGBX8ZGD19949T3BM4FW1C3",
"created_at": "2021-06-25T19:07:33.155Z",
"updated_at": "2021-06-25T19:07:33.155Z"
})
.to_string(),
)
.create_async()
.await;
let invitation = workos
.user_management()
.send_invitation(&SendInvitationParams {
email: "marcelina@example.com",
organization_id: None,
expires_in_days: None,
inviter_user_id: None,
role_slug: None,
locale: None,
})
.await
.unwrap();
assert_eq!(
invitation.id,
InvitationId::from("invitation_01E4ZCR3C56J083X43JQXF3JK5")
)
}
}