88require 'base64'
99require 'date'
1010require 'json'
11+ require 'mx-platform-ruby'
12+ require 'posthog'
1113require 'sinatra'
1214require 'sinatra/cross_origin'
13- require 'mx-platform-ruby'
1415
1516set :port , ENV [ 'APP_PORT' ] || 8000
1617
3839api_client . default_headers [ 'Accept' ] = 'application/vnd.mx.api.v1+json'
3940mx_platform_api = ::MxPlatformRuby ::MxPlatformApi . new ( api_client )
4041
42+ posthog = PostHog ::Client . new ( {
43+ api_key : ENV [ 'POST_HOG_API_KEY' ] ,
44+ host : ENV [ 'POST_HOG_HOST' ] , # You can remove this line if you're using https://app.posthog.com
45+ on_error : proc { |_status , msg | print msg }
46+ } )
47+
4148# Checks the env file and production config if in production mode
4249test_config ( mx_platform_api )
4350
@@ -69,13 +76,11 @@ def create_user(user_id, mx_platform_api)
6976end
7077
7178delete '/api/user/:guid' do
72- begin
73- mx_platform_api . delete_user ( params [ :guid ] )
74- { :user_guid => params [ :guid ] } . to_json
75- rescue ::MxPlatformRuby ::ApiError => e
76- puts "Error when calling MxPlatformApi->delete_user: #{ e . message } "
77- [ 400 , e . response_body ]
78- end
79+ mx_platform_api . delete_user ( params [ :guid ] )
80+ { user_guid : params [ :guid ] } . to_json
81+ rescue ::MxPlatformRuby ::ApiError => e
82+ puts "Error when calling MxPlatformApi->delete_user: #{ e . message } "
83+ [ 400 , e . response_body ]
7984end
8085
8186post '/api/get_mxconnect_widget_url' do
@@ -87,6 +92,13 @@ def create_user(user_id, mx_platform_api)
8792
8893 # create user if no user_guid given
8994 user_guid = data [ 'user_guid' ] . nil? ? create_user ( external_id , mx_platform_api ) : data [ 'user_guid' ]
95+ posthog . capture ( {
96+ distinct_id : user_guid ,
97+ event : 'widget_request_api' ,
98+ properties : {
99+ test : '123'
100+ }
101+ } )
90102
91103 request_body = ::MxPlatformRuby ::WidgetRequestBody . new (
92104 widget_url : ::MxPlatformRuby ::WidgetRequest . new (
@@ -111,12 +123,23 @@ def create_user(user_id, mx_platform_api)
111123get '/users/:user_guid/members/:member_guid/verify' do
112124 content_type :json
113125 begin
126+ posthog . capture ( {
127+ distinct_id : params [ :user_guid ] ,
128+ event : 'begin verify job'
129+ } )
114130 # if widget was not in verification mode
115131 # mx_platform_api.verify_member(member_guid, user_guid)
116132 # poll member status answer MFAs
117133 response = mx_platform_api . list_account_numbers_by_member ( params [ :member_guid ] , params [ :user_guid ] )
118134 response . to_hash . to_json
119135 rescue ::MxPlatformRuby ::ApiError => e
136+ posthog . capture ( {
137+ distinct_id : params [ :user_guid ] ,
138+ event : 'verify failed' ,
139+ properties : {
140+ error_message : "Error when calling MxPlatformApi->list_account_numbers_by_member: #{ e . message } "
141+ }
142+ } )
120143 puts "Error when calling MxPlatformApi->list_account_numbers_by_member: #{ e . message } "
121144 [ 400 , e . response_body ]
122145 end
@@ -125,6 +148,10 @@ def create_user(user_id, mx_platform_api)
125148post '/users/:user_guid/members/:member_guid/identify' do
126149 content_type :json
127150 begin
151+ posthog . capture ( {
152+ distinct_id : params [ :user_guid ] ,
153+ event : 'begin identify job'
154+ } )
128155 response = mx_platform_api . identify_member (
129156 params [ :member_guid ] ,
130157 params [ :user_guid ]
@@ -143,6 +170,10 @@ def create_user(user_id, mx_platform_api)
143170 params [ :member_guid ] ,
144171 params [ :user_guid ]
145172 )
173+ posthog . capture ( {
174+ distinct_id : params [ :user_guid ] ,
175+ event : 'finish identify job' , properties : { response : response . to_hash }
176+ } )
146177 response . to_hash . to_json
147178 rescue ::MxPlatformRuby ::ApiError => e
148179 puts "Error when calling MxPlatformApi->list_account_owners_by_member: #{ e . message } "
@@ -154,6 +185,10 @@ def create_user(user_id, mx_platform_api)
154185 content_type :json
155186 begin
156187 response = mx_platform_api . list_user_accounts ( params [ :user_guid ] )
188+ posthog . capture ( {
189+ distinct_id : params [ :user_guid ] ,
190+ event : 'finish check_balance job' , properties : { response : response . to_hash }
191+ } )
157192 response . to_hash . to_json
158193 rescue ::MxPlatformRuby ::ApiError => e
159194 puts "Error when calling MxPlatformApi->list_user_accounts: #{ e . message } "
@@ -164,6 +199,10 @@ def create_user(user_id, mx_platform_api)
164199post '/users/:user_guid/members/:member_guid/check_balance' do
165200 content_type :json
166201 begin
202+ posthog . capture ( {
203+ distinct_id : params [ :user_guid ] ,
204+ event : 'begin check_balance job'
205+ } )
167206 response = mx_platform_api . check_balances (
168207 params [ :member_guid ] ,
169208 params [ :user_guid ]
@@ -182,6 +221,10 @@ def create_user(user_id, mx_platform_api)
182221 params [ :member_guid ] ,
183222 params [ :user_guid ]
184223 )
224+ posthog . capture ( {
225+ distinct_id : params [ :user_guid ] ,
226+ event : 'getting transactions' , properties : { response : response . to_hash }
227+ } )
185228 response . to_hash . to_json
186229 rescue ::MxPlatformRuby ::ApiError => e
187230 puts "Error when calling MxPlatformApi->list_transactions_by_member: #{ e . message } "
@@ -196,6 +239,10 @@ def create_user(user_id, mx_platform_api)
196239 params [ :member_guid ] ,
197240 params [ :user_guid ]
198241 )
242+ posthog . capture ( {
243+ distinct_id : params [ :user_guid ] ,
244+ event : 'getting member status' , properties : { response : response . to_hash }
245+ } )
199246 response . to_hash . to_json
200247 rescue ::MxPlatformRuby ::ApiError => e
201248 puts "Error when calling MxPlatformApi->read_member_status: #{ e . message } "
0 commit comments