Class: Kaui::SubscriptionsController
- Inherits:
-
EngineController
- Object
- EngineController
- Kaui::SubscriptionsController
- Defined in:
- app/controllers/kaui/subscriptions_controller.rb
Instance Method Summary collapse
- #create ⇒ Object
- #create_usage ⇒ Object
- #destroy ⇒ Object
- #edit ⇒ Object
- #edit_bcd ⇒ Object
- #edit_quantity ⇒ Object
- #new ⇒ Object
- #record_usage ⇒ Object
- #reinstate ⇒ Object
- #restful_show ⇒ Object
- #show ⇒ Object
- #show_json ⇒ Object
- #update ⇒ Object
- #update_bcd ⇒ Object
- #update_quantity ⇒ Object
- #update_tags ⇒ Object
- #validate_bundle_external_key ⇒ Object
- #validate_external_key ⇒ Object
Instance Method Details
#create ⇒ Object
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/controllers/kaui/subscriptions_controller.rb', line 37 def create plan_name = params.require(:plan_name) @base_product_name = params[:base_product_name] @subscription = Kaui::Subscription.new(params.require(:subscription).permit!.to_h.compact_blank) begin @bundle, plans_details = lookup_bundle_and_plan_details(@subscription, @base_product_name) plan_details = plans_details.find { |p| p.plan == plan_name } raise "Unable to find plan #{plan_name}" if plan_details.nil? @subscription.plan_name = plan_name requested_date = params[:type_change] == 'DATE' ? params[:requested_date].presence : nil overrides = price_overrides(plan_details) @subscription.price_overrides = overrides if overrides.present? @subscription.quantity = params[:quantity].to_i if params[:quantity].present? && params[:quantity].to_i.positive? # un-set product_category since is not needed if plan name exist @subscription.product_category = nil @subscription = @subscription.create(current_user.kb_username, params[:reason], params[:comment], requested_date, false, ) redirect_to kaui_engine.account_bundles_path(@subscription.account_id), notice: 'Subscription was successfully created' rescue StandardError => e @plans = plans_details.nil? ? [] : plans_details.map(&:plan) if e.is_a?(::KillBillClient::API::BadRequest) && !e.response.nil? && !e.response.body.nil? = begin JSON.parse(e.response.body) rescue StandardError nil end if (!.nil? & !['code'].nil?) && ['code'] == 2010 # CAT_NO_PRICE_FOR_CURRENCY # Hack for lack of proper Kill Bill messaging (https://github.com/killbill/killbill-admin-ui/issues/266) flash.now[:error] = "Unable to create the subscription: a price for this currency hasn't been specified in the catalog" render :new and return end end flash.now[:error] = "Error while creating the subscription: #{as_string(e)}" render :new end end |
#create_usage ⇒ Object
187 188 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 |
# File 'app/controllers/kaui/subscriptions_controller.rb', line 187 def create_usage subscription_id = params.require(:id) unit_type = params[:unit_type].to_s.strip amount_raw = params[:amount].to_s.strip record_date = params[:record_date].to_s.strip # Input validation errors = [] errors << 'Unit type is required' if unit_type.blank? errors << 'Amount is required' if amount_raw.blank? amount = Integer(amount_raw, exception: false) errors << 'Amount must be a positive integer' if amount.nil? || amount <= 0 errors << 'Date/time of usage is required' if record_date.blank? parsed_date = parse_usage_date(record_date) if record_date.present? errors << 'Date/time of usage must be a valid date or datetime' if record_date.present? && parsed_date.nil? if errors.any? flash.now[:error] = errors.join('. ') @subscription = Kaui::Subscription.find_by_id(subscription_id, 'NONE', ) @unit_types = fetch_unit_types_from_subscription(@subscription) @unit_type = unit_type @amount = amount_raw @record_date = record_date @tracking_id = params[:tracking_id] render :record_usage and return end begin usage_record = KillBillClient::Model::UsageRecordAttributes.new usage_record.record_date = parsed_date.utc.iso8601 usage_record.amount = amount unit_usage_record = KillBillClient::Model::UnitUsageRecordAttributes.new unit_usage_record.unit_type = unit_type unit_usage_record.usage_records = [usage_record] usage = Kaui::Usage.new usage.subscription_id = subscription_id usage.tracking_id = params[:tracking_id].presence usage.unit_usage_records = [unit_usage_record] usage.create(current_user.kb_username, nil, nil, ) subscription = Kaui::Subscription.find_by_id(subscription_id, 'NONE', ) redirect_to kaui_engine.account_bundles_path(subscription.account_id), notice: 'Usage was successfully recorded' rescue StandardError => e Rails.logger.error("Failed to record usage for subscription #{subscription_id}: #{e.class}: #{e.}") Rails.logger.error(e.backtrace.join("\n")) if e.backtrace flash.now[:error] = "Error while recording usage: #{as_string(e)}" @subscription = Kaui::Subscription.find_by_id(subscription_id, 'NONE', ) @unit_types = fetch_unit_types_from_subscription(@subscription) @unit_type = unit_type @amount = amount_raw @record_date = record_date @tracking_id = params[:tracking_id] render :record_usage end end |
#destroy ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'app/controllers/kaui/subscriptions_controller.rb', line 112 def destroy requested_date = params[:requested_date].presence billing_policy = params[:policy].presence # START_OF_TERM is *not* a valid entitlement_policy and so would default to IMMEDIATE entitlement_policy = billing_policy && billing_policy == 'START_OF_TERM' ? 'IMMEDIATE' : billing_policy # true by default except default policy use_requested_date_for_billing = if requested_date (params[:use_requested_date_for_billing] || '1') == '1' else nil end subscription = Kaui::Subscription.find_by_id(params.require(:id), 'NONE', ) subscription.cancel(current_user.kb_username, params[:reason], params[:comment], requested_date, entitlement_policy, billing_policy, use_requested_date_for_billing, ) redirect_to kaui_engine.account_bundles_path(subscription.account_id), notice: 'Subscription was successfully cancelled' end |
#edit ⇒ Object
29 30 31 32 33 34 35 |
# File 'app/controllers/kaui/subscriptions_controller.rb', line 29 def edit @subscription = Kaui::Subscription.find_by_id(params.require(:id), 'NONE', ) _, plans_details = lookup_bundle_and_plan_details(@subscription) # Use a Set to deal with multiple pricelists @plans = Set.new.merge(plans_details.map(&:plan)) @plan_phases = build_plan_phases_map(plans_details) end |
#edit_bcd ⇒ Object
137 138 139 |
# File 'app/controllers/kaui/subscriptions_controller.rb', line 137 def edit_bcd @subscription = Kaui::Subscription.find_by_id(params.require(:id), 'NONE', ) end |
#edit_quantity ⇒ Object
153 154 155 |
# File 'app/controllers/kaui/subscriptions_controller.rb', line 153 def edit_quantity @subscription = Kaui::Subscription.find_by_id(params.require(:id), 'NONE', ) end |
#new ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'app/controllers/kaui/subscriptions_controller.rb', line 9 def new @base_product_name = params[:base_product_name] @subscription = Kaui::Subscription.new(bundle_id: params[:bundle_id], account_id: params[:account_id], product_category: params[:product_category] || 'BASE') @bundle, plans_details = lookup_bundle_and_plan_details(@subscription, @base_product_name) @plans = plans_details.map(&:plan) @plan_phases = build_plan_phases_map(plans_details) return unless @plans.empty? flash[:error] = if @subscription.product_category == 'BASE' 'No plan available in the catalog' else "No add-on available in the catalog for product #{@base_product_name}" end redirect_to kaui_engine.account_bundles_path(@subscription.account_id), error: 'No available plan' end |
#record_usage ⇒ Object
182 183 184 185 |
# File 'app/controllers/kaui/subscriptions_controller.rb', line 182 def record_usage @subscription = Kaui::Subscription.find_by_id(params.require(:id), 'NONE', ) @unit_types = fetch_unit_types_from_subscription(@subscription) end |
#reinstate ⇒ Object
129 130 131 132 133 134 135 |
# File 'app/controllers/kaui/subscriptions_controller.rb', line 129 def reinstate subscription = Kaui::Subscription.find_by_id(params.require(:id), 'NONE', ) subscription.uncancel(current_user.kb_username, params[:reason], params[:comment], ) redirect_to kaui_engine.account_bundles_path(subscription.account_id), notice: 'Subscription was successfully reinstated' end |
#restful_show ⇒ Object
246 247 248 249 |
# File 'app/controllers/kaui/subscriptions_controller.rb', line 246 def restful_show subscription = Kaui::Subscription.find_by_id(params.require(:id), 'NONE', ) redirect_to kaui_engine.account_bundles_path(subscription.account_id) end |
#show ⇒ Object
5 6 7 |
# File 'app/controllers/kaui/subscriptions_controller.rb', line 5 def show restful_show end |
#show_json ⇒ Object
251 252 253 254 255 256 257 258 |
# File 'app/controllers/kaui/subscriptions_controller.rb', line 251 def show_json raw_body = Kaui::Subscription.find_raw_by_id(params.require(:id), 'NONE', ) render body: raw_body, content_type: 'application/json' rescue KillBillClient::API::ResponseError => e render body: e.response.body, content_type: 'application/json', status: e.code rescue StandardError => e render json: { error: e. }, status: :internal_server_error end |
#update ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'app/controllers/kaui/subscriptions_controller.rb', line 80 def update plan_name = params.require(:plan_name) requested_date = params[:type_change] == 'DATE' ? params[:requested_date].presence : nil billing_policy = params[:type_change] == 'POLICY' ? params[:policy].presence : nil wait_for_completion = params[:wait_for_completion] == '1' subscription = Kaui::Subscription.find_by_id(params.require(:id), 'NONE', ) input = { planName: plan_name } _, plans_details = lookup_bundle_and_plan_details(subscription) plan_details = plans_details.find { |p| p.plan == plan_name } overrides = plan_details ? price_overrides(plan_details) : nil input[:priceOverrides] = overrides if overrides.present? subscription.change_plan(input, current_user.kb_username, params[:reason], params[:comment], requested_date, billing_policy, nil, wait_for_completion, ) redirect_to kaui_engine.account_bundles_path(subscription.account_id), notice: 'Subscription plan successfully changed' rescue StandardError => e redirect_to edit_subscription_path(params.require(:id)), flash: { error: "Error while changing subscription: #{as_string(e)}" } end |
#update_bcd ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 |
# File 'app/controllers/kaui/subscriptions_controller.rb', line 141 def update_bcd input_subscription = params.require(:subscription) subscription = Kaui::Subscription.new subscription.subscription_id = params.require(:id) subscription.bill_cycle_day_local = input_subscription['bill_cycle_day_local'] effective_from_date = params['effective_from_date'] subscription.update_bcd(current_user.kb_username, params[:reason], params[:comment], effective_from_date, nil, ) redirect_to kaui_engine.account_bundles_path(input_subscription['account_id']), notice: 'Subscription BCD was successfully changed' end |
#update_quantity ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'app/controllers/kaui/subscriptions_controller.rb', line 157 def update_quantity id = params.require(:id) input_subscription = params.require(:subscription) quantity_raw = input_subscription['quantity'].to_s.strip quantity = Integer(quantity_raw, exception: false) if quantity.nil? || quantity <= 0 flash.now[:error] = 'Quantity must be a positive integer' @subscription = Kaui::Subscription.find_by_id(id, 'NONE', ) @subscription.quantity = quantity_raw render :edit_quantity and return end subscription = Kaui::Subscription.new subscription.subscription_id = id subscription.quantity = quantity effective_from_date = params['effective_from_date'] subscription.update_quantity(current_user.kb_username, params[:reason], params[:comment], effective_from_date, nil, ) redirect_to kaui_engine.account_bundles_path(input_subscription['account_id']), notice: 'Subscription quantity was successfully changed' rescue ActionController::ParameterMissing redirect_to kaui_engine.edit_quantity_path(params[:id]), flash: { error: 'Required parameter missing: subscription' } end |
#update_tags ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'app/controllers/kaui/subscriptions_controller.rb', line 288 def subscription_id = params.require(:id) subscription = Kaui::Subscription.find_by_id(subscription_id, 'NONE', ) = [] params.each_key do |key| next unless key.include? 'tag' tag_info = key.split('_') next if (tag_info.size != 2) || (tag_info[0] != 'tag') << tag_info[1] end Kaui::Tag.set_for_subscription(subscription_id, , current_user.kb_username, params[:reason], params[:comment], ) redirect_to kaui_engine.account_bundles_path(subscription.account_id), notice: 'Subscription tags successfully set' end |
#validate_bundle_external_key ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'app/controllers/kaui/subscriptions_controller.rb', line 260 def validate_bundle_external_key json_response do external_key = params.require(:external_key) begin bundle = Kaui::Bundle.find_by_external_key(external_key, false, ) rescue KillBillClient::API::NotFound bundle = nil end { is_found: !bundle.nil? } end end |
#validate_external_key ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'app/controllers/kaui/subscriptions_controller.rb', line 274 def validate_external_key json_response do external_key = params.require(:external_key) begin subscription = Kaui::Subscription.find_by_external_key(external_key, 'NONE', ) rescue KillBillClient::API::NotFound subscription = nil end { is_found: !subscription.nil? } end end |