Repository navigation
Aiaiaiaiaia - #35
Aiaiaiaiaia#35
Conversation
…ChoiceModulesRepository
…LoggingMiddleware
…PredictionClient interface
There was a problem hiding this comment.
Pull request overview
This pull request introduces a comprehensive AI prediction feature to the backend application, enabling course/module recommendations based on user preferences. The implementation follows a layered architecture with proper separation of concerns across presentation, application, domain, and infrastructure layers. Additionally, the PR refactors the logging system to use structured JSON output and makes database schema improvements for better type safety.
- Added complete AI prediction infrastructure including DTOs, controllers, services, HTTP client, and domain models
- Refactored LoggerService to extend NestJS ConsoleLogger with structured JSON logging and consistent context setting
- Enhanced Prisma schema with explicit relations for UserFavorites and improved type safety by removing non-null assertions in the repository
Reviewed changes
Copilot reviewed 16 out of 17 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| src/presentation/dtos/ai.dto.ts | Defines validation rules for AI prediction request payload with user preferences |
| src/presentation/controllers/ai.controller.ts | Exposes POST /ai/predict endpoint with session-based authentication |
| src/application/services/ai.service.ts | Application service that orchestrates prediction requests and wraps results in Result type |
| src/application/ports/ai.port.ts | Defines IAiService interface for dependency injection |
| src/infrastructure/ai-service/prediction-client.ts | HTTP client implementation for external AI service communication with error handling |
| src/domain/predictions/prediction.model.ts | Domain models for prediction payload, response, and match data structures |
| src/domain/predictions/prediction-client.interface.ts | Interface for prediction client abstraction |
| src/common/logger.service.ts | Refactored to extend ConsoleLogger with structured JSON logging output |
| src/infrastructure/middleware/request-logging.middleware.ts | Updated to set logger context for better traceability |
| src/infrastructure/database/prisma.ts | Improved type safety for Prisma event listeners and added context setting |
| src/infrastructure/database/schema/schema.prisma | Added explicit relations between UserFavorites, User, and ChoiceModule |
| src/infrastructure/database/repositories/choicemodules.repository.ts | Removed unnecessary non-null assertions for improved type safety |
| src/application/services/auth.service.ts | Added logger context setting in constructor |
| src/app.module.ts | Registered AI service, HTTP client, and controller with dependency injection |
| package.json | Added @nestjs/axios, @nestjs/config, and axios dependencies |
| package-lock.json | Lock file updates for new dependencies |
| .env.example | Added AI_SERVICE_URL configuration variable |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| /** | ||
| * Centralized formatter to ensure consistent JSON structure. | ||
| * Handles objects, strings, and circular references safely. | ||
| */ | ||
| private formatEntry( | ||
| level: LogLevel, | ||
| message: unknown, | ||
| context?: string, | ||
| meta?: unknown, | ||
| ): string { | ||
| const entry = { | ||
| timestamp: new Date().toISOString(), | ||
| level, | ||
| context: context ?? this.name, | ||
| message: typeof message === 'string' ? message : JSON.stringify(message), | ||
| context: context ?? this.contextName, | ||
| message: typeof message === 'string' ? message : message, | ||
| ...(meta ? { meta } : {}), | ||
| }; | ||
|
|
||
| if (meta) payload.meta = meta; | ||
| return JSON.stringify(payload); | ||
| // Using a replacer or library like 'safe-stable-stringify' is better for prod, but standard JSON.stringify works fine for now. | ||
| return JSON.stringify(entry); |
There was a problem hiding this comment.
The comment on line 17 claims the formatter "Handles objects, strings, and circular references safely", but the implementation at line 34 uses standard JSON.stringify which does NOT handle circular references safely - it will throw a TypeError if circular references are encountered. Either update the implementation to actually handle circular references (e.g., using a replacer function or a library like 'safe-stable-stringify'), or update the documentation to remove the claim about handling circular references safely.
This pull request introduces a new AI prediction feature to the backend, including all necessary infrastructure, service, controller, and model layers. It also improves logging, updates environment and dependency configurations, and makes minor data model and repository adjustments for consistency.
AI Prediction Feature Implementation:
PredictionPayload,PredictionResponse, andPredictionMatchinprediction.model.ts, theIAiServiceinterface inai.port.ts, and the HTTP client implementation inprediction-client.ts. [1] [2] [3] [4] [5]POST /ai/predictendpoint viaAiController. [1] [2] [3] [4] [5]AI_SERVICE_URLenvironment variable to.env.examplefor external AI service configuration.@nestjs/axios,@nestjs/config, andaxios.Logging and Observability Improvements:
LoggerServiceto extend Nest'sConsoleLogger, ensuring structured JSON logging and consistent context setting across services and middleware. [1] [2] [3]Data Model and Repository Adjustments:
UserFavorites,User, andChoiceModule, and added auserFavoritesrelation onUserandChoiceModule. [1] [2]!) in theChoiceModulesRepositoryto align with updated Prisma typing and improve type safety. [1] [2] [3] [4] [5]