Module: AppManager::Model

Extended by:
ActiveSupport::Concern
Defined in:
lib/app_manager/model.rb

Constant Summary collapse

@@remaining_cache_key =
"get_remaining_days_key"
@@get_charge_key =
"get_charge_key"

Instance Method Summary collapse

Instance Method Details

#cancel_chargeObject



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/app_manager/model.rb', line 148

def cancel_charge
    begin
        shop_plan_id = self[AppManager.configuration.plan_id_or_name_field]
        shop_domain = self[AppManager.configuration.shopify_domain_field]
        if shop_plan_id && shop_domain
            plan_obj = AppManager::Client.new
            plan_obj.cancel_charge(shop_domain,shop_plan_id)
        end
    rescue Exception => e
        return "#{e.inspect}" 
    end     
end

#fetch_cached_static_get_charge(cache_key) ⇒ Object



239
240
241
242
243
# File 'lib/app_manager/model.rb', line 239

def fetch_cached_static_get_charge(cache_key)
    if Rails.cache.read(cache_key).present?
      return Rails.cache.read(cache_key) 
    end
end

#fetch_cached_static_remaining_days(cache_key) ⇒ Object



221
222
223
224
225
# File 'lib/app_manager/model.rb', line 221

def fetch_cached_static_remaining_days(cache_key)
  if Rails.cache.read(cache_key).present?
    return Rails.cache.read(cache_key) 
  end
end

#fetch_static_get_chargeObject



229
230
231
232
233
234
235
236
237
# File 'lib/app_manager/model.rb', line 229

def fetch_static_get_charge
    @get_charge_key = "app-manager-cache/#{self.shopify_domain}-get_charge"
    if fetch_cached_static_get_charge(@get_charge_key).present?
      return fetch_cached_static_get_charge(@get_charge_key)
    end
    charge = self.get_charge
    Rails.cache.write(@get_charge_key, charge, expires_in: AppManager.configuration.expires_in)
    return charge
end

#fetch_static_remaining_daysObject



211
212
213
214
215
216
217
218
219
# File 'lib/app_manager/model.rb', line 211

def fetch_static_remaining_days
  @remaining_cache_key = "app-manager-cache/#{self.shopify_domain}-remaining_days"
  if fetch_cached_static_remaining_days(@remaining_cache_key).present?
    return fetch_cached_static_remaining_days(@remaining_cache_key)
  end
  remaining_days = self.get_remaining_days
  Rails.cache.write(@remaining_cache_key, remaining_days, expires_in: AppManager.configuration.expires_in)
  return remaining_days
end

#get_all_plansObject



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/app_manager/model.rb', line 135

def get_all_plans
    begin
        shop_domain = self[AppManager.configuration.shopify_domain_field]
        shop_plan_id = self[AppManager.configuration.plan_id_or_name_field]
        active_plan_id = shop_plan_id && shop_plan_id.nil? ? nil : shop_plan_id
        plan_obj = AppManager::Client.new
        return plan_obj.get_plans(shop_domain,active_plan_id)
    rescue Exception => e
        Rails.logger.info "#{e.inspect}" 
        return []
    end
end

#get_chargeObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/app_manager/model.rb', line 112

def get_charge
    shop_plan_id = self[AppManager.configuration.plan_id_or_name_field]
    found_free_plan = false
    if shop_plan_id && !shop_plan_id.nil?
        plan_obj = AppManager::Client.new 
        plan = plan_obj.get_plan(shop_plan_id) rescue nil
        if plan && plan['price'] == 0 && plan['public'] == true
            found_free_plan = true
            return nil
        end
    end
    if !found_free_plan
        begin
            shop_domain = self[AppManager.configuration.shopify_domain_field]
            plan_obj = AppManager::Client.new
          return  plan_obj.get_charge(shop_domain) 
        rescue Exception => e
          Rails.logger.info "#{e.inspect}" 
          return nil
        end
    end
end

#get_feature(slug) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/app_manager/model.rb', line 72

def get_feature(slug)
    plan_features = self.plan_features
    features = plan_features.select{|x| x['slug'].to_s == slug }
	if features.any? 
        feature = features.first
        return casted_value(feature['value'],feature['value_type'])
    else
        nil
    end
end

#get_plan(plan_id = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/app_manager/model.rb', line 95

def get_plan(plan_id=nil)
    req_plan_id = nil
    if !plan_id.nil? 
        req_plan_id = plan_id
    else
        shop_plan_id = self[AppManager.configuration.plan_id_or_name_field]
        if shop_plan_id && !shop_plan_id.nil?
          req_plan_id = shop_plan_id
        else
          Rails.logger.info "Invalid params, must have charge_id,shop && plan"
          return {}
        end
    end
    plan_obj = AppManager::Client.new if req_plan_id
    return req_plan_id ? plan_obj.get_plan(req_plan_id) : {}
end

#get_plans_by_features(feature_slugs, params = "false") ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/app_manager/model.rb', line 186

