app_manager 1.3.4 → 1.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 73c07b966d8cbce529a652bd4765b5f4107280a9438868e4b5d898631d8d8c38
4
- data.tar.gz: a7fe2691818b06e7f43feb481046f3c6ff4bd3e4c744b737f2c6ca81abf80a56
3
+ metadata.gz: ac3a1d17d520c154419fd5917813fda210900f58d12686723bacf05d99ecbd14
4
+ data.tar.gz: 0f62d38f1160f30e07eb96007f845395ce397f70d82043f76f71aaf8c564e0e0
5
5
  SHA512:
6
- metadata.gz: 335f46ddc9d884bd9edf6b3361d8e4c2c0fda5f4ed8ece90a18eec729b00bdc5fed894700dee1a423678a259af025ff9013fb21fc7e016d8f0a07e1f34c84a0c
7
- data.tar.gz: 4624f11210812f05fb6f421b8ceb624a31b64aabde7a757bafceca97cd21705f17eb3b2851d49139526940f37f999abeed5497cab88b705de47e2a936d130229
6
+ metadata.gz: c6136666c06d579b448ab775a4e9cc6c72f314f0ffc39baccece2d9fe3951a56c1853848d6e83bdfb847037680d2b1e978a4e04b0d440fbc783169a6ecac3e06
7
+ data.tar.gz: 730a1f415a8d7a679ade4bb74b60b84ddc109ecfa661dc950c666c8fa8bdef44dd3faf6d14d0979052ae73c58350ff53225e0fe51f96f4f463a19f7c50314e95
data/Gemfile.lock CHANGED
@@ -1,7 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- app_manager (1.3.4)
4
+ app_manager (1.3.7)
5
+ activerecord-import (~> 1.4)
5
6
  httparty
6
7
  kaminari (>= 0.16.3)
7
8
  rails (>= 5.2.0)
@@ -62,6 +63,8 @@ GEM
62
63
  activerecord (7.0.3.1)
63
64
  activemodel (= 7.0.3.1)
64
65
  activesupport (= 7.0.3.1)
66
+ activerecord-import (1.4.0)
67
+ activerecord (>= 4.2)
65
68
  activestorage (7.0.3.1)
66
69
  actionpack (= 7.0.3.1)
67
70
  activejob (= 7.0.3.1)
@@ -92,7 +95,7 @@ GEM
92
95
  httparty (0.20.0)
93
96
  mime-types (~> 3.0)
94
97
  multi_xml (>= 0.5.2)
95
- i18n (1.11.0)
98
+ i18n (1.12.0)
96
99
  concurrent-ruby (~> 1.0)
97
100
  kaminari (1.2.2)
98
101
  activesupport (>= 4.1.0)
@@ -10,43 +10,50 @@ module AppManager
10
10
  def process_plan
11
11
  if params[:shop].present? && params[:plan_id].present?
12
12
  @shop = shop_data
13
+ grandfathered_field = @field_names['grandfathered']
13
14
  if !@shop.nil?
14
15
  plan_obj = AppManager::Client.new
15
16
  plan_data = plan_obj.get_plan(params[:plan_id], params[:shop])
16
17
  # render json: plan_data and return #use this to test plan failsafe api
17
18
  if plan_data.present? && plan_data.is_a?(Hash)
