use super::api::{MailchimpApi, MailchimpApiUpdate};
use super::internal::request::MailchimpResult;
use super::iter::{BuildIter, MalchimpIter, ResourceFilter};
use super::types::{
AutomationCampaignSettingsType, AutomationModifier, AutomationTriggerType,
AutomationWorkflowType, CollectionAutomation, RecipientType,
};
use log::error;
use std::collections::HashMap;
use std::rc::Rc;
#[derive(Debug, Clone)]
pub struct AutomationsFilter {
pub fields: Option<String>,
pub exclude_fields: Option<String>,
pub count: Option<u64>,
pub offset: Option<u64>,
pub status: Option<String>,
pub before_send_time: Option<String>,
pub since_send_time: Option<String>,
pub before_create_time: Option<String>,
pub since_create_time: Option<String>,
}
impl Default for AutomationsFilter {
fn default() -> Self {
AutomationsFilter {
fields: None,
exclude_fields: None,
count: Some(50),
offset: Some(0),
status: None,
before_send_time: None,
since_send_time: None,
before_create_time: None,
since_create_time: None,
}
}
}
impl ResourceFilter for AutomationsFilter {
fn build_payload(&self) -> HashMap<String, String> {
let mut payload = HashMap::new();
if self.fields.is_some() {
payload.insert("fields".to_string(), self.fields.as_ref().unwrap().clone());
}
if self.exclude_fields.is_some() {
payload.insert(
"exclude_fields".to_string(),
self.exclude_fields.as_ref().unwrap().clone(),
);
}
if self.count.is_some() {
payload.insert(
"count".to_string(),
format!("{:}", self.count.as_ref().unwrap().clone()),
);
}
if self.offset.is_some() {
payload.insert(
"offset".to_string(),
format!("{:}", self.offset.as_ref().unwrap().clone()),
);
}
if self.status.is_some() {
payload.insert("status".to_string(), self.status.as_ref().unwrap().clone());
}
if self.before_send_time.is_some() {
payload.insert(
"before_send_time".to_string(),
self.before_send_time.as_ref().unwrap().clone(),
);
}
if self.since_send_time.is_some() {
payload.insert(
"since_send_time".to_string(),
self.since_send_time.as_ref().unwrap().clone(),
);
}
if self.before_create_time.is_some() {
payload.insert(
"before_create_time".to_string(),
self.before_create_time.as_ref().unwrap().clone(),
);
}
if self.since_create_time.is_some() {
payload.insert(
"since_create_time".to_string(),
self.since_create_time.as_ref().unwrap().clone(),
);
}
payload
}
}
#[derive(Debug, Clone)]
pub struct Automations {
api: Rc<MailchimpApi>,
}
#[derive(Debug)]
pub struct AutomationsBuilder {}
impl BuildIter for AutomationsBuilder {
type Item = AutomationWorkflowType;
type FilterItem = AutomationsFilter;
type Collection = CollectionAutomation;
fn update_item(&self, data: &Self::Item, api: Rc<MailchimpApi>) -> Self::Item {
let mut in_data = data.clone();
in_data.set_api(api);
in_data
}
fn update_filter_offset(&self, filter: &Self::FilterItem) -> Self::FilterItem {
let mut f = filter.clone();
f.offset = Some(f.count.unwrap() + f.offset.unwrap());
f
}
}
impl Automations {
pub fn new(api: MailchimpApi) -> Self {
Automations { api: Rc::new(api) }
}
pub fn get_automations_from_remote(
&self,
filters: Option<&AutomationsFilter>,
) -> Option<CollectionAutomation> {
let mut payload = HashMap::new();
if filters.is_some() {
payload = filters.as_ref().unwrap().build_payload();
}
let response = self.api.get::<CollectionAutomation>("automations", payload);
match response {
Ok(value) => Some(value),
Err(e) => {
error!( target: "mailchimp", "Load Campaigns from remote: Response Error details: {:?}", e);
None
}
}
}
pub fn get_automation_workflow_info<'a>(
&self,
workflow_id: &'a str,
filters: HashMap<String, String>,
) -> MailchimpResult<AutomationWorkflowType> {
let endpoint = String::from("automations/") + workflow_id;
let response = self
.api
.get::<AutomationWorkflowType>(endpoint.as_str(), filters);
match response {
Ok(automation) => {
let mut au = automation;
au.set_api(self.api.clone());
Ok(au)
}
Err(e) => Err(e),
}
}
pub fn create_automation<'a>(
&self,
recipients: RecipientType,
trigger_settings: AutomationTriggerType,
settings: Option<AutomationCampaignSettingsType>,
) -> MailchimpResult<AutomationWorkflowType> {
let modifier = AutomationModifier {
settings: settings,
delay: None,
recipients: Some(recipients),
trigger_settings: Some(trigger_settings),
};
let response = self
.api
.post::<AutomationWorkflowType, AutomationModifier>("automations", modifier);
match response {
Ok(automation) => {
let mut au = automation;
au.set_api(self.api.clone());
Ok(au)
}
Err(e) => Err(e),
}
}
pub fn iter(&self, filters: AutomationsFilter) -> MalchimpIter<AutomationsBuilder> {
if let Some(remote) = self.get_automations_from_remote(Some(&filters)) {
return MalchimpIter {
builder: AutomationsBuilder {},
data: remote.automations,
cur_filters: filters.clone(),
cur_it: 0,
total_items: remote.total_items,
api: self.api.clone(),
endpoint: "automations".to_string(),
};
}
MalchimpIter {
builder: AutomationsBuilder {},
data: Vec::new(),
cur_filters: filters.clone(),
cur_it: 0,
total_items: 0,
api: self.api.clone(),
endpoint: "automations".to_string(),
}
}
}