def get_plans_by_features(feature_slugs,params="false")
    if feature_slugs.any?
        shop_plan_id = self[AppManager.configuration.plan_id_or_name_field]
        plan_data = []
        shop_domain = self[AppManager.configuration.shopify_domain_field]
        plan_obj = AppManager::Client.new
        plans = plan_obj.get_plans(shop_domain)
        plans = plans.select{|x| x['interval'] == 'EVERY_30_DAYS'  && x['public'] == true}
        plans.each do |plan|
            next if (params && !params.nil? && params == "exclude_current_plan" && !shop_plan_id.nil? && plan["id"] == shop_plan_id)
            if plan && (plan['features'] && plan['features'].any?) && (plan['features'].values && plan['features'].values.any?) && (plan['features'].values.collect{|c| c['slug']} && plan['features'].values.collect{|c| c['slug']}.any?)
             plan_feature_slugs = plan['features'].values.collect{|c| c['slug']}.sort
             if (feature_slugs & plan_feature_slugs).any? && ((feature_slugs & plan_feature_slugs).sort == feature_slugs.sort)
                    plan_data.push(plan)      
             end
            end
        end
        return plan_data
    else
        return []
    end
end

#get_remaining_daysObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/app_manager/model.rb', line 83

def get_remaining_days
        shop_domain = self[AppManager.configuration.shopify_domain_field]
        trial_activated_at_field = AppManager.configuration.field_names['trial_activated_at']
        trial_activated_at_val = self[trial_activated_at_field]
        plan_field = AppManager.configuration.field_names['plan_id']
        plan_id = self[plan_field]
        plan_obj = AppManager::Client.new
        res = plan_obj.get_remaining_days(shop_domain,trial_activated_at_val,plan_id)
        return (res && !res.nil?) ? res : 0
end

#has_feature(slug) ⇒ Object



67
68
69
# File 'lib/app_manager/model.rb', line 67

def has_feature(slug)
	self.plan_features.any? && self.plan_features.select{|x| x['slug'].to_s == slug }.size > 0 ? true : false
end

#has_planObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/app_manager/model.rb', line 31

def has_plan
    plan_id = self[AppManager.configuration.plan_id_or_name_field]
    if !plan_id
        return false;
    end
    grandfathered = self[AppManager.configuration.field_names['grandfathered']] 
    grandfathered = grandfathered ? 1 : 0
    shop_domain = self[AppManager.configuration.shopify_domain_field]
    trial_activated_at_field = AppManager.configuration.field_names['trial_activated_at']
    trial_activated_at_val = self[trial_activated_at_field]
    plan_obj = AppManager::Client.new
    response = plan_obj.has_plan(shop_domain,plan_id,trial_activated_at_val,grandfathered)
    return (response && response['has_plan']) || false
end

#has_plan_oldObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/app_manager/model.rb', line 8

def has_plan_old
    if !self[AppManager.configuration.plan_id_or_name_field]
        return false;
    end 
    if self[AppManager.configuration.field_names['grandfathered']]
        return true;
    end
    plan_id = self[AppManager.configuration.plan_id_or_name_field]
    if !plan_id
        Rails.logger.info "Plan id found nil or not set"
        return false;
    end
    remaining_days = fetch_static_remaining_days
    if remaining_days > 0
        return true
    end
    # plan_obj = AppManager::Client.new
    # shop_domain = self[AppManager.configuration.shopify_domain_field]
    # active_charge = plan_obj.get_charge(shop_domain) rescue nil
    active_charge = fetch_static_get_charge
    return active_charge && active_charge['active_charge'].present? && !active_charge['active_charge'].nil? ? true : false
end

#plan_featuresObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/app_manager/model.rb', line 47

def plan_features
	plan_id = self[AppManager.configuration.plan_id_or_name_field]
	if !plan_id
        Rails.logger.info "Plan id found nil or not set in DB"
		return []
	end
		plan_obj = AppManager::Client.new
		plans = plan_obj.get_plan(plan_id)
		if plans && plans['features'].blank?
			return []
		end
	features_by_plan = plans['features'].map { |h| h.slice('value', 'feature_id') }
		all_features = AppManager.configuration.plan_features
    feature_plan_ids = features_by_plan.map{|c| c['feature_id']}
    pf = all_features.select{|x| feature_plan_ids.include?(x['uuid'])}.each{|g| g["value"] = features_by_plan.find{|e| e['feature_id'] == g['uuid']}['value'] } 
    return pf
end

#set_default_plan(plan_id = nil) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/app_manager/model.rb', line 161

def set_default_plan(plan_id=nil)
    begin
        if plan_id.nil?
            shop_domain = self[AppManager.configuration.shopify_domain_field]
            plan_obj = AppManager::Client.new
            plans = plan_obj.get_plans(shop_domain)
            free_plans = plans.select{|x| x['price'] == 0 && x['public'] == true}
            if free_plans.any?
                self.plan_id = free_plans.first["id"]
                self.save
            else
                raise Error, "Free Plan is not available"
            end
        else
            self.plan_id = plan_id
            self.save
        end
    rescue Exception => e
        return "#{e.inspect}" 
    end
end