Add API method for checking invoice status

This commit is contained in:
silverpill 2022-09-07 10:43:50 +00:00
parent 383fc13059
commit 86fe717a77
4 changed files with 67 additions and 2 deletions

View file

@ -773,6 +773,26 @@ paths:
description: Sender or recipient not found.
418:
description: Blockchain integration is not enabled.
/api/v1/subscriptions/invoices/{invoice_id}:
get:
summary: View information about an invoice.
parameters:
- name: invoice_id
in: path
description: Invoice ID
required: true
schema:
type: string
format: uuid
responses:
200:
description: Successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/Invoice'
404:
description: Invoice not found
/api/v1/timelines/public:
get:
summary: View local public posts.
@ -1052,6 +1072,14 @@ components:
payment_address:
description: Payment address.
type: string
status:
description: Invoice status.
type: string
enum:
- open
- paid
- forwarded
- timeout
Mention:
type: object
properties:

View file

@ -2,7 +2,7 @@ use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::models::invoices::types::DbInvoice;
use crate::models::invoices::types::{DbInvoice, InvoiceStatus};
use crate::models::profiles::types::PaymentOption;
#[derive(Deserialize)]
@ -17,15 +17,23 @@ pub struct Invoice {
pub sender_id: Uuid,
pub recipient_id: Uuid,
pub payment_address: String,
pub status: String,
}
impl From<DbInvoice> for Invoice {
fn from(value: DbInvoice) -> Self {
let status = match value.invoice_status {
InvoiceStatus::Open => "open",
InvoiceStatus::Paid => "paid",
InvoiceStatus::Forwarded => "forwarded",
InvoiceStatus::Timeout => "timeout",
};
Self {
id: value.id,
sender_id: value.sender_id,
recipient_id: value.recipient_id,
payment_address: value.payment_address,
status: status.to_string(),
}
}
}

View file

@ -1,5 +1,6 @@
use actix_web::{get, post, web, HttpResponse, Scope};
use actix_web_httpauth::extractors::bearer::BearerAuth;
use uuid::Uuid;
use crate::activitypub::builders::update_person::prepare_update_person;
use crate::config::Config;
@ -12,7 +13,7 @@ use crate::ethereum::subscriptions::{
};
use crate::mastodon_api::accounts::types::Account;
use crate::mastodon_api::oauth::auth::get_current_user;
use crate::models::invoices::queries::create_invoice;
use crate::models::invoices::queries::{create_invoice, get_invoice_by_id};
use crate::models::profiles::queries::{
get_profile_by_id,
update_profile,
@ -193,6 +194,17 @@ async fn create_invoice_view(
Ok(HttpResponse::Ok().json(invoice))
}
#[get("/invoices/{invoice_id}")]
async fn get_invoice(
db_pool: web::Data<Pool>,
invoice_id: web::Path<Uuid>,
) -> Result<HttpResponse, HttpError> {
let db_client = &**get_database_client(&db_pool).await?;
let db_invoice = get_invoice_by_id(db_client, &invoice_id).await?;
let invoice = Invoice::from(db_invoice);
Ok(HttpResponse::Ok().json(invoice))
}
pub fn subscription_api_scope() -> Scope {
web::scope("/api/v1/subscriptions")
.route("/authorize", web::get().to(authorize_subscription))
@ -200,4 +212,5 @@ pub fn subscription_api_scope() -> Scope {
.route("/enable", web::post().to(subscriptions_enabled))
.service(find_subscription)
.service(create_invoice_view)
.service(get_invoice)
}

View file

@ -39,6 +39,22 @@ pub async fn create_invoice(
Ok(invoice)
}
pub async fn get_invoice_by_id(
db_client: &impl GenericClient,
invoice_id: &Uuid,
) -> Result<DbInvoice, DatabaseError> {
let maybe_row = db_client.query_opt(
"
SELECT invoice
FROM invoice WHERE id = $1
",
&[&invoice_id],
).await?;
let row = maybe_row.ok_or(DatabaseError::NotFound("invoice"))?;
let invoice = row.try_get("invoice")?;
Ok(invoice)
}
pub async fn get_invoice_by_address(
db_client: &impl GenericClient,
chain_id: &ChainId,