Skip to content

Commit f8ec7c0

Browse files
committed
Formatting issues + WA OTP Fix
1 parent 2156d3d commit f8ec7c0

File tree

5 files changed

+422
-359
lines changed

5 files changed

+422
-359
lines changed

src/api/api.controller.ts

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
UserRegistration,
2323
UsersResponse,
2424
ResponseCode,
25-
ResponseStatus
25+
ResponseStatus,
2626
} from './api.interface';
2727
import { ApiService } from './api.service';
2828
import { ConfigResolverService } from './config.resolver.service';
@@ -36,7 +36,7 @@ import * as Sentry from '@sentry/node';
3636
import { LoginDto, LoginWithUniqueIdDto } from './dto/login.dto';
3737
import { SendOtpDto } from './dto/send-otp.dto';
3838
import { VerifyOtpDto } from './dto/verify-otp.dto';
39-
import { Throttle, SkipThrottle} from '@nestjs/throttler';
39+
import { Throttle, SkipThrottle } from '@nestjs/throttler';
4040
import { ConfigService } from '@nestjs/config';
4141
import { v4 as uuidv4 } from 'uuid';
4242
import { VerifyJWTDto } from './dto/verify-jwt.dto';
@@ -61,7 +61,7 @@ export class ApiController {
6161
private readonly configResolverService: ConfigResolverService,
6262
private readonly gupshupWhatsappService: GupshupWhatsappService,
6363
private readonly telemetryService: TelemetryService,
64-
@InjectRedis() private readonly redis: Redis
64+
@InjectRedis() private readonly redis: Redis,
6565
) {}
6666

6767
@Get()
@@ -79,9 +79,12 @@ export class ApiController {
7979
@Query() params: SendOtpDto,
8080
@Headers('x-application-id') applicationId?,
8181
): Promise<any> {
82-
let startTime = Date.now();
82+
const startTime = Date.now();
8383

84-
let status: any, isWhatsApp = false, countryCode, number;
84+
let status: any,
85+
isWhatsApp = false,
86+
countryCode,
87+
number;
8588

8689
if (params.phone.includes('-')) {
8790
[countryCode, number] = params.phone.split('-');
@@ -111,35 +114,38 @@ export class ApiController {
111114
}
112115
}
113116

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') {
116119
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+
);
124130
} else {
125131
status = await this.otpService.sendOTP(params.phone);
126132
}
127133

128-
if(this.configService.get('TELEMETRY_INTERNAL_BASE_URL')) {
134+
if (this.configService.get('TELEMETRY_INTERNAL_BASE_URL')) {
129135
this.telemetryService.sendEvent(
130136
{
131137
botId: params.botId,
132138
orgId: params.orgId,
133-
timeTaken: Date.now() - startTime,
139+
timeTaken: Date.now() - startTime,
134140
createdAt: Math.floor(new Date().getTime() / 1000),
135141
phoneNumber: params.phone,
136-
eventLog: `Response from OTP provider - ${status.providerSuccessResponse}`
142+
eventLog: `Response from OTP provider - ${status.providerSuccessResponse}`,
137143
},
138144
'E117',
139145
'Send OTP',
140146
'sendOTP',
141-
isWhatsApp ? 'Whatsapp' : 'PWA'
142-
)
147+
isWhatsApp ? 'Whatsapp' : 'PWA',
148+
);
143149
}
144150
return { status };
145151
}
@@ -422,9 +428,9 @@ export class ApiController {
422428
async loginWithUniqueId(
423429
@Body() user: LoginWithUniqueIdDto,
424430
@Headers('authorization') authHeader,
425-
@Headers('ADMIN-API-KEY') adminApiKey
431+
@Headers('ADMIN-API-KEY') adminApiKey,
426432
): Promise<any> {
427-
if(adminApiKey!=this.configService.get('ADMIN_API_KEY')){
433+
if (adminApiKey != this.configService.get('ADMIN_API_KEY')) {
428434
const response: SignupResponse = new SignupResponse().init(uuidv4());
429435
response.responseCode = ResponseCode.FAILURE;
430436
response.params.err = 'UNAUTHORIZED';
@@ -434,20 +440,16 @@ export class ApiController {
434440
}
435441
return await this.apiService.loginWithUniqueId(user, authHeader);
436442
}
437-
443+
438444
@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> {
443447
return await this.apiService.verifyJWT(body.token);
444448
}
445449

446450
@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> {
451453
return await this.apiService.logout(body.token);
452454
}
453455

@@ -469,14 +471,14 @@ export class ApiController {
469471
const url = new URL(`${fusionAuthBaseUrl}/api/user/search`);
470472
// Add query params to URL
471473
if (query) {
472-
Object.keys(query).forEach(key => {
474+
Object.keys(query).forEach((key) => {
473475
url.searchParams.append(key, query[key]);
474476
});
475477
}
476478

477479
// Add params to URL
478480
if (params) {
479-
Object.keys(params).forEach(key => {
481+
Object.keys(params).forEach((key) => {
480482
url.searchParams.append(key, params[key]);
481483
});
482484
}
@@ -512,27 +514,32 @@ export class ApiController {
512514
return JSON.parse(cachedData);
513515
}
514516

515-
const userData = await this.fetchUserDataFromService(id, authorization, appId);
517+
const userData = await this.fetchUserDataFromService(
518+
id,
519+
authorization,
520+
appId,
521+
);
516522

517523
await this.redis.set(cacheKey, JSON.stringify(userData));
518524
return userData;
519525
}
520526

521-
private async fetchUserDataFromService(id: string, authorization: string, appId: string) {
527+
private async fetchUserDataFromService(
528+
id: string,
529+
authorization: string,
530+
appId: string,
531+
) {
522532
try {
523533
const fusionAuthBaseUrl = this.configService.get('FUSIONAUTH_BASE_URL');
524534
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,
534541
},
535-
);
542+
});
536543
return response.json();
537544
} catch (error) {
538545
throw new HttpException(
@@ -542,22 +549,15 @@ export class ApiController {
542549
}
543550
}
544551

545-
private async searchUserData(
546-
url: URL,
547-
authorization: string,
548-
appId: string,
549-
) {
552+
private async searchUserData(url: URL, authorization: string, appId: string) {
550553
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,
559559
},
560-
);
560+
});
561561
return response.json();
562562
} catch (error) {
563563
throw new HttpException(
@@ -578,17 +578,17 @@ export class ApiController {
578578
): Promise<any> {
579579
const fusionAuthBaseUrl = this.configService.get('FUSIONAUTH_BASE_URL');
580580
const url = new URL(`${fusionAuthBaseUrl}${request.url}`);
581-
581+
582582
// Add query params to URL
583583
if (query) {
584-
Object.keys(query).forEach(key => {
584+
Object.keys(query).forEach((key) => {
585585
url.searchParams.append(key, query[key]);
586586
});
587587
}
588588

589589
// Add params to URL
590590
if (params) {
591-
Object.keys(params).forEach(key => {
591+
Object.keys(params).forEach((key) => {
592592
url.searchParams.append(key, params[key]);
593593
});
594594
}
@@ -597,10 +597,10 @@ export class ApiController {
597597
method: request.method,
598598
body: Object.keys(body).length ? JSON.stringify(body) : undefined,
599599
headers: {
600-
'Authorization': authHeader,
600+
Authorization: authHeader,
601601
'x-application-id': applicationId,
602-
'Content-Type': 'application/json'
603-
}
602+
'Content-Type': 'application/json',
603+
},
604604
});
605605

606606
return await response.json();

0 commit comments

Comments
 (0)