From 2b4179abaab542b42919f01de648c0e4839ca8d3 Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Wed, 31 Jan 2024 11:43:52 +0200 Subject: [PATCH] added profile controller --- .../auctions/update_list_broadcast_service.rb | 6 +++- .../api/v1/profiles/passwords_controller.rb | 32 +++++++++++++++++++ app/controllers/api/v1/profiles_controller.rb | 23 +++++++++++++ app/models/invoice.rb | 18 ----------- app/views/auctions/index.json.jbuilder | 2 ++ config/routes.rb | 5 +++ 6 files changed, 67 insertions(+), 19 deletions(-) create mode 100644 app/controllers/api/v1/profiles/passwords_controller.rb create mode 100644 app/controllers/api/v1/profiles_controller.rb diff --git a/app/broadcasts/auctions/update_list_broadcast_service.rb b/app/broadcasts/auctions/update_list_broadcast_service.rb index ff3a24e08..749da21b5 100644 --- a/app/broadcasts/auctions/update_list_broadcast_service.rb +++ b/app/broadcasts/auctions/update_list_broadcast_service.rb @@ -16,6 +16,8 @@ def call private + # rubocop:disable Metrics/AbcSize + # rubocop:disable Metrics/MethodLength def post_call auction_json = { domain_name: auction.domain_name, @@ -25,7 +27,9 @@ def post_call highest_bid: auction.currently_winning_offer&.price.to_f, highest_bidder: auction.currently_winning_offer&.username, min_bids_step: auction.min_bids_step, - auction_type: auction&.platform + auction_type: auction&.platform, + enable_deposit: auction.enable_deposit, + requirement_deposit_in_cents: auction.requirement_deposit_in_cents } ActionCable.server.broadcast('auctions_api', { auction: auction_json }) diff --git a/app/controllers/api/v1/profiles/passwords_controller.rb b/app/controllers/api/v1/profiles/passwords_controller.rb new file mode 100644 index 000000000..0ce36489a --- /dev/null +++ b/app/controllers/api/v1/profiles/passwords_controller.rb @@ -0,0 +1,32 @@ +module Api + module V1 + module Profiles + class PasswordsController < ApplicationController + before_action :authenticate_user! + respond_to :json + + skip_before_action :verify_authenticity_token + + def update + puts params_for_update + + if current_user.valid_password?(params[:user][:current_password]) + if current_user.update(params_for_update) + bypass_sign_in(current_user) + render json: current_user, status: :ok + else + Rails.logger.info current_user.errors.inspect + render json: current_user.errors, status: :unprocessable_entity + end + else + render json: { errors: [t('.incorrect_password')] }, status: :unprocessable_entity + end + end + + def params_for_update + params.require(:user).permit(:password, :password_confirmation) + end + end + end + end +end diff --git a/app/controllers/api/v1/profiles_controller.rb b/app/controllers/api/v1/profiles_controller.rb new file mode 100644 index 000000000..48a5c6ee6 --- /dev/null +++ b/app/controllers/api/v1/profiles_controller.rb @@ -0,0 +1,23 @@ +module Api + module V1 + class ProfilesController < ApplicationController + before_action :authenticate_user! + respond_to :json + + skip_before_action :verify_authenticity_token + + def update + if current_user.update(params_for_update) + render json: current_user, status: :ok + else + Rails.logger.info @user.errors.inspect + render json: current_user.errors, status: :unprocessable_entity + end + end + + def params_for_update + params.require(:user).permit(:email, :country_code, :given_names, :surname, :mobile_phone) + end + end + end +end diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 92fe73b00..f7c9cb427 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -79,27 +79,9 @@ def self.search(params = {}) case params[:sort] when 'channel' - # invoices_array = query.to_a - - # if sort_direction == 'asc' - # invoices_array.sort_by do |invoice| - # invoice.paid_with_payment_order&.channel || '' - # end - # else - # invoices_array.sort_by do |invoice| - # invoice.paid_with_payment_order&.channel || '' - # end.reverse - # end - query.left_outer_joins(:paid_with_payment_order) .select("invoices.*, REPLACE(payment_orders.type, 'PaymentOrders::', '') AS payment_order_channel") .order(Arel.sql("payment_order_channel #{sort_direction} NULLS LAST")) - - # query.left_outer_joins(:paid_with_payment_order) - # .select("invoices.*, COALESCE(REPLACE(paid_with_payment_orders.type, 'PaymentOrders::', ''), '') AS payment_order_channel") - # .order(Arel.sql("payment_order_channel #{sort_direction} NULLS LAST")) - - when 'billing_profile_name' query.left_outer_joins(:billing_profile).order("billing_profiles.name #{sort_direction}") when 'total' diff --git a/app/views/auctions/index.json.jbuilder b/app/views/auctions/index.json.jbuilder index c4b43154b..a32715482 100644 --- a/app/views/auctions/index.json.jbuilder +++ b/app/views/auctions/index.json.jbuilder @@ -7,4 +7,6 @@ json.array! @auctions_list do |auction| json.highest_bidder auction.currently_winning_offer&.username json.min_bids_step auction.min_bids_step.to_f json.auction_type auction&.platform + json.enable_deposit auction.enable_deposit + json.requirement_deposit_in_cents auction.requirement_deposit_in_cents end diff --git a/config/routes.rb b/config/routes.rb index 2a56e3987..af5e66917 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,6 +12,11 @@ resource :offers, only: :create resource :autobiders, only: :create resource :auctions, only: :show + resource :profiles, only: :update do + scope module: :profiles do + resource :passwords, only: :update + end + end resources :invoices, only: :index end end