@@ -22,7 +22,7 @@ import {
22
22
UserRegistration ,
23
23
UsersResponse ,
24
24
ResponseCode ,
25
- ResponseStatus
25
+ ResponseStatus ,
26
26
} from './api.interface' ;
27
27
import { ApiService } from './api.service' ;
28
28
import { ConfigResolverService } from './config.resolver.service' ;
@@ -36,7 +36,7 @@ import * as Sentry from '@sentry/node';
36
36
import { LoginDto , LoginWithUniqueIdDto } from './dto/login.dto' ;
37
37
import { SendOtpDto } from './dto/send-otp.dto' ;
38
38
import { VerifyOtpDto } from './dto/verify-otp.dto' ;
39
- import { Throttle , SkipThrottle } from '@nestjs/throttler' ;
39
+ import { Throttle , SkipThrottle } from '@nestjs/throttler' ;
40
40
import { ConfigService } from '@nestjs/config' ;
41
41
import { v4 as uuidv4 } from 'uuid' ;
42
42
import { VerifyJWTDto } from './dto/verify-jwt.dto' ;
@@ -61,7 +61,7 @@ export class ApiController {
61
61
private readonly configResolverService : ConfigResolverService ,
62
62
private readonly gupshupWhatsappService : GupshupWhatsappService ,
63
63
private readonly telemetryService : TelemetryService ,
64
- @InjectRedis ( ) private readonly redis : Redis
64
+ @InjectRedis ( ) private readonly redis : Redis ,
65
65
) { }
66
66
67
67
@Get ( )
@@ -79,9 +79,12 @@ export class ApiController {
79
79
@Query ( ) params : SendOtpDto ,
80
80
@Headers ( 'x-application-id' ) applicationId ?,
81
81
) : Promise < any > {
82
- let startTime = Date . now ( ) ;
82
+ const startTime = Date . now ( ) ;
83
83
84
- let status : any , isWhatsApp = false , countryCode , number ;
84
+ let status : any ,
85
+ isWhatsApp = false ,
86
+ countryCode ,
87
+ number ;
85
88
86
89
if ( params . phone . includes ( '-' ) ) {
87
90
[ countryCode , number ] = params . phone . split ( '-' ) ;
@@ -111,35 +114,38 @@ export class ApiController {
111
114
}
112
115
}
113
116
114
- // Check if phone number contains country code (e.g. 91-1234567890)
115
- if ( params . deliveryType == 'WA' ) {
117
+ // Check if phone number contains country code (e.g. 91-1234567890)
118
+ if ( params . deliveryType == 'WA' ) {
116
119
isWhatsApp = true ;
117
-
118
- status = await this . gupshupWhatsappService . sendWhatsappOTP ( {
119
- phone : number ,
120
- template : null ,
121
- type : null ,
122
- params : null
123
- } , countryCode ) ;
120
+
121
+ status = await this . gupshupWhatsappService . sendWhatsappOTP (
122
+ {
123
+ phone : number ,
124
+ template : null ,
125
+ type : null ,
126
+ params : null ,
127
+ } ,
128
+ countryCode ,
129
+ ) ;
124
130
} else {
125
131
status = await this . otpService . sendOTP ( params . phone ) ;
126
132
}
127
133
128
- if ( this . configService . get ( 'TELEMETRY_INTERNAL_BASE_URL' ) ) {
134
+ if ( this . configService . get ( 'TELEMETRY_INTERNAL_BASE_URL' ) ) {
129
135
this . telemetryService . sendEvent (
130
136
{
131
137
botId : params . botId ,
132
138
orgId : params . orgId ,
133
- timeTaken : Date . now ( ) - startTime ,
139
+ timeTaken : Date . now ( ) - startTime ,
134
140
createdAt : Math . floor ( new Date ( ) . getTime ( ) / 1000 ) ,
135
141
phoneNumber : params . phone ,
136
- eventLog : `Response from OTP provider - ${ status . providerSuccessResponse } `
142
+ eventLog : `Response from OTP provider - ${ status . providerSuccessResponse } ` ,
137
143
} ,
138
144
'E117' ,
139
145
'Send OTP' ,
140
146
'sendOTP' ,
141
- isWhatsApp ? 'Whatsapp' : 'PWA'
142
- )
147
+ isWhatsApp ? 'Whatsapp' : 'PWA' ,
148
+ ) ;
143
149
}
144
150
return { status } ;
145
151
}
@@ -422,9 +428,9 @@ export class ApiController {
422
428
async loginWithUniqueId (
423
429
@Body ( ) user : LoginWithUniqueIdDto ,
424
430
@Headers ( 'authorization' ) authHeader ,
425
- @Headers ( 'ADMIN-API-KEY' ) adminApiKey
431
+ @Headers ( 'ADMIN-API-KEY' ) adminApiKey ,
426
432
) : Promise < any > {
427
- if ( adminApiKey != this . configService . get ( 'ADMIN_API_KEY' ) ) {
433
+ if ( adminApiKey != this . configService . get ( 'ADMIN_API_KEY' ) ) {
428
434
const response : SignupResponse = new SignupResponse ( ) . init ( uuidv4 ( ) ) ;
429
435
response . responseCode = ResponseCode . FAILURE ;
430
436
response . params . err = 'UNAUTHORIZED' ;
@@ -434,20 +440,16 @@ export class ApiController {
434
440
}
435
441
return await this . apiService . loginWithUniqueId ( user , authHeader ) ;
436
442
}
437
-
443
+
438
444
@Post ( 'jwt/verify' )
439
- @UsePipes ( new ValidationPipe ( { transform : true } ) )
440
- async jwtVerify (
441
- @Body ( ) body : VerifyJWTDto
442
- ) : Promise < any > {
445
+ @UsePipes ( new ValidationPipe ( { transform : true } ) )
446
+ async jwtVerify ( @Body ( ) body : VerifyJWTDto ) : Promise < any > {
443
447
return await this . apiService . verifyJWT ( body . token ) ;
444
448
}
445
449
446
450
@Post ( 'logout' )
447
- @UsePipes ( new ValidationPipe ( { transform : true } ) )
448
- async logout (
449
- @Body ( ) body : VerifyJWTDto
450
- ) : Promise < any > {
451
+ @UsePipes ( new ValidationPipe ( { transform : true } ) )
452
+ async logout ( @Body ( ) body : VerifyJWTDto ) : Promise < any > {
451
453
return await this . apiService . logout ( body . token ) ;
452
454
}
453
455
@@ -469,14 +471,14 @@ export class ApiController {
469
471
const url = new URL ( `${ fusionAuthBaseUrl } /api/user/search` ) ;
470
472
// Add query params to URL
471
473
if ( query ) {
472
- Object . keys ( query ) . forEach ( key => {
474
+ Object . keys ( query ) . forEach ( ( key ) => {
473
475
url . searchParams . append ( key , query [ key ] ) ;
474
476
} ) ;
475
477
}
476
478
477
479
// Add params to URL
478
480
if ( params ) {
479
- Object . keys ( params ) . forEach ( key => {
481
+ Object . keys ( params ) . forEach ( ( key ) => {
480
482
url . searchParams . append ( key , params [ key ] ) ;
481
483
} ) ;
482
484
}
@@ -512,27 +514,32 @@ export class ApiController {
512
514
return JSON . parse ( cachedData ) ;
513
515
}
514
516
515
- const userData = await this . fetchUserDataFromService ( id , authorization , appId ) ;
517
+ const userData = await this . fetchUserDataFromService (
518
+ id ,
519
+ authorization ,
520
+ appId ,
521
+ ) ;
516
522
517
523
await this . redis . set ( cacheKey , JSON . stringify ( userData ) ) ;
518
524
return userData ;
519
525
}
520
526
521
- private async fetchUserDataFromService ( id : string , authorization : string , appId : string ) {
527
+ private async fetchUserDataFromService (
528
+ id : string ,
529
+ authorization : string ,
530
+ appId : string ,
531
+ ) {
522
532
try {
523
533
const fusionAuthBaseUrl = this . configService . get ( 'FUSIONAUTH_BASE_URL' ) ;
524
534
const url = new URL ( `${ fusionAuthBaseUrl } /api/user/${ id } ` ) ;
525
- const response = await fetch (
526
- url ,
527
- {
528
- method : "GET" ,
529
- headers : {
530
- Authorization : authorization ,
531
- 'Content-Type' : 'application/json' ,
532
- 'X-FusionAuth-Application-Id' : appId ,
533
- } ,
535
+ const response = await fetch ( url , {
536
+ method : 'GET' ,
537
+ headers : {
538
+ Authorization : authorization ,
539
+ 'Content-Type' : 'application/json' ,
540
+ 'X-FusionAuth-Application-Id' : appId ,
534
541
} ,
535
- ) ;
542
+ } ) ;
536
543
return response . json ( ) ;
537
544
} catch ( error ) {
538
545
throw new HttpException (
@@ -542,22 +549,15 @@ export class ApiController {
542
549
}
543
550
}
544
551
545
- private async searchUserData (
546
- url : URL ,
547
- authorization : string ,
548
- appId : string ,
549
- ) {
552
+ private async searchUserData ( url : URL , authorization : string , appId : string ) {
550
553
try {
551
- const response = await fetch (
552
- url ,
553
- {
554
- headers : {
555
- Authorization : authorization ,
556
- 'Content-Type' : 'application/json' ,
557
- 'X-FusionAuth-Application-Id' : appId ,
558
- }
554
+ const response = await fetch ( url , {
555
+ headers : {
556
+ Authorization : authorization ,
557
+ 'Content-Type' : 'application/json' ,
558
+ 'X-FusionAuth-Application-Id' : appId ,
559
559
} ,
560
- ) ;
560
+ } ) ;
561
561
return response . json ( ) ;
562
562
} catch ( error ) {
563
563
throw new HttpException (
@@ -578,17 +578,17 @@ export class ApiController {
578
578
) : Promise < any > {
579
579
const fusionAuthBaseUrl = this . configService . get ( 'FUSIONAUTH_BASE_URL' ) ;
580
580
const url = new URL ( `${ fusionAuthBaseUrl } ${ request . url } ` ) ;
581
-
581
+
582
582
// Add query params to URL
583
583
if ( query ) {
584
- Object . keys ( query ) . forEach ( key => {
584
+ Object . keys ( query ) . forEach ( ( key ) => {
585
585
url . searchParams . append ( key , query [ key ] ) ;
586
586
} ) ;
587
587
}
588
588
589
589
// Add params to URL
590
590
if ( params ) {
591
- Object . keys ( params ) . forEach ( key => {
591
+ Object . keys ( params ) . forEach ( ( key ) => {
592
592
url . searchParams . append ( key , params [ key ] ) ;
593
593
} ) ;
594
594
}
@@ -597,10 +597,10 @@ export class ApiController {
597
597
method : request . method ,
598
598
body : Object . keys ( body ) . length ? JSON . stringify ( body ) : undefined ,
599
599
headers : {
600
- ' Authorization' : authHeader ,
600
+ Authorization : authHeader ,
601
601
'x-application-id' : applicationId ,
602
- 'Content-Type' : 'application/json'
603
- }
602
+ 'Content-Type' : 'application/json' ,
603
+ } ,
604
604
} ) ;
605
605
606
606
return await response . json ( ) ;
0 commit comments