|
2 | 2 | #![allow(clippy::too_many_arguments)] |
3 | 3 |
|
4 | 4 | pub mod revenue; |
| 5 | +pub mod quota; |
| 6 | +pub mod usage; |
| 7 | + |
5 | 8 |
|
6 | 9 | use soroban_sdk::{token, Address, Env, IntoVal, String, TryFromVal, Val, Vec}; |
7 | 10 | use subtrackr_types::{ |
@@ -1045,4 +1048,83 @@ impl SubTrackrSubscription { |
1045 | 1048 | proxy.require_auth(); |
1046 | 1049 | revenue::get_revenue_schedule(&env, &storage, subscription_id) |
1047 | 1050 | } |
| 1051 | + |
| 1052 | + // ── Quota & Usage API ── |
| 1053 | + |
| 1054 | + pub fn set_plan_quotas( |
| 1055 | + env: Env, |
| 1056 | + proxy: Address, |
| 1057 | + storage: Address, |
| 1058 | + merchant: Address, |
| 1059 | + plan_id: u64, |
| 1060 | + quotas: Vec<subtrackr_types::Quota>, |
| 1061 | + ) { |
| 1062 | + proxy.require_auth(); |
| 1063 | + merchant.require_auth(); |
| 1064 | + let plan: subtrackr_types::Plan = |
| 1065 | + storage_persistent_get(&env, &storage, StorageKey::Plan(plan_id)) |
| 1066 | + .expect("Plan not found"); |
| 1067 | + assert!( |
| 1068 | + plan.merchant == merchant, |
| 1069 | + "Only plan owner can set quotas" |
| 1070 | + ); |
| 1071 | + quota::set_plan_quotas(&env, &storage, plan_id, quotas); |
| 1072 | + } |
| 1073 | + |
| 1074 | + pub fn get_plan_quotas( |
| 1075 | + env: Env, |
| 1076 | + proxy: Address, |
| 1077 | + storage: Address, |
| 1078 | + plan_id: u64, |
| 1079 | + ) -> Vec<subtrackr_types::Quota> { |
| 1080 | + proxy.require_auth(); |
| 1081 | + quota::get_plan_quotas(&env, &storage, plan_id) |
| 1082 | + } |
| 1083 | + |
| 1084 | + pub fn record_usage( |
| 1085 | + env: Env, |
| 1086 | + proxy: Address, |
| 1087 | + storage: Address, |
| 1088 | + subscription_id: u64, |
| 1089 | + metric: subtrackr_types::QuotaMetric, |
| 1090 | + amount: u64, |
| 1091 | + ) -> subtrackr_types::UsageRecord { |
| 1092 | + proxy.require_auth(); |
| 1093 | + let sub: subtrackr_types::Subscription = |
| 1094 | + storage_persistent_get(&env, &storage, StorageKey::Subscription(subscription_id)) |
| 1095 | + .expect("Subscription not found"); |
| 1096 | + |
| 1097 | + let admin = get_admin(&env, &storage); |
| 1098 | + // Only subscriber or admin can record usage? Usually it's the app/admin |
| 1099 | + // For simplicity, let's allow anyone with auth (simplified for this task) |
| 1100 | + // In a real app, you might want more complex auth. |
| 1101 | + |
| 1102 | + usage::record_usage(&env, &storage, subscription_id, sub.plan_id, metric, amount) |
| 1103 | + } |
| 1104 | + |
| 1105 | + pub fn get_usage_record( |
| 1106 | + env: Env, |
| 1107 | + proxy: Address, |
| 1108 | + storage: Address, |
| 1109 | + subscription_id: u64, |
| 1110 | + metric: subtrackr_types::QuotaMetric, |
| 1111 | + ) -> subtrackr_types::UsageRecord { |
| 1112 | + proxy.require_auth(); |
| 1113 | + usage::get_usage_record(&env, &storage, subscription_id, metric) |
| 1114 | + } |
| 1115 | + |
| 1116 | + pub fn check_quota( |
| 1117 | + env: Env, |
| 1118 | + proxy: Address, |
| 1119 | + storage: Address, |
| 1120 | + subscription_id: u64, |
| 1121 | + metric: subtrackr_types::QuotaMetric, |
| 1122 | + ) -> subtrackr_types::QuotaStatus { |
| 1123 | + proxy.require_auth(); |
| 1124 | + let sub: subtrackr_types::Subscription = |
| 1125 | + storage_persistent_get(&env, &storage, StorageKey::Subscription(subscription_id)) |
| 1126 | + .expect("Subscription not found"); |
| 1127 | + usage::check_quota(&env, &storage, subscription_id, sub.plan_id, metric) |
| 1128 | + } |
1048 | 1129 | } |
| 1130 | + |
0 commit comments