Skip to content

Commit eeaf61e

Browse files
Merge pull request #97 from Amruth-Vamshi/feature/rajai-otp-service
2 parents 1e37cdd + 4edcf17 commit eeaf61e

File tree

4 files changed

+230
-2
lines changed

4 files changed

+230
-2
lines changed

.env.sample

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@ CDAC_OTP_TEMPLATE_ID="123456"
1313
CDAC_OTP_TEMPLATE="Respected User, The OTP to reset password for %phone% is %code%."
1414

1515
# SMS Adapter
16-
SMS_ADAPTER_TYPE= # CDAC or GUPSHUP
16+
SMS_ADAPTER_TYPE= # CDAC or GUPSHUP or RAJAI
1717
SMS_TOTP_SECRET= # any random string, needed for CDAC
1818
SMS_TOTP_EXPIRY=600 # in seconds, needed for CDAC
1919

20+
#RAJAI OTP Service
21+
RAJAI_USERNAME=
22+
RAJAI_PASSWORD=
23+
RAJAI_BASEURL=
24+
2025
# Fusionauth
2126
FUSIONAUTH_APPLICATION_ID="f0ddb3f6-091b-45e4-8c0f-889f89d4f5da"
2227
FUSIONAUTH_SAMARTH_HP_APPLICATION_ID=f18c3f6f-45b8-4928-b978-a9906fd03f22

src/api/api.module.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { GupshupService } from './sms/gupshup/gupshup.service';
1111
import { SmsService } from './sms/sms.service';
1212
import got from 'got/dist/source';
1313
import { CdacService } from './sms/cdac/cdac.service';
14+
import { RajaiOtpService } from '../user/sms/rajaiOtpService/rajaiOtpService.service';
1415

