Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class AccountsController < ApplicationController
- protect_from_forgery except: :webhook
- skip_before_action :require_login, only: [:new, :create]
- def index
- end
- def new
- @account = Account.new
- @account.admin = Employee.new
- @admin = @account.admin
- @admin.is_admin = true
- @admin.build_address
- @admin.build_primary_phone
- @admin.build_secondary_phone
- @admin.build_primary_fax
- end
- def create
- @account = Account.create(account_params)
- @admin = @account.admin(params[:employee_id])
- if @account.save
- @admin.update!(account: @account)
- auto_login(@admin)
- AdminMailer.welcome(@admin).deliver_later
- send_daily_summary
- flash[:success] = "Welcome #{ @admin.first_name.capitalize } please select a subscription plan below."
- redirect_to new_subscription_path
- # redirect_back_or_to new_subscription_path
- else
- flash.now[:error] = "There was a problem with the form."
- render :new
- end
- end
- def show
- @account = Account.find(params[:id])
- end
- def edit
- @account = Account.find(params[:id])
- @admin = @account.admin
- @subscriber = @account.subscription
- end
- def update
- @account = Account.find(params[:id])
- if @account.update_attributes(account_params)
- flash[:success] = "Account has been updated!"
- redirect_to(root_path)
- else
- flash.now[:error] = "Unable to update form."
- render(:edit)
- end
- end
- def destroy
- @account = Account.find(params[:id])
- @admin = @account.admin
- flash.now[:warning] = "You sure? This will remove all employee & resident records for this account."
- if @account.delete && @admin.delete
- AdminMailer.farewell(@admin).deliver
- redirect_to(root_path)
- flash[:info] = "Sorry to see you leave :-("
- end
- end
- def webhook
- @account = Account.find(params[:id])
- @admin = @account.admin
- event = Stripe::Event.retrieve(params["id"])
- case event.type
- when "invoice.payment_succeeded"
- AdminMailer.subscription_payment_received(@admin)
- when "invoice.payment_failed"
- AdminMailer.subscription_payment_declined(@admin)
- when "customer.source.updated"
- AdminMailer.card_updated(@admin)
- when "customer.subscription.deleted"
- AdminMailer.subscription_cancellation_notice(@admin)
- end
- render status: :ok, json: "success"
- end
- private
- def send_daily_summary
- DailySummaryJob.perform_later(@admin.id)
- end
- def account_params
- params.require(:account).permit(
- :id,
- :facility_name,
- :subdomain,
- :license_number,
- :daily_summary_dispatch_time,
- subscription_attributes:
- [
- :last_4_digits,
- :stripe_customer_token,
- :plan_id, :employee_id,
- :stripe_card_token
- ],
- admin_attributes:
- [
- :account_id,
- :avatar,
- :avatar_id,
- :avatar_size,
- :avatar_content_type,
- :remove_avatar,
- :id,
- :_destroy,
- :is_admin,
- :email,
- :first_name,
- :last_name,
- :dob,
- :gender,
- :password,
- :password_confirmation,
- address_attributes:
- [:id, :_destroy, :street_address, :city, :state, :zip],
- primary_phone_attributes:
- [:id, :_destroy, :area_code, :number],
- secondary_phone_attributes:
- [:id, :_destroy, :area_code, :number],
- primary_fax_attributes:
- [:id, :_destroy, :area_code, :number]
- ]
- )
- end
- end
Add Comment
Please, Sign In to add comment