Skip to content

Instantly share code, notes, and snippets.

@noel9999
Created April 7, 2017 07:07
Show Gist options
  • Save noel9999/0ceda51e2b4fddb8e60b4a01dae01531 to your computer and use it in GitHub Desktop.
Save noel9999/0ceda51e2b4fddb8e60b4a01dae01531 to your computer and use it in GitHub Desktop.
refactor & test demo
def checkout
m = @merchant_data
seller_id = m["_id"]
unfulfillable_items = get_invalid_cart_items
if unfulfillable_items.present?
render status: :unprocessable_entity, json: { message: I18n.t('orders.error.fulfillment_error'), data: unfulfillable_items}
return
end
delivery_option_id = params.fetchpath("order/delivery_option/_id")
payment_method_id = params.fetchpath("order/payment_method/_id")
# 這邊在整理參數讓給這方法最後可以去跟 api 要資料,但這部份應該抽出來成方法,才能驗證結果等等,不然測試很難測到這段。除非準備一堆真的資料去跑
# delivery = DeliveryOption.find(delivery_option_id)
# delivery = DeliveryOption.find(delivery_option_id)
delivery_address = params.fetchpath('order/delivery_address',{})
if (delivery_address.has_key?("country"))
country_code =delivery_address.fetch("country","")
if (country_code != "")
country = ISO3166::Country.find_country_by_alpha2(country_code)
delivery_address["country_code"] = country_code
delivery_address["country"] = country_code == 'TW' ? I18n.t('countries.TW') : country.name
end
end
# 又一堆應該抽出去成獨立方的邏輯
# save user information! --------------
save_fields = params[:saveFields]
if !user_signed_in? # user is not signed in
email = params.fetchpath("order/customer_email",nil)
render status: :unprocessable_entity, json: { message: I18n.t('themes.orders.email_required')} and return if email.blank?
@customer = find_or_create_by_email email
# wants to create account
if save_fields['all'] == true && !@customer.is_password_set
@sl_client.put("#{@customer.owner_id}/users/#{@customer.id}/send_notification",{:type=>"shop_welcome_password_notification"})
end
else # user is signed in
@customer = current_user
end
# 後面都是做不同目的雜七雜八的邏輯
# blahblahblahblahblahblahblahblahblahblahblahblahblahblahblah
# blahblahblahblahblahblahblahblahblahblahblahblahblahblahblah
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment