Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# AGENTS Guide for `amplication/sample-app`

## 1. Project Overview
- **Purpose:** Sample commerce + logistics system generated by Amplication, split into two NestJS backends and one React Admin frontend. Services cover order management, warehouse/shipment workflows, and an admin UI.
- **Technologies:** NestJS 10, React 18 + React Admin 5 running on Vite, Prisma ORM, PostgreSQL/MySQL plugins, Kafka (plus NATS helpers in logistics), Docker/Docker Compose, Jest, ESLint/Prettier.
- **Monorepo Layout:** Root contains `apps/` and `README.md`. There is **no root `package.json`**; every service is isolated. Always `cd` into the target app before installing dependencies or running scripts.

## 2. Repository Structure
| Path | Description | Key Files & Notes |
| --- | --- | --- |
| `/README.md` | High-level summary of the three services and broker topics. | Use it for quick context on plugins (JWT, Kafka, PostgreSQL/MySQL). |
| `/apps/ecommerce-server/` | Commerce backend (NestJS). | `.env`, `Dockerfile`, `docker-compose*.yml`, `nest-cli.json`, `package.json`, `prisma/schema.prisma`, `scripts/seed.ts`, `src/*`. |
| `/apps/logistic-server/` | Logistics backend (NestJS) focused on warehouses & shipments. | Same layout as ecommerce plus NATS helpers under `src/nats/`. Uses MySQL and Kafka receive topics. |
| `/apps/ecommerce-admin/` | Admin UI (React Admin on Vite). | `.env`, `Dockerfile`, `configuration/nginx.conf`, `vite.config.ts`, `public/`, `src/`, `package.json`. README still mentions CRA tooling—trust the package scripts. |

**Naming conventions:** Directories use kebab-case; React components and Nest modules use PascalCase. Configuration stays near service roots (`nest-cli.json`, `tsconfig*.json`, `vite.config.ts`). Environment variables live in each service’s `.env`.

## 3. Development Guidelines
- **Prerequisites:** Node.js ≥16, npm, and Docker (per backend READMEs). Install these before working with any service.
- **Environment files:** Each service ships with its own `.env`. Populate DB URLs, Kafka hostnames, JWT secrets, etc. Backend READMEs enumerate defaults such as `DB_URL`, `JWT_SECRET_KEY`, and `BCRYPT_SALT`. The frontend expects `PORT` and `REACT_APP_SERVER_URL`.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[MAJOR]: Section 3 line 20 and Section 5 line 36 instruct contributors to set REACT_APP_SERVER_URL, but the Vite admin actually consumes import.meta.env.VITE_REACT_APP_SERVER_URL (see apps/ecommerce-admin/src/data-provider/graphqlDataProvider.ts L6-L8 and .env L1-L2). Following the guide leaves the GraphQL client pointing to undefined/graphql, so the UI cannot talk to the backend when onboarding new agents.

Suggested fix: Update every frontend environment reference to VITE_REACT_APP_SERVER_URL (and explain that both the login shortcuts and the GraphQL data provider rely on that name) so the documented setup matches the actual env file and build tooling.

- **Per-app workflows:** Run `npm install`, `npm run prisma:generate`, and subsequent commands **inside the specific `apps/<service>` directory**.
- **Database bootstrap:** Use Docker (`npm run docker:dev`) to start local PostgreSQL/MySQL instances, then `npm run db:init` to apply migrations and seeds.
- **Credentials:** Generated services provide `admin` / `admin` credentials for first login. Update these outside of development contexts.

## 4. Backend Patterns (NestJS Services)
- **Module layout:** Feature folders like `apps/ecommerce-server/src/product/` or `apps/logistic-server/src/shipment/` contain controllers, resolvers, DTOs, and services. Shared infrastructure lives under `auth/`, `swagger/`, `providers/`, `validators/`, etc.
- **Prisma integration:** Schemas sit at `apps/*/prisma/schema.prisma` (PostgreSQL for ecommerce, MySQL for logistics). Run `npm run prisma:generate` after changing schemas, and use `db:migrate-*` scripts for migrations. Utility helpers live in `src/prisma/` and `src/prisma.util.ts`.
- **Seeding:** `scripts/seed.ts` (plus optional `customSeed.ts`) populate baseline data invoked via `npm run seed` or automatically through `npm run db:init`.
- **Messaging:** Kafka helpers under `src/kafka/` publish/consume topics listed in `README.md`. Logistics also ships `src/nats/` for optional NATS transport wiring.
- **Testing:** Jest is wired via `ts-jest` (`npm run test`). Spec samples live in `src/prisma.util.spec.ts` and `src/tests/` directories.
- **Docker workflows:** `docker-compose.dev.yml` spins up DBs and optional brokers; `docker:dev` script wraps it. `compose:up` / `compose:down` manage app containers. `package:container` builds service images.

## 5. Frontend Patterns (React Admin + Vite)
- **Tooling:** Though the README references Create React App (and `npm run eject`), the current tooling is Vite (`vite.config.ts`, `scripts.start = "vite"`). Prefer `npm run start`, `npm run build`, and `npm run serve` for preview.
- **Structure:** `src/` holds resource folders (`order/`, `product/`, `pages/`), shared providers under `api/` and `data-provider/`, auth wiring in `auth-provider/`, and theming in `theme/`. Static assets live in `public/`; `configuration/nginx.conf` supports containerized hosting.
- **Environment variables:** `.env` defines `REACT_APP_SERVER_URL` to point at whichever backend URL is active (default `http://localhost:<server-port>`). Update this before running `npm run start`.
- **Linting & formatting:** Use `npm run lint`, `npm run type-check`, and `npm run format` before committing UI changes.

