11import { ServiceSchema } from 'moleculer' ;
22import Stripe from 'stripe' ;
3- import { resultErr , resultOk } from '@event-inc/utils' ;
3+ import { resultErr , resultOk , makeHttpNetworkCall } from '@event-inc/utils' ;
4+
5+ const TRACK_EVENT = `${ process . env . CONNECTIONS_API_BASE_URL } v1/public/mark` ;
6+
7+ const context = {
8+ locale : "" ,
9+ page : {
10+ path : "" ,
11+ search : "" ,
12+ title : "" ,
13+ url : ""
14+ } ,
15+ userAgent : "" ,
16+ }
417
518export default {
619 name : 'stripe-webhook' ,
@@ -65,15 +78,22 @@ export default {
6578 billing
6679 } ) ;
6780
68- await ctx . broker . call ( 'v1.tracking.public.track' , {
69- path : 't' ,
81+ await makeHttpNetworkCall ( {
82+ url : TRACK_EVENT ,
83+ method : 'POST' ,
7084 data : {
71- event : 'Created Subscription' ,
72- properties : subscriptionCreated ,
73- userId : client ?. author ?. _id
85+ path : 't' ,
86+ data : {
87+ event : 'Created Subscription' ,
88+ properties : {
89+ ...subscriptionCreated ,
90+ version : "pica-1.0.0"
91+ } ,
92+ context,
93+ userId : client ?. author ?. _id
94+ }
7495 }
7596 } ) ;
76-
7797 }
7898
7999 break ;
@@ -103,13 +123,21 @@ export default {
103123 }
104124 ) ;
105125
106- await ctx . broker . call ( 'v1.tracking.public.track' , {
107- path : 't' ,
126+ await makeHttpNetworkCall ( {
127+ url : TRACK_EVENT ,
128+ method : 'POST' ,
108129 data : {
109- event : 'Updated Subscription' ,
110- properties : subscriptionUpdated ,
111- userId : updatedClient ?. author ?. _id ,
112- } ,
130+ path : 't' ,
131+ data : {
132+ event : 'Updated Subscription' ,
133+ properties : {
134+ ...subscriptionUpdated ,
135+ version : "pica-1.0.0"
136+ } ,
137+ context,
138+ userId : updatedClient ?. author ?. _id ,
139+ }
140+ }
113141 } ) ;
114142
115143 break ;
@@ -151,14 +179,22 @@ export default {
151179 billing : updatedBilling ,
152180 }
153181 ) ;
154-
155- await ctx . broker . call ( 'v1.tracking.public.track' , {
156- path : 't' ,
182+
183+ await makeHttpNetworkCall ( {
184+ url : TRACK_EVENT ,
185+ method : 'POST' ,
157186 data : {
158- event : 'Created Subscription' ,
159- properties : subscriptionCreated ,
160- userId : client ?. author ?. _id ,
161- } ,
187+ path : 't' ,
188+ data : {
189+ event : 'Created Subscription' ,
190+ properties : {
191+ ...subscriptionCreated ,
192+ version : "pica-1.0.0"
193+ } ,
194+ context,
195+ userId : client ?. author ?. _id ,
196+ }
197+ }
162198 } ) ;
163199 }
164200
@@ -169,15 +205,23 @@ export default {
169205 }
170206 ) ;
171207
172- await ctx . broker . call ( 'v1.tracking.public.track' , {
173- path : 't' ,
208+ await makeHttpNetworkCall ( {
209+ url : TRACK_EVENT ,
210+ method : 'POST' ,
174211 data : {
175- event : 'Deleted Subscription' ,
176- properties : subscriptionDeleted ,
177- userId : client ?. author ?. _id ,
178- } ,
212+ path : 't' ,
213+ data : {
214+ event : 'Deleted Subscription' ,
215+ properties : {
216+ ...subscriptionDeleted ,
217+ version : "pica-1.0.0"
218+ } ,
219+ context,
220+ userId : client ?. author ?. _id ,
221+ }
222+ }
179223 } ) ;
180-
224+
181225 break ;
182226
183227 case 'invoice.payment_failed' :
@@ -200,16 +244,24 @@ export default {
200244 }
201245 ) ;
202246
203- await ctx . broker . call ( 'v1.tracking.public.track' , {
204- path : 't' ,
247+ await makeHttpNetworkCall ( {
248+ url : TRACK_EVENT ,
249+ method : 'POST' ,
205250 data : {
206- event : 'Failed Invoice Payment' ,
207- properties : invoicePaymentFailed ,
208- userId : currentClient ?. author ?. _id ,
209- } ,
251+ path : 't' ,
252+ data : {
253+ event : 'Failed Invoice Payment' ,
254+ properties : {
255+ ...invoicePaymentFailed ,
256+ version : "pica-1.0.0"
257+ } ,
258+ context,
259+ userId : currentClient ?. author ?. _id ,
260+ }
261+ }
210262 } ) ;
211- break ;
212263
264+ break ;
213265 case 'invoice.payment_succeeded' :
214266 const invoicePaymentSucceeded = event . data . object ;
215267
@@ -225,13 +277,21 @@ export default {
225277 }
226278 ) ;
227279
228- await ctx . broker . call ( 'v1.tracking.public.track' , {
229- path : 't' ,
280+ await makeHttpNetworkCall ( {
281+ url : TRACK_EVENT ,
282+ method : 'POST' ,
230283 data : {
231- event : 'Successful Invoice Payment' ,
232- properties : invoicePaymentSucceeded ,
233- userId : succeededInvoiceClient ?. author ?. _id ,
234- } ,
284+ path : 't' ,
285+ data : {
286+ event : 'Successful Invoice Payment' ,
287+ properties : {
288+ ...invoicePaymentSucceeded ,
289+ version : "pica-1.0.0"
290+ } ,
291+ context,
292+ userId : succeededInvoiceClient ?. author ?. _id ,
293+ }
294+ }
235295 } ) ;
236296
237297 break ;
0 commit comments