Repository navigation
Fixed analytics tests in api gateway and analytics services - #220
Conversation
There was a problem hiding this comment.
Disclaimer: This comment was AI-generated and is not necessarily completely accurate. Please take code comments with a grain of salt.
Greptile Overview
Greptile Summary
Fixed analytics tests across API gateway and analytics service by correcting parameter usage and implementing proper test mocking strategies.
Key Changes
- Bug Fixes: Corrected
configEnvironment/environmentparameter swaps inlogClickEvent,logInputEvent,logVisitEvent, and all viewer methods inanalytics.service.ts - Test Infrastructure: Implemented comprehensive mock services for both analytics-service and api-gateway tests, replacing incomplete mocking with proper constructor-based mocks
- Test Updates: Updated all test requests to use
configId/configEnvironmentinstead of the oldprojectId/environmentpattern - Code Cleanup: Removed debug
console.logstatements from analytics service - Type Safety: Added
@Type(() => Number)decorator to DTO limit fields to ensure proper type coercion from query string parameters - Documentation: Updated README with more complete Prisma migration command including
--schemaflag
Issue Found
One critical inconsistency remains: logCustomEvent at analytics.service.ts:242 still uses event.environment instead of event.configEnvironment for the analytics logger lookup, unlike all other event logging methods which were correctly fixed.
Confidence Score: 3/5
- This PR is mostly safe to merge but has one critical bug that needs fixing first
- The PR successfully fixes most of the configEnvironment/environment parameter issues and implements comprehensive test mocking. However, the
logCustomEventmethod was missed in the bug fix sweep and still incorrectly usesevent.environmentinstead ofevent.configEnvironment, which is inconsistent with the fixes made to the other three event logging methods. This will cause runtime issues when logging custom events with the wrong analytics config. - Pay close attention to
packages/analytics-service/src/modules/analytics/analytics.service.ts- the logCustomEvent method needs the same fix applied to the other event logging methods
Important Files Changed
File Analysis
| Filename | Score | Overview |
|---|---|---|
| packages/analytics-service/src/modules/analytics/analytics.service.ts | 3/5 | Fixed configEnvironment/environment parameter swaps in logClickEvent, logInputEvent, logVisitEvent, and viewer methods. Removed debug console.log statements. However, logCustomEvent still incorrectly uses event.environment instead of event.configEnvironment at line 242. |
| packages/analytics-service/test/analytics.e2e-spec.ts | 5/5 | Comprehensive test updates with proper mocking strategy. Updated all test requests to use configId/configEnvironment instead of projectId/environment. Improved mock implementations with proper parameter handling and eslint-disable comments for unused parameters. |
| packages/api-gateway/test/analytics.e2e-spec.ts | 5/5 | Added comprehensive mock analytics service to properly test API gateway endpoints. Created analytics config during setup. Cleaned up test assertions and removed unnecessary apiKey parameters from test payloads. |
| packages/api-gateway/src/models/analytics.dto.ts | 5/5 | Added @type(() => Number) decorator to limit fields in GetEventsQuery and GetAllEventsQuery to ensure proper type transformation from query string parameters. |
Sequence Diagram
sequenceDiagram
participant Client
participant APIGateway
participant AnalyticsService
participant DBService
participant BogAnalytics
Note over Client,BogAnalytics: Analytics Event Logging Flow
Client->>APIGateway: POST /analytics/events/click<br/>(with API key)
APIGateway->>APIGateway: Extract configId & configEnvironment<br/>from API key
APIGateway->>AnalyticsService: logClickEvent(configId, configEnvironment, eventData)
AnalyticsService->>DBService: getAnalyticsConfig(configId, configEnvironment)
DBService-->>AnalyticsService: Analytics Config (server/client keys)
AnalyticsService->>AnalyticsService: Create AnalyticsLogger instance<br/>with bog-analytics keys
AnalyticsService->>BogAnalytics: logClickEvent(objectId, userId)
BogAnalytics-->>AnalyticsService: Event Response
AnalyticsService-->>APIGateway: Event Response
APIGateway-->>Client: 201 Created
Note over Client,BogAnalytics: Analytics Event Viewing Flow
Client->>APIGateway: GET /analytics/events/click<br/>(with API key + query params)
APIGateway->>APIGateway: Extract configId & configEnvironment<br/>Transform limit param to Number
APIGateway->>AnalyticsService: getClickEventsPaginated(configId, configEnvironment, params)
AnalyticsService->>DBService: getAnalyticsConfig(configId, configEnvironment)
DBService-->>AnalyticsService: Analytics Config
AnalyticsService->>AnalyticsService: Create AnalyticsViewer instance
AnalyticsService->>BogAnalytics: getClickEventsPaginated(queryParams)
BogAnalytics-->>AnalyticsService: Events Array
AnalyticsService-->>APIGateway: Events Response
APIGateway-->>Client: 200 OK
Additional Comments (1)
-
packages/analytics-service/src/modules/analytics/analytics.service.ts, line 242 (link)logic: Inconsistent parameter usage - should be
event.configEnvironmentinstead ofevent.environment. All other event logging methods (logClickEvent,logInputEvent,logVisitEvent) useconfigEnvironmentfor looking up the analytics config.
10 files reviewed, 1 comment
|
LGTM, thanks! |
Currently have the API gateway analytics tests working. Also analytics endpoint tests working. Also some bug fixes on in the methods where configEnvironment and environment where swapped a lot.
Solves #219