## 6. Common Tasks & Commands
### Backends (`apps/ecommerce-server` and `apps/logistic-server` share the same scripts)
| Intent | Command |
| --- | --- |
| Install dependencies | `npm install` |
| Generate Prisma client | `npm run prisma:generate` |
| Save new migration | `npm run db:migrate-save -- --name "<description>"` |
| Apply migrations | `npm run db:migrate-up` |
| Reset database | `npm run db:clean` |
| Initialize DB (migrate + seed) | `npm run db:init` |
| Run seed only | `npm run seed` |
| Start dev database containers | `npm run docker:dev` |
| Start HTTP server (prod) | `npm run start` |
| Start with watch mode | `npm run start:watch` |
| Start with debugger | `npm run start:debug` |
| Run tests | `npm run test` |
| Build distributable | `npm run build` |
| Compose stack up / down | `npm run compose:up` / `npm run compose:down` |
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[MAJOR]: The backend command table (line 56) treats npm run compose:down as the routine way to stop the stack, but both backend packages define that script as docker-compose down --volumes (apps/ecommerce-server/package.json L18-L19 and the logistic variant). Running it as documented wipes the Postgres/MySQL and Kafka volumes every time contributors bring services down, forcing them to recreate databases, reapply migrations, and reseed brokers unexpectedly.

Suggested fix: Either update the guide to warn that compose:down destroys volumes or change the script to a plain docker-compose down and create a separate compose:down:clean for destructive resets.

| Build container image | `npm run package:container` |

**Bootstrap from scratch (backend):**
1. `cd apps/<backend>`
2. `npm install`
3. `cp .env.example .env` (if needed) and adjust DB/Kafka/JWT secrets.
4. `npm run prisma:generate`
5. `npm run docker:dev` (starts PostgreSQL/MySQL)
6. `npm run db:init`
7. `npm run start` (or `start:watch` during development)

### Frontend (`apps/ecommerce-admin`)
| Intent | Command |
| --- | --- |
| Install dependencies | `npm install` |
| Start dev server (Vite) | `npm run start` (defaults to port 3001) |
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[MINOR]: The frontend command table (line 72) claims npm run start serves Vite on port 3001, but apps/ecommerce-admin/package.json (L16) just runs vite and vite.config.ts (L5-L13) never overrides server.port, so the dev server actually binds to Vite’s default 5173. This misleads anyone wiring proxies or env files based on the guide.

Suggested fix: Either correct the guide to state the real default port (5173) or add server: { port: 3001 } to vite.config.ts so the command behaves as documented.

| Build production bundle | `npm run build` |
| Preview built bundle | `npm run serve` |
| Type-check | `npm run type-check` |
| Lint & auto-fix | `npm run lint` |
| Format source | `npm run format` |
| Build container image | `npm run package:container` |

**Bootstrap from scratch (frontend):**
1. `cd apps/ecommerce-admin`
2. Ensure backend URL is reachable, then set `REACT_APP_SERVER_URL` in `.env`.
3. `npm install`
4. `npm run start`

## 7. Quality & Validation
- **Testing:** Run backend Jest suites with `npm run test`. For UI, rely on `npm run type-check` and `npm run lint` + `npm run format` before publishing changes.
- **Prisma safety:** Any schema change requires `npm run prisma:generate` and either `db:migrate-save` (dev) or `db:migrate-up` (deploy). `db:clean` wipes data—use carefully.
- **Security considerations:** Default `admin` / `admin` exists in both backends and exposed in the frontend README. Rotate credentials and secrets before production. Keep JWT secrets, DB passwords, and Kafka configs out of source control by storing them only in `.env` or vaults.
- **Consistency checks:** Swagger docs (`src/swagger/`) and `grants.json` (RBAC) should be updated when adding resources. Messaging topics listed in `/README.md` must stay in sync with Kafka publishers/consumers under `src/kafka/`.

## 8. Reference Examples
- **Simple UI resource:** `apps/ecommerce-admin/src/pages/` (straightforward React Admin resource declarations and routing).
- **Complex backend module:** `apps/logistic-server/src/shipment/` demonstrates multi-entity relations and messaging hooks; `apps/logistic-server/src/warehouse/` ties into Prisma models defined in `prisma/schema.prisma`.
- **Data schema:** `apps/logistic-server/prisma/schema.prisma` (MySQL) vs. `apps/ecommerce-server/prisma/schema.prisma` (PostgreSQL) illustrate provider configuration.
- **Infrastructure helpers:** `apps/logistic-server/docker-compose.dev.yml` and `apps/ecommerce-server/docker-compose.dev.yml` define DB containers; `apps/ecommerce-admin/configuration/nginx.conf` shows how the UI is served inside containers.
- **Cross-cutting utilities:** `apps/ecommerce-server/src/auth/`, `src/validators/`, and `src/providers/` illustrate reusable NestJS patterns also mirrored in logistics.

## 9. Additional Tips & Resources
- Each service README (`apps/<service>/README.md`) details environment variables, scripts, and Docker usage—consult them before editing automation.
- When in doubt about messaging or auth, inspect `apps/*/src/kafka/`, `src/auth/`, and `src/swagger/` for current wiring.
- For containerized workflows, pair the Dockerfiles with their respective compose files (`docker-compose.dev.yml` for local DBs, `docker-compose.yml` for full stack).
- Use the root README’s broker topic list to keep Kafka producers/consumers aligned when introducing new entities.
- Remember that CRA-specific commands (`npm run eject`) in the admin README are legacy; Vite commands in `package.json` are authoritative.

With this guide and the per-service READMEs, future agents can confidently modify, test, and deploy each part of the monorepo without stepping on neighboring services.