Class: AppManager::FailSafe
- Inherits:
-
Object
- Object
- AppManager::FailSafe
- Defined in:
- lib/app_manager/fail_safe.rb
Instance Method Summary collapse
- #create_app_structures_table ⇒ Object
- #create_apps_table ⇒ Object
- #create_charges_table ⇒ Object
- #create_discount_plans_table ⇒ Object
- #create_extend_trials_table ⇒ Object
- #create_plan_table ⇒ Object
- #create_plan_users_table ⇒ Object
- #get_local_app_structures ⇒ Object
- #get_local_charge(params, options) ⇒ Object
- #get_local_plan(params) ⇒ Object
- #get_local_plans(params) ⇒ Object
- #get_local_remaining_days(params, options) ⇒ Object
-
#initialize(db_name = 'app_manager_local') ⇒ FailSafe
constructor
A new instance of FailSafe.
- #save_api_app_structures(app_structures) ⇒ Object
- #save_api_apps(apps) ⇒ Object
- #save_api_charges(charges) ⇒ Object
- #save_api_data(params) ⇒ Object
- #save_api_discount_plans(discount_plans) ⇒ Object
- #save_api_extend_trials(extend_trials) ⇒ Object
- #save_api_plan_users(plan_users) ⇒ Object
- #save_api_plans(plans) ⇒ Object
- #store_cancel_charge(params, options) ⇒ Object
- #store_local_charge(params, options) ⇒ Object
- #sync_app_manager ⇒ Object
Constructor Details
#initialize(db_name = 'app_manager_local') ⇒ FailSafe
Returns a new instance of FailSafe.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/app_manager/fail_safe.rb', line 8 def initialize(db_name='app_manager_local') @apm_db = SQLite3::Database.open "db/#{db_name}.db" @apm_db.results_as_hash = true create_plan_table create_charges_table create_apps_table create_app_structures_table create_discount_plans_table create_extend_trials_table create_plan_users_table end |
Instance Method Details
#create_app_structures_table ⇒ Object
172 173 174 |
# File 'lib/app_manager/fail_safe.rb', line 172 def create_app_structures_table @apm_db.execute "CREATE TABLE IF NOT EXISTS app_structures(banners text)" end |
#create_apps_table ⇒ Object
168 169 170 |
# File 'lib/app_manager/fail_safe.rb', line 168 def create_apps_table @apm_db.execute "CREATE TABLE IF NOT EXISTS apps(id integer,name varchar(255),slug varchar(255),url varchar(255),image varchar(255),api_token varchar(255),slack varchar(255), created_at datetime, updated_at datetime)" end |
#create_charges_table ⇒ Object
164 165 166 |
# File 'lib/app_manager/fail_safe.rb', line 164 def create_charges_table @apm_db.execute "CREATE TABLE IF NOT EXISTS charges(id INTEGER PRIMARY KEY,charge_id varchar(255),test boolean, status varchar(255),name varchar(255), type varchar(255), price float,interval varchar(255),trial_days integer,billing_on datetime,activated_on datetime,trial_ends_on datetime,cancelled_on datetime, expires_on datetime,plan_id integer,description text,shop_domain varchar(255),created_at datetime, updated_at datetime, app_id integer, sync boolean DEFAULT 0,process_type varchar(255))" end |
#create_discount_plans_table ⇒ Object
176 177 178 |
# File 'lib/app_manager/fail_safe.rb', line 176 def create_discount_plans_table @apm_db.execute "CREATE TABLE IF NOT EXISTS discount_plans(id integer,discount integer,shop_domain varchar(255),cycle_count integer,plan_id integer, created_by integer,created_at datetime,updated_at datetime,app_id integer,discount_type varchar(255))" end |
#create_extend_trials_table ⇒ Object
180 181 182 |
# File 'lib/app_manager/fail_safe.rb', line 180 def create_extend_trials_table @apm_db.execute "CREATE TABLE IF NOT EXISTS extend_trials(id integer,shop_domain varchar(255),plan_id integer,app_id integer,days integer,created_by integer,created_at datetime,updated_at datetime,extend_trial_start_at datetime)" end |
#create_plan_table ⇒ Object
160 161 162 |
# File 'lib/app_manager/fail_safe.rb', line 160 def create_plan_table @apm_db.execute "CREATE TABLE IF NOT EXISTS plans(id integer,type varchar(255), name varchar(255), price float, offer_text varchar(255), description varchar(255), interval text, shopify_plans text, trial_days integer, test boolean, on_install integer, is_custom boolean, app_id integer, base_plan integer, created_at datetime, updated_at datetime, public boolean, discount integer, cycle_count integer, store_base_plan boolean, choose_later_plan boolean, discount_type varchar(255), affiliate text, features text, deleted_at datetime)" end |
#create_plan_users_table ⇒ Object
184 185 186 |
# File 'lib/app_manager/fail_safe.rb', line 184 def create_plan_users_table @apm_db.execute "CREATE TABLE IF NOT EXISTS plan_users(id integer,shop_domain varchar(255),plan_id integer,created_by integer,created_at datetime,updated_at datetime)" end |
#get_local_app_structures ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/app_manager/fail_safe.rb', line 145 def get_local_app_structures app_structures = {} app_structures = @apm_db.execute( "SELECT * FROM app_structures;" ) rescue {} if app_structures.any? new_app_structure = {} app_structures.first.each_with_index do |(key, value), index| val = eval(value) new_app_structure[key] = val unless key.class == Integer end app_structures = new_app_structure end return app_structures end |
#get_local_charge(params, options) ⇒ Object
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/app_manager/fail_safe.rb', line 367 def get_local_charge(params,) charge_data = nil if params["shop_domain"].present? @apm_db.execute( "SELECT * FROM charges WHERE status = ? AND shop_domain = ? ",'active',params["shop_domain"]) do |charge| if charge charge_values = {} charge.each_with_index do |(key, value), index| charge_values[key] = value unless key.class == Integer end charge_data = charge_values break end end end return charge_data end |
#get_local_plan(params) ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/app_manager/fail_safe.rb', line 275 def get_local_plan(params) plan_data = {} if params.any? if params["plan_id"].present? && !params["plan_id"].nil? @apm_db.execute( "SELECT * FROM plans WHERE id = ?", params["plan_id"]) do |plan| new_plan = {} plan.each_with_index do |(key, value), index| if ['interval'].include?(key) val = eval(value) new_plan[key] = val elsif ['shopify_plans','affiliate','features'].include?(key) new_plan[key] = eval(value) elsif ['is_custom','public','store_base_plan'].include?(key) new_plan[key] = (value == 0 ? false : true) elsif ['test'].include?(key) new_plan[key] = (value == 0 ? nil : true) else new_plan[key] = value unless key.class == Integer end end plan_data = new_plan app = {} @apm_db.execute( "SELECT * FROM apps;") do |app| app_data = {} app.each_with_index do |(key, value), index| app_data[key] = value unless key.class == Integer end plan_data['app'] = app_data end if params["shop_domain"].present? && plan_data @apm_db.execute( "SELECT * FROM discount_plans WHERE plan_id = ? AND shop_domain = ? ", params["plan_id"],params["shop_domain"]) do |cd| plan_data['discount'] = cd['discount'] if cd rescue plan_data['discount'] plan_data['discount_type'] = cd['discount_type'] if cd rescue plan_data['discount_type'] plan_data['cycle_count'] = cd['cycle_count'] if cd rescue plan_data['cycle_count'] end end end end end return plan_data end |
#get_local_plans(params) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/app_manager/fail_safe.rb', line 189 def get_local_plans(params) plans_data = [] active_plan_id = nil charges = @apm_db.execute( "SELECT * FROM charges WHERE shop_domain = ? ",params['shop_domain']) active_plan_id = charges.first['plan_id'] if charges.any? custom_plan_ids = [] plan_users = @apm_db.execute( "SELECT * FROM plan_users WHERE shop_domain = ? ",params['shop_domain']) custom_plan_ids = plan_users.collect{|e|e['plan_id']} if plan_users.any? custom_plan_base_ids = [] plan_data = @apm_db.execute( "SELECT * FROM plans WHERE id IN (?) AND base_plan IS NOT NULL;",custom_plan_ids) custom_plan_base_ids = plan_data.collect{|e|e['base_plan']} if plan_data.any? if active_plan_id && custom_plan_base_ids.include?(active_plan_id) custom_plan_base_ids.delete(active_plan_id) end if custom_plan_base_ids.any? plans = @apm_db.execute( "SELECT * FROM plans WHERE (public = ? OR id IN (?)) AND id NOT IN (?)", 1,custom_plan_ids,custom_plan_base_ids) else plans = @apm_db.execute( "SELECT * FROM plans WHERE (public = ? OR id IN (?))", 1,custom_plan_ids) end if plans.any? plans.each do |plan| new_plan = {} plan.each_with_index do |(key, value), index| if ['interval'].include?(key) val = eval(value) new_plan[key] = val new_plan[key] = val['value'] if val rescue {} elsif ['shopify_plans'].include?(key) val = eval(value) new_plan[key] = val.collect{|e|e['value']} elsif ['affiliate'].include?(key) new_plan[key] = eval(value) elsif ['is_custom','public','store_base_plan'].include?(key) new_plan[key] = (value == 0 ? false : true) elsif ['test'].include?(key) new_plan[key] = (value == 0 ? nil : true) elsif ['features'].include?(key) value = eval(value) value = value.each {|e| e.delete("id")}.each {|e| e.delete("created_at")}.each {|e| e.delete("updated_at")} new_plan[key] = value else new_plan[key] = value unless key.class == Integer end end plans_data.push(new_plan) end features_by_plans = plans_data.collect{|e|e['features']} if features_by_plans.any? && AppManager.configuration.plan_features.any? features_by_plans_data = [] features = AppManager.configuration.plan_features features_by_plans.each do |features_by_plan| features_by_plan.each do |fp| fp['name'] = features.find{|e| e['uuid'] == fp['feature_id']}['name'] rescue nil fp['format'] = features.find{|e| e['uuid'] == fp['feature_id']}['format'] rescue nil features_by_plans_data.push(fp) end end end plans_data.each do |plan| if features_by_plans_data.select{|e| e['plan_id'] == plan['id']}.size > 0 feature_hash = {} features_by_plans_data.select{|e| e['plan_id'] == plan['id']}.each do |fp| feature_hash[fp["feature_id"]] = fp end features = feature_hash else features = nil end plan['features'] = features end plans = plans_data end return plans end |
#get_local_remaining_days(params, options) ⇒ Object
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/app_manager/fail_safe.rb', line 321 def get_local_remaining_days(params,) @remaining_days = 0 @shop_domain = params['shop_domain'] if params && params['trial_activated_at'].present? && !params['trial_activated_at'].nil? && params['shop_domain'].present? && params['plan_id'].present? && !params['plan_id'].nil? @trial_activated_at = params['trial_activated_at'] @plan_id = params['plan_id'] plan_data = @apm_db.execute( "SELECT * FROM plans WHERE id = ?", @plan_id) if plan_data.any? trial_days = plan_data.first['trial_days'] trial_start_date = Date.parse(@trial_activated_at) trial_end_date = trial_start_date + trial_days.days if trial_end_date > DateTime.now remaining_days = (trial_end_date - DateTime.now).to_i end trial_extension_data = @apm_db.execute( "SELECT * FROM extend_trials WHERE shop_domain = ? AND plan_id = ? ORDER BY extend_trial_start_at DESC ",@shop_domain, @plan_id) if trial_extension_data.any? trial_extension_data = trial_extension_data.first extend_trial_date = Date.parse(trial_extension_data['created_at']) + trial_extension_data['days'].to_i.days remaining_extended_days = DateTime.now < extend_trial_date ? (extend_trial_date - DateTime.now).to_i : 0 @remaining_days = @remaining_days + remaining_extended_days end end return @remaining_days end @charges = @apm_db.execute( "SELECT * FROM charges WHERE shop_domain = ? ORDER BY created_at DESC ",@shop_domain) if @charges.any? charge = @charges.first if charge['trial_days'] trial_end_date = Date.parse(charge['trial_ends_on']) if DateTime.now < trial_end_date @remaining_days = (trial_end_date - DateTime.now).to_i end trial_extension_data = @apm_db.execute( "SELECT * FROM extend_trials WHERE shop_domain = ? AND plan_id = ? ORDER BY extend_trial_start_at DESC ",@shop_domain, charge["plan_id"]) if trial_extension_data.any? trial_extension_data = trial_extension_data.first extend_trial_date = Date.parse(trial_extension_data['created_at']) + trial_extension_data['days'].to_i.days remaining_extended_days = DateTime.now < extend_trial_date ? (extend_trial_date - DateTime.now).to_i : 0 @remaining_days = @remaining_days + remaining_extended_days end end return @remaining_days end end |
#save_api_app_structures(app_structures) ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/app_manager/fail_safe.rb', line 106 def save_api_app_structures(app_structures) @apm_db.execute("DROP TABLE IF EXISTS app_structures;") create_app_structures_table if !app_structures.nil? @apm_db.execute("INSERT INTO app_structures (banners) VALUES (?)", "#{app_structures.to_h}") end end |
#save_api_apps(apps) ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/app_manager/fail_safe.rb', line 95 def save_api_apps(apps) @apm_db.execute("DROP TABLE IF EXISTS apps;") create_apps_table if apps.any? apps.each do |app| @apm_db.execute("INSERT INTO apps (id ,name ,slug ,url ,image ,api_token ,slack , created_at , updated_at ) VALUES (?,?,?,?,?,?,?,?,?)", app['id'],app['name'],app['slug'],app['url'],app['image'],app['api_token'],app['slack'],app['created_at'],app['updated_at']) end end end |
#save_api_charges(charges) ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/app_manager/fail_safe.rb', line 83 def save_api_charges(charges) @apm_db.execute("DROP TABLE IF EXISTS charges;") create_charges_table if charges.any? charges.each do |charge| charge_test = charge['test'] ? 1 : 0 @apm_db.execute("INSERT INTO charges (id ,charge_id ,test , status ,name , type , price ,interval ,trial_days ,billing_on ,activated_on ,trial_ends_on ,cancelled_on , expires_on ,plan_id ,description ,shop_domain ,created_at , updated_at, app_id, sync ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", nil,charge["charge_id"],charge_test,charge["status"],charge["name"],charge["type"],charge["price"],charge["interval"],charge["trial_days"],charge["billing_on"],charge["activated_on"],charge["trial_ends_on"],charge["cancelled_on"],charge["expires_on"],charge["plan_id"],charge["description"],charge["shop_domain"],charge["created_at"],charge["updated_at"],charge["app_id"],1) end end end |
#save_api_data(params) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/app_manager/fail_safe.rb', line 20 def save_api_data(params) begin save_api_plans(params["plans"]) rescue Exception => e Rails.logger.info "APP MANGAGER >>>> #{e.inspect}" end begin save_api_charges(params["charges"]) rescue Exception => e Rails.logger.info "APP MANGAGER >>>> #{e.inspect}" end begin save_api_apps(params["apps"]) rescue Exception => e Rails.logger.info "APP MANGAGER >>>> #{e.inspect}" end begin save_api_app_structures(params["app_structures"]) rescue Exception => e Rails.logger.info "APP MANGAGER >>>> #{e.inspect}" end begin save_api_discount_plans(params["discount_plans"]) rescue Exception => e Rails.logger.info "APP MANGAGER >>>> #{e.inspect}" end begin save_api_extend_trials(params["extend_trials"]) rescue Exception => e Rails.logger.info "APP MANGAGER >>>> #{e.inspect}" end begin save_api_plan_users(params["plan_users"]) rescue Exception => e Rails.logger.info "APP MANGAGER >>>> #{e.inspect}" end end |
#save_api_discount_plans(discount_plans) ⇒ Object
114 115 116 117 118 119 120 121 122 |
# File 'lib/app_manager/fail_safe.rb', line 114 def save_api_discount_plans(discount_plans) @apm_db.execute("DROP TABLE IF EXISTS discount_plans;") create_discount_plans_table if discount_plans.any? discount_plans.each do |discount_plan| @apm_db.execute("INSERT INTO discount_plans (id ,discount ,shop_domain ,cycle_count ,plan_id , created_by ,created_at ,updated_at ,app_id ,discount_type ) VALUES (?,?,?,?,?,?,?,?,?,?)", discount_plan['id'],discount_plan['discount'],discount_plan['shop_domain'],discount_plan['cycle_count'],discount_plan['plan_id'],discount_plan['created_by'],discount_plan['created_at'],discount_plan['updated_at'],discount_plan['app_id'],discount_plan['discount_type']) end end end |
#save_api_extend_trials(extend_trials) ⇒ Object
124 125 126 127 128 129 130 131 132 |
# File 'lib/app_manager/fail_safe.rb', line 124 def save_api_extend_trials(extend_trials) @apm_db.execute("DROP TABLE IF EXISTS extend_trials;") create_extend_trials_table if extend_trials.any? extend_trials.each do |extend_trial| @apm_db.execute("INSERT INTO extend_trials (id ,shop_domain ,plan_id ,app_id ,days ,created_by ,created_at ,updated_at ,extend_trial_start_at ) VALUES (?,?,?,?,?,?,?,?,?)", extend_trial['id'], extend_trial['shop_domain'], extend_trial['plan_id'], extend_trial['app_id'], extend_trial['days'], extend_trial['created_by'], extend_trial['created_at'], extend_trial['updated_at'], extend_trial['extend_trial_start_at']) end end end |
#save_api_plan_users(plan_users) ⇒ Object
134 135 136 137 138 139 140 141 142 |
# File 'lib/app_manager/fail_safe.rb', line 134 def save_api_plan_users(plan_users) @apm_db.execute("DROP TABLE IF EXISTS plan_users;") create_plan_users_table if plan_users.any? plan_users.each do |plan_user| @apm_db.execute("INSERT INTO plan_users (id ,shop_domain, plan_id, created_by, created_at, updated_at ) VALUES (?,?,?,?,?,?)", plan_user['id'], plan_user['shop_domain'], plan_user['plan_id'], plan_user['created_by'], plan_user['created_at'], plan_user['updated_at']) end end end |
#save_api_plans(plans) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/app_manager/fail_safe.rb', line 58 def save_api_plans(plans) @apm_db.execute("DROP TABLE IF EXISTS plans;") create_plan_table if plans.any? plans.each do |plan| interval = {} shopify_plans = [] affiliate = [] features = {} interval = plan["interval"].each{|e|e} if plan["interval"] rescue {} shopify_plans = plan["shopify_plans"].map{|e| e.to_h} if plan["shopify_plans"] rescue [] affiliate = plan["affiliate"].map{|e|e.to_h} if plan["affiliate"] rescue [] features = plan["features"].map{|e|e.to_h} rescue [] plan_test = plan['test'].nil? ? 0 : plan['test'] is_custom = plan['is_custom'] ? 1 : 0 public_val = plan['public'] ? 1 : 0 store_base_plan = plan['store_base_plan'] ? 1 : 0 choose_later_plan = plan['choose_later_plan'] ? 1 : 0 @apm_db.execute("INSERT INTO plans ( id , type , name , price , offer_text, description , interval , shopify_plans , trial_days , test , on_install , is_custom , app_id , base_plan , created_at, updated_at , public , discount , cycle_count , store_base_plan, choose_later_plan , discount_type, affiliate, features, deleted_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",plan["id"], plan["type"], plan["name"], plan["price"], plan["offer_text"], plan["description"], "#{interval}", "#{shopify_plans}", plan["trial_days"], plan_test, plan["on_install"], is_custom, plan["app_id"], plan["base_plan"], plan["created_at"], plan["updated_at"],public_val, plan["discount"], plan["cycle_count"], store_base_plan, choose_later_plan, plan["discount_type"], "#{affiliate}", "#{features}",plan["deleted_at"]) end end end |
#store_cancel_charge(params, options) ⇒ Object
405 406 407 408 409 410 411 412 413 |
# File 'lib/app_manager/fail_safe.rb', line 405 def store_cancel_charge(params,) = {"message" => 'fail'} if && [:shop_domain].present? && [:plan_id].present? time = "#{DateTime.now}" @apm_db.execute( "UPDATE charges SET status= ?, cancelled_on = ?, sync = ? WHERE plan_id = ? AND shop_domain = ? ",'cancelled',time,0,[:plan_id],[:shop_domain]) = {"message" => 'success'} end return end |
#store_local_charge(params, options) ⇒ Object
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/app_manager/fail_safe.rb', line 385 def store_local_charge(params,) = {"message" => 'fail'} if .gsub!('null','nil') rescue nil charge = eval() rescue nil if charge charge = charge.as_json if charge.class == Hash test_value = charge["test"] == true ? 1 : 0 plan_id = charge["plan_id"].to_i begin @charge = @apm_db.execute("INSERT INTO charges (id, charge_id ,test , status ,name , type , price ,interval ,trial_days ,billing_on ,activated_on ,trial_ends_on ,cancelled_on , expires_on ,plan_id ,description ,shop_domain ,created_at , updated_at, sync ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",nil,"#{charge["charge_id"]}",test_value,charge["status"],charge["name"],charge["type"],charge["price"],charge["interval"],charge["trial_days"],charge["billing_on"],charge["activated_on"],charge["trial_ends_on"],charge["cancelled_on"],charge["expires_on"],plan_id,charge["description"],charge["shop_domain"],charge["created_at"],charge["updated_at"],0) = {"message" => 'success'} rescue Exception => e Rails.logger.info ">>>>>>>>> Charge not saved on local DB due to #{e.inspect}" end end end return end |
#sync_app_manager ⇒ Object
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 |
# File 'lib/app_manager/fail_safe.rb', line 417 def sync_app_manager plan_obj = AppManager::Client.new response = plan_obj.get_status if response && response.code == 200 @apm_db.execute( "SELECT * FROM charges WHERE sync = ?", 0) do |charge| if charge if !charge["cancelled_on"].nil? charge["cancelled_on"] = Date.parse(charge["cancelled_on"]) end plan_ob = AppManager::Client.new(nil,json_req=true) res = plan_ob.sync_charge(charge.to_json) if res && res["message"] == "success" @apm_db.execute( "UPDATE charges SET sync= ? WHERE charge_id = ?",1,charge['charge_id']) end end end end end |