module Spree module Admin module SolidusBling class AccountsController < BaseController before_action :set_account, only: [:show, :edit, :update, :destroy] def index @accounts = ::SolidusBling::Account.all end def show end def new if ::SolidusBling::Account.any? flash[:error] = "Você só pode ter uma conta Bling cadastrada" redirect_to admin_solidus_bling_accounts_path end @bling_account = ::SolidusBling::Account.new @payment_methods = ::Spree::PaymentMethod.all.pluck(:name, :id) @shipping_methods = ::Spree::ShippingMethod.all.pluck(:name, :id) @bling_account.payment_methods.build @bling_account.shipping_methods.build @bling_account.sellers.build end def create bling_account = ::SolidusBling::Account.new account_params if bling_account.save flash[:success] = "Conta criada com sucesso" redirect_to admin_solidus_bling_accounts_path else flash[:error] = bling_account.errors.full_messages.join("\n") redirect_to new_admin_solidus_bling_account_path end end def edit @payment_methods = ::Spree::PaymentMethod.all.pluck(:name, :id) @shipping_methods = ::Spree::ShippingMethod.all.pluck(:name, :id) @sellers = @bling_account.sellers.actives.order(:name).pluck(:name, :id) end def update @bling_account.sellers.find_by_id(params[:account]["default_seller"]).update(default: true) @bling_account.update account_params redirect_to edit_admin_solidus_bling_account_path(@bling_account.id) end def destroy @bling_account.delete redirect_to admin_solidus_bling_accounts_path end def authorize bling_account = ::SolidusBling::Account.first code = params[:code] unless ::SolidusBling::Token.new.authorize(bling_account, code) bling_account.errors.add(:base, "Erro ao autorizar aplicativo 🦆") flash[:error] = bling_account.errors.full_messages.join("\n") end redirect_to edit_admin_solidus_bling_account_path(bling_account.id) end def upsert_sellers ::SolidusBling::UpsertSellersJob.perform_later flash[:success] = "Vendedores atualizados" redirect_to admin_solidus_bling_accounts_path end private def set_account @bling_account = ::SolidusBling::Account.find(params[:id]) end def account_params params.require(:account).permit( :client_id, :client_secret, :refresh_token, :redirect_url, :state, :access_token, :api_key, :external_store_id, payment_methods_attributes: [:id, :spree_payment_method_id, :external_id, :incoming_category_id], shipping_methods_attributes: [:id, :spree_shipping_method_id, :alias, :company], sellers_attributes: [:id, :name, :external_id] ) end end end end end