Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
darioAnongba committed Jul 2, 2024
1 parent f81cea2 commit 01d1d1a
Show file tree
Hide file tree
Showing 39 changed files with 1,403 additions and 254 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ lint:
@cargo clippy

lint-fix:
@cargo fix
@cargo fmt
6 changes: 3 additions & 3 deletions src/application/entities/ordering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ pub enum OrderDirection {
Asc,
}

impl Into<Order> for OrderDirection {
fn into(self) -> Order {
match self {
impl From<OrderDirection> for Order {
fn from(val: OrderDirection) -> Self {
match val {
OrderDirection::Asc => Order::Asc,
OrderDirection::Desc => Order::Desc,
}
Expand Down
6 changes: 3 additions & 3 deletions src/application/entities/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use strum_macros::{Display, EnumString};
#[derive(Clone, Debug, EnumString, Deserialize, Serialize, Display, PartialEq, Eq, Default)]
pub enum Ledger {
#[default]
LIGHTNING,
INTERNAL,
ONCHAIN,
Lightning,
Internal,
Onchain,
}

#[derive(Clone, Debug, EnumString, Deserialize, Serialize, Display, PartialEq, Eq, Default)]
Expand Down
2 changes: 1 addition & 1 deletion src/application/errors/authentication_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use thiserror::Error;
#[derive(Debug, Error)]
pub enum AuthenticationError {
#[error("Failed to fetch JWKS: {0}")]
JWKS(String),
Jwks(String),

#[error("Failed to decode JWT header: {0}")]
DecodeJWTHeader(String),
Expand Down
6 changes: 3 additions & 3 deletions src/domains/invoices/adapters/invoice_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ impl ActiveModelBehavior for ActiveModel {}
impl From<Model> for Invoice {
fn from(model: Model) -> Self {
let status = match model.payment_time {
Some(_) => InvoiceStatus::SETTLED,
Some(_) => InvoiceStatus::Settled,
None => match model.expires_at {
Some(expires_at) if Utc::now() > expires_at => InvoiceStatus::EXPIRED,
_ => InvoiceStatus::PENDING,
Some(expires_at) if Utc::now() > expires_at => InvoiceStatus::Expired,
_ => InvoiceStatus::Pending,
},
};

Expand Down
14 changes: 7 additions & 7 deletions src/domains/invoices/adapters/invoice_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ impl InvoiceRepository for SeaOrmInvoiceRepository {
.apply_if(filter.user_id, |q, user| q.filter(Column::UserId.eq(user)))
.apply_if(filter.ids, |q, ids| q.filter(Column::Id.is_in(ids)))
.apply_if(filter.status, |q, s| match s {
InvoiceStatus::PENDING => q.filter(
InvoiceStatus::Pending => q.filter(
Condition::all()
.add(Expr::col(Column::ExpiresAt).gt(Expr::current_timestamp()))
.add(Expr::col(Column::PaymentTime).is_null()),
),
InvoiceStatus::SETTLED => q.filter(Expr::col(Column::PaymentTime).is_not_null()),
InvoiceStatus::EXPIRED => q.filter(
InvoiceStatus::Settled => q.filter(Expr::col(Column::PaymentTime).is_not_null()),
InvoiceStatus::Expired => q.filter(
Condition::all()
.add(Expr::col(Column::ExpiresAt).lte(Expr::current_timestamp()))
.add(Expr::col(Column::PaymentTime).is_null()),
Expand Down Expand Up @@ -99,7 +99,7 @@ impl InvoiceRepository for SeaOrmInvoiceRepository {
..Default::default()
};

if invoice.ledger == Ledger::LIGHTNING {
if invoice.ledger == Ledger::Lightning {
let lightning = invoice.lightning.unwrap();
model.bolt11 = Set(lightning.bolt11.into());
model.payee_pubkey = Set(lightning.payee_pubkey.into());
Expand Down Expand Up @@ -150,13 +150,13 @@ impl InvoiceRepository for SeaOrmInvoiceRepository {
.apply_if(filter.user_id, |q, user| q.filter(Column::UserId.eq(user)))
.apply_if(filter.ids, |q, ids| q.filter(Column::Id.is_in(ids)))
.apply_if(filter.status, |q, status| match status {
InvoiceStatus::PENDING => q.filter(
InvoiceStatus::Pending => q.filter(
Condition::all()
.add(Expr::col(Column::ExpiresAt).gt(Expr::current_timestamp()))
.add(Expr::col(Column::PaymentTime).is_null()),
),
InvoiceStatus::SETTLED => q.filter(Expr::col(Column::PaymentTime).is_not_null()),
InvoiceStatus::EXPIRED => q.filter(
InvoiceStatus::Settled => q.filter(Expr::col(Column::PaymentTime).is_not_null()),
InvoiceStatus::Expired => q.filter(
Condition::all()
.add(Expr::col(Column::ExpiresAt).lte(Expr::current_timestamp()))
.add(Expr::col(Column::PaymentTime).is_null()),
Expand Down
8 changes: 4 additions & 4 deletions src/domains/invoices/api/invoice_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl InvoiceHandler {
) -> Result<Json<Invoice>, ApplicationError> {
user.check_permission(Permission::WriteLnTransaction)?;

let ln_address = app_state
let invoice = app_state
.services
.invoice
.invoice(
Expand All @@ -46,7 +46,7 @@ impl InvoiceHandler {
payload.expiry,
)
.await?;
Ok(Json(ln_address.into()))
Ok(invoice.into())
}

async fn get(
Expand All @@ -56,8 +56,8 @@ impl InvoiceHandler {
) -> Result<Json<Invoice>, ApplicationError> {
user.check_permission(Permission::ReadLnTransaction)?;

let ln_address = app_state.services.invoice.get(id).await?;
Ok(Json(ln_address.into()))
let invoice = app_state.services.invoice.get(id).await?;
Ok(invoice.into())
}

async fn list(
Expand Down
6 changes: 3 additions & 3 deletions src/domains/invoices/entities/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ pub struct LnInvoice {
#[derive(Clone, Debug, EnumString, Deserialize, Serialize, Display, PartialEq, Eq, Default)]
pub enum InvoiceStatus {
#[default]
PENDING,
SETTLED,
EXPIRED,
Pending,
Settled,
Expired,
}

#[derive(Clone, Debug, Deserialize, Serialize, Default)]
Expand Down
2 changes: 1 addition & 1 deletion src/domains/invoices/services/invoice_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl InvoiceUseCases for InvoiceService {
expiry.unwrap_or(self.invoice_expiry),
)
.await?;
invoice.user_id = user_id.clone();
invoice.user_id.clone_from(&user_id);

let invoice = self.store.invoice.insert(None, invoice).await?;

Expand Down
4 changes: 2 additions & 2 deletions src/domains/lightning/api/breez_node_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl BreezNodeHandler {
let client = app_state.ln_node_client.as_breez_client()?;
let data = client.backup()?;

return match data {
match data {
Some(data) => {
let filename = "channels_backup.txt";
let body = Body::from(data.join("\n").into_bytes());
Expand All @@ -202,7 +202,7 @@ impl BreezNodeHandler {
Ok((headers, body).into_response())
}
None => Err(LightningError::Backup("No backup data found".to_string()))?,
};
}
}

async fn health_check(
Expand Down
4 changes: 2 additions & 2 deletions src/domains/lightning/api/ln_address_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl LnAddressHandler {
.lnurl
.register(payload.user_id.unwrap_or(user.sub), payload.username)
.await?;
Ok(Json(ln_address.into()))
Ok(ln_address.into())
}

async fn get(
Expand All @@ -52,7 +52,7 @@ impl LnAddressHandler {
user.check_permission(Permission::ReadLnAddress)?;

let ln_address = app_state.services.lnurl.get(id).await?;
Ok(Json(ln_address.into()))
Ok(ln_address.into())
}

async fn list(
Expand Down
4 changes: 2 additions & 2 deletions src/domains/lightning/api/lnurlp_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ impl LnURLpHandler {
Query(query_params): Query<LNUrlpInvoiceQueryParams>,
State(app_state): State<Arc<AppState>>,
) -> Result<Json<LnUrlCallbackResponse>, ApplicationError> {
let invoice = app_state
let callback = app_state
.services
.lnurl
.lnurlp_callback(username, query_params.amount, query_params.comment)
.await?;
Ok(Json(invoice.into()))
Ok(callback.into())
}
}
12 changes: 6 additions & 6 deletions src/domains/lightning/services/ln_events_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ impl LnEventsUseCases for LnEventsService {
.store
.invoice
.find_many(InvoiceFilter {
status: Some(InvoiceStatus::SETTLED),
ledger: Some(Ledger::LIGHTNING),
status: Some(InvoiceStatus::Settled),
ledger: Some(Ledger::Lightning),
pagination: PaginationFilter {
limit: Some(1),
..Default::default()
Expand Down Expand Up @@ -87,15 +87,15 @@ impl LnEventsUseCases for LnEventsService {
.await?;

if let Some(mut payment_retrieved) = payment_option {
if payment_retrieved.status == PaymentStatus::SETTLED {
if payment_retrieved.status == PaymentStatus::Settled {
debug!(
id = %payment_retrieved.id,
"Lightning payment already settled"
);
return Ok(());
}

payment_retrieved.status = PaymentStatus::SETTLED;
payment_retrieved.status = PaymentStatus::Settled;
payment_retrieved.payment_time = Some(event.payment_time);
payment_retrieved.payment_preimage = Some(event.payment_preimage);
payment_retrieved.amount_msat = event.amount_msat;
Expand Down Expand Up @@ -124,15 +124,15 @@ impl LnEventsUseCases for LnEventsService {
.await?;

if let Some(mut payment_retrieved) = payment_option {
if payment_retrieved.status == PaymentStatus::FAILED {
if payment_retrieved.status == PaymentStatus::Failed {
debug!(
id = %payment_retrieved.id,
"Lightning payment already failed"
);
return Ok(());
}

payment_retrieved.status = PaymentStatus::FAILED;
payment_retrieved.status = PaymentStatus::Failed;
payment_retrieved.error = Some(event.reason);

let payment = self.store.payment.update(payment_retrieved).await?;
Expand Down
2 changes: 1 addition & 1 deletion src/domains/lightning/services/lnurl_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl LnUrlUseCases for LnUrlService {
self.invoice_expiry,
)
.await?;
invoice.user_id = ln_address.user_id.clone();
invoice.user_id.clone_from(&ln_address.user_id);
invoice.ln_address = Some(ln_address.id);

// TODO: Get or add more information to make this a LNURLp invoice (like fetching a success action specific to the user)
Expand Down
10 changes: 5 additions & 5 deletions src/domains/payments/api/payment_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl PaymentHandler {
user.check_permission(Permission::WriteLnTransaction)?;

let payment = app_state.services.payment.pay(payload).await?;
Ok(Json(payment.into()))
Ok(payment.into())
}

async fn get(
Expand All @@ -47,8 +47,8 @@ impl PaymentHandler {
) -> Result<Json<Payment>, ApplicationError> {
user.check_permission(Permission::ReadLnTransaction)?;

let ln_address = app_state.services.payment.get(id).await?;
Ok(Json(ln_address.into()))
let payment = app_state.services.payment.get(id).await?;
Ok(payment.into())
}

async fn list(
Expand All @@ -58,9 +58,9 @@ impl PaymentHandler {
) -> Result<Json<Vec<Payment>>, ApplicationError> {
user.check_permission(Permission::ReadLnTransaction)?;

let lightning_payments = app_state.services.payment.list(query_params).await?;
let payments = app_state.services.payment.list(query_params).await?;

let response: Vec<Payment> = lightning_payments.into_iter().map(Into::into).collect();
let response: Vec<Payment> = payments.into_iter().map(Into::into).collect();

Ok(response.into())
}
Expand Down
6 changes: 3 additions & 3 deletions src/domains/payments/entities/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ pub struct Payment {
#[derive(Clone, Debug, EnumString, Display, Deserialize, Serialize, PartialEq, Eq, Default)]
pub enum PaymentStatus {
#[default]
PENDING,
SETTLED,
FAILED,
Pending,
Settled,
Failed,
}

#[derive(Clone, Debug, Deserialize, Serialize, Default)]
Expand Down
Loading

0 comments on commit 01d1d1a

Please sign in to comment.