Skip to content

Commit

Permalink
[Transactions] refactor without entity builders
Browse files Browse the repository at this point in the history
  • Loading branch information
aivils committed Apr 9, 2018
1 parent d45c653 commit 0c41ca0
Show file tree
Hide file tree
Showing 134 changed files with 2,537 additions and 3,559 deletions.
56 changes: 24 additions & 32 deletions app/controllers/accept_preauthorized_conversations_controller.rb
Expand Up @@ -4,7 +4,7 @@ class AcceptPreauthorizedConversationsController < ApplicationController
controller.ensure_logged_in t("layouts.notifications.you_must_log_in_to_accept_or_reject")
end

before_action :fetch_conversation
before_action :fetch_transaction
before_action :fetch_listing

before_action :ensure_is_author
Expand All @@ -14,14 +14,14 @@ class AcceptPreauthorizedConversationsController < ApplicationController

def accept
tx_id = params[:id]
tx = TransactionService::API::Api.transactions.query(tx_id)
tx = @current_community.transactions.find(tx_id)

if tx[:current_state] != :preauthorized
if tx.current_state != 'preauthorized'
redirect_to person_transaction_path(person_id: @current_user.id, id: tx_id)
return
end

payment_type = tx[:payment_gateway]
payment_type = tx.payment_gateway
case payment_type
when :paypal, :stripe
render_payment_form("accept", payment_type)
Expand All @@ -32,14 +32,14 @@ def accept

def reject
tx_id = params[:id]
tx = TransactionService::API::Api.transactions.query(tx_id)
tx = @current_community.transactions.find(tx_id)

if tx[:current_state] != :preauthorized
if tx.current_state != 'preauthorized'
redirect_to person_transaction_path(person_id: @current_user.id, id: tx_id)
return
end

payment_type = tx[:payment_gateway]
payment_type = tx.payment_gateway
case payment_type
when :paypal, :stripe
render_payment_form("reject", payment_type)
Expand All @@ -50,13 +50,13 @@ def reject

def accepted_or_rejected
tx_id = params[:id]
message = params[:listing_conversation][:message_attributes][:content]
status = params[:listing_conversation][:status].to_sym
message = params[:transaction][:message_attributes][:content]
status = params[:transaction][:status].to_sym
sender_id = @current_user.id

tx = TransactionService::API::Api.transactions.query(tx_id)
tx = @current_community.transactions.find(tx_id)

if tx[:current_state] != :preauthorized
if tx.current_state != 'preauthorized'
redirect_to person_transaction_path(person_id: @current_user.id, id: tx_id)
return
end
Expand All @@ -69,9 +69,9 @@ def accepted_or_rejected
record_event(
flash,
status == :paid ? "PreauthorizedTransactionAccepted" : "PreauthorizedTransactionRejected",
{ listing_id: tx[:listing_id],
listing_uuid: tx[:listing_uuid].to_s,
transaction_id: tx[:id] })
{ listing_id: tx.listing_id,
listing_uuid: UUIDUtils.parse_raw(tx.listing_uuid).to_s,
transaction_id: tx.id })

redirect_to person_transaction_path(person_id: sender_id, id: tx_id)
else
Expand Down Expand Up @@ -136,35 +136,27 @@ def ensure_is_author
end

def fetch_listing
@listing = @listing_conversation.listing
@listing = @transaction.listing
end

def fetch_conversation
@listing_conversation = @current_community.transactions.find(params[:id])
def fetch_transaction
@transaction = @current_community.transactions.find(params[:id])
end

def render_payment_form(preselected_action, payment_type)
transaction_conversation = MarketplaceService::Transaction::Query.transaction(@listing_conversation.id)
result = TransactionService::Transaction.get(community_id: @current_community.id, transaction_id: @listing_conversation.id)
transaction = result[:data]
community_country_code = LocalizationUtils.valid_country_code(@current_community.country)
payment_details = TransactionService::Transaction.payment_details(@transaction)