18
- if plan_data['price'] == 0
19
- active_charge_data = plan_obj.get_charge(params[:shop])
20
- begin
21
- if active_charge_data && active_charge_data.any? && !active_charge_data['active_charge'].nil? && !active_charge_data['active_charge']['charge_id'].nil?
22
- gq_obj = AppManager::GraphqlHelper.new(@shop.shopify_domain, @shop.shopify_token)
23
- rec_cancel_data = gq_obj.recurring_charge_cancel_api_call(active_charge_data['active_charge']['charge_id'], @shop)
24
- if !rec_cancel_data["errors"].present? && (rec_cancel_data["data"].present? && rec_cancel_data["data"]["appSubscriptionCancel"].present? && !rec_cancel_data["data"]["appSubscriptionCancel"]["userErrors"].any? && (rec_cancel_data["data"]["appSubscriptionCancel"]["appSubscription"]["status"] == 'CANCELLED'))
25
- cancelled_charge_id = rec_cancel_data["data"]["appSubscriptionCancel"]["appSubscription"]["id"].split('/')[-1]
26
- plan_obj.cancel_charge(params[:shop], cancelled_charge_id)
19
+ if plan_data['price'] == 0
20
+ active_charge_data = plan_obj.get_charge(params[:shop])
21
+ begin
22
+ if active_charge_data && active_charge_data.any? && !active_charge_data['active_charge'].nil? && !active_charge_data['active_charge']['charge_id'].nil?
23
+ gq_obj = AppManager::GraphqlHelper.new(@shop.shopify_domain, @shop.shopify_token)
24
+ rec_cancel_data = gq_obj.recurring_charge_cancel_api_call(active_charge_data['active_charge']['charge_id'], @shop)
25
+ if !rec_cancel_data["errors"].present? && (rec_cancel_data["data"].present? && rec_cancel_data["data"]["appSubscriptionCancel"].present? && !rec_cancel_data["data"]["appSubscriptionCancel"]["userErrors"].any? && (rec_cancel_data["data"]["appSubscriptionCancel"]["appSubscription"]["status"] == 'CANCELLED'))
26
+ cancelled_charge_id = rec_cancel_data["data"]["appSubscriptionCancel"]["appSubscription"]["id"].split('/')[-1]
27
+ plan_obj.cancel_charge(params[:shop], cancelled_charge_id)
28
+ end
27
29
  end
30
+ rescue Exception => e
31
+ Rollbar.error("APP MANAGER Process Plan Failed #{e}")
28
32
  end
29
- rescue Exception => e
30
- Rollbar.error("APP MANAGER Process Plan Failed #{e}")
31
- end
32
-
33
- if @shop.update(@plan_field => params[:plan_id])
34
- begin
35
- AppManager::EventHandler.new('charge_created', {
36
- "plan" => plan_data,
37
- "charge" => nil,
38
- "previous_charge" => nil,
39
- "shopify_domain" => params[:shop]
40
- })
41
- rescue Exception => e
42
- Rollbar.error("APP MANAGER Error in Process Plan #{e}")
33
+ @trial_activated_field = AppManager.configuration.field_names['trial_activated_at']
34
+ update_info = {@plan_field => params[:plan_id], @trial_activated_field => nil,grandfathered_field => 0}
35
+ if !config_trial_days.nil? && !plan_data.nil?
36
+ trial_days = plan_data['trial_days'] || 0
37
+ update_info[config_trial_days] = trial_days
38
+ end
39
+ if @shop.update(update_info)
40
+ begin
41
+ AppManager::EventHandler.new('charge_created', {
42
+ "plan" => plan_data,
43
+ "charge" => nil,
44
+ "previous_charge" => nil,
45
+ "shopify_domain" => params[:shop]
46
+ })
47
+ rescue Exception => e
48
+ Rollbar.error("APP MANAGER Error in Process Plan #{e}")
49
+ end
50
+ AppManager.clear_cache
51
+ # render json: {'redirect_url' => "#{app_url}?shop=#{params[:shop]}"} and return true
52
+ render json: {'status' => true,'plan_type' => 'free_plan'} and return true
53
+ else
54
+ raise Error, "Invalid charge"
43
55
  end
44
- AppManager.clear_cache
45
- render json: {'redirect_url' => "#{app_url}?shop=#{params[:shop]}"} and return true
46
- else
47
- raise Error, "Invalid charge"
48
56
  end
49
- end
50
57
  request_data = {'shop' => @shop.shopify_domain, 'timestamp' => Time.now.to_i, 'plan' => params[:plan_id]}
51
58
  return_url = "#{app_url}#{plan_callback_path}?#{Rack::Utils.build_query(request_data)}"
52
59
  gq_obj = AppManager::GraphqlHelper.new(@shop.shopify_domain, @shop.shopify_token)
@@ -91,30 +98,40 @@ module AppManager
91
98
  charge['shop_domain'] = params[:shop]
92
99
  charge['interval'] = plan_data['interval']['value']
93
100
 
