Add API method for finding subscription by participants

This commit is contained in:
silverpill 2022-09-05 22:08:38 +00:00
parent 05e295744d
commit 6accf8ac2e
3 changed files with 74 additions and 3 deletions

View file

@ -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

View file

@ -1,3 +1,4 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
@ -30,7 +31,7 @@ impl From<DbInvoice> 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<Utc>,
}

View file

@ -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<Config>,
db_pool: web::Data<Pool>,
query_params: web::Query<SubscriptionQueryParams>,
query_params: web::Query<SubscriptionAuthorizationQueryParams>,
) -> Result<HttpResponse, HttpError> {
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<Pool>,
query_params: web::Query<SubscriptionQueryParams>,
) -> Result<HttpResponse, HttpError> {
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<Config>,
@ -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)
}