Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. # frozen_string_literal: true
  2.  
  3. class ChangePackage
  4. include Failable
  5.  
  6. attr_reader :subscription, :package, :expire_date, :package_history
  7. private *delegate :store_campagin!, :upcoming_package_change?
  8. :current_invoice, to: :subscription
  9.  
  10. def initialize(subscription, package)
  11. @subscription = subscription
  12. @package = package
  13. @expire_date = Subscription::CaculateLastCycle.call(subscription)
  14. @package_history = PackageHistory.new(
  15. package: package,
  16. subscription: subscription,
  17. expire_date: expire_date
  18. )
  19. @cdrator = Cdrator::ChangePackage.new(
  20. cdrator_id: subscription.cdrator_id,
  21. product_id: package.cdrator_product_id,
  22. rate_plan_id: package.cdrator_rate_plan_id,
  23. new_campaigns: package_history.request_params,
  24. expire_campaigns: package_history.expire_campaigns
  25. )
  26. end
  27.  
  28. def call
  29. check_invoice
  30. check_upcoming_package_change
  31. change_package!
  32. cancel_extend_package!
  33. create_package_history!
  34. update_subscription_previous_campaigns!
  35. end
  36.  
  37. def check_invoice
  38. fail!('invoice.not_opened') unless invoice_open?
  39. end
  40.  
  41. def check_upcoming_package_change
  42. fail!(:upcoming_package_change_exists) if upcoming_package_change?
  43. end
  44.  
  45. def change_package
  46. fail!(cdrator.result) unless cdrator.call
  47. end
  48.  
  49. def cancel_extend_package!
  50. return unless extending_package?
  51.  
  52. Subscription::CancelExtendPackageJob.perform_later(subscription)
  53. end
  54.  
  55. def create_package_history!
  56. package_history.update!(cdrator_response: cdrator.result)
  57. rescue ActiveRecord::RecordInvalid => failure
  58. fail!(failure)
  59. end
  60.  
  61. def store_campagin!
  62. store_campagin!
  63. rescue ActiveRecord::RecordInvalid => failure
  64. fail!(failure)
  65. end
  66.  
  67. private def invoice_open?
  68. current_invoice&.open?
  69. end
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement