- Task 1: Admin endpoints for wallet management (balance, ledger, adjust, reconcile) - Task 2: AI Credits admin UI (pricing.tsx, credit.tsx) - Task 3: Ollama security (NetworkPolicy, prompt validation, audit logging) - Task 4: LiteLLM integration (litellm.rs, migrated AI feature handlers) - Task 5: Refund architecture (ai_refunds table, endpoints) - Task 6: Coupons, promotions, referrals (order creation with coupon validation) - Task 7: Subscription lifecycle (plan upgrades/downgrades/cancellations, cron jobs) - Task 8: Credit expiration enforcement (daily cron task) - Task 9: Token cost engine (ai_model_cost_config, margin view) - Task 10: Observability (metrics tables, aggregation function) New files: - apps/users/src/litellm.rs (LiteLLM client) - apps/users/src/ai_credits.rs (Charging primitives) - apps/users/src/ai_subscription.rs (Plan lifecycle) - apps/users/src/handlers/ai_credits.rs (Admin endpoints) - apps/payments/src/ai_credits.rs (Package purchase) - apps/payments/src/payu.rs (PayU integration) - apps/cron/src/tasks/ai_credits.rs (Cron jobs) - 9 SQL migrations for schema - crates/db/src/models/ai_credits.rs (Repository) - tests for ai_credits
48 lines
2 KiB
PL/PgSQL
48 lines
2 KiB
PL/PgSQL
-- AI credit purchase packages + PayU order tracking, matching the
|
|
-- already-shipped frontend contract in
|
|
-- nxtgauge-frontend-solid/src/components/dashboard/CreditsPage.tsx
|
|
-- (GET /api/ai-credits, POST /api/ai-credits/order, POST /api/ai-credits/verify).
|
|
-- price_inr is stored in paise (matches pricing_packages.price_inr's
|
|
-- existing convention in this codebase), not rupees.
|
|
|
|
BEGIN;
|
|
|
|
CREATE TABLE ai_credit_packages (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
name VARCHAR(150) NOT NULL,
|
|
description TEXT,
|
|
credits INT NOT NULL,
|
|
price_inr INT NOT NULL, -- paise
|
|
is_active BOOLEAN NOT NULL DEFAULT TRUE,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
|
);
|
|
|
|
CREATE INDEX idx_ai_credit_packages_active ON ai_credit_packages(is_active, price_inr);
|
|
|
|
INSERT INTO ai_credit_packages (name, description, credits, price_inr) VALUES
|
|
('Starter AI Credits', '50 AI credits for casual usage', 50, 9900),
|
|
('Pro AI Credits', '200 AI credits for power users', 200, 34900),
|
|
('Business AI Credits', '750 AI credits for teams', 750, 99900),
|
|
('Enterprise AI Credits', '2500 AI credits for heavy usage', 2500, 249900);
|
|
|
|
-- PayU order tracking for AI credit purchases, separate from the
|
|
-- TraceCoins `payments` table (crates/db/migrations/20260317190300_portfolio_payments.up.sql)
|
|
-- per the earlier decision to keep AI credits fully separate from TraceCoins.
|
|
CREATE TABLE ai_credit_orders (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
user_id UUID NOT NULL REFERENCES users(id),
|
|
package_id UUID NOT NULL REFERENCES ai_credit_packages(id),
|
|
txnid VARCHAR(100) UNIQUE NOT NULL,
|
|
payu_payment_id VARCHAR(100),
|
|
amount_inr INT NOT NULL, -- paise
|
|
credits INT NOT NULL,
|
|
status VARCHAR(20) NOT NULL DEFAULT 'PENDING', -- PENDING, SUCCESS, FAILED
|
|
verified_at TIMESTAMPTZ,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
|
);
|
|
|
|
CREATE INDEX idx_ai_credit_orders_user_id ON ai_credit_orders(user_id);
|
|
CREATE INDEX idx_ai_credit_orders_txnid ON ai_credit_orders(txnid);
|
|
|
|
COMMIT;
|