api.faculytics is a NestJS-based backend application designed to integrate with Moodle. It serves as an API layer that authenticates users via Moodle credentials, synchronizes user data, and likely provides analytics or extended functionality on top of Moodle data.
- Framework: NestJS (TypeScript)
- Database: PostgreSQL (via MikroORM)
- Authentication: JWT, Passport, Moodle Token Integration
- Validation: Zod, class-validator
- Documentation: Swagger (OpenAPI)
The application follows the standard NestJS modular architecture, split into Infrastructure and Application layers.
- InfrastructureModules:
ConfigModule: Loads and validates environment variables (src/configurations/env/).MikroORMModule: Handles database connections and entity management.JwtModule: Global JWT configuration for signing/verifying tokens.PassportModule: Authentication strategies.
- ApplicationModules:
AuthModule: Handles user login, token refresh, and session management. It authenticates users against Moodle and issues local JWTs.MoodleModule: Core integration with Moodle. ContainsMoodleServicefor API calls andMoodleSyncServicefor data synchronization.HealthModule: Health check endpoints.
- User: Represents a local user account, mapped 1:1 to a Moodle user via
moodleUserId. Stores basic profile info (first name, last name, picture). - MoodleToken: Stores Moodle access tokens associated with a user.
- MoodleCategory: Cache for Moodle's category hierarchy, used to map courses and organizational structures.
- Campus, Semester, Department, Program: Local representations of the institutional hierarchy derived from Moodle categories.
- MikroORM: configured in
mikro-orm.config.ts. Supports migrations and seeding (src/migrations/,src/seeders/).
- Node.js (v20+ recommended)
- PostgreSQL Database
- Copy
.env.sampleto.env. - Configure the database URL (supports Neon.tech SSL defaults) and Moodle API credentials.
- Start Development:
npm run start:dev(Watch mode) - Build:
npm run build - Production Start:
npm run start:prod - Lint:
npm run lint - Format:
npm run format
- Unit Tests:
npm run test - E2E Tests:
npm run test:e2e - Coverage:
npm run test:cov
- Configuration: All environment variables are validated using
zodinsrc/configurations/env/. Usesrc/configurations/index.config.tsto access them. - DTOs: Request/Response DTOs are located within each module (e.g.,
src/modules/auth/dto/). - Database:
- Use
MikroORMfor all database interactions. - Run migrations via MikroORM CLI (commands not explicitly in package.json scripts, likely accessed via
npx mikro-orm).
- Use
- Code Style: strict ESLint and Prettier rules are enforced via
huskypre-commit hooks.
To ensure data integrity during synchronization from external sources (like Moodle):
- Business Keys: Use external IDs (e.g.,
moodleCategoryId) as the conflict target forem.upsert. - Primary Key Stability: Always exclude
idandcreated_atfrom the update set usingonConflictMergeFieldsto prevent overwriting local UUIDs or record creation timestamps. - Entity Initialization: Use
tx.create(Entity, data, { managed: false })before upserting. This ensures entity property initializers (like UUID generation) are executed, allowing the database to decide whether to use the new ID (on insert) or ignore it (on conflict).
- Base Class: All cron jobs must extend
BaseJobto ensure consistent startup logging and standardized error handling. - Shutdown Handling: Do not manually attempt to stop cron jobs in
onApplicationShutdown. NestJS'sScheduleModulehandles the cleanup of theSchedulerRegistryautomatically. Manual cleanup often leads to "Job not found" warnings during the shutdown sequence.
To ensure efficiency and adherence to project standards, use the following specialized agents for their respective domains:
- git-agent: Git expert agent for all local and remote operations (commits, branching, bisect, remote sync).
- Tools:
run_shell_command,read_file,grep_search,list_directory. - Usage: Invoke for any version control tasks, preparing PRs, or investigating history.
- Tools:
- pr-agent: PR expert agent for automating pull requests and release workflows.
- Tools:
run_shell_command,read_file,grep_search,list_directory. - Usage: Invoke to create PRs, automate cherry-picking between branches (develop/staging/master), and generate descriptions.
- Tools:
- e2e-test-agent: Expert in End-to-End testing for the NestJS application.
- Usage: Invoke for scenario generation, database management for tests, and failure investigation.
- moodle-api-agent: Expert in Moodle Web Service integration.
- Usage: Invoke to scaffold Moodle API calls, generate DTOs, and integrate with MoodleClient and MoodleService.