94
- if !@plan_field.nil?
95
- begin
96
- plan_obj.cancel_charge(@shop[shopify_domain], @shop[@plan_field])
97
- rescue Exception => e
98
- Rollbar.error("APP MANAGER Error in Cancel charge >>>> #{e.inspect}")
99
- end
100
- end
101
+ # new_plan_obj = AppManager::Client.new
102
+ # if !@plan_field.nil?
103
+ # begin
104
+ # new_plan_obj.cancel_charge(@shop[shopify_domain], @shop[@plan_field])
105
+ # rescue Exception => e
106
+ # Rollbar.error("APP MANAGER Error in Cancel charge >>>> #{e.inspect}")
107
+ # end
108
+ # end
101
109
 
102
110
  charge_ob = AppManager::Client.new(nil, json_req = true)
103
111
  response = charge_ob.store_charge(charge.to_json)
104
112
 
105
113
  if response['message'] == "success"
106
114
  AppManager.clear_cache
107
- @shop.update(@plan_field => params[:plan],grandfathered_field => 0)
108
- charge_data = plan_obj.get_charge(@shop[shopify_domain])
109
- begin
110
- AppManager::EventHandler.new('charge_created', {
111
- "plan" => plan_data,
112
- "charge" => charge,
113
- "previous_charge" => charge_data ? (charge_data['cancelled_charge'] || nil) : nil,
114
- "shopify_domain" => params[:shop]
115
- })
116
- rescue Exception => e
117
- Rollbar.error("Error in APP MANAGER Charge Created Callback >>>> #{e.inspect}")
115
+
116
+ update_info = {@plan_field => params[:plan],grandfathered_field => 0}
117
+ if !config_trial_days.nil? && !plan_data.nil?
118
+ trial_days = plan_data['trial_days'] || 0
119
+ update_info[config_trial_days] = trial_days
120
+ end
121
+
122
+ @shop.update(update_info)
123
+ Thread.new do
124
+ charge_data = plan_obj.get_charge(@shop[shopify_domain])
125
+ begin
126
+ AppManager::EventHandler.new('charge_created', {
127
+ "plan" => plan_data,
128
+ "charge" => charge,
129
+ "previous_charge" => charge_data ? (charge_data['cancelled_charge'] || nil) : nil,
130
+ "shopify_domain" => params[:shop]
131
+ })
132
+ rescue Exception => e
133
+ Rollbar.error("Error in APP MANAGER Charge Created Callback >>>> #{e.inspect}")
134
+ end
118
135
  end
119
136
  end
120
137
  redirect_to "#{app_url}?shop=#{params[:shop]}", :status => 301 and return
@@ -125,7 +142,11 @@ module AppManager
125
142
  raise ModelNotFound, "Shop not found"
126
143
  end
127
144
  else
128
- raise Error, "Invalid params, must have charge_id,shop && plan"
145
+ if params[:shop].present?
146
+ redirect_to "#{app_url}?shop=#{params[:shop]}", :status => 301 and return
147
+ else
148
+ raise Error, "Invalid params, must have charge_id,shop && plan in charge controller"
149
+ end
129
150
  end
130
151
  end
131
152
 
@@ -161,6 +182,15 @@ module AppManager
161
182
  AppManager.configuration.app_url
162
183
  end
163
184
 
185
+ def config_trial_days
186
+ @field_names = AppManager.configuration.field_names
187
+ if !@field_names.nil? && @field_names.has_key?('total_trial_days') && !@field_names['total_trial_days'].nil? && !@field_names['total_trial_days'].blank?
188
+ return @field_names['total_trial_days']
189
+ else
190
+ return nil
191
+ end
192
+ end
193
+
164
194
 
165
195
  end
166
196
  end
@@ -86,17 +86,17 @@ module AppManager
86
86
  plans = plans.values if !plans.nil?
87
87
  shopify_plans = params[:shopify_plans] || nil
88
88
  shopify_plans = shopify_plans.values if !shopify_plans.nil?
89
- items_per_page = params[:itemsPerPage] || 25
89
+ items_per_page = 25
90
90
  @shopify_email = AppManager.configuration.field_names['shopify_email']
91
91
  @shopify_plan_name_field = AppManager.configuration.field_names['shopify_plan']
92
- data = model
93
92
  if params[:search]
94
93
  data = model.where("#{@shopify_domain} LIKE :search OR #{@shopify_email} LIKE :search", search: "%#{search}%")
95
94
  data = data.where(@plan_field => plans) if !plans.nil?
