11import Amplitude from '@amplitude/node' ;
2- import { Injectable } from '@nestjs/common' ;
2+ import { Injectable , OnModuleInit } from '@nestjs/common' ;
33import { InjectRepository } from '@nestjs/typeorm' ;
44import { Repository } from 'typeorm' ;
55import { AmplitudeEventTypeEnum } from '../../enums/index.js' ;
66import { UserEntity } from '../user/user.entity.js' ;
77
8+ export interface AmplitudeLogOptions {
9+ user_email ?: string ;
10+ tablesCount ?: number ;
11+ reason ?: string ;
12+ message ?: string ;
13+ operationCount ?: number ;
14+ }
15+
816@Injectable ( )
9- export class AmplitudeService {
17+ export class AmplitudeService implements OnModuleInit {
18+ private client : ReturnType < typeof Amplitude . init > ;
19+
1020 constructor (
1121 @InjectRepository ( UserEntity )
1222 private readonly userRepository : Repository < UserEntity > ,
1323 ) { }
1424
15- public async formAndSendLogRecord ( event_type : AmplitudeEventTypeEnum , user_id : string , options = null ) {
25+ public onModuleInit ( ) : void {
26+ if ( process . env . AMPLITUDE_API_KEY ) {
27+ this . client = Amplitude . init ( process . env . AMPLITUDE_API_KEY ) ;
28+ }
29+ }
30+
31+ public async formAndSendLogRecord (
32+ event_type : AmplitudeEventTypeEnum ,
33+ user_id : string ,
34+ options ?: AmplitudeLogOptions ,
35+ ) : Promise < void > {
1636 try {
1737 if ( process . env . NODE_ENV === 'test' ) return ;
1838 let user_email = ( await this . userRepository . findOne ( { where : { id : user_id } } ) ) ?. email ;
1939 if ( ! user_email && options ) {
20- user_email = options ? .user_email ;
40+ user_email = options . user_email ;
2141 }
22- let event_properties ;
42+ let event_properties : Record < string , unknown > | undefined ;
2343 if ( user_email ) {
2444 event_properties = {
2545 user_properties : {
26- email : user_email ? user_email : 'unknown' ,
27- tablesCount : options ?. tablesCount ? options . tablesCount : undefined ,
28- reason : options ?. reason ? options ?. reason : undefined ,
29- message : options ?. message ? options . message : undefined ,
46+ email : user_email ?? 'unknown' ,
47+ tablesCount : options ?. tablesCount ,
48+ reason : options ?. reason ,
49+ message : options ?. message ,
3050 } ,
3151 } ;
3252 }
33- if ( options ?. operationCount && options ? .operationCount > 0 ) {
53+ if ( options ?. operationCount && options . operationCount > 0 ) {
3454 const promisesArr = Array . from ( Array ( options . operationCount ) , ( ) =>
3555 this . sendLog ( event_type , user_id , event_properties ) ,
3656 ) ;
@@ -43,21 +63,19 @@ export class AmplitudeService {
4363 }
4464 }
4565
46- private async sendLog ( eventType , cognitoUserName , eventProperties ) {
47- const client = Amplitude . init ( process . env . AMPLITUDE_API_KEY ) ;
66+ private async sendLog (
67+ eventType : AmplitudeEventTypeEnum ,
68+ userId : string ,
69+ eventProperties ?: Record < string , unknown > ,
70+ ) : Promise < void > {
71+ if ( ! this . client ) return ;
4872 try {
49- client
50- . logEvent ( {
51- event_type : eventType ,
52- user_id : cognitoUserName ,
53- event_properties : eventProperties ? eventProperties : undefined ,
54- } )
55- . catch ( ( e ) => {
56- throw new Error ( e ) ;
57- } ) ;
58- client . flush ( ) . catch ( ( e ) => {
59- throw new Error ( e ) ;
73+ await this . client . logEvent ( {
74+ event_type : eventType ,
75+ user_id : userId ,
76+ event_properties : eventProperties ,
6077 } ) ;
78+ await this . client . flush ( ) ;
6179 } catch ( e ) {
6280 console . error ( e ) ;
6381 }
0 commit comments