1#![allow(clippy::too_many_arguments)]
13
14use crate::client::Ghl;
15use crate::error::Result;
16use ghl_models::v2::users as models;
17
18#[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#[derive(Debug, Clone, Default)]
33pub struct GetUserByLocationParams {
34 pub location_id: String,
37}
38
39impl GetUserByLocationParams {
40 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#[derive(Debug, Clone, Default)]
55pub struct SearchUsersParams {
56 pub company_id: String,
59 pub query: Option<String>,
61 pub skip: Option<String>,
63 pub limit: Option<String>,
65 pub location_id: Option<String>,
67 pub type_: Option<String>,
69 pub role: Option<String>,
71 pub ids: Option<String>,
73 pub sort: Option<String>,
76 pub sort_direction: Option<String>,
78 pub enabled2way_sync: Option<bool>,
80}
81
82impl SearchUsersParams {
83 pub fn new(company_id: impl Into<String>) -> Self {
85 Self {
86 company_id: company_id.into(),
87 ..Default::default()
88 }
89 }
90
91 pub fn query(mut self, v: impl Into<String>) -> Self {
93 self.query = Some(v.into());
94 self
95 }
96
97 pub fn skip(mut self, v: impl Into<String>) -> Self {
99 self.skip = Some(v.into());
100 self
101 }
102
103 pub fn limit(mut self, v: impl Into<String>) -> Self {
105 self.limit = Some(v.into());
106 self
107 }
108
109 pub fn location_id(mut self, v: impl Into<String>) -> Self {
111 self.location_id = Some(v.into());
112 self
113 }
114
115 pub fn type_(mut self, v: impl Into<String>) -> Self {
117 self.type_ = Some(v.into());
118 self
119 }
120
121 pub fn role(mut self, v: impl Into<String>) -> Self {
123 self.role = Some(v.into());
124 self
125 }
126
127 pub fn ids(mut self, v: impl Into<String>) -> Self {
129 self.ids = Some(v.into());
130 self
131 }
132
133 pub fn sort(mut self, v: impl Into<String>) -> Self {
136 self.sort = Some(v.into());
137 self
138 }
139
140 pub fn sort_direction(mut self, v: impl Into<String>) -> Self {
142 self.sort_direction = Some(v.into());
143 self
144 }
145
146 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 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 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 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 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 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 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 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}