96
95
  data = data.where(@shopify_plan_name_field => shopify_plans) if !shopify_plans.nil?
97
96
  data = data.page(params[:page]).per(items_per_page)
98
97
  else
99
- data = model.where(@plan_field => plans) if !plans.nil?
98
+ data = model
99
+ data = data.where(@plan_field => plans) if !plans.nil?
100
100
  data = data.where(@shopify_plan_name_field => shopify_plans) if !shopify_plans.nil?
101
101
  data = data.page(params[:page]).per(items_per_page)
102
102
  end
@@ -126,10 +126,16 @@ module AppManager
126
126
  if params[:shop_domain].present? && params[:plan_id].present? && model
127
127
  @shop = shop_data
128
128
  @trial_activated_field = AppManager.configuration.field_names['trial_activated_at']
129
- if @shop.update(@plan_field => params[:plan_id], @trial_activated_field => DateTime.now)
129
+ plan_obj = AppManager::Client.new
130
+ plan_data = plan_obj.get_plan(params[:plan_id], params[:shop_domain]) rescue nil
131
+ update_info = {@plan_field => params[:plan_id], @trial_activated_field => DateTime.now}
132
+ if !config_trial_days.nil? && !plan_data.nil?
133
+ trial_days = plan_data['trial_days'] || 0
134
+ update_info[config_trial_days] = trial_days
135
+ end
136
+ if @shop.update(update_info)
130
137
  begin
