|
1 | 1 | import { status } from '@grpc/grpc-js'; |
2 | | -import { Injectable } from '@nestjs/common'; |
| 2 | +import { Inject, Injectable } from '@nestjs/common'; |
3 | 3 | import { RpcException } from '@nestjs/microservices'; |
4 | 4 | import { AnalyticsProto } from 'juno-proto'; |
5 | | -import { BogAnalyticsService } from 'src/bog-analytics.service'; |
| 5 | +import { AnalyticsConfigService } from '../analytics_config/analytics_config.service'; |
6 | 6 |
|
7 | 7 | @Injectable() |
8 | 8 | export class AnalyticsService { |
9 | | - constructor(private readonly bogAnalytics: BogAnalyticsService) {} |
| 9 | + constructor( |
| 10 | + @Inject('BOG_ANALYTICS') private readonly bogAnalytics: any, |
| 11 | + private readonly analyticsConfigService: AnalyticsConfigService, |
| 12 | + ) {} |
10 | 13 |
|
11 | | - authenticateAnalytics(key: string) { |
| 14 | + async authenticateAnalytics(projectId: number, environment: string) { |
12 | 15 | try { |
| 16 | + const key = await this.analyticsConfigService.getAnalyticsKey( |
| 17 | + projectId, |
| 18 | + environment, |
| 19 | + ); |
13 | 20 | this.bogAnalytics.authenticate(key); |
14 | 21 | } catch (e) { |
15 | 22 | throw new RpcException({ |
16 | 23 | code: status.UNAUTHENTICATED, |
17 | | - message: 'Invalid API key', |
| 24 | + message: 'Invalid API key or analytics config not found', |
18 | 25 | }); |
19 | 26 | } |
20 | 27 | } |
21 | 28 |
|
22 | 29 | async logClickEvent( |
23 | 30 | event: AnalyticsProto.ClickEventRequest, |
24 | 31 | ): Promise<AnalyticsProto.ClickEventResponse> { |
25 | | - this.authenticateAnalytics(event.apiKey); |
| 32 | + await this.authenticateAnalytics( |
| 33 | + Number(event.projectId), |
| 34 | + event.environment, |
| 35 | + ); |
26 | 36 |
|
27 | 37 | if ( |
28 | 38 | !event || |
@@ -62,7 +72,10 @@ export class AnalyticsService { |
62 | 72 | async logInputEvent( |
63 | 73 | event: AnalyticsProto.InputEventRequest, |
64 | 74 | ): Promise<AnalyticsProto.InputEventResponse> { |
65 | | - this.authenticateAnalytics(event.apiKey); |
| 75 | + await this.authenticateAnalytics( |
| 76 | + Number(event.projectId), |
| 77 | + event.environment, |
| 78 | + ); |
66 | 79 |
|
67 | 80 | if ( |
68 | 81 | !event || |
@@ -106,7 +119,10 @@ export class AnalyticsService { |
106 | 119 | async logVisitEvent( |
107 | 120 | event: AnalyticsProto.VisitEventRequest, |
108 | 121 | ): Promise<AnalyticsProto.VisitEventResponse> { |
109 | | - this.authenticateAnalytics(event.apiKey); |
| 122 | + await this.authenticateAnalytics( |
| 123 | + Number(event.projectId), |
| 124 | + event.environment, |
| 125 | + ); |
110 | 126 |
|
111 | 127 | if ( |
112 | 128 | !event || |
@@ -146,7 +162,10 @@ export class AnalyticsService { |
146 | 162 | async logCustomEvent( |
147 | 163 | event: AnalyticsProto.CustomEventRequest, |
148 | 164 | ): Promise<AnalyticsProto.CustomEventResponse> { |
149 | | - this.authenticateAnalytics(event.apiKey); |
| 165 | + await this.authenticateAnalytics( |
| 166 | + Number(event.projectId), |
| 167 | + event.environment, |
| 168 | + ); |
150 | 169 |
|
151 | 170 | if ( |
152 | 171 | !event || |
|
0 commit comments