Guest User

Untitled

a guest
Jun 20th, 2018
4,832
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. namespace :premium_users do
  2. desc 'Get the user id & data in a hash'
  3. task get_users: :environment do
  4. users = Medic.includes(:payment_methods).where.not payment_methods: { period_end_date: nil }
  5. request_hash = {}
  6.  
  7. users.each do |user|
  8. period_end_date = user.payment_methods.last.period_end_date
  9.  
  10. if Date.today - period_end_date <= 172_800
  11. openpay_customer_id = user.payment_methods.last.openpay_customer_id
  12. openpay_subscription_id = user.payment_methods.last.openpay_subscription_id
  13. medic_id = user.payment_methods.last.medic_id
  14. user_hash = {}
  15. user_hash[medic_id] = [openpay_customer_id, openpay_subscription_id]
  16. else
  17. user_hash = {}
  18. end
  19.  
  20. request_hash.merge!(user_hash) # If no users is empty => {}
  21.  
  22. if request_hash.empty?
  23. puts 'No User to check!'
  24. else
  25. request_hash.each_value do |value|
  26. openpay_customer_id = value[0]
  27. openpay_subscription_id = value[1]
  28.  
  29. @openpay = OpenpayApi.new(ENV['MERCHANT_ID_OPENPAY'], ENV['SECRET_KEY_OPENPAY'])
  30. @subscriptions = @openpay.create(:subscriptions)
  31. response_hash = @subscriptions.get(openpay_subscription_id, openpay_customer_id)
  32.  
  33. @charge_date, @creation_date, @current_period_number, @period_end_date, @status, @trial_end_date = response_hash.values_at('charge_date', 'creation_date', 'current_period_number', 'period_end_date', 'status', 'trial_end_date')
  34.  
  35. case @status
  36. when 'past_due'
  37. @premium = false
  38. @cancelled = Time.now
  39. when 'unpaid'
  40. @premium = false
  41. @cancelled = Time.now
  42. when 'cancelled'
  43. @premium = false
  44. @cancelled = Time.now
  45. else
  46. @premium = true
  47. @cancelled = nil
  48. end
  49.  
  50. end
  51.  
  52. @payment_method = PaymentMethod.update(
  53. premium: @premium,
  54. charge_date: @charge_date,
  55. creation_date: @creation_date,
  56. current_period_number: @current_period_number,
  57. period_end_date: @period_end_date,
  58. status: @status,
  59. trial_end_date: @trial_end_date,
  60. cancelled_on: @cancelled
  61. )
  62.  
  63. puts "User #{user.email} update, Complete!"
  64.  
  65. end
  66. end
  67. end
  68. end
  69.  
  70. class Medic < ApplicationRecord
  71. has_many :payment_methods, dependent: :destroy
  72. end
  73.  
  74. class PaymentMethod < ApplicationRecord
  75. belongs_to :medic, optional: true
  76. end
  77.  
  78. SQL (0.3ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:15:13', `updated_at` = '2018-06-15 21:01:19' WHERE `payment_methods`.`id` = 1
  79. (11.9ms) COMMIT
  80. (0.1ms) BEGIN
  81.  
  82. SQL (0.3ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:15:13', `updated_at` = '2018-06-15 21:01:19' WHERE `payment_methods`.`id` = 2
  83. (2.7ms) COMMIT
  84. (0.1ms) BEGIN
  85.  
  86. SQL (0.2ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:15:13', `updated_at` = '2018-06-15 21:01:19' WHERE `payment_methods`.`id` = 3
  87. (2.6ms) COMMIT
  88. User medic1@mail.com update, Complete!
  89.  
  90. PaymentMethod Load (0.3ms) SELECT `payment_methods`.* FROM `payment_methods`
  91. (0.1ms) BEGIN
  92.  
  93. SQL (0.3ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:16:24', `updated_at` = '2018-06-15 21:01:20' WHERE `payment_methods`.`id` = 1
  94. (11.9ms) COMMIT
  95. (0.1ms) BEGIN
  96.  
  97. SQL (0.3ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:16:24', `updated_at` = '2018-06-15 21:01:20' WHERE `payment_methods`.`id` = 2
  98. (2.7ms) COMMIT
  99. (0.1ms) BEGIN
  100.  
  101. SQL (0.3ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:16:24', `updated_at` = '2018-06-15 21:01:20' WHERE `payment_methods`.`id` = 3
  102. (2.5ms) COMMIT
  103. User medic2@mail.com update, Complete!
  104.  
  105. PaymentMethod Load (0.3ms) SELECT `payment_methods`.* FROM `payment_methods`
  106. (0.1ms) BEGIN
  107.  
  108. SQL (0.2ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:18:30', `updated_at` = '2018-06-15 21:01:22' WHERE `payment_methods`.`id` = 1
  109. (2.6ms) COMMIT
  110. (0.1ms) BEGIN
  111.  
  112. SQL (0.2ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:18:30', `updated_at` = '2018-06-15 21:01:22' WHERE `payment_methods`.`id` = 2
  113. (2.6ms) COMMIT
  114. (0.1ms) BEGIN
  115.  
  116. SQL (0.3ms) UPDATE `payment_methods` SET `creation_date` = '2018-06-15 19:18:30', `updated_at` = '2018-06-15 21:01:22' WHERE `payment_methods`.`id` = 3
  117. (2.6ms) COMMIT
  118. User medic4@mail.com update, Complete!
Add Comment
Please, Sign In to add comment