Guest User

Untitled

a guest
Oct 19th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. class AccountsController < ApplicationController
  2. protect_from_forgery except: :webhook
  3. skip_before_action :require_login, only: [:new, :create]
  4.  
  5. def index
  6. end
  7.  
  8. def new
  9. @account = Account.new
  10. @account.admin = Employee.new
  11. @admin = @account.admin
  12. @admin.is_admin = true
  13. @admin.build_address
  14. @admin.build_primary_phone
  15. @admin.build_secondary_phone
  16. @admin.build_primary_fax
  17. end
  18.  
  19. def create
  20. @account = Account.create(account_params)
  21. @admin = @account.admin(params[:employee_id])
  22. if @account.save
  23. @admin.update!(account: @account)
  24. auto_login(@admin)
  25. AdminMailer.welcome(@admin).deliver_later
  26. send_daily_summary
  27. flash[:success] = "Welcome #{ @admin.first_name.capitalize } please select a subscription plan below."
  28. redirect_to new_subscription_path
  29. # redirect_back_or_to new_subscription_path
  30. else
  31. flash.now[:error] = "There was a problem with the form."
  32. render :new
  33. end
  34. end
  35.  
  36. def show
  37. @account = Account.find(params[:id])
  38. end
  39.  
  40. def edit
  41. @account = Account.find(params[:id])
  42. @admin = @account.admin
  43. @subscriber = @account.subscription
  44. end
  45.  
  46. def update
  47. @account = Account.find(params[:id])
  48. if @account.update_attributes(account_params)
  49. flash[:success] = "Account has been updated!"
  50. redirect_to(root_path)
  51. else
  52. flash.now[:error] = "Unable to update form."
  53. render(:edit)
  54. end
  55. end
  56.  
  57. def destroy
  58. @account = Account.find(params[:id])
  59. @admin = @account.admin
  60. flash.now[:warning] = "You sure? This will remove all employee & resident records for this account."
  61. if @account.delete && @admin.delete
  62. AdminMailer.farewell(@admin).deliver
  63. redirect_to(root_path)
  64. flash[:info] = "Sorry to see you leave :-("
  65. end
  66. end
  67.  
  68. def webhook
  69. @account = Account.find(params[:id])
  70. @admin = @account.admin
  71. event = Stripe::Event.retrieve(params["id"])
  72.  
  73. case event.type
  74. when "invoice.payment_succeeded"
  75. AdminMailer.subscription_payment_received(@admin)
  76. when "invoice.payment_failed"
  77. AdminMailer.subscription_payment_declined(@admin)
  78. when "customer.source.updated"
  79. AdminMailer.card_updated(@admin)
  80. when "customer.subscription.deleted"
  81. AdminMailer.subscription_cancellation_notice(@admin)
  82. end
  83. render status: :ok, json: "success"
  84. end
  85.  
  86. private
  87.  
  88. def send_daily_summary
  89. DailySummaryJob.perform_later(@admin.id)
  90. end
  91.  
  92. def account_params
  93. params.require(:account).permit(
  94. :id,
  95. :facility_name,
  96. :subdomain,
  97. :license_number,
  98. :daily_summary_dispatch_time,
  99. subscription_attributes:
  100. [
  101. :last_4_digits,
  102. :stripe_customer_token,
  103. :plan_id, :employee_id,
  104. :stripe_card_token
  105. ],
  106. admin_attributes:
  107. [
  108. :account_id,
  109. :avatar,
  110. :avatar_id,
  111. :avatar_size,
  112. :avatar_content_type,
  113. :remove_avatar,
  114. :id,
  115. :_destroy,
  116. :is_admin,
  117. :email,
  118. :first_name,
  119. :last_name,
  120. :dob,
  121. :gender,
  122. :password,
  123. :password_confirmation,
  124. address_attributes:
  125. [:id, :_destroy, :street_address, :city, :state, :zip],
  126. primary_phone_attributes:
  127. [:id, :_destroy, :area_code, :number],
  128. secondary_phone_attributes:
  129. [:id, :_destroy, :area_code, :number],
  130. primary_fax_attributes:
  131. [:id, :_destroy, :area_code, :number]
  132. ]
  133. )
  134. end
  135. end
Add Comment
Please, Sign In to add comment