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+
0 commit comments