diff --git a/docs/openapi.yaml b/docs/openapi.yaml index 34a86ee..0206251 100644 --- a/docs/openapi.yaml +++ b/docs/openapi.yaml @@ -683,6 +683,42 @@ paths: description: User's wallet address is not known or not verified 418: description: Blockchain integration is not enabled + /api/v1/subscriptions/find: + get: + summary: Find subscription by sender and recipient + parameters: + - name: sender_id + in: query + description: Sender ID. + required: true + schema: + type: string + format: uuid + - name: recipient_id + in: query + description: Recipient ID. + required: true + schema: + type: string + format: uuid + responses: + 200: + description: Successful operation + content: + application/json: + schema: + type: object + properties: + id: + description: Subscription ID. + type: number + example: 1 + expires_at: + description: The date when subscription expires. + type: string + format: dateTime + 404: + description: Subscription not found /api/v1/subscriptions/invoices: post: summary: Create invoice diff --git a/src/mastodon_api/subscriptions/types.rs b/src/mastodon_api/subscriptions/types.rs index eb266ae..3e107c6 100644 --- a/src/mastodon_api/subscriptions/types.rs +++ b/src/mastodon_api/subscriptions/types.rs @@ -1,3 +1,4 @@ +use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use uuid::Uuid; @@ -30,7 +31,7 @@ impl From for Invoice { } #[derive(Deserialize)] -pub struct SubscriptionQueryParams { +pub struct SubscriptionAuthorizationQueryParams { pub price: u64, } @@ -54,3 +55,15 @@ impl SubscriptionOption { Some(settings) } } + +#[derive(Deserialize)] +pub struct SubscriptionQueryParams { + pub sender_id: Uuid, + pub recipient_id: Uuid, +} + +#[derive(Serialize)] +pub struct SubscriptionDetails { + pub id: i32, + pub expires_at: DateTime, +} diff --git a/src/mastodon_api/subscriptions/views.rs b/src/mastodon_api/subscriptions/views.rs index 41c37bf..99b3adf 100644 --- a/src/mastodon_api/subscriptions/views.rs +++ b/src/mastodon_api/subscriptions/views.rs @@ -23,21 +23,24 @@ use crate::models::profiles::types::{ PaymentType, ProfileUpdateData, }; +use crate::models::subscriptions::queries::get_subscription_by_participants; use crate::models::users::queries::get_user_by_id; use crate::monero::wallet::create_monero_address; use crate::utils::currencies::Currency; use super::types::{ Invoice, InvoiceData, - SubscriptionQueryParams, + SubscriptionAuthorizationQueryParams, + SubscriptionDetails, SubscriptionOption, + SubscriptionQueryParams, }; pub async fn authorize_subscription( auth: BearerAuth, config: web::Data, db_pool: web::Data, - query_params: web::Query, + query_params: web::Query, ) -> Result { let db_client = &**get_database_client(&db_pool).await?; let current_user = get_current_user(db_client, auth.token()).await?; @@ -145,6 +148,24 @@ pub async fn subscriptions_enabled( Ok(HttpResponse::Ok().json(account)) } +#[get("/find")] +async fn find_subscription( + db_pool: web::Data, + query_params: web::Query, +) -> Result { + let db_client = &**get_database_client(&db_pool).await?; + let subscription = get_subscription_by_participants( + db_client, + &query_params.sender_id, + &query_params.recipient_id, + ).await?; + let details = SubscriptionDetails { + id: subscription.id, + expires_at: subscription.expires_at, + }; + Ok(HttpResponse::Ok().json(details)) +} + #[post("/invoices")] async fn create_invoice_view( config: web::Data, @@ -177,5 +198,6 @@ pub fn subscription_api_scope() -> Scope { .route("/authorize", web::get().to(authorize_subscription)) .service(get_subscription_options) .route("/enable", web::post().to(subscriptions_enabled)) + .service(find_subscription) .service(create_invoice_view) }