use std::collections::BTreeMap;
use std::sync::Arc;
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use fakecloud_core::multi_account::{AccountState, MultiAccountState};
pub const SUPPORT_SNAPSHOT_SCHEMA_VERSION: u32 = 1;
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SupportData {
#[serde(default)]
pub account_id: String,
#[serde(default)]
pub region: String,
#[serde(default)]
pub cases: BTreeMap<String, Value>,
#[serde(default)]
pub communications: BTreeMap<String, Vec<Value>>,
#[serde(default)]
pub attachment_sets: BTreeMap<String, Value>,
#[serde(default)]
pub attachments: BTreeMap<String, Value>,
#[serde(default)]
pub ta_refresh: BTreeMap<String, String>,
}
impl AccountState for SupportData {
fn new_for_account(account_id: &str, region: &str, _endpoint: &str) -> Self {
Self {
account_id: account_id.to_string(),
region: region.to_string(),
..Default::default()
}
}
}
impl SupportData {
pub fn reconcile(&mut self) -> bool {
false
}
pub fn advance_refresh(&mut self, check_id: &str) -> String {
let cur = self
.ta_refresh
.get(check_id)
.cloned()
.unwrap_or_else(|| "none".to_string());
let next = match cur.as_str() {
"enqueued" => "processing",
"processing" => "success",
other => other,
};
self.ta_refresh
.insert(check_id.to_string(), next.to_string());
next.to_string()
}
}
pub type SharedSupportState = Arc<RwLock<MultiAccountState<SupportData>>>;
#[derive(Debug, Serialize, Deserialize)]
pub struct SupportSnapshot {
pub schema_version: u32,
pub accounts: MultiAccountState<SupportData>,
}
#[cfg(test)]
mod tests {
use super::*;
fn data() -> SupportData {
SupportData::new_for_account("000000000000", "us-east-1", "")
}
#[test]
fn refresh_advances_through_states() {
let mut d = data();
assert_eq!(d.advance_refresh("Qch7DwouX1"), "none");
d.ta_refresh.insert("Qch7DwouX1".into(), "enqueued".into());
assert_eq!(d.advance_refresh("Qch7DwouX1"), "processing");
assert_eq!(d.advance_refresh("Qch7DwouX1"), "success");
assert_eq!(d.advance_refresh("Qch7DwouX1"), "success");
}
#[test]
fn reconcile_is_noop() {
let mut d = data();
assert!(!d.reconcile());
}
}