From 04943fea8b42d9732565fa0d2b492e7ad3a949dc Mon Sep 17 00:00:00 2001 From: oleghasjanov Date: Fri, 3 May 2024 13:28:30 +0300 Subject: [PATCH] remove uneccesary comments, updated json response for offers --- app/controllers/api/v1/offers_controller.rb | 15 ++++--- app/controllers/api/v1/profiles_controller.rb | 4 -- app/models/offer.rb | 41 ++++++++++++++++--- 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/app/controllers/api/v1/offers_controller.rb b/app/controllers/api/v1/offers_controller.rb index 09092892a..6eb061cf0 100644 --- a/app/controllers/api/v1/offers_controller.rb +++ b/app/controllers/api/v1/offers_controller.rb @@ -8,16 +8,15 @@ class OffersController < ApplicationController def index offers = Offer.includes(:auction) - .includes(:result) - .where(user_id: current_user) - .order('auctions.ends_at DESC') - - Rails.logger.info '---- offers ----' - Rails.logger.info(offers.inspect) - Rails.logger.info '----' + .includes(:result) + .where(user_id: current_user) + .order('auctions.ends_at DESC') # price with tax - render json: offers.as_json(include: [:auction, :billing_profile]) + render json: offers.as_json( + include: %i[auction billing_profile], + methods: %i[auction_status api_price api_total api_bidders] + ) end # rubocop:disable Metrics/AbcSize diff --git a/app/controllers/api/v1/profiles_controller.rb b/app/controllers/api/v1/profiles_controller.rb index 2ce8d985c..4637d6796 100644 --- a/app/controllers/api/v1/profiles_controller.rb +++ b/app/controllers/api/v1/profiles_controller.rb @@ -16,10 +16,6 @@ def update end def create - puts '----' - puts params_for_create - puts '----' - user = User.new(params_for_create) if user.save diff --git a/app/models/offer.rb b/app/models/offer.rb index 335c8b7b9..01af80d84 100644 --- a/app/models/offer.rb +++ b/app/models/offer.rb @@ -105,12 +105,43 @@ def price=(value) def total return price * (DEFAULT_PRICE_VALUE + billing_profile.vat_rate) if billing_profile.present? - if user&.country_code == 'EE' || user&.country_code.nil? - default_vat = Setting.find_by(code: :estonian_vat_rate).retrieve - else - default_vat = Countries.vat_rate_from_alpha2_code(user.country_code) + default_vat = if user&.country_code == 'EE' || user&.country_code.nil? + Setting.find_by(code: :estonian_vat_rate).retrieve + else + Countries.vat_rate_from_alpha2_code(user.country_code) + end + + price * (DEFAULT_PRICE_VALUE + (Invoice.find_by(result:)&.vat_rate || default_vat)) + end + + def auction_status + return 'you_won' if auction.finished? && result + return 'you_lost' if auction.finished? && auction.result && !result + + if auction.english? + return 'you_are_winning' if auction.currently_winning_offer == self + + return 'you_are_loosing' end - price * (DEFAULT_PRICE_VALUE + (Invoice.find_by(result: result)&.vat_rate || default_vat)) + 'still_in_progress' + end + + def api_total + total.to_d + end + + def api_price + price.to_d + end + + def api_bidders + auction.offers.map do |offer| + { + username: offer.username, + price: offer.price.to_d, + updated_at: offer.updated_at + } + end end end