1#![allow(clippy::too_many_arguments)]
13
14use crate::client::Ghl;
15use crate::error::Result;
16use ghl_models::v2::emails as models;
17
18#[derive(Debug, Clone)]
21pub struct EmailsService {
22 pub(crate) client: Ghl,
23}
24
25impl EmailsService {
26 pub(crate) fn new(client: Ghl) -> Self {
27 Self { client }
28 }
29}
30
31#[derive(Debug, Clone, Default)]
33pub struct FetchEmailTemplatesParams {
34 pub location_id: String,
37 pub limit: Option<String>,
39 pub offset: Option<String>,
41 pub search: Option<String>,
43 pub sort_by_date: Option<String>,
45 pub archived: Option<String>,
47 pub builder_version: Option<String>,
50 pub name: Option<String>,
52 pub parent_id: Option<String>,
54 pub origin_id: Option<String>,
56 pub templates_only: Option<String>,
58}
59
60impl FetchEmailTemplatesParams {
61 pub fn new(location_id: impl Into<String>) -> Self {
63 Self {
64 location_id: location_id.into(),
65 ..Default::default()
66 }
67 }
68
69 pub fn limit(mut self, v: impl Into<String>) -> Self {
71 self.limit = Some(v.into());
72 self
73 }
74
75 pub fn offset(mut self, v: impl Into<String>) -> Self {
77 self.offset = Some(v.into());
78 self
79 }
80
81 pub fn search(mut self, v: impl Into<String>) -> Self {
83 self.search = Some(v.into());
84 self
85 }
86
87 pub fn sort_by_date(mut self, v: impl Into<String>) -> Self {
89 self.sort_by_date = Some(v.into());
90 self
91 }
92
93 pub fn archived(mut self, v: impl Into<String>) -> Self {
95 self.archived = Some(v.into());
96 self
97 }
98
99 pub fn builder_version(mut self, v: impl Into<String>) -> Self {
101 self.builder_version = Some(v.into());
102 self
103 }
104
105 pub fn name(mut self, v: impl Into<String>) -> Self {
107 self.name = Some(v.into());
108 self
109 }
110
111 pub fn parent_id(mut self, v: impl Into<String>) -> Self {
113 self.parent_id = Some(v.into());
114 self
115 }
116
117 pub fn origin_id(mut self, v: impl Into<String>) -> Self {
119 self.origin_id = Some(v.into());
120 self
121 }
122
123 pub fn templates_only(mut self, v: impl Into<String>) -> Self {
125 self.templates_only = Some(v.into());
126 self
127 }
128
129 fn to_query(&self) -> Vec<(String, String)> {
130 let mut q: Vec<(String, String)> = vec![("locationId".into(), self.location_id.clone())];
131 if let Some(v) = &self.limit {
132 q.push(("limit".into(), v.to_string()));
133 }
134 if let Some(v) = &self.offset {
135 q.push(("offset".into(), v.to_string()));
136 }
137 if let Some(v) = &self.search {
138 q.push(("search".into(), v.to_string()));
139 }
140 if let Some(v) = &self.sort_by_date {
141 q.push(("sortByDate".into(), v.to_string()));
142 }
143 if let Some(v) = &self.archived {
144 q.push(("archived".into(), v.to_string()));
145 }
146 if let Some(v) = &self.builder_version {
147 q.push(("builderVersion".into(), v.to_string()));
148 }
149 if let Some(v) = &self.name {
150 q.push(("name".into(), v.to_string()));
151 }
152 if let Some(v) = &self.parent_id {
153 q.push(("parentId".into(), v.to_string()));
154 }
155 if let Some(v) = &self.origin_id {
156 q.push(("originId".into(), v.to_string()));
157 }
158 if let Some(v) = &self.templates_only {
159 q.push(("templatesOnly".into(), v.to_string()));
160 }
161 q
162 }
163}
164
165#[derive(Debug, Clone, Default)]
167pub struct GetCampaignsParams {
168 pub location_id: String,
171 pub limit: Option<f64>,
173 pub offset: Option<f64>,
175 pub status: Option<String>,
179 pub email_status: Option<String>,
183 pub name: Option<String>,
185 pub parent_id: Option<String>,
187 pub limited_fields: Option<bool>,
192 pub archived: Option<bool>,
194 pub campaigns_only: Option<bool>,
196 pub show_stats: Option<bool>,
200}
201
202impl GetCampaignsParams {
203 pub fn new(location_id: impl Into<String>) -> Self {
205 Self {
206 location_id: location_id.into(),
207 ..Default::default()
208 }
209 }
210
211 pub fn limit(mut self, v: f64) -> Self {
213 self.limit = Some(v);
214 self
215 }
216
217 pub fn offset(mut self, v: f64) -> Self {
219 self.offset = Some(v);
220 self
221 }
222
223 pub fn status(mut self, v: impl Into<String>) -> Self {
225 self.status = Some(v.into());
226 self
227 }
228
229 pub fn email_status(mut self, v: impl Into<String>) -> Self {
231 self.email_status = Some(v.into());
232 self
233 }
234
235 pub fn name(mut self, v: impl Into<String>) -> Self {
237 self.name = Some(v.into());
238 self
239 }
240
241 pub fn parent_id(mut self, v: impl Into<String>) -> Self {
243 self.parent_id = Some(v.into());
244 self
245 }
246
247 pub fn limited_fields(mut self, v: bool) -> Self {
252 self.limited_fields = Some(v);
253 self
254 }
255
256 pub fn archived(mut self, v: bool) -> Self {
258 self.archived = Some(v);
259 self
260 }
261
262 pub fn campaigns_only(mut self, v: bool) -> Self {
264 self.campaigns_only = Some(v);
265 self
266 }
267
268 pub fn show_stats(mut self, v: bool) -> Self {
272 self.show_stats = Some(v);
273 self
274 }
275
276 fn to_query(&self) -> Vec<(String, String)> {
277 let mut q: Vec<(String, String)> = vec![("locationId".into(), self.location_id.clone())];
278 if let Some(v) = &self.limit {
279 q.push(("limit".into(), v.to_string()));
280 }
281 if let Some(v) = &self.offset {
282 q.push(("offset".into(), v.to_string()));
283 }
284 if let Some(v) = &self.status {
285 q.push(("status".into(), v.to_string()));
286 }
287 if let Some(v) = &self.email_status {
288 q.push(("emailStatus".into(), v.to_string()));
289 }
290 if let Some(v) = &self.name {
291 q.push(("name".into(), v.to_string()));
292 }
293 if let Some(v) = &self.parent_id {
294 q.push(("parentId".into(), v.to_string()));
295 }
296 if let Some(v) = &self.limited_fields {
297 q.push(("limitedFields".into(), v.to_string()));
298 }
299 if let Some(v) = &self.archived {
300 q.push(("archived".into(), v.to_string()));
301 }
302 if let Some(v) = &self.campaigns_only {
303 q.push(("campaignsOnly".into(), v.to_string()));
304 }
305 if let Some(v) = &self.show_stats {
306 q.push(("showStats".into(), v.to_string()));
307 }
308 q
309 }
310}
311
312impl EmailsService {
313 pub async fn fetch_email_templates(
321 &self,
322 params: &FetchEmailTemplatesParams,
323 ) -> Result<models::FetchBuilderSuccesfulResponseDto> {
324 let query = params.to_query();
325 self.client
326 .send_versioned(
327 reqwest::Method::GET,
328 "/emails/builder",
329 &query,
330 None::<&()>,
331 Some("2021-07-28"),
332 )
333 .await
334 }
335
336 pub async fn create_a_new_template(
342 &self,
343 body: &models::CreateBuilderDto,
344 ) -> Result<models::CreateBuilderSuccesfulResponseDto> {
345 let query = Vec::new();
346 self.client
347 .send_versioned(
348 reqwest::Method::POST,
349 "/emails/builder",
350 &query,
351 Some(body),
352 Some("2021-07-28"),
353 )
354 .await
355 }
356
357 pub async fn update_a_template(
363 &self,
364 body: &models::SaveBuilderDataDto,
365 ) -> Result<models::BuilderUpdateSuccessfulDTO> {
366 let query = Vec::new();
367 self.client
368 .send_versioned(
369 reqwest::Method::POST,
370 "/emails/builder/data",
371 &query,
372 Some(body),
373 Some("2021-07-28"),
374 )
375 .await
376 }
377
378 pub async fn delete_a_template(
382 &self,
383 location_id: &str,
384 template_id: &str,
385 ) -> Result<models::DeleteBuilderSuccesfulResponseDto> {
386 let path = format!(
387 "/emails/builder/{}/{}",
388 crate::services::encode(location_id),
389 crate::services::encode(template_id)
390 );
391 let query = Vec::new();
392 self.client
393 .send_versioned(
394 reqwest::Method::DELETE,
395 &path,
396 &query,
397 None::<&()>,
398 Some("2021-07-28"),
399 )
400 .await
401 }
402
403 pub async fn get_campaigns(
409 &self,
410 params: &GetCampaignsParams,
411 ) -> Result<models::ScheduleFetchSuccessfulDTO> {
412 let query = params.to_query();
413 self.client
414 .send_versioned(
415 reqwest::Method::GET,
416 "/emails/schedule",
417 &query,
418 None::<&()>,
419 Some("2021-07-28"),
420 )
421 .await
422 }
423}