Guidelines for AI agents working on this NestJS backend repository.
TeachMe is an educational platform backend built with NestJS, TypeORM, and PostgreSQL.
The backend is a NestJS REST API that:
- Provides CRUD operations for materials, users, carts
- Integrates with Stripe for payments
- Validates JWT tokens via Keycloak
- Stores files in
/app/assets(PDFs and thumbnails)
See root AGENTS.md for complete architecture overview and request flows.
# Install dependencies
npm install
# Development server (watch mode)
npm run start:dev
# Build for production
npm run build
# Run production build
npm run start:prod
# Linting (auto-fix enabled)
npm run lint
# Format code
npm run format
# Run all unit tests
npm run test
# Run single test file
npm run test -- src/users/usersService/users.service.spec.ts
# Run tests matching pattern
npm run test -- --testNamePattern="findAll"
# Run tests in watch mode
npm run test:watch
# Run tests with coverage
npm run test:cov
# Debug tests
npm run test:debug
# Run e2e tests
npm run test:e2eNode version: 20.9.0 (see .nvmrc)
Docker dependencies (from tmuxinator config):
- PostgreSQL:
docker start postgres - Keycloak:
docker start my_keycloak_local
Environment variables (see .env):
DB_HOST,DB_PORT,DB_USERNAME,DB_PASSWORDJWT_SECRET,JWT_REFRESH_SECRETSTRIPE_TEST,STRIPE_WEBHOOK_SECRETKEYCLOAK_REALM_URL
- NestJS imports first (
@nestjs/*) - External libraries second
- Internal modules third
- Use single quotes
- Add trailing commas
- Group related imports
- Classes: PascalCase (e.g.,
UsersService,CreateUserDto) - Methods/Variables: camelCase
- Database entities: singular nouns (e.g.,
User,Material) - Controllers: suffix with
Controller(e.g.,UsersController) - Services: suffix with
Service(e.g.,UsersService) - DTOs: suffix with
Dto(e.g.,UpdateUserDto)
- Prettier config: single quotes, trailing commas
- Indent: 2 spaces
- Line width: default (80)
- Strict null checks: disabled (see tsconfig.json)
- Explicit return types: optional
- Use decorators for NestJS metadata
- Entity classes should use TypeORM decorators
Each feature module should have:
[feature].module.ts- Module definition[feature].controller.ts- HTTP routes[feature].service.ts- Business logic[feature].entity.ts- TypeORM entities- Subdirectories for complex features (e.g.,
usersService/,usersController/)
Follow Arrange-Act-Assert:
describe('feature', () => {
it('should do something', async () => {
// Arrange
const input = { id: '1' };
mockRepo.find.mockResolvedValue(input);
// Act
const result = await service.method(input);
// Assert
expect(result).toEqual(input);
expect(mockRepo.find).toHaveBeenCalledWith(input);
});
});- Use NestJS exceptions (
BadRequestException,NotFoundException) - Validate DTOs using
class-validatordecorators - Return null for "not found" in repository methods
- Throw errors for invalid business logic
- Use Swagger decorators for API documentation
- Use DTOs for request/response validation
- Controllers should delegate to services
- Services should contain business logic
The backend is deployed via GitHub Actions when changes are pushed to main or master branches:
Trigger: Automatic deployment on push to be/** path
Workflow: .github/workflows/deploy_backend.yml
- Builds Docker image from
be/dockerfile - Uses Node 22 with Ghostscript, GraphicsMagick, and Poppler for PDF processing
- SSHs into production server
- Runs
docker compose build backendin/root/teachme/deploy - Restarts container with
docker compose stop backend && docker compose up -d backend
Docker Configuration:
- Multi-stage build (base → build → production)
- Production command:
npm run start:prod - Exposes port 3000
- Assets persisted via Docker volume
Infrastructure:
- Deployment files located in
deploy/directory (sibling tobe/) - Uses external Docker network
app-network - Connected to reverse proxy for SSL termination
Frontend: ../fe (React + Vite + TailwindCSS)