FarmClient backend - Node/Express/TS + Prisma + Moolre payments, USSD, SMS, FarmScore. Ghana agri-marketplace.
# FarmClient API
Backend for **FarmClient** — an AI-powered agricultural marketplace and farmer-finance platform for
Ghana, built on **Moolre**'s payment rails (Collections, Disbursements, SMS, USSD). Implements the
SRS (FR-01 … FR-13): farmer registration, harvest listings, buyer orders with escrow, instant farmer
payouts, the `*789#` USSD interface, FarmScore credit scoring, harvest-advance loans, SMS
notifications, an admin dashboard API and scheduled jobs.
> Companion services: **`farmclient`** (Vite React buyer/marketplace frontend) and **`farmclient-ai`**
> (Python FastAPI price-intelligence microservice).
## Stack
Node 20 · Express 4 · TypeScript · Prisma · PostgreSQL 16 · Upstash Redis · Firebase Admin (Google /
Phone auth) · Moolre APIs · node-cron.
## Architecture
```
src/
config/ env, prisma, redis (Upstash + in-memory dev fallback), firebase, constants
middleware/ auth (Firebase→JWT, requireAuth/requireRole), rateLimit (Redis), error
services/
moolre.service.ts all 8 Moolre endpoints (auth headers, retry+backoff, sandbox)
sms.service.ts 9 SMS templates + Moolre VAS send + sms_log
payment.service.ts escrow init, payment links, payout (validate→transfer), webhooks,
loan auto-repayment, idempotency, transaction audit log
price.service.ts AI price client + rolling-average fallback (graceful degradation)
score.service.ts FarmScore (0–1000) calculation
auth.service.ts Google / Phone sign-in → find-or-create → JWT
ussd.service.ts Redis-backed USSD state machine (register, list, prices, orders,
wallet, loan)
routes/ auth, farmers, listings, orders, payments, prices, loans, admin, ussd, webhooks
jobs/ cron: price alerts, expire listings, auto-confirm deliveries, retrain, balance check
app.ts Express wiring server.ts bootstrap
prisma/ schema.prisma (farmers, buyers, agents, listings, orders, transactions, loans,
sms_log, price_history, audit_log) + seed.ts
```
## Moolre integration (SRS FR-06 …