use crate::error::Error;
use crate::objects::currency_amount::CurrencyAmount;
use crate::objects::entity::Entity;
use crate::objects::incoming_payment_attempt_status::IncomingPaymentAttemptStatus;
use crate::objects::incoming_payment_to_attempts_connection::IncomingPaymentToAttemptsConnection;
use crate::objects::lightning_transaction::LightningTransaction;
use crate::objects::post_transaction_data::PostTransactionData;
use crate::objects::transaction::Transaction;
use crate::objects::transaction_status::TransactionStatus;
use crate::types::custom_date_formats::custom_date_format;
use crate::types::custom_date_formats::custom_date_format_option;
use crate::types::entity_wrapper::EntityWrapper;
use crate::types::get_entity::GetEntity;
use crate::types::graphql_requester::GraphQLRequester;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::vec::Vec;
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct IncomingPayment {
#[serde(rename = "incoming_payment_id")]
pub id: String,
#[serde(with = "custom_date_format", rename = "incoming_payment_created_at")]
pub created_at: DateTime<Utc>,
#[serde(with = "custom_date_format", rename = "incoming_payment_updated_at")]
pub updated_at: DateTime<Utc>,
#[serde(rename = "incoming_payment_status")]
pub status: TransactionStatus,
#[serde(
with = "custom_date_format_option",
rename = "incoming_payment_resolved_at"
)]
pub resolved_at: Option<DateTime<Utc>>,
#[serde(rename = "incoming_payment_amount")]
pub amount: CurrencyAmount,
#[serde(rename = "incoming_payment_transaction_hash")]
pub transaction_hash: Option<String>,
#[serde(rename = "incoming_payment_is_uma")]
pub is_uma: bool,
#[serde(rename = "incoming_payment_destination")]
pub destination: EntityWrapper,
#[serde(rename = "incoming_payment_payment_request")]
pub payment_request: Option<EntityWrapper>,
#[serde(rename = "incoming_payment_uma_post_transaction_data")]
pub uma_post_transaction_data: Option<Vec<PostTransactionData>>,
#[serde(rename = "incoming_payment_is_internal_payment")]
pub is_internal_payment: bool,
#[serde(rename = "__typename")]
pub typename: String,
}
impl LightningTransaction for IncomingPayment {
fn type_name(&self) -> &'static str {
"IncomingPayment"
}
}
impl Transaction for IncomingPayment {
fn get_status(&self) -> TransactionStatus {
self.status.clone()
}
fn get_resolved_at(&self) -> Option<DateTime<Utc>> {
self.resolved_at
}
fn get_amount(&self) -> CurrencyAmount {
self.amount.clone()
}
fn get_transaction_hash(&self) -> Option<String> {
self.transaction_hash.clone()
}
fn type_name(&self) -> &'static str {
"IncomingPayment"
}
}
impl Entity for IncomingPayment {
fn get_id(&self) -> String {
self.id.clone()
}
fn get_created_at(&self) -> DateTime<Utc> {
self.created_at
}
fn get_updated_at(&self) -> DateTime<Utc> {
self.updated_at
}
fn type_name(&self) -> &'static str {
"IncomingPayment"
}
}
impl GetEntity for IncomingPayment {
fn get_entity_query() -> String {
format!(
"
query GetEntity($id: ID!) {{
entity(id: $id) {{
... on IncomingPayment {{
... IncomingPaymentFragment
}}
}}
}}
{}",
FRAGMENT
)
}
}
pub const FRAGMENT: &str = "
fragment IncomingPaymentFragment on IncomingPayment {
__typename
incoming_payment_id: id
incoming_payment_created_at: created_at
incoming_payment_updated_at: updated_at
incoming_payment_status: status
incoming_payment_resolved_at: resolved_at
incoming_payment_amount: amount {
__typename
currency_amount_original_value: original_value
currency_amount_original_unit: original_unit
currency_amount_preferred_currency_unit: preferred_currency_unit
currency_amount_preferred_currency_value_rounded: preferred_currency_value_rounded
currency_amount_preferred_currency_value_approx: preferred_currency_value_approx
}
incoming_payment_transaction_hash: transaction_hash
incoming_payment_is_uma: is_uma
incoming_payment_destination: destination {
id
}
incoming_payment_payment_request: payment_request {
id
}
incoming_payment_uma_post_transaction_data: uma_post_transaction_data {
__typename
post_transaction_data_utxo: utxo
post_transaction_data_amount: amount {
__typename
currency_amount_original_value: original_value
currency_amount_original_unit: original_unit
currency_amount_preferred_currency_unit: preferred_currency_unit
currency_amount_preferred_currency_value_rounded: preferred_currency_value_rounded
currency_amount_preferred_currency_value_approx: preferred_currency_value_approx
}
}
incoming_payment_is_internal_payment: is_internal_payment
}
";
impl IncomingPayment {
pub async fn get_attempts(
&self,
requester: &impl GraphQLRequester,
first: Option<i64>,
statuses: Option<Vec<IncomingPaymentAttemptStatus>>,
after: Option<String>,
) -> Result<IncomingPaymentToAttemptsConnection, Error> {
let query = "query FetchIncomingPaymentToAttemptsConnection($entity_id: ID!, $first: Int, $statuses: [IncomingPaymentAttemptStatus!], $after: String) {
entity(id: $entity_id) {
... on IncomingPayment {
attempts(, first: $first, statuses: $statuses, after: $after) {
__typename
incoming_payment_to_attempts_connection_count: count
incoming_payment_to_attempts_connection_page_info: page_info {
__typename
page_info_has_next_page: has_next_page
page_info_has_previous_page: has_previous_page
page_info_start_cursor: start_cursor
page_info_end_cursor: end_cursor
}
incoming_payment_to_attempts_connection_entities: entities {
__typename
incoming_payment_attempt_id: id
incoming_payment_attempt_created_at: created_at
incoming_payment_attempt_updated_at: updated_at
incoming_payment_attempt_status: status
incoming_payment_attempt_resolved_at: resolved_at
incoming_payment_attempt_amount: amount {
__typename
currency_amount_original_value: original_value
currency_amount_original_unit: original_unit
currency_amount_preferred_currency_unit: preferred_currency_unit
currency_amount_preferred_currency_value_rounded: preferred_currency_value_rounded
currency_amount_preferred_currency_value_approx: preferred_currency_value_approx
}
incoming_payment_attempt_channel: channel {
id
}
}
}
}
}
}";
let mut variables: HashMap<&str, Value> = HashMap::new();
variables.insert("entity_id", self.id.clone().into());
variables.insert("first", first.into());
variables.insert("statuses", statuses.into());
variables.insert("after", after.into());
let value = serde_json::to_value(variables).map_err(Error::ConversionError)?;
let result = requester.execute_graphql(query, Some(value)).await?;
let json = result["entity"]["attempts"].clone();
let result = serde_json::from_value(json).map_err(Error::JsonError)?;
Ok(result)
}
}