Skip to main content

ghl_sdk/services/
users.rs

1// @generated by xtask/generate_services.py — do not edit by hand.
2//! `users` — typed methods for all 7 API v2 operations
3//! in this module.
4//!
5//! Access via [`Ghl::users`](crate::Ghl::users).
6//!
7//! Request and response types come from [`ghl_models::v2::users`](https://docs.rs/ghl-models/latest/ghl_models/v2/users/); every endpoint is also documented in the
8//! [`users` API reference](https://github.com/Shahroz/ghl-rs/blob/main/docs/api/users.md).
9//!
10//! Enable with `features = ["users"]`.
11
12#![allow(clippy::too_many_arguments)]
13
14use crate::client::Ghl;
15use crate::error::Result;
16use ghl_models::v2::users as models;
17
18/// Typed access to the `users` API v2 surface (7 operations). Obtained via
19/// [`Ghl::users`](crate::Ghl::users).
20#[derive(Debug, Clone)]
21pub struct UsersService {
22    pub(crate) client: Ghl,
23}
24
25impl UsersService {
26    pub(crate) fn new(client: Ghl) -> Self {
27        Self { client }
28    }
29}
30
31/// Query parameters for [`UsersService::get_user_by_location`].
32#[derive(Debug, Clone, Default)]
33pub struct GetUserByLocationParams {
34    /// `locationId` query parameter.
35    /// Required by the API.
36    pub location_id: String,
37}
38
39impl GetUserByLocationParams {
40    /// Start from the parameters the API requires.
41    pub fn new(location_id: impl Into<String>) -> Self {
42        Self {
43            location_id: location_id.into(),
44        }
45    }
46
47    fn to_query(&self) -> Vec<(String, String)> {
48        let q: Vec<(String, String)> = vec![("locationId".into(), self.location_id.clone())];
49        q
50    }
51}
52
53/// Query parameters for [`UsersService::search_users`].
54#[derive(Debug, Clone, Default)]
55pub struct SearchUsersParams {
56    /// Company ID in which the search needs to be performed
57    /// Required by the API.
58    pub company_id: String,
59    /// The search term for the user is matched based on the user full name, email or phone
60    pub query: Option<String>,
61    /// No of results to be skipped before returning the result
62    pub skip: Option<String>,
63    /// No of results to be limited before returning the result
64    pub limit: Option<String>,
65    /// Location ID in which the search needs to be performed
66    pub location_id: Option<String>,
67    /// Type of the users to be filtered in the search
68    pub type_: Option<String>,
69    /// Role of the users to be filtered in the search
70    pub role: Option<String>,
71    /// List of User IDs to be filtered in the search
72    pub ids: Option<String>,
73    /// The field on which sort is applied in which the results need to be sorted. Default
74    /// is based on the first and last name
75    pub sort: Option<String>,
76    /// The direction in which the results need to be sorted
77    pub sort_direction: Option<String>,
78    /// `enabled2waySync` query parameter.
79    pub enabled2way_sync: Option<bool>,
80}
81
82impl SearchUsersParams {
83    /// Start from the parameters the API requires.
84    pub fn new(company_id: impl Into<String>) -> Self {
85        Self {
86            company_id: company_id.into(),
87            ..Default::default()
88        }
89    }
90
91    /// The search term for the user is matched based on the user full name, email or phone
92    pub fn query(mut self, v: impl Into<String>) -> Self {
93        self.query = Some(v.into());
94        self
95    }
96
97    /// No of results to be skipped before returning the result
98    pub fn skip(mut self, v: impl Into<String>) -> Self {
99        self.skip = Some(v.into());
100        self
101    }
102
103    /// No of results to be limited before returning the result
104    pub fn limit(mut self, v: impl Into<String>) -> Self {
105        self.limit = Some(v.into());
106        self
107    }
108
109    /// Location ID in which the search needs to be performed
110    pub fn location_id(mut self, v: impl Into<String>) -> Self {
111        self.location_id = Some(v.into());
112        self
113    }
114
115    /// Type of the users to be filtered in the search
116    pub fn type_(mut self, v: impl Into<String>) -> Self {
117        self.type_ = Some(v.into());
118        self
119    }
120
121    /// Role of the users to be filtered in the search
122    pub fn role(mut self, v: impl Into<String>) -> Self {
123        self.role = Some(v.into());
124        self
125    }
126
127    /// List of User IDs to be filtered in the search
128    pub fn ids(mut self, v: impl Into<String>) -> Self {
129        self.ids = Some(v.into());
130        self
131    }
132
133    /// The field on which sort is applied in which the results need to be sorted. Default
134    /// is based on the first and last name
135    pub fn sort(mut self, v: impl Into<String>) -> Self {
136        self.sort = Some(v.into());
137        self
138    }
139
140    /// The direction in which the results need to be sorted
141    pub fn sort_direction(mut self, v: impl Into<String>) -> Self {
142        self.sort_direction = Some(v.into());
143        self
144    }
145
146    /// Set the `enabled2waySync` query parameter.
147    pub fn enabled2way_sync(mut self, v: bool) -> Self {
148        self.enabled2way_sync = Some(v);
149        self
150    }
151
152    fn to_query(&self) -> Vec<(String, String)> {
153        let mut q: Vec<(String, String)> = vec![("companyId".into(), self.company_id.clone())];
154        if let Some(v) = &self.query {
155            q.push(("query".into(), v.to_string()));
156        }
157        if let Some(v) = &self.skip {
158            q.push(("skip".into(), v.to_string()));
159        }
160        if let Some(v) = &self.limit {
161            q.push(("limit".into(), v.to_string()));
162        }
163        if let Some(v) = &self.location_id {
164            q.push(("locationId".into(), v.to_string()));
165        }
166        if let Some(v) = &self.type_ {
167            q.push(("type".into(), v.to_string()));
168        }
169        if let Some(v) = &self.role {
170            q.push(("role".into(), v.to_string()));
171        }
172        if let Some(v) = &self.ids {
173            q.push(("ids".into(), v.to_string()));
174        }
175        if let Some(v) = &self.sort {
176            q.push(("sort".into(), v.to_string()));
177        }
178        if let Some(v) = &self.sort_direction {
179            q.push(("sortDirection".into(), v.to_string()));
180        }
181        if let Some(v) = &self.enabled2way_sync {
182            q.push(("enabled2waySync".into(), v.to_string()));
183        }
184        q
185    }
186}
187
188impl UsersService {
189    /// Get User by Location
190    ///
191    /// Deprecated. Use `GET /users/search` instead. Pass `locationId` as a query parameter
192    /// to filter results by location, along with the required `companyId` and other search
193    /// filters as needed.
194    ///
195    /// `GET /users/`
196    ///
197    /// Requires scope: `users.readonly`.
198    pub async fn get_user_by_location(
199        &self,
200        params: &GetUserByLocationParams,
201    ) -> Result<models::LocationSuccessfulResponseDto> {
202        let query = params.to_query();
203        self.client
204            .send_versioned(
205                reqwest::Method::GET,
206                "/users/",
207                &query,
208                None::<&()>,
209                Some("2021-07-28"),
210            )
211            .await
212    }
213
214    /// Create User
215    ///
216    /// `POST /users/`
217    ///
218    /// Requires scope: `users.write`.
219    pub async fn create_user(
220        &self,
221        body: &models::CreateUserDto,
222    ) -> Result<models::UserSuccessfulResponseDto> {
223        let query = Vec::new();
224        self.client
225            .send_versioned(
226                reqwest::Method::POST,
227                "/users/",
228                &query,
229                Some(body),
230                Some("2021-07-28"),
231            )
232            .await
233    }
234
235    /// Search Users
236    ///
237    /// `GET /users/search`
238    ///
239    /// Requires scope: `users.readonly`.
240    pub async fn search_users(
241        &self,
242        params: &SearchUsersParams,
243    ) -> Result<models::SearchUserSuccessfulResponseDto> {
244        let query = params.to_query();
245        self.client
246            .send_versioned(
247                reqwest::Method::GET,
248                "/users/search",
249                &query,
250                None::<&()>,
251                Some("2021-07-28"),
252            )
253            .await
254    }
255
256    /// Filter Users by Email
257    ///
258    /// Filter users by company ID, deleted status, and email array
259    ///
260    /// `POST /users/search/filter-by-email`
261    ///
262    /// Requires scope: `users.readonly`.
263    pub async fn filter_users_by_email(
264        &self,
265        body: &models::FilterByEmailDto,
266    ) -> Result<models::SearchUserSuccessfulResponseDto> {
267        let query = Vec::new();
268        self.client
269            .send_versioned(
270                reqwest::Method::POST,
271                "/users/search/filter-by-email",
272                &query,
273                Some(body),
274                Some("2021-07-28"),
275            )
276            .await
277    }
278
279    /// Delete User
280    ///
281    /// `DELETE /users/{userId}`
282    ///
283    /// Requires scope: `users.write`.
284    pub async fn delete_user(
285        &self,
286        user_id: &str,
287    ) -> Result<models::DeleteUserSuccessfulResponseDto> {
288        let path = format!("/users/{}", crate::services::encode(user_id));
289        let query = Vec::new();
290        self.client
291            .send_versioned(
292                reqwest::Method::DELETE,
293                &path,
294                &query,
295                None::<&()>,
296                Some("2021-07-28"),
297            )
298            .await
299    }
300
301    /// Get User
302    ///
303    /// `GET /users/{userId}`
304    ///
305    /// Requires scope: `users.readonly`.
306    pub async fn get_user(&self, user_id: &str) -> Result<models::UserSuccessfulResponseDto> {
307        let path = format!("/users/{}", crate::services::encode(user_id));
308        let query = Vec::new();
309        self.client
310            .send_versioned(
311                reqwest::Method::GET,
312                &path,
313                &query,
314                None::<&()>,
315                Some("2021-07-28"),
316            )
317            .await
318    }
319
320    /// Update User
321    ///
322    /// `PUT /users/{userId}`
323    ///
324    /// Requires scope: `users.write`.
325    pub async fn update_user(
326        &self,
327        user_id: &str,
328        body: &models::UpdateUserDto,
329    ) -> Result<models::UserSuccessfulResponseDto> {
330        let path = format!("/users/{}", crate::services::encode(user_id));
331        let query = Vec::new();
332        self.client
333            .send_versioned(
334                reqwest::Method::PUT,
335                &path,
336                &query,
337                Some(body),
338                Some("2021-07-28"),
339            )
340            .await
341    }
342}