Skip to content

Commit fa36170

Browse files
committed
added telemetry for OTP sent event
1 parent b9c0e89 commit fa36170

File tree

4 files changed

+109
-6
lines changed

4 files changed

+109
-6
lines changed

src/api/api.controller.ts

+26-5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import { v4 as uuidv4 } from 'uuid';
4040
import { VerifyJWTDto } from './dto/verify-jwt.dto';
4141
import { Request } from 'express';
4242
import { GupshupWhatsappService } from './sms/gupshupWhatsapp/gupshupWhatsapp.service';
43+
import { TelemetryService } from 'src/telemetry/telemetry.service';
4344
// eslint-disable-next-line @typescript-eslint/no-var-requires
4445
const CryptoJS = require('crypto-js');
4546

@@ -54,7 +55,8 @@ export class ApiController {
5455
private readonly otpService: OtpService,
5556
private readonly apiService: ApiService,
5657
private readonly configResolverService: ConfigResolverService,
57-
private readonly gupshupWhatsappService: GupshupWhatsappService
58+
private readonly gupshupWhatsappService: GupshupWhatsappService,
59+
private readonly telemetryService: TelemetryService
5860
) {}
5961

6062
@Get()
@@ -72,6 +74,7 @@ export class ApiController {
7274
@Query() params: SendOtpDto,
7375
@Headers('x-application-id') applicationId?,
7476
): Promise<any> {
77+
let startTime = Date.now();
7578
if (applicationId) {
7679
const { total }: { total: number; users: Array<User> } =
7780
await this.fusionAuthService.getUsersByString(
@@ -93,21 +96,39 @@ export class ApiController {
9396
);
9497
}
9598
}
99+
100+
let status: any, isWhatsApp = false;
96101
// Check if phone number contains country code (e.g. 91-1234567890)
97102
if (params.phone.includes('-')) {
103+
isWhatsApp = true;
98104
const [countryCode, number] = params.phone.split('-');
99105
params.phone = number;
100-
const status: any = await this.gupshupWhatsappService.sendWhatsappOTP({
106+
status = await this.gupshupWhatsappService.sendWhatsappOTP({
101107
phone: number,
102108
template: null,
103109
type: null,
104110
params: null
105111
});
106-
return { status };
107112
} else {
108-
const status: any = await this.otpService.sendOTP(params.phone);
109-
return { status };
113+
status = await this.otpService.sendOTP(params.phone);
114+
}
115+
116+
if(this.configService.get('TELEMETRY_INTERNAL_BASE_URL')) {
117+
this.telemetryService.sendEvent(
118+
{
119+
botId: params.botId,
120+
orgId: params.orgId,
121+
timeTaken: Date.now() - startTime,
122+
createdAt: Math.floor(new Date().getTime() / 1000),
123+
phoneNumber: params.phone
124+
},
125+
'E117',
126+
'Send OTP',
127+
'sendOTP',
128+
isWhatsApp ? 'Whatsapp' : 'PWA'
129+
)
110130
}
131+
return { status };
111132
}
112133

113134
@Get('verifyOTP')

src/api/api.module.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import got from 'got/dist/source';
1313
import { CdacService } from './sms/cdac/cdac.service';
1414
import { RajaiOtpService } from '../user/sms/rajaiOtpService/rajaiOtpService.service';
1515
import { GupshupWhatsappService } from './sms/gupshupWhatsapp/gupshupWhatsapp.service';
16+
import { TelemetryService } from 'src/telemetry/telemetry.service';
1617

1718
const otpServiceFactory = {
1819
provide: OtpService,
@@ -69,7 +70,8 @@ const otpServiceFactory = {
6970
otpServiceFactory,
7071
QueryGeneratorService,
7172
ConfigResolverService,
72-
GupshupWhatsappService
73+
GupshupWhatsappService,
74+
TelemetryService
7375
],
7476
})
7577
export class ApiModule {

src/api/dto/send-otp.dto.ts

+8
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,12 @@ export class SendOtpDto {
1111
@IsString()
1212
@IsOptional()
1313
errorMessage?: string;
14+
15+
@IsString()
16+
@IsOptional()
17+
botId?: string;
18+
19+
@IsString()
20+
@IsOptional()
21+
orgId?: string;
1422
}

src/telemetry/telemetry.service.ts

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { HttpException, HttpStatus, Injectable, Logger } from '@nestjs/common';
2+
import { ConfigService } from '@nestjs/config';
3+
4+
@Injectable()
5+
export class TelemetryService {
6+
private readonly logger = new Logger(TelemetryService.name);
7+
constructor(
8+
private readonly configService: ConfigService
9+
) {}
10+
11+
async sendEvent(
12+
eventData: any,
13+
eventId: string,
14+
event: string,
15+
subEvent: string,
16+
generator?: string,
17+
userId?: string,
18+
): Promise<any> {
19+
const myHeaders = new Headers();
20+
myHeaders.append('Content-Type', 'application/json');
21+
22+
console.log([
23+
{
24+
generator,
25+
version: '0.0.1',
26+
timestamp: Math.floor(new Date().getTime() / 1000),
27+
actorId: userId,
28+
actorType: 'User',
29+
env: this.configService.get('ENVIRONMENT'),
30+
eventId,
31+
event,
32+
subEvent,
33+
eventData,
34+
},
35+
]);
36+
37+
const raw = JSON.stringify([
38+
{
39+
generator,
40+
version: '0.0.1',
41+
timestamp: Math.floor(new Date().getTime() / 1000),
42+
actorId: userId,
43+
actorType: 'User',
44+
env: this.configService.get('ENVIRONMENT'),
45+
eventId,
46+
event,
47+
subEvent,
48+
eventData,
49+
},
50+
]);
51+
52+
const requestOptions: any = {
53+
method: 'POST',
54+
headers: myHeaders,
55+
body: raw,
56+
redirect: 'follow',
57+
retry: 1,
58+
};
59+
60+
try {
61+
const response = await fetch(
62+
`${this.configService.get('TELEMETRY_INTERNAL_BASE_URL')}/metrics/v1/save`,
63+
requestOptions,
64+
);
65+
this.logger.verbose(`Sucessfully sent ${subEvent} event`);
66+
return response.body;
67+
} catch (error) {
68+
this.logger.error(`Failed to send ${subEvent} event.`);
69+
throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)