@@ -11,7 +11,7 @@ let SendEmailBodyInvite = emailTemp.sendInviteemail;
1111let SendEmailBodyDecline = emailTemp . sendDeclineemail ;
1212
1313let domainKey = process . env . domainKey ;
14- let baseUrl = 'http ://api.' + domainKey ;
14+ let baseUrl = 'https ://api.' + domainKey ;
1515
1616let schemaName = {
1717 'properties' : {
@@ -65,7 +65,7 @@ class Service {
6565 return new Promise ( ( resolve , reject ) => {
6666 axios . post ( baseUrl + '/auth/api/userdetailsbyemail' , {
6767 'email' : data . toEmail
68- } ) . then ( async ( ( res ) => {
68+ } ) . then ( ( res ) => {
6969 userId = res . data . data [ 0 ] . _id ;
7070 previous_packages = res . data . data [ 0 ] . package ;
7171 if ( previous_packages == undefined || previous_packages . length == 0 ) {
@@ -95,98 +95,109 @@ class Service {
9595 // previous_packages[subscriptionId].role = _.omit(previous_packages[subscriptionId].role, Role1);
9696 }
9797 /* eslint-disable no-undef */
98- axios . put ( baseUrl + '/user/updateuserdetails/' + userId , { package : previous_packages } , { headers : { 'Authorization' : apiHeaders . authorization } } ) . then ( async ( ( result ) => {
99- if ( result . data . code == 201 ) {
100- let subscription_invite = await ( self . subscription_invitation ( data , res ) ) ;
101- self . sendEmail ( data , res ) ;
102- }
103- resolve ( result . data ) ;
104- } ) . catch ( ( err ) => {
105- let errorObj = { } ;
106- if ( apiHeaders . authorization == undefined ) {
107- errorObj . statusText = 'missing Authorization header' ;
108- errorObj . status = 404 ;
109- errorObj . data = '\'Auth token is required in header\'' ;
110- resolve ( errorObj ) ;
111- } else {
112- errorObj . statusText = err . response . statusText ;
113- errorObj . status = err . response . status ;
114- errorObj . data = err . response . data ;
115- resolve ( errorObj ) ;
116- }
117- /* eslint-disable no-undef */
118- } ) ) ;
98+ if ( ! params . headers || ! params . headers . authorization ) {
99+ let errorObj = {
100+ statusText : 'missing Authorization header' ,
101+ status : 404 ,
102+ data : '\'Auth token is required in header\''
103+ } ;
104+ resolve ( errorObj ) ;
105+ }
106+ return self . updateuserdetails ( userId , previous_packages , params ) . then ( r => {
107+ return { res :res , userDetail :r } ;
108+ } ) ;
109+ } ) . then ( async ( ( result ) => {
110+ let userDetailResult = result . userDetail ;
111+ let res = result . res ;
112+ if ( userDetailResult . data . code == 201 ) {
113+ let subscription_invite = await ( self . subscription_invitation ( data , res , params ) ) ;
114+ self . sendEmail ( data , res , params . headers . authorization ) ;
115+ }
116+ resolve ( userDetailResult . data ) ;
119117 } ) ) . catch ( ( err ) => {
120118 let errorObj = { } ;
121- errorObj . statusText = 'Not Found' ;
122- errorObj . status = 404 ;
123- errorObj . data = 'No data found with this email ID' ;
119+ if ( err . response . data === 'data not found!' ) {
120+ errorObj . statusText = 'Not Found' ;
121+ errorObj . status = 404 ;
122+ errorObj . data = 'No data found with this email ID' ;
123+ } else {
124+ errorObj . statusText = err . response . statusText ;
125+ errorObj . status = err . response . status ;
126+ errorObj . data = err . response . data ;
127+ }
124128 resolve ( errorObj ) ;
125129 } ) ;
126130 } ) ;
127-
128-
131+ }
132+
133+ updateuserdetails ( userId , previous_packages , params ) {
134+ return axios . put ( baseUrl + '/user/updateuserdetails/' + userId , { package : previous_packages } , { headers : { 'Authorization' : params . headers . authorization } } )
135+ . then ( ( ( result ) => {
136+ return result ;
137+ } ) ) . catch ( ( err ) => {
138+ let errorObj = { } ;
139+ errorObj . statusText = err . response . statusText ;
140+ errorObj . status = err . response . status ;
141+ errorObj . data = err . response . data ;
142+ throw ( errorObj ) ;
143+ /* eslint-disable no-undef */
144+ } ) ;
129145 }
130146
131- subscription_invitation ( data , res ) {
147+ subscription_invitation ( data , res , params ) {
132148 let self = this ;
133- this . app . service ( 'subscription-invitation' ) . find ( { query : { 'toEmail' : data . toEmail , 'subscriptionId' : data . subscriptionId , 'isDeleted' : false } } )
149+ // return axios.get(baseUrl+'/subscription/subscription-invitation', {query : { 'toEmail': data.toEmail, 'subscriptionId': data.subscriptionId, 'isDeleted': false }, headers: { 'Authorization': params.headers.authorization }})
150+ return axios ( {
151+ method : 'get' ,
152+ url : baseUrl + '/subscription/subscription-invitation?toEmail=' + data . toEmail + '&subscriptionId=' + data . subscriptionId + '&isDeleted=false' ,
153+ headers : { 'Authorization' : params . headers . authorization }
154+ } )
134155 . then ( function ( response ) {
135- if ( response . data . length == 0 ) {
136- self . app . service ( 'subscription-invitation' ) . create ( data ) . then ( function ( response ) {
156+ if ( response . data . data . length == 0 ) {
157+ return self . app . service ( 'subscription-invitation' ) . create ( data ) . then ( function ( response ) {
137158 } ) . catch ( function ( err ) {
138- return err ;
159+ throw ( err ) ;
139160 } ) ;
140161 } else {
141- // console.log(response.data);
142- response . data [ 0 ] . isDeleted = true ;
143- self . app . service ( 'subscription-invitation' ) . patch ( response . data [ 0 ] . id , response . data [ 0 ] , '' ) . then ( function ( response2 ) {
144- // console.log('response2-----' ,response2);
162+ response . data . data [ 0 ] . isDeleted = true ;
163+ return self . app . service ( 'subscription-invitation' ) . patch ( response . data . data [ 0 ] . id , response . data . data [ 0 ] , '' ) . then ( function ( response2 ) {
145164 self . app . service ( 'subscription-invitation' ) . create ( data ) . then ( function ( response ) {
146165 } ) . catch ( function ( err ) {
147- return err ;
166+ throw ( err ) ;
148167 } ) ;
149168 } ) . catch ( function ( err ) {
150- return err ;
169+ throw ( err ) ;
151170 } ) ;
152-
153171 }
154172 } ) . catch ( function ( err ) {
155- return err ;
173+ throw ( err ) ;
156174 } ) ;
157-
158-
159- // this.app.service("subscription-invitation").create(data).then(function (response){
160- // }).catch(function(err){
161- // return err
162- // })
163175 }
164-
165- sendEmail ( data , res ) {
176+
177+ sendEmail ( data , res , authToken ) {
166178 let SendEmailBody = SendEmailBodyInvite . replace ( / W r i t e S e n d e r N a m e H e r e / i, data . fromEmail ) ;
167179 SendEmailBody = SendEmailBody . replace ( / D O M A I N / g, 'https://www.dashboard.' + domainKey ) ;
168180 SendEmailBody = SendEmailBody . replace ( / S Y S T E M N A M E / g, Object . keys ( data . role ) [ 0 ] ) ;
169181 SendEmailBody = SendEmailBody . replace ( / R O L E / g, Object . values ( data . role ) [ 0 ] ) ;
170-
171182 axios ( { method : 'post' ,
172183 url : baseUrl + '/vmailmicro/sendEmail' ,
173- headers : { 'Authorization' : apiHeaders . authorization } ,
184+ headers : { 'Authorization' : authToken } ,
174185 data : { 'to' : data . toEmail , 'from' : data . fromEmail , 'subject' : 'Invitation from Flowz' , 'body' : SendEmailBody } } ) . then ( async ( ( result ) => {
175186 return true ;
176187 } ) ) . catch ( function ( err ) {
177188 return err ;
178189 } ) ;
179190 }
180191
181- sendDeclineEmail ( params , res ) {
192+ sendDeclineEmail ( params , res , authToken ) {
182193 var SendEmailBody = SendEmailBodyDecline . replace ( / W r i t e S e n d e r N a m e H e r e / i, params . query . fromEmail ) ;
183194 SendEmailBody = SendEmailBody . replace ( / D O M A I N / g, 'https://www.dashboard.' + domainKey ) ;
184195 SendEmailBody = SendEmailBody . replace ( / S Y S T E M N A M E / g, Object . keys ( params . query . role ) [ 0 ] ) ;
185196 SendEmailBody = SendEmailBody . replace ( / R O L E / g, Object . values ( params . query . role ) [ 0 ] ) ;
186197 axios ( {
187198 method : 'post' ,
188199 url : baseUrl + '/vmailmicro/sendEmail' ,
189- headers : { 'Authorization' : apiHeaders . authorization } ,
200+ headers : { 'Authorization' : authToken } ,
190201 data : { 'to' : params . query . toEmail , 'from' : params . query . fromEmail , 'subject' : 'Your role is now no longer with Flowz.' , 'body' : SendEmailBody }
191202 } ) . then ( async ( ( result ) => {
192203 return true ;
@@ -231,23 +242,26 @@ class Service {
231242 delete previous_packages [ subscriptionId ] ;
232243 }
233244 }
234- axios . put ( baseUrl + '/user/updateuserdetails/' + userId , { package : previous_packages } , { headers : { 'Authorization' : apiHeaders . authorization } } ) . then ( async ( ( result ) => {
245+
246+ if ( ! params . headers || ! params . headers . authorization ) {
247+ let errorObj = {
248+ statusText : 'missing Authorization header' ,
249+ status : 404 ,
250+ data : '\'Auth token is required in header\''
251+ } ;
252+ resolve ( errorObj ) ;
253+ }
254+
255+ axios . put ( baseUrl + '/user/updateuserdetails/' + userId , { package : previous_packages } , { headers : { 'Authorization' : params . headers . authorization } } ) . then ( async ( ( result ) => {
235256 let subscription_invite = await ( self . subscription_invitation_remove ( params , res ) ) ;
236- self . sendDeclineEmail ( params , res ) ;
257+ self . sendDeclineEmail ( params , res , params . headers . authorization ) ;
237258 resolve ( result . data ) ;
238259 } ) ) . catch ( function ( err ) {
239260 let errorObj = { } ;
240- if ( apiHeaders . authorization == undefined ) {
241- errorObj . statusText = 'missing Authorization header' ;
242- errorObj . status = 404 ;
243- errorObj . data = '\'Auth token is required in header\'' ;
244- resolve ( errorObj ) ;
245- } else {
246- errorObj . statusText = err . response . statusText ;
247- errorObj . status = err . response . status ;
248- errorObj . data = err . response . data ;
249- resolve ( errorObj ) ;
250- }
261+ errorObj . statusText = err . response . statusText ;
262+ errorObj . status = err . response . status ;
263+ errorObj . data = err . response . data ;
264+ resolve ( errorObj ) ;
251265 } ) ;
252266 } ) ) . catch ( function ( err ) {
253267 let errorObj = { } ;
0 commit comments