1516
const otpServiceFactory = {
1617
provide: OtpService,
@@ -24,7 +25,21 @@ const otpServiceFactory = {
2425
},
2526
inject: [],
2627
}.useFactory();
27-
} else {
28+
} else if(config.get<string>('SMS_ADAPTER_TYPE') == 'RAJAI'){
29+
factory = {
30+
provide: 'OtpService',
31+
useFactory: (username, password, baseUrl)=>{
32+
return new RajaiOtpService(
33+
username,
34+
password,
35+
baseUrl,
36+
got
37+
);
38+
},
39+
inject: [],
40+
}.useFactory(config.get('RAJAI_USERNAME'), config.get('RAJAI_PASSWORD'), config.get('RAJAI_BASEURL'));
41+
}
42+
else {
2843
factory = {
2944
provide: 'OtpService',
3045
useFactory: (username, password, baseUrl) => {
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
import {
2+
OTPResponse,
3+
SMS,
4+
SMSData,
5+
SMSError,
6+
SMSProvider,
7+
SMSResponse,
8+
SMSResponseStatus,
9+
SMSType,
10+
TrackResponse,
11+
} from '../sms.interface';
12+
13+
import { Injectable } from '@nestjs/common';
14+
import { SmsService } from '../sms.service';
15+
import { Got } from 'got/dist/source';
16+
import { json } from 'stream/consumers';
17+
18+
@Injectable()
19+
export class RajaiOtpService extends SmsService implements SMS {
20+
21+
otpApiConstants: any = {
22+
srvnm: "ChatbotAPIs",
23+
srvmethodnm: ""
24+
};
25+
26+
auth: any = {
27+
usrnm: '',
28+
psw: '',
29+
};
30+
31+
httpClient: Got;
32+
33+
baseURL: string;
34+
path = '';
35+
data: SMSData;
36+
37+
constructor(username: string, password: string, baseURL: string, got: Got) {
38+
super();
39+
this.auth.usrnm = username;
40+
this.auth.psw = password;
41+
this.baseURL = baseURL;
42+
this.httpClient = got;
43+
}
44+
45+
send(data: SMSData): Promise<SMSResponse> {
46+
if (!data) {
47+
throw new Error('Data cannot be null');
48+
}
49+
this.data = data;
50+
if (this.data.type === SMSType.otp) return this.doOTPRequest(data);
51+
else return this.doRequest();
52+
}
53+
54+
doRequest(): Promise<SMSResponse> {
55+
throw new Error('Method not implemented.');
56+
}
57+
58+
track(data: SMSData): Promise<SMSResponse> {
59+
if (!data) {
60+
throw new Error('Data cannot be null');
61+
}
62+
this.data = data;
63+
if (this.data.type === SMSType.otp) return this.verifyOTP(data);
64+
else return this.doRequest();
65+
}
66+
67+
private doOTPRequest(data: SMSData): Promise<OTPResponse> {
68+
this.otpApiConstants.srvmethodnm = 'ChatBotGenerateOtpMobile'
69+
const body: any = {
70+
obj: {
71+
...this.otpApiConstants,
72+
...this.auth,
73+
mobileNo: data.phone,
74+
}
75+
}
76+
const options = {
77+
headers: {
78+
'Content-Type': 'application/json'
79+
},
80+
json: body
81+
};
82+
console.log(options)
83+
const url = this.baseURL + '' + this.path;
84+
console.log(url)
85+
const status: OTPResponse = {} as OTPResponse;
86+
status.provider = SMSProvider.rajai;
87+
status.phone = data.phone;
88+
89+
return this.httpClient
90+
.post(url,options)
91+
.then((response): OTPResponse => {
92+
status.networkResponseCode = 200;
93+
const r = this.parseResponse(response.body);
94+
console.log("otp response", r);
95+
status.messageID = r.messageID;
96+
status.error = r.error;
97+
status.providerResponseCode = r.providerResponseCode;
98+
status.providerSuccessResponse = r.providerSuccessResponse;
99+
status.status = r.status;
100+
return status;
101+
})
102+
.catch((e: Error): OTPResponse => {
103+
console.log("otp response error", e);
104+
const error: SMSError = {
105+
errorText: `Uncaught Exception :: ${e.message}`,
106+
errorCode: 'CUSTOM ERROR',
107+
};
108+
status.networkResponseCode = 200;
109+
status.messageID = null;
110+
status.error = error;
111+
status.providerResponseCode = null;
112+
status.providerSuccessResponse = null;
113+
status.status = SMSResponseStatus.failure;
114+
return status;
115+
});
116+
}
117+
118+
verifyOTP(data: SMSData): Promise<TrackResponse> {
119+
this.otpApiConstants.srvmethodnm = 'ChatBotVerifyOtpMobile'
120+
console.log({ data });
121+
const body: any = {
122+
obj: {
123+
...this.otpApiConstants,
124+
...this.auth,
125+
mobileNo: data.phone,
126+
otp: data.params.otp
127+
}
128+
}
129+
const options = {
130+
headers: {
131+
'Content-Type': 'application/json'
132+
},
133+
json: body
134+
};
135+
const url = this.baseURL + '' + this.path;
136+
const status: TrackResponse = {} as TrackResponse;
137+
status.provider = SMSProvider.rajai;
138+
status.phone = data.phone;
139+
140+
return this.httpClient
141+
.post(url, options)
142+
.then((response): OTPResponse => {
143+
console.log(response.body);
144+
status.networkResponseCode = 200;
145+
const r = this.parseResponse(response.body);
146+
status.messageID = r.messageID;
147+
status.error = r.error;
148+
status.providerResponseCode = r.providerResponseCode;
149+
status.providerSuccessResponse = r.providerSuccessResponse;
150+
status.status = r.status;
151+
return status;
152+
})
153+
.catch((e: Error): OTPResponse => {
154+
const error: SMSError = {
155+
errorText: `Uncaught Exception :: ${e.message}`,
156+
errorCode: 'CUSTOM ERROR',
157+
};
158+
status.networkResponseCode = 200;
159+
status.messageID = null;
160+
status.error = error;
161+
status.providerResponseCode = null;
162+
status.providerSuccessResponse = null;
163+
status.status = SMSResponseStatus.failure;
164+
return status;
165+
});
166+
}
167+
168+
parseResponse(response: string): any{
169+
try {
170+
const responseData = JSON.parse(response);
171+
if (responseData[0]["status"] === '0' || responseData[0]["message"] === 'OTP Verify Successfully' ) {
172+
return {
173+
providerResponseCode: null,
174+
status: SMSResponseStatus.success,
175+
messageID: responseData[0]["data"],
176+
error: null,
177+
providerSuccessResponse: responseData[0]["message"],
178+
};
179+
} else {
180+
const error: SMSError = {
181+
errorText: responseData[0]["message"],
182+
errorCode: responseData[0]["status"],
183+
};
184+
return {
185+
providerResponseCode: responseData[0]["message"],
186+
status: SMSResponseStatus.failure,
187+
messageID: null,
188+
error,
189+
providerSuccessResponse: null,
190+
};
191+
}
192+
} catch (e) {
193+
const error: SMSError = {
194+
errorText: `Gupshup response could not be parsed :: ${e.message}; Provider Response - ${response}`,
195+
errorCode: 'CUSTOM ERROR',
196+
};
197+
return {
198+
providerResponseCode: null,
199+
status: SMSResponseStatus.failure,
200+
messageID: null,
201+
error,
202+
providerSuccessResponse: null,
203+
};
204+
}
205+
}
206+
}
207+

src/user/sms/sms.interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export enum SMSResponseStatus {
1818
export enum SMSProvider {
1919
gupshup = 'Gupshup',
2020
cdac = 'CDAC',
21+
rajai = 'Rajai'
2122
}
2223

2324
export type SMSError = {

0 commit comments

Comments
 (0)