Skip to content

Fixed analytics tests in api gateway and analytics services - #220

Merged
UZ9 merged 6 commits into
mainfrom
ankith/analytics-test-fixes
Oct 27, 2025
Merged

UZ9 merged 6 commits into
mainfrom
ankith/analytics-test-fixes

Conversation

@Duckierstone42

@Duckierstone42 Duckierstone42 commented Oct 25, 2025 •

Copy link
Copy Markdown
Contributor

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

@Duckierstone42 Duckierstone42 changed the title Updated test to pass in config env and config id Fixed analytics tests in api gateway and analytics services Oct 26, 2025
@Duckierstone42
Duckierstone42 marked this pull request as ready for review October 26, 2025 02:56
@Duckierstone42
Duckierstone42 requested a review from UZ9 October 26, 2025 02:56

@greptile-apps greptile-apps Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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/environment parameter swaps in logClickEvent, logInputEvent, logVisitEvent, and all viewer methods in analytics.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/configEnvironment instead of the old projectId/environment pattern
  • Code Cleanup: Removed debug console.log statements 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 --schema flag

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 logCustomEvent method was missed in the bug fix sweep and still incorrectly uses event.environment instead of event.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
Loading

Additional Comments (1)

  1. packages/analytics-service/src/modules/analytics/analytics.service.ts, line 242 (link)

    logic: Inconsistent parameter usage - should be event.configEnvironment instead of event.environment. All other event logging methods (logClickEvent, logInputEvent, logVisitEvent) use configEnvironment for looking up the analytics config.

10 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@UZ9

UZ9 commented Oct 27, 2025

Copy link
Copy Markdown
Contributor

LGTM, thanks!

@UZ9
UZ9 merged commit 68eef62 into main Oct 27, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants