use jiff::civil::Date;
use jiff::{Span, Timestamp};
use crate::error::{Error, Result};
use crate::model::DataBoxId;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CreditQuery {
id: DataBoxId,
from: Option<Date>,
to: Option<Date>,
}
impl CreditQuery {
pub fn current(id: DataBoxId) -> Self {
Self {
id,
from: None,
to: None,
}
}
pub fn history(id: DataBoxId, from: Date, to: Date) -> Result<Self> {
if to < from {
return Err(Error::invalid_input(
"credit_history",
"the end date must not precede the start date",
));
}
let maximum = from
.checked_add(Span::new().years(1))
.map_err(|error| Error::invalid_input("credit_history", error.to_string()))?;
if to > maximum {
return Err(Error::invalid_input(
"credit_history",
"the history interval must not exceed 12 months",
));
}
Ok(Self {
id,
from: Some(from),
to: Some(to),
})
}
pub fn id(&self) -> &DataBoxId {
&self.id
}
pub fn from(&self) -> Option<Date> {
self.from
}
pub fn to(&self) -> Option<Date> {
self.to
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct CreditInfo {
pub current_credit: Option<i64>,
pub notification_email: Option<String>,
pub events: Vec<CreditEvent>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct CreditEvent {
pub time: Timestamp,
pub event_type: i64,
pub credit_change: i64,
pub credit_after: i64,
pub details: CreditEventDetails,
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum CreditEventDetails {
Transaction {
transaction_id: String,
},
PostalMessage {
recipient_id: DataBoxId,
postal_message_id: String,
},
DataVault(DataVaultChange),
Administrative {
done_by: String,
},
Unknown,
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct DataVaultChange {
pub new_capacity: i64,
pub new_from: Date,
pub new_to: Date,
pub old_capacity: Option<i64>,
pub old_from: Option<Date>,
pub old_to: Option<Date>,
pub done_by: Option<String>,
}