131
- plan_obj = AppManager::Client.new
132
- plan_data = plan_obj.get_plan(params[:plan_id], params[:shop_domain]) rescue nil
138
+ AppManager.clear_cache
133
139
  AppManager::EventHandler.new('charge_created', {
134
140
  "plan" => plan_data,
135
141
  "charge" => nil,
@@ -161,7 +167,7 @@ module AppManager
161
167
  Thread.new do
162
168
  @fs = AppManager::FailSafe.new
163
169
  begin
164
- @fs.sync_app_manager
170
+ @fs.sync_app_manager
165
171
  rescue Exception => e
166
172
  Rails.logger.info "APP MANAGER >>>> LOCAL DB couldn't sync with POTAL DB #{e.inspect}"
167
173
  end
@@ -209,5 +215,14 @@ module AppManager
209
215
  AppManager.configuration.app_url
210
216
  end
211
217
 
218
+ def config_trial_days
219
+ @field_names = AppManager.configuration.field_names
220
+ if !@field_names.nil? && @field_names.has_key?('total_trial_days') && !@field_names['total_trial_days'].nil? && !@field_names['total_trial_days'].blank?
221
+ return @field_names['total_trial_days']
222
+ else
223
+ return nil
224
+ end
225
+ end
226
+
212
227
  end
213
228
  end
data/app_manager.gemspec CHANGED
@@ -33,6 +33,9 @@ Gem::Specification.new do |spec|
33
33
  spec.add_dependency "httparty"
34
34
  spec.add_dependency "rails", '>= 5.2.0'
35
35
  spec.add_dependency "kaminari", '>= 0.16.3'
36
+ spec.add_dependency "activerecord-import", '~> 1.4'
37
+
38
+
36
39
  # spec.add_dependency "sqlite3", '~> 1.3.0'
37
40
  # For more information and examples about making a new gem, checkout our
38
41
  # guide at: https://bundler.io/guides/creating_gem.html
@@ -28,7 +28,8 @@ module AppManager
28
28
  response = response_from_cache_for_api(http_method, path, options)
29
29
  failsafe_or_caching_done = true
30
30
  else
31
- response = self.class.send(http_method, path, { body: options, timeout: 10 })
31
+ time_out = Rails.env.development? ? 120 : 30
32
+ response = self.class.send(http_method, path, { body: options, timeout: time_out })
32
33
  end
33
34
  if path.include? "/get-status"
34
35
  data = response
@@ -59,14 +60,16 @@ module AppManager
59
60
  return fetch_static_cached_response(cache_key)
60
61
  end
61
62
  begin
62
- response = self.class.send(http_method, path, { body: options, timeout: 10 })
63
- if response.class.to_s == "HTTParty::Response" && (response.code.to_s.start_with?('2') or response.code.to_s.start_with?('4')) && (response.code.to_s != '429')
64
- Rails.cache.write(cache_key, response, expires_in: AppManager.configuration.expires_in)
65
- else
66
- response = response_from_failsafe(path,options)
67
- end
63
+ response = self.class.send(http_method, path, { body: options, timeout: 15 })
64
+ if response.class.to_s == "HTTParty::Response" && (response.code.to_s.start_with?('2') or response.code.to_s.start_with?('4')) && (response.code.to_s != '429')
65
+ Rails.cache.write(cache_key, response, expires_in: AppManager.configuration.expires_in)
66
+ else
67
+ response = response_from_failsafe(path,options)
68
+ end
68
69
  rescue Net::ReadTimeout => error
69
70
  response = response_from_failsafe(path,options)
71
+ rescue Exception => e
72
+ response = response_from_failsafe(path,options)
70
73
  end
71
74
  return response
72
75
  end
@@ -9,6 +9,8 @@ module AppManager
9
9
  include AppManager::Client::Banners
10
10
  include AppManager::Client::Plans
11
11
 
12
+ # debug_output $stderr if Rails.env.development?
13
+
12
14
  format :json
13
15
 
14
16
 
@@ -2,17 +2,18 @@ require 'json'
2
2
  require 'date'
3
3
  require 'time'
4
4
  require "fileutils"
5
-
5
+ require 'active_record'
6
+ require 'activerecord-import'
6
7
  module AppManager
7
8
 
8
9
  class FailSafe
9
10
 
10
11
  def initialize(db_name = 'app_manager_local')
11
- begin
12
- FileUtils.chmod 0664, "db/#{db_name}.db"
13
- rescue Exception => e
14
- puts ">>>>>> #{e.inspect}"
15
- end
12
+ # begin
13
+ # FileUtils.chmod 0664, "db/#{db_name}.db"
14
+ # rescue Exception => e
15
+ # puts ">>>>>> #{e.inspect}"
16
+ # end
16
17
  # @apm_db = SQLite3::Database.open "db/#{db_name}.db"
17
18
  # @apm_db.results_as_hash = true
18
19
  end
@@ -62,8 +63,14 @@ module AppManager
62
63
  end
63
64
 
64
65
  def save_api_plans(plans)
65
- AppManager::Plan.destroy_all
66
+ begin
67
+ # ActiveRecord::Base.establish_connection(:app_manager).connection.execute("TRUNCATE plans RESTART IDENTITY")
68
+ AppManager::Plan.connection.truncate(AppManager::Plan.table_name)
69
+ rescue
70
+ AppManager::Plan.delete_all
71
+ end
66
72
  if plans.any?
73
+ plan_data = []
67
74
  plans.each do |plan|
68
75
  interval = {}
69
76
  shopify_plans = []
@@ -77,62 +84,103 @@ module AppManager
77
84
  public_val = plan['public'] ? 1 : 0
78
85
  store_base_plan = plan['store_base_plan'] ? 1 : 0
79
86
  choose_later_plan = plan['choose_later_plan'] ? 1 : 0
80
- AppManager::Plan.create(id: plan["id"], type: plan["type"], name: plan["name"], price: plan["price"], offer_text: plan["offer_text"], description: plan["description"], interval: interval, shopify_plans: shopify_plans, trial_days: plan["trial_days"], test: plan_test, on_install: plan["on_install"], is_custom: is_custom, app_id: plan["app_id"], base_plan: plan["base_plan"], created_at: plan["created_at"], updated_at: plan["updated_at"], public: public_val, discount: plan["discount"], cycle_count: plan["cycle_count"], store_base_plan: store_base_plan, choose_later_plan: choose_later_plan, discount_type: plan["discount_type"], affiliate: affiliate, features: features, deleted_at: plan["deleted_at"])
87
+ plan_data << AppManager::Plan.new(id: plan["id"], type: plan["type"], name: plan["name"], price: plan["price"], offer_text: plan["offer_text"], description: plan["description"], interval: interval, shopify_plans: shopify_plans, trial_days: plan["trial_days"], test: plan_test, on_install: plan["on_install"], is_custom: is_custom, app_id: plan["app_id"], base_plan: plan["base_plan"], created_at: plan["created_at"], updated_at: plan["updated_at"], public: public_val, discount: plan["discount"], cycle_count: plan["cycle_count"], store_base_plan: store_base_plan, choose_later_plan: choose_later_plan, discount_type: plan["discount_type"], affiliate: affiliate, features: features, deleted_at: plan["deleted_at"])
81
88
  end
89
+ AppManager::Plan.bulk_import plan_data
82
90
  end
83
91
  end
84
92
 
85
93
 
86
94
  def save_api_charges(charges)
87
- AppManager::Charge.destroy_all
95
+ begin
96
+ # ActiveRecord::Base.establish_connection(:app_manager).connection.execute("TRUNCATE charges RESTART IDENTITY")
97
+ AppManager::Charge.connection.truncate(AppManager::Charge.table_name)
98
+ rescue
99
+ AppManager::Charge.delete_all
100
+ end
88
101
  if charges.any?
102
+ charge_data = []
89
103
  charges.each do |charge|
90
104
  charge_test = charge['test'] ? true : false
91
- AppManager::Charge.create(charge_id: charge["charge_id"], test: charge_test, status: charge["status"], name: charge["name"], type: charge["type"], price: charge["price"], interval: charge["interval"], trial_days: charge["trial_days"], billing_on: charge["billing_on"], activated_on: charge["activated_on"], trial_ends_on: charge["trial_ends_on"], cancelled_on: charge["cancelled_on"], expires_on: charge["expires_on"], plan_id: charge["plan_id"], description: charge["description"], shop_domain: charge["shop_domain"], created_at: charge["created_at"], updated_at: charge["updated_at"], app_id: charge["app_id"], sync: true)
105
+ charge_data << AppManager::Charge.new(charge_id: charge["charge_id"], test: charge_test, status: charge["status"], name: charge["name"], type: charge["type"], price: charge["price"], interval: charge["interval"], trial_days: charge["trial_days"], billing_on: charge["billing_on"], activated_on: charge["activated_on"], trial_ends_on: charge["trial_ends_on"], cancelled_on: charge["cancelled_on"], expires_on: charge["expires_on"], plan_id: charge["plan_id"], description: charge["description"], shop_domain: charge["shop_domain"], created_at: charge["created_at"], updated_at: charge["updated_at"], app_id: charge["app_id"], sync: true)
92
106
  end
107
+ AppManager::Charge.bulk_import charge_data
93
108
  end
94
109
  end
95
110
 
96
111
 
97
112
  def save_api_apps(apps)
98
- AppManager::App.destroy_all
113
+ begin
114
+ # ActiveRecord::Base.establish_connection(:app_manager).connection.execute("TRUNCATE apps RESTART IDENTITY")
115
+ AppManager::App.connection.truncate(AppManager::App.table_name)
116
+ rescue
117
+ AppManager::App.delete_all
118
+ end
99
119
  if apps.any?
120
+ apps_data = []
100
121
  apps.each do |app|
101
- AppManager::App.create(id: app['id'], name: app['name'], slug: app['slug'], url: app['url'], image: app['image'], api_token: app['api_token'], slack: app['slack'], created_at: app['created_at'], updated_at: app['updated_at'])
122
+ apps_data << AppManager::App.new(id: app['id'], name: app['name'], slug: app['slug'], url: app['url'], image: app['image'], api_token: app['api_token'], slack: app['slack'], created_at: app['created_at'], updated_at: app['updated_at'])
102
123
  end
124
+ AppManager::Charge.bulk_import apps_data
103
125
  end
104
126
  end
105
127
 
106
128
  #Complete
107
129
  def save_api_app_structures(app_structures)
108
- AppManager::AppStructure.destroy_all
130
+ begin
131
+ # ActiveRecord::Base.establish_connection(:app_manager).connection.execute("TRUNCATE app_structures RESTART IDENTITY")
132
+ AppManager::AppStructure.connection.truncate(AppManager::AppStructure.table_name)
133
+ rescue
134
+ AppManager::AppStructure.delete_all
135
+ end
109
136
  AppManager::AppStructure.create(banners: app_structures.to_h)
110
137
  end
111
138
 
112
139
  def save_api_discount_plans(discount_plans)
113
- AppManager::DiscountPlan.destroy_all
140
+ begin
141
+ # ActiveRecord::Base.establish_connection(:app_manager).connection.execute("TRUNCATE discount_plans RESTART IDENTITY")
142
+ AppManager::DiscountPlan.connection.truncate(AppManager::DiscountPlan.table_name)
143
+ rescue
144
+ AppManager::DiscountPlan.delete_all
145
+ end
114
146
  if discount_plans.any?
147
+ discount_plans_data = []
115
148
  discount_plans.each do |discount_plan|
116
- AppManager::DiscountPlan.create(id: discount_plan['id'], discount: discount_plan['discount'], shop_domain: discount_plan['shop_domain'], cycle_count: discount_plan['cycle_count'], plan_id: discount_plan['plan_id'], created_by: discount_plan['created_by'], created_at: discount_plan['created_at'], updated_at: discount_plan['updated_at'], app_id: discount_plan['app_id'], discount_type: discount_plan['discount_type'])
149
+ discount_plans_data << AppManager::DiscountPlan.new(id: discount_plan['id'], discount: discount_plan['discount'], shop_domain: discount_plan['shop_domain'], cycle_count: discount_plan['cycle_count'], plan_id: discount_plan['plan_id'], created_by: discount_plan['created_by'], created_at: discount_plan['created_at'], updated_at: discount_plan['updated_at'], app_id: discount_plan['app_id'], discount_type: discount_plan['discount_type'])
117
150
  end
151
+ AppManager::DiscountPlan.bulk_import discount_plans_data
118
152
  end
119
153
  end
120
154
 
121
155
  def save_api_extend_trials(extend_trials)
122
- AppManager::ExtendTrial.destroy_all
156
+ begin
157
+ # ActiveRecord::Base.establish_connection(:app_manager).connection.execute("TRUNCATE extend_trials RESTART IDENTITY")
158
+ AppManager::ExtendTrial.connection.truncate(AppManager::ExtendTrial.table_name)
159
+ rescue
160
+ AppManager::ExtendTrial.delete_all
161
+ end
123
162
  if extend_trials.any?
163
+ extend_trials_data = []
124
164
  extend_trials.each do |extend_trial|
125
- AppManager::ExtendTrial.create(id: extend_trial['id'], shop_domain: extend_trial['shop_domain'], plan_id: extend_trial['plan_id'], app_id: extend_trial['app_id'], days: extend_trial['days'], created_by: extend_trial['created_by'], created_at: extend_trial['created_at'], updated_at: extend_trial['updated_at'], extend_trial_start_at: extend_trial['extend_trial_start_at'])
165
+ extend_trials_data << AppManager::ExtendTrial.new(id: extend_trial['id'], shop_domain: extend_trial['shop_domain'], plan_id: extend_trial['plan_id'], app_id: extend_trial['app_id'], days: extend_trial['days'], created_by: extend_trial['created_by'], created_at: extend_trial['created_at'], updated_at: extend_trial['updated_at'], extend_trial_start_at: extend_trial['extend_trial_start_at'])
126
166
  end
167
+ AppManager::ExtendTrial.bulk_import extend_trials_data
127
168
  end
128
169
  end
129
170
 
130
171
  def save_api_plan_users(plan_users)
131
- AppManager::PlanUser.destroy_all
172
+ begin
173
+ # ActiveRecord::Base.establish_connection(:app_manager).connection.execute("TRUNCATE plan_users RESTART IDENTITY")
174
+ AppManager::PlanUser.connection.truncate(AppManager::PlanUser.table_name)
175
+ rescue
176
+ AppManager::PlanUser.delete_all
177
+ end
132
178
  if plan_users.any?
179
+ extend_plan_users = []
133
180
  plan_users.each do |plan_user|
134
- AppManager::PlanUser.create(id: plan_user['id'], shop_domain: plan_user['shop_domain'], plan_id: plan_user['plan_id'], created_by: plan_user['created_by'], created_at: plan_user['created_at'], updated_at: plan_user['updated_at'])
181
+ extend_plan_users << AppManager::PlanUser.new(id: plan_user['id'], shop_domain: plan_user['shop_domain'], plan_id: plan_user['plan_id'], created_by: plan_user['created_by'], created_at: plan_user['created_at'], updated_at: plan_user['updated_at'])
135
182
  end
183
+ AppManager::ExtendTrial.bulk_import extend_plan_users
136
184
  end
137
185
  end
138
186
 
@@ -363,6 +411,11 @@ module AppManager
363
411
  if charge['trial_ends_on'] && DateTime.now < charge['trial_ends_on']
364
412
  @remaining_days = (charge['trial_ends_on'].to_datetime - DateTime.now.to_datetime).to_i
365
413
  end
414
+
415
+ # ADD EXTRA DAY
416
+ if charge['created_at'] && ((charge['created_at'].to_datetime - DateTime.now.to_datetime).to_i == 0)
417
+ @remaining_days = @remaining_days + 1
418
+ end
366
419
  # TODO: Uncomment this code when we implement Shopify trial extension apis
367
420
  # trial_extension_data = AppManager::ExtendTrial.where(shop_domain: @shop_domain, plan_id: @plan_id).order(extend_trial_start_at: :desc)
368
421
  # if trial_extension_data.any?
@@ -59,15 +59,27 @@ module AppManager
59
59
  plan_test = true
60
60
  end
61
61
  trial_days = plan['trial_days'] || 0
62
-
63
62
  if shop && shop.shopify_domain && trial_days
64
63
  trial_activated_at = shop[AppManager.configuration.field_names['trial_activated_at']] rescue nil
65
64
  plan_id_field = shop[AppManager.configuration.plan_id_or_name_field] rescue nil
66
65
  remaining_obj = AppManager::Client.new
67
66
  remaining = remaining_obj.get_remaining_days(shop.shopify_domain,trial_activated_at,plan_id_field)
68
- trial_days = !remaining.nil? ? remaining : trial_days
67
+ # trial_days = !remaining.nil? ? remaining : trial_days
68
+ if !remaining.nil?
69
+ if !plan_id_field.nil?
70
+ plan_obj = AppManager::Client.new
71
+ current_plan = plan_obj.get_plan(plan_id_field) rescue nil
72
+ used_days = (current_plan['trial_days'].to_i - remaining.to_i) if current_plan rescue 0
73
+ if used_days > 0
74
+ days = trial_days - used_days
75
+ trial_days = days > 0 ? days : 0
76
+ end
77
+ else
78
+ trial_days = remaining
79
+ end
80
+ end
69
81
  end
70
-
82
+
71
83
  discount_type = plan['discount_type'] || "percentage"
72
84
  discount_val = discount_type == "percentage" ? (plan['discount'].to_f/ 100) : "#{plan['discount']}"
73
85
  plan_discount = plan['discount'] ? { "discount" => { "durationLimitInIntervals" => (plan['cycle_count'].to_i || 0), "value" => {"#{discount_type}" => discount_val} } } : {}
@@ -84,7 +84,7 @@ module AppManager
84
84
  res = plan_obj.get_remaining_days(shop_domain, trial_activated_at_val, plan_id)
85
85
  return (res && !res.nil?) ? res : 0
86
86
  rescue Exception => e
87
- Rollbar.error("APP MANAGER Get Remaining Days Failed #{e}")
87
+ return 0
88
88
  end
89
89
  end
90
90
 
@@ -98,7 +98,6 @@ module AppManager
98
98
  if shop_plan_id && !shop_plan_id.nil?
99
99
  req_plan_id = shop_plan_id
100
100
  else
101
- Rollbar.error("Invalid params, must have charge_id,shop && plan")
102
101
  return {}
103
102
  end
104
103
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AppManager
4
- VERSION = "1.3.4"
4
+ VERSION = "1.3.7"
5
5
  end
@@ -15,7 +15,8 @@ AppManager.configure do |config|
15
15
  'plan_id' => 'plan_id', # 1. t
16
16
  'created_at' => 'created_at', # 2022-04-15 10:43:05
17
17
  'trial_activated_at' => 'trial_activated_at', # field name that stores trial start/activated date
18
- 'grandfathered' => 'grandfathered'
18
+ 'grandfathered' => 'grandfathered',
19
+ 'total_trial_days' => '' #optional, put a trial days field from your shops table otherwise leave it blank
19
20
  }
20
21
  config.plan_features = [
21
22
  {
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: app_manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.4
4
+ version: 1.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hulkapps
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-13 00:00:00.000000000 Z
11
+ date: 2022-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: 0.16.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord-import
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.4'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.4'
55
69
  description: Provides helper function to access AppManager API
56
70
  email: rahul.t@hulkapps.com
57
71
  executables: []