render "accept", locals: {
payment_gateway: payment_type,
listing: @listing,
listing_quantity: transaction[:listing_quantity],
booking: transaction[:booking],
orderer: @listing_conversation.starter,
sum: transaction[:item_total] + (transaction[:payment_gateway_fee] || 0),
fee: transaction[:commission_total],
gateway_fee: transaction[:payment_gateway_fee],
shipping_price: transaction[:shipping_price],
shipping_address: transaction[:shipping_address],
seller_gets: transaction[:checkout_total] - transaction[:commission_total],
form: @listing_conversation, # TODO FIX ME, DONT USE MODEL
sum: @transaction.item_total + (payment_details[:payment_gateway_fee] || 0),
fee: @transaction.commission,
gateway_fee: payment_details[:payment_gateway_fee],
seller_gets: payment_details[:total_price]- @transaction.commission,
form: @transaction,
form_action: acceptance_preauthorized_person_message_path(
person_id: @current_user.id,
id: @listing_conversation.id
id: @transaction.id
),
preselected_action: preselected_action,
paypal_fees_url: PaypalCountryHelper.fee_link(community_country_code),
Expand Down
25 changes: 8 additions & 17 deletions app/controllers/admin/communities_controller.rb
Expand Up @@ -162,7 +162,7 @@ def topbar
@selected_left_navi_link = "topbar"

if FeatureFlagHelper.feature_enabled?(:topbar_v1) || CustomLandingPage::LandingPageStore.enabled?(@current_community.id)
limit_priority_links = MarketplaceService::API::Api.configurations.get(community_id: @current_community.id).data[:limit_priority_links]
limit_priority_links = @current_community.configuration&.limit_priority_links
all = view_context.t("admin.communities.menu_links.all")
limit_priority_links_options = (0..5).to_a.map {|o| [o, o]}.concat([[all, -1]])
limit_priority_links_selected = Maybe(limit_priority_links).or_else(-1)
Expand All @@ -185,12 +185,7 @@ def update_topbar

if FeatureFlagHelper.feature_enabled?(:topbar_v1) || CustomLandingPage::LandingPageStore.enabled?(@current_community.id)
limit_priority_links = params[:limit_priority_links].to_i
MarketplaceService::API::Api.configurations.update({
community_id: @current_community.id,
configurations: {
limit_priority_links: limit_priority_links
}
})
@current_community.configuration.update(limit_priority_links: limit_priority_links)
end

translations = h_params[:post_new_listing_button].map{ |k, v| {locale: k, translation: v}}
Expand Down Expand Up @@ -229,7 +224,7 @@ def settings

# When feature flag is removed, make this pretty
if(FeatureFlagHelper.location_search_available)
marketplace_configurations = MarketplaceService::API::Api.configurations.get(community_id: @current_community.id).data
marketplace_configurations = @current_community.configuration

keyword_and_location =
if FeatureFlagService::API::Api.features.get_for_community(community_id: @current_community.id).data[:features].include?(:topbar_v1)
Expand Down Expand Up @@ -354,15 +349,11 @@ def update_settings

maybe_update_payment_settings(@current_community.id, params[:community][:automatic_confirmation_after_days])

if(FeatureFlagHelper.location_search_available)
MarketplaceService::API::Api.configurations.update({
community_id: @current_community.id,
configurations: {
main_search: params[:main_search],
distance_unit: params[:distance_unit],
limit_search_distance: params[:limit_distance].present?
}
})
if FeatureFlagHelper.location_search_available
@current_community.configuration.update(
main_search: params[:main_search],
distance_unit: params[:distance_unit],
limit_search_distance: params[:limit_distance].present?)
end

update(@current_community,
Expand Down
54 changes: 9 additions & 45 deletions app/controllers/admin/community_conversations_controller.rb
@@ -1,65 +1,29 @@
class Admin::CommunityConversationsController < Admin::AdminBaseController
ConversationQuery = MarketplaceService::Conversation::Query

def index
@selected_left_navi_link = "conversations"
pagination_opts = PaginationViewUtils.parse_pagination_opts(params)

conversations = ConversationQuery.conversations_for_community(
@current_community.id,
conversations = Conversation.free_for_community(
@current_community,
simple_sort_column(params[:sort]),
sort_direction,
pagination_opts[:limit],
pagination_opts[:offset]
)

count = ConversationQuery.count_for_community(@current_community.id)

conversations = conversations.map do |conversation|
author = Maybe(conversation[:other_person]).or_else({is_deleted: true})
starter = Maybe(conversation[:starter_person]).or_else({is_deleted: true})
[author, starter].each { |p|
p[:url] = person_path(p[:username]) unless p[:username].nil?
p[:display_name] = PersonViewUtils.person_entity_display_name(p, "fullname")
}
conversation.merge({author: author, starter: starter})
end

conversations = WillPaginate::Collection.create(pagination_opts[:page], pagination_opts[:per_page], count) do |pager|
pager.replace(conversations)
end

sort_direction)
.paginate(page: pagination_opts[:page], per_page: pagination_opts[:per_page])
render "index", { locals: { community: @current_community, conversations: conversations } }
end

def show
@selected_left_navi_link = "conversations"
conversation_id = params[:id]
conversation = Conversation.find(conversation_id)

conversation = MarketplaceService::Conversation::Query.conversation_for_person(
conversation_id,
conversation.starter.id,
@current_community.id)

transaction = Transaction.find_by_conversation_id(conversation[:id])
conversation = Conversation.find(params[:id])
transaction = conversation.tx

if transaction.present?
redirect_to person_transaction_url(@current_user, {:id => transaction.id}) and return
end

author = Maybe(conversation[:other_person]).or_else({is_deleted: true})
starter = Maybe(conversation[:starter_person]).or_else({is_deleted: true})
[author, starter].each { |p|
p[:url] = person_path(p[:username]) unless p[:username].nil?
p[:display_name] = PersonViewUtils.person_entity_display_name(p, "fullname")
}
conversation = conversation.merge({author: author, starter: starter})

messages = TransactionViewUtils.conversation_messages(conversation[:messages], @current_community.name_display_type)
messages = TransactionViewUtils.conversation_messages(conversation.messages.latest, @current_community.name_display_type)
render locals: {
messages: messages.reverse,
conversation_data: conversation
messages: messages,
conversation: conversation
}
end

Expand Down
10 changes: 5 additions & 5 deletions app/controllers/admin/community_customizations_controller.rb
Expand Up @@ -4,7 +4,7 @@ def edit_details
@selected_left_navi_link = "tribe_details"
# @community_customization is fetched in application_controller
@community_customizations ||= find_or_initialize_customizations(@current_community.locales)
all_locales = MarketplaceService::API::Marketplaces.all_locales.map { |l|
all_locales = MarketplaceService.all_locales.map { |l|
{locale_key: l[:locale_key], translated_name: t("admin.communities.available_languages.#{l[:locale_key]}")}
}.sort_by { |l| l[:translated_name] }
enabled_locale_keys = available_locales.map(&:second)
Expand Down Expand Up @@ -43,10 +43,10 @@ def update_details

if process_locales
enabled_locales = params[:enabled_locales]
all_locales = MarketplaceService::API::Marketplaces.all_locales.map{|l| l[:locale_key]}.to_set
all_locales = MarketplaceService.all_locales.map{|l| l[:locale_key]}.to_set
enabled_locales_valid = enabled_locales.present? && enabled_locales.map{ |locale| all_locales.include? locale }.all?
if enabled_locales_valid
MarketplaceService::API::Marketplaces.set_locales(@current_community, enabled_locales)
MarketplaceService.set_locales(@current_community, enabled_locales)
end
end

Expand Down Expand Up @@ -96,7 +96,7 @@ def build_customization_with_defaults(locale)
end

def unofficial_locales
all_locales = MarketplaceService::API::Marketplaces.all_locales.map{|l| l[:locale_key]}
all_locales = MarketplaceService.all_locales.map{|l| l[:locale_key]}
@current_community.locales.select { |locale| !all_locales.include?(locale) }
.map { |unsupported_locale_key|
unsupported_locale_name = Sharetribe::AVAILABLE_LOCALES.select { |l| l[:ident] == unsupported_locale_key }.map { |l| l[:name] }.first
Expand All @@ -105,6 +105,6 @@ def unofficial_locales
end

def has_preauthorize_process?(processes)
processes.any? { |p| p[:process] == :preauthorize }
processes.any? { |p| p.process == :preauthorize }
end
end
45 changes: 8 additions & 37 deletions app/controllers/admin/community_transactions_controller.rb
@@ -1,67 +1,38 @@
require 'csv'

class Admin::CommunityTransactionsController < Admin::AdminBaseController
TransactionQuery = MarketplaceService::Transaction::Query

def index
@selected_left_navi_link = "transactions"
pagination_opts = PaginationViewUtils.parse_pagination_opts(params)

conversations = if params[:sort].nil? || params[:sort] == "last_activity"
TransactionQuery.transactions_for_community_sorted_by_activity(
transactions = if params[:sort].nil? || params[:sort] == "last_activity"
Transaction.for_community_sorted_by_activity(
@current_community.id,
sort_direction,
pagination_opts[:limit],
pagination_opts[:offset],
request.format == :csv)
else
TransactionQuery.transactions_for_community_sorted_by_column(
Transaction.for_community_sorted_by_column(
@current_community.id,
simple_sort_column(params[:sort]),
sort_direction,
pagination_opts[:limit],
pagination_opts[:offset])
end

count = TransactionQuery.transactions_count_for_community(@current_community.id)

# TODO THIS IS COPY-PASTE
conversations = conversations.map do |transaction|
conversation = transaction[:conversation]
# TODO Embed author and starter to the transaction entity
# author = conversation[:other_person]
author = Maybe(conversation[:other_person]).or_else({is_deleted: true})
starter = Maybe(conversation[:starter_person]).or_else({is_deleted: true})

[author, starter].each { |p|
p[:url] = person_path(p[:username]) unless p[:username].nil?
p[:display_name] = PersonViewUtils.person_entity_display_name(p, "fullname")
}

if transaction[:listing].present?
# This if was added to tolerate cases where listing has been deleted
# due the author deleting his/her account completely
# UPDATE: December 2014, we did an update which keeps the listing row even if user is deleted.
# So, we do not need to tolerate this anymore. However, there are transactions with deleted
# listings in DB, so those have to be handled.
transaction[:listing_url] = listing_path(id: transaction[:listing][:id])
end

transaction[:last_activity_at] = ExportTransactionsJob.last_activity_for(transaction)

transaction.merge({author: author, starter: starter})
end

conversations = WillPaginate::Collection.create(pagination_opts[:page], pagination_opts[:per_page], count) do |pager|
pager.replace(conversations)
count = Transaction.exist.by_community(@current_community.id).with_payment_conversation.count
transactions = WillPaginate::Collection.create(pagination_opts[:page], pagination_opts[:per_page], count) do |pager|
pager.replace(transactions)
end

respond_to do |format|
format.html do
render("index", {
locals: {
community: @current_community,
conversations: conversations
transactions: transactions,
}
})
end
Expand All @@ -79,7 +50,7 @@ def index
self.response.headers["Last-Modified"] = Time.now.ctime.to_s

self.response_body = Enumerator.new do |yielder|
ExportTransactionsJob.generate_csv_for(yielder, conversations)
ExportTransactionsJob.generate_csv_for(yielder, transactions)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/admin/listing_shapes_controller.rb
Expand Up @@ -281,8 +281,8 @@ def pick_categories(category_tree, ids)

def process_summary
@process_summary ||= processes.reduce({}) { |info, process|
info[:preauthorize_available] = true if process[:process] == :preauthorize
info[:request_available] = true if process[:author_is_seller] == false
info[:preauthorize_available] = true if process.process == :preauthorize
info[:request_available] = true if process.author_is_seller == false
info
}
end
Expand Down

0 comments on commit 0c41ca0

Please sign in to comment.