feat: Monthly token limits per user

Database:
- Add monthly_token_limit and token_limit_reset_day to users table
- Create ai_usage_monthly view for current month usage
- Add check_token_limit() function to verify available tokens
- Add get_user_usage_stats() function for historical usage

API Endpoints:
- PUT /admin/users/{user_id}/token-limit - Set monthly limit
- GET /admin/users/{user_id}/token-usage - Get user's current usage
- GET /admin/users/{user_id}/token-limit/check - Check if user has tokens

Features:
- Default limit: 100,000 tokens/month
- Reset day: 1st of month (configurable 1-28)
- Limit of 0 = unlimited tokens
- Enforce limits at API level (check before AI requests)

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
2026-03-23 16:10:27 -03:00
parent ef213a61a0
commit 9e57756528
3 changed files with 265 additions and 1 deletions
+12
View File
@@ -401,6 +401,18 @@ async fn main() {
"/admin/ai-usage/global",
get(handlers_admin::get_ai_usage_global),
)
.route(
"/admin/users/{user_id}/token-limit",
put(handlers_admin::set_user_token_limit),
)
.route(
"/admin/users/{user_id}/token-usage",
get(handlers_admin::get_user_token_usage),
)
.route(
"/admin/users/{user_id}/token-limit/check",
get(handlers_admin::check_user_token_limit),
)
.route_layer(middleware::from_fn(
common::middleware::org_extractor_middleware,
));