managarten/services/mana-credits/CLAUDE.md
Till JS e068335dd4 refactor(credits): simplify credit system — remove productivity credits, guild pools, complex gift types
The credit system was overengineered for the local-first architecture:
- Productivity micro-credits (task/event/contact creation at 0.02 credits) made no sense
  since these operations happen locally in IndexedDB with zero server cost and were never enforced
- Guild pool system (6 DB tables, spending limits, membership checks) had no active users
- Gift system had 5 types (simple/personalized/split/first_come/riddle) when 2 suffice

Now credits are only charged for operations that actually cost money: AI API calls and
premium features (sync, exports). This makes the value proposition clear to users.

Changes:
- Remove 8 productivity operations + CreditCategory.PRODUCTIVITY from @mana/credits
- Delete guild pool service, routes, schema (3 files); remove guild refs from 8 backend files
- Simplify gifts to simple + personalized only; remove bcrypt/riddle/portions logic
- Update all frontend pages (credits dashboard, gift create/redeem, public gift page)
- Update shared-hono consumeCredits() to remove creditSource parameter
- Update mana-credits CLAUDE.md

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-10 19:08:42 +02:00

3.1 KiB

mana-credits

Standalone credit management service for the Mana ecosystem. Extracted from mana-auth.

Tech Stack

Layer Technology
Runtime Bun
Framework Hono
Database PostgreSQL + Drizzle ORM
Payments Stripe (Payment Intents, Checkout Sessions)
Auth JWT validation via JWKS from mana-auth

Quick Start

# Start (requires PostgreSQL running)
bun run dev

# Database
bun run db:push    # Push schema
bun run db:studio  # Open Drizzle Studio

Port: 3061

API Endpoints

Personal Credits (JWT auth)

Method Path Description
GET /api/v1/credits/balance Get personal balance
POST /api/v1/credits/use Use credits
GET /api/v1/credits/transactions Transaction history
GET /api/v1/credits/purchases Purchase history
GET /api/v1/credits/packages Available packages
POST /api/v1/credits/purchase Initiate Stripe purchase
GET /api/v1/credits/purchase/:id Purchase status

Gift Codes (Mixed auth)

Method Path Description
GET /api/v1/gifts/:code Get gift info (public)
POST /api/v1/gifts Create gift (JWT)
GET /api/v1/gifts/me/created My created gifts (JWT)
GET /api/v1/gifts/me/received My received gifts (JWT)
POST /api/v1/gifts/:code/redeem Redeem gift (JWT)
DELETE /api/v1/gifts/:id Cancel gift (JWT)

Internal (X-Service-Key auth)

Method Path Description
GET /api/v1/internal/credits/balance/:userId Get user balance
POST /api/v1/internal/credits/use Use credits for user
POST /api/v1/internal/credits/refund Refund credits
POST /api/v1/internal/credits/init Initialize balance
POST /api/v1/internal/gifts/redeem-pending Auto-redeem on registration

Webhooks

Method Path Description
POST /api/v1/webhooks/stripe Stripe payment events

Environment Variables

PORT=3061
DATABASE_URL=postgresql://mana:devpassword@localhost:5432/mana_credits
MANA_AUTH_URL=http://localhost:3001
MANA_SERVICE_KEY=dev-service-key
BASE_URL=http://localhost:3061
STRIPE_SECRET_KEY=sk_test_...
STRIPE_WEBHOOK_SECRET=whsec_...
CORS_ORIGINS=http://localhost:5173,http://localhost:5180

Database

Own database: mana_credits

Schemas: credits.*, gifts.*

Tables: balances, transactions, packages, purchases, usage_stats, stripe_customers, gift_codes, gift_redemptions

Credit Operations

Credits are only charged for operations that cost real money:

  • AI operations (2-25 credits): Chat with GPT-4/Claude/Gemini, image generation, research, food/plant analysis
  • Premium features (0.5-5 credits): CalDAV/Google sync, cloud sync, PDF export, bulk import

Local-first CRUD operations (tasks, events, contacts, etc.) are free — they happen in IndexedDB with no server cost.

Gift Types

Two gift types: simple (anyone with code can redeem) and personalized (auto-redeemed when target email registers). Each gift is single-use.