forked from StellarSend/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscriptions.rs
More file actions
78 lines (69 loc) · 2.45 KB
/
Copy pathsubscriptions.rs
File metadata and controls
78 lines (69 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use crate::{
error::{AppError, AppResult},
middleware::auth::AuthUser,
models::subscription::{CreateSubscriptionRequest, ListSubscriptionsParams},
services::subscription::SubscriptionService,
AppState,
};
use axum::{
extract::{Path, Query, State},
Json,
};
use serde_json::{json, Value};
use std::sync::Arc;
use uuid::Uuid;
/// POST /api/subscriptions
///
/// Records a recurring-payment subscription. If the client has already
/// submitted the on-chain `create_subscription` call, pass
/// `onchain_subscription_id` so the keeper knows which on-chain
/// subscription to execute later.
pub async fn create_subscription(
State(state): State<Arc<AppState>>,
auth: AuthUser,
Json(req): Json<CreateSubscriptionRequest>,
) -> AppResult<Json<Value>> {
let svc = SubscriptionService::new(state.pool.clone());
let subscription = svc.create(auth.user_id, &req).await?;
Ok(Json(json!({ "success": true, "data": subscription })))
}
/// GET /api/subscriptions
pub async fn list_subscriptions(
State(state): State<Arc<AppState>>,
auth: AuthUser,
Query(params): Query<ListSubscriptionsParams>,
) -> AppResult<Json<Value>> {
let svc = SubscriptionService::new(state.pool.clone());
let subscriptions = svc.list_for_user(auth.user_id, ¶ms).await?;
Ok(Json(json!({ "success": true, "data": subscriptions })))
}
/// GET /api/subscriptions/:id
pub async fn get_subscription(
State(state): State<Arc<AppState>>,
auth: AuthUser,
Path(id): Path<Uuid>,
) -> AppResult<Json<Value>> {
let svc = SubscriptionService::new(state.pool.clone());
let subscription = svc
.get_by_id(id)
.await?
.ok_or_else(|| AppError::NotFound("Subscription".into()))?;
if subscription.payer_id != auth.user_id {
return Err(AppError::Forbidden);
}
Ok(Json(json!({ "success": true, "data": subscription })))
}
/// POST /api/subscriptions/:id/cancel
///
/// Cancels the internal record. In production the client should also submit
/// the on-chain `cancel_subscription` call; the keeper will not execute a
/// subscription this endpoint has marked cancelled regardless.
pub async fn cancel_subscription(
State(state): State<Arc<AppState>>,
auth: AuthUser,
Path(id): Path<Uuid>,
) -> AppResult<Json<Value>> {
let svc = SubscriptionService::new(state.pool.clone());
let subscription = svc.cancel(auth.user_id, id).await?;
Ok(Json(json!({ "success": true, "data": subscription })))
}