Files
backmanager-server/CLAUDE.md
2026-06-24 22:44:14 +08:00

45 lines
2.6 KiB
Markdown

# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
## Project Overview
Enterprise information management system backend (企业信息管理系统后端服务). Node.js REST API built on Express 5 with raw MySQL queries (no ORM). Provides CRUD APIs for users, customers (franchisees), employees, contracts, after-sales services, and products/inventory.
## Commands
- `npm run dev` — start dev server with auto-restart (`node --watch server.js`)
- `npm start` — start production server (`node server.js`)
- `node test-all.js` — run integration tests (server must be running on port 3000 first; uses Node's built-in `http` module, no test framework)
No build step, no linter, no formatter configured.
## Architecture
**Flat structure** — no controllers/services/layers. Entry point `server.js` directly imports route handlers and registers them on the Express app.
- `server.js` — Express app setup, JWT `auth` middleware, `requireAdmin` middleware, all route registration, server startup
- `db.js` — MySQL connection pool (`mysql2/promise`), auto-initialization on startup (creates database, tables, seeds default admin `admin`/`123456`)
- `routes/*.js` — each exports `{ list, detail, create, update, remove }` functions for a single resource
**Auth flow**: JWT in `Authorization: Bearer <token>` header. Two middleware layers in `server.js`: `auth` (verifies token, attaches `req.user`) and `requireAdmin` (checks `req.user.role === 'admin'`).
## Database
MySQL 5.7+ / MariaDB 10.2+. Raw SQL with `pool.query()` and `?` parameterized placeholders. No foreign key constraints — referential integrity is enforced in application code by checking entity existence before inserts/updates.
Tables: `users`, `customers`, `employees`, `products`, `contracts`, `after_sales`
Relationships: contracts and after_sales reference customers and employees by ID, with `LEFT JOIN` in list/detail queries to include related names.
## Conventions
- **API response format**: `{ "code": 0, "message": "ok", "data": { ... } }` (errors use non-zero `code` and descriptive `message`)
- **List endpoints** return `{ list, total, page, pageSize, totalPages }` with pagination (`page`, `pageSize` params, max 100)
- **Route modules** use dynamic `WHERE 1=1` clause building for search/filter, and dynamic `SET` clause building for partial updates
- **Environment config**: dotenv via `.env` file (see `.env.example` for required variables: `PORT`, `DB_HOST`, `DB_PORT`, `DB_USER`, `DB_PASSWORD`, `DB_NAME`, `JWT_SECRET`, `JWT_EXPIRES_IN`)
## Dependencies
express, mysql2, jsonwebtoken, bcryptjs, dotenv