Last active
August 25, 2017 09:11
-
-
Save noel9999/cb93a9da120abf8ed45b7aa7cadfcb84 to your computer and use it in GitHub Desktop.
打破一堆原則的不良示範
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def index | |
@page = params.fetch("page",1).to_i | |
@offset = params.fetch("offset",0).to_i | |
@limit = params.fetch("limit",24).to_i | |
@search_fields = params["search_fields"] | |
# Order status | |
@status_filter = params.fetch("status_filter","all") | |
@status_filter = "all" if @status_filter.blank? | |
# Payment status | |
@payment_status_filter = params.fetch("payment_status_filter","all") | |
@payment_status_filter = "all" if @payment_status_filter.blank? | |
# Delivery status | |
@delivery_status_filter = params.fetch("delivery_status_filter","all") | |
@delivery_status_filter = "all" if @delivery_status_filter.blank? | |
# Label printed and export status | |
@label_and_export_status_filter = params[:label_and_export_status_filter] | |
@payment_method_id = params[:payment_filter] | |
@delivery_option_id = params[:delivery_filter] | |
@must_match_params = params[:must_match_params] | |
@promotion_id = params[:promotion_id] | |
@coupon_id = params[:coupon_id] | |
@addon_product_id = params[:addon_product_id] | |
@merchant_id = params[:merchant_id] | |
@user_id = params[:user_id] | |
# For stock picking | |
@start_date = params[:start_date] | |
@end_date = params[:end_date] | |
@query_filter = params.fetch("query_filter","{}") | |
@query_filter = JSON.parse(@query_filter) | |
@query = params.fetch(:query,"") | |
# For order invoice | |
@order_ids = params.fetch("target_ids", []) | |
@is_batch_invoice = params.fetch("is_batch_invoice", false) | |
result = {} | |
if @promotion_id.present? | |
# Promotion orders history | |
@orders = Order.valid_or_temp.where(:_id.in => Promotion.find(params[:promotion_id]).order_promotion_items.distinct(:order_id)) | |
elsif @coupon_id.present? | |
# Coupon orders history | |
@orders = Order.valid_or_temp.where(:_id.in => Coupon.find(params[:coupon_id]).coupon_items.pluck(:order_id,:subscription_order_id).flatten.compact) | |
elsif @addon_product_id.present? | |
# Add_on product orders history? not sure where will use | |
@show_addon_products_count = true | |
@addon_product_id = params[:addon_product_id] | |
order_items = OrderItem.where(:status.ne => "removed", :item_type => "AddonProduct", :item_id => params[:addon_product_id]) | |
@orders = Order.valid_or_temp.where(:_id.in => order_items.distinct(:order_id)) | |
elsif @query_filter.present? | |
# user orders history (storefront) | |
# storefront passes something like {customer_id: <id>, status: {$nin: [temp, removed] }....amazing huh? | |
@orders = Order.belongs_to_merchant(@merchant._id).where(@query_filter) | |
elsif @merchant_id.present? | |
# Attention: NO ONE WILL CALL IT | |
# /v1/merchants/:merchant_id/orders coz we use /v1/:mid/orders | |
# Display all orders belongs to this merchants | |
@orders = Order.valid_or_temp.belongs_to_merchant(@merchant_id).desc(:id) | |
@tags = ["#{Order.name}","#{Order.name}:#{__method__}:owner_id:#{@merchant._id}", "Merchant:#{@merchant._id}:orders"] | |
elsif @user_id.present? && params[:order_delivery_types].present? | |
# TODO: Move to user preference | |
order_delivery_types = params[:order_delivery_types] | |
order_delivery_types = JSON.parse(order_delivery_types) if order_delivery_types.is_a?(String) | |
# The story: We are searching orders with given delivery type for the shopper in a shop | |
# We were planned to use search but customer id is not included in the data, so we can only perform in an old style way | |
order_ids = Order.valid_or_temp | |
.belongs_to_merchant(@merchant.id) | |
.belongs_to_user(params[:user_id]) | |
.desc(:id) | |
.limit(24) | |
.skip(0) | |
.pluck(:id) | |
response = search_orders( | |
from: @offset, | |
size: @limit, | |
must_match_params: { | |
_id: order_ids, | |
status: ["temp", "pending", "completed"], | |
order_delivery_type: order_delivery_types | |
} | |
) | |
result.merge!({ | |
limit: @limit, | |
offset: @offset, | |
items: response.results.map { |r| r._source.as_json }, | |
total: response.results.total, | |
}) unless response.blank? | |
render json: {data:result} and return | |
elsif @user_id.present? | |
# User orders history (admin) | |
# Update on 23/01/17: I think storefront is using it as well | |
@orders = Order.valid_or_temp.belongs_to_merchant(@merchant.id).belongs_to_user(params[:user_id]) | |
elsif @order_ids.present? && @is_batch_invoice | |
@orders = Order.valid_or_temp.belongs_to_merchant(@merchant.id).where(:id.in => @order_ids).limit(@limit) | |
@orders.update_all(invoice_last_exported_at: DateTime.current) | |
@orders.each(&:after_save) | |
return render :invoice | |
elsif @status_filter == "all" && | |
@payment_status_filter == "all" && | |
@delivery_status_filter == "all" && | |
@label_and_export_status_filter.blank? && | |
@must_match_params.blank? && | |
@delivery_option_id.blank? && | |
@payment_method_id.blank? && | |
@start_date.blank? && | |
@end_date.blank? && | |
@query.blank? | |
# This is normal use, all orders of a merhcants | |
# this is deplicated as it is a duplicated of the handling with params[:merchant_id] present | |
@orders = Order.valid_or_temp.belongs_to_merchant(@merchant._id).desc(:id) | |
@tags = ["#{Order.name}","#{Order.name}:#{__method__}:owner_id:#{@merchant._id}", "Merchant:#{@merchant._id}:orders"] | |
else | |
# This is for order search but with/without query | |
# With query, then search | |
# Without query, then like filtering | |
response = search_orders from: @offset, | |
size: @limit, | |
query: @query, | |
status: @status_filter, | |
payment_status: @payment_status_filter, | |
delivery_status: @delivery_status_filter, | |
label_and_export_status: @label_and_export_status_filter, | |
must_match_params: @must_match_params, | |
payment_method_id: @payment_method_id, | |
delivery_option_id: @delivery_option_id, | |
start_date: @start_date, | |
end_date: @end_date, | |
search_fields: @search_fields | |
result.merge!({ | |
limit: @limit, | |
offset: @offset, | |
items: response.results.map { |r| r._source.as_json }, | |
total: response.results.total, | |
}) unless response.blank? | |
render json: {data:result} | |
return | |
end | |
if (order_ids = params[:order_ids]).present? | |
order_ids = order_ids.tr(' ', '').split(',') if order_ids.is_a? String | |
order_ids = Array(order_ids).select { |id| BSON::ObjectId.legal?(id.to_s.tr(' ','')) } | |
@orders = @orders.where(:id.in => order_ids) | |
end | |
@count = @orders.length | |
@orders = @orders.desc(:id).limit(@limit).skip(@offset) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment