Class: AppManager::PlansController

Inherits:
ApplicationController show all
Defined in:
app/controllers/app_manager/plans_controller.rb

Instance Method Summary collapse

Instance Method Details

#active_without_planObject



95
96
97
98
99
100
101
102
103
# File 'app/controllers/app_manager/plans_controller.rb', line 95

def active_without_plan
	if params[:shop_domain].present? && params[:plan_id].present? && model
		@trial_activated_field = AppManager.configuration.field_names['trial_activated_at'] 
		model.update(@plan_field => params[:plan_id],@trial_activated_field => DateTime.now)
		render json: {'status' => true}
	else
		render json: {'status' => false,'error' => 'Shop not found or missing shop'}, status: 422
	end
end

#burst_cacheObject



106
107
108
109
# File 'app/controllers/app_manager/plans_controller.rb', line 106

def burst_cache
	AppManager.clear_cache
			head :ok	
end

#fail_safe_backupObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/controllers/app_manager/plans_controller.rb', line 112

def fail_safe_backup
	params_permit
	Thread.new do
 	@fs = AppManager::FailSafe.new
 	begin
 		@fs.sync_app_manager	
 	rescue Exception => e
 		Rails.logger.info "APP MANGAGER >>>> LOCAL DB couldn't sync with POTAL DB #{e.inspect}"
 	end

 	begin
 	@fs.save_api_data(params)
 	rescue Exception => e
 	Rails.logger.info "APP MANGAGER >>>> #{e.inspect}"	
 	end
 	AppManager.clear_cache
 end
	head :ok
end

#indexObject



6
7
8
# File 'app/controllers/app_manager/plans_controller.rb', line 6

def index
			render :json => {"features" => AppManager.configuration.plan_features || []}
end

#plansObject



11
12
13
14
15
16
17
18
19
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
57
58
59
60
61
62
# File 'app/controllers/app_manager/plans_controller.rb', line 11

def plans
	active_plan_id_or_name = shopify_plan = plan = nil
			default_plan_id = nil
			plan_obj = AppManager::Client.new
			plans = plan_obj.get_plans
			if params[:shop_domain].present? && !AppManager.configuration.plan_features.nil?
				@shop = shop_data
				if !@shop.nil?
active_plan_id_or_name = @shop[@plan_field] 
if !@field_names.nil? && @field_names.has_key?('shopify_plan') && !@field_names.nil?
shopify_plan_field = AppManager.configuration.field_names['shopify_plan']
shopify_plan = @shop[shopify_plan_field] rescue nil
plan = plans.any? && !active_plan_id_or_name.nil? ? (plans.find{|x| x["id"] == active_plan_id_or_name}.present? ? plans.find{|x| x["id"] == active_plan_id_or_name} : nil ) : nil
   end
				else
Rails.logger.info "APP MANGAGER >>>>  Either model is defined wrong or config.plan_id_or_name_field is nil in initializer ==="	
				end	
			else
		 Rails.logger.info "APP MANGAGER >>>>  Either params missing store_domain or config.plan_features is nil in initializer ==="	
			end


			default_plan_id = 0;
			default_plan_data = plans.select{|x| x['interval'] == 'EVERY_30_DAYS' }.sort_by { |k| -k["price"] }
			store_base_plan = default_plan_data.map{|e| e['store_base_plan']}.first rescue false 

			if store_base_plan
				begin
shopify_plans = default_plan_data.map { |e| e.map { |key, value| [e['shopify_plans'], e['id']] }.to_h }
shopify_plans.each do |shp|
	if shp.keys && shp.keys.first && shp.keys.first.class == Array
		if shp.keys.first.include?(shopify_plan) && shp.values && shp.values.first && shp.values.first.to_i > 0
			default_plan_id = shp.values.first.to_i
			break
		end
	end
end
				rescue Exception => e
Rails.logger.info "APP MANGAGER >>>> ERROR #{e.inspect}"				
				end
			else
				default_plan_id = default_plan_data.map{|e| e['id']}.first if default_plan_data.any? rescue nil
			end

			response = {
				'plans' => plan_obj.get_plans,
				'shopify_plan' => shopify_plan,
				'plan' => plan,
				'default_plan_id' => default_plan_id
			}
			render json: response
end

#usersObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/app_manager/plans_controller.rb', line 64

def users
	if model
		if params[:search]
		search = params[:search]
		@shopify_email = AppManager.configuration.field_names['shopify_email']
		data = model.where("#{@shopify_domain} LIKE :search OR #{@shopify_email} LIKE :search", search: "%#{search}%").page(params[:page]).per(10)
		else
		data = model.page(params[:page]).per(10)
		end
		users = {
		"current_page" => data && data.current_page ? data.current_page : 0,
		"data" => data ? data.collect{|st| @field_names.map{ |key, value| [key, "#{st[value]}"] }.to_h } : [],
		"first_page_url" => "#{app_url}/api/app-manager/users?page=1",
		"from" => data && data.total_pages ? 1 : 0,
		"last_page" => data && data.total_pages ? data.total_pages : 0,
		"last_page_url" => data && data.total_pages ? "#{app_url}/api/app-manager/users?page=#{data.total_pages}" : nil,
		"links"=> '',
		"next_page_url" => data && data.next_page ? "#{app_url}/api/app-manager/users?page=#{data.next_page}" : nil,
		"path" => "#{app_url}/api/app-manager/users", 
		"per_page" => 1,
		"prev_page_url" => data && data.prev_page ? "#{app_url}/api/app-manager/users?page=#{data.prev_page}" : nil,
		"to" => data && data.total_pages ? data.total_pages : 0,
		"total" => data && data.total_pages ? data.total_pages : 0,
		}
		render json: users

	else	
		render json: nil
	end	
end