diff --git a/app/controllers/sellers/sessions_controller.rb b/app/controllers/sellers/sessions_controller.rb new file mode 100644 index 00000000..a0dc7ae7 --- /dev/null +++ b/app/controllers/sellers/sessions_controller.rb @@ -0,0 +1,10 @@ +module Sellers + class SessionsController < BaseController + before_action :authenticate_user! + + # = GET /sellers/user + def show + # = show.json.jbuilder + end + end +end diff --git a/app/models/adjustment_product_item.rb b/app/models/adjustment_product_item.rb index 890fa099..32f41b12 100644 --- a/app/models/adjustment_product_item.rb +++ b/app/models/adjustment_product_item.rb @@ -13,11 +13,6 @@ # index_adjustment_product_items_on_adjustment_id (adjustment_id) # index_adjustment_product_items_on_product_item_id (product_item_id) # -# Foreign Keys -# -# fk_rails_... (adjustment_id => adjustments.id) -# fk_rails_... (product_item_id => product_items.id) -# class AdjustmentProductItem < ApplicationRecord belongs_to :adjustment belongs_to :product_item diff --git a/app/models/barcode.rb b/app/models/barcode.rb index 7ff6b7b5..e17a0098 100644 --- a/app/models/barcode.rb +++ b/app/models/barcode.rb @@ -14,11 +14,6 @@ # index_barcodes_on_cart_item_id (cart_item_id) # index_barcodes_on_product_id (product_id) # -# Foreign Keys -# -# fk_rails_... (cart_item_id => cart_items.id) -# fk_rails_... (product_id => products.id) -# class Barcode < ApplicationRecord belongs_to :product, dependent: :destroy diff --git a/app/models/barcode_option.rb b/app/models/barcode_option.rb index 3434b8f1..b7e93466 100644 --- a/app/models/barcode_option.rb +++ b/app/models/barcode_option.rb @@ -13,11 +13,6 @@ # index_barcode_options_on_barcode_id (barcode_id) # index_barcode_options_on_product_option_id (product_option_id) # -# Foreign Keys -# -# fk_rails_... (barcode_id => barcodes.id) -# fk_rails_... (product_option_id => product_options.id) -# class BarcodeOption < ApplicationRecord belongs_to :barcode, dependent: :destroy belongs_to :product_option, dependent: :destroy diff --git a/app/models/cart_item.rb b/app/models/cart_item.rb index f8ff1f4e..578e2509 100644 --- a/app/models/cart_item.rb +++ b/app/models/cart_item.rb @@ -54,6 +54,8 @@ class CartItem < ApplicationRecord has_one :order_info, through: :cart + has_one :item_sold_paper, class_name: 'Sellers::ItemSoldPaper', foreign_key: :item_id, dependent: :destroy + scope :cancelled, -> { where(cancelled_tag: CartItemCancelledTag.all) } scope :not_cancelled, -> { where.not(cancelled_tag: CartItemCancelledTag.all) } diff --git a/app/models/interest_tag.rb b/app/models/interest_tag.rb index c59178c2..68500659 100644 --- a/app/models/interest_tag.rb +++ b/app/models/interest_tag.rb @@ -3,9 +3,19 @@ # Table name: interest_tags # # id :bigint not null, primary key +# created_by :string(255) # name :json # created_at :datetime not null # updated_at :datetime not null +# country_id :bigint # -class InterestTag < ApplicationRecord +# Indexes +# +# index_interest_tags_on_country_id (country_id) +# +# Foreign Keys +# +# fk_rails_... (country_id => countries.id) +# +class InterestTag < NationRecord end diff --git a/app/models/order_info.rb b/app/models/order_info.rb index 76b689c4..a10db260 100644 --- a/app/models/order_info.rb +++ b/app/models/order_info.rb @@ -34,6 +34,7 @@ class OrderInfo < NationRecord has_many :items, through: :cart has_many :product_options, through: :items, source: :product_option has_many :adjustments, class_name: 'Adjustment' + has_many :sellers_papers, through: :items, source: :item_sold_paper # =============================================== has_many :order_info_brands, class_name: 'OrderInfoBrand', dependent: :delete_all @@ -43,6 +44,7 @@ class OrderInfo < NationRecord validates_presence_of :cart_id, :enc_id validates_uniqueness_of :cart_id, :enc_id + delegate :order_status, to: :cart # alias_attribute :status, :order_status delegate :delivery_amount, to: :ship_info, allow_nil: true @@ -53,6 +55,7 @@ class OrderInfo < NationRecord scope :sold, -> { includes(:cart).where(cart: Cart.where(order_status: Cart::SOLD_STATUSES)) } scope :eager_index, -> { includes(:payment, :ship_info) } scope :stage_in, ->(stage) { includes(:cart).where(cart: Cart.send((stage || :all).to_sym)) } + scope :sellers_order, -> { includes(:items).where(cart: Cart.where(items: CartItem.sold_by_seller)) } def self.gen_enc_id [ @@ -77,4 +80,8 @@ def first_product def quantity cart.items.sum(:barcode_count) end + + def sellers_items + items.filter(&:item_sold_paper) + end end diff --git a/app/models/sellers/item_sold_paper.rb b/app/models/sellers/item_sold_paper.rb new file mode 100644 index 00000000..fc03aec8 --- /dev/null +++ b/app/models/sellers/item_sold_paper.rb @@ -0,0 +1,14 @@ +module Sellers + class ItemSoldPaper < ApplicationRecord + belongs_to :item, class_name: 'CartItem' + belongs_to :seller_info, class_name: 'Sellers::SellerInfo' + + def paid? + paid + end + + def pay! + update(paid: true, paid_at: DateTime.now) + end + end +end diff --git a/app/models/sellers/seller_info.rb b/app/models/sellers/seller_info.rb index 108d424a..c2d1d53d 100644 --- a/app/models/sellers/seller_info.rb +++ b/app/models/sellers/seller_info.rb @@ -6,11 +6,14 @@ # cumulative_amount :integer default(0) # cumulative_profit :integer default(0) # present_profit :integer default(0) +# purpose :text(65535) +# sns_name :string(255) # withdrawable_profit :integer default(0) # created_at :datetime not null # updated_at :datetime not null # grade_id :bigint not null # seller_id :bigint not null +# sns_id :string(255) # # Indexes # @@ -27,6 +30,8 @@ class SellerInfo < ApplicationRecord belongs_to :seller has_one :store_info, class_name: 'Sellers::StoreInfo', dependent: :destroy has_one :account_info, class_name: 'Sellers::AccountInfo', dependent: :destroy + has_many :user_interest_tags, class_name: 'UserInterestTag', dependent: :destroy + has_many :interest_tags, through: :user_interest_tags belongs_to :grade, class_name: 'Sellers::Grade' has_many :permit_change_lists, class_name: 'Sellers::PermitChangeList', dependent: :destroy @@ -34,12 +39,12 @@ class SellerInfo < ApplicationRecord has_one :permit_status, through: :permission, class_name: 'Sellers::PermitStatus' has_many :settlement_statements, class_name: 'Sellers::SettlementStatement', dependent: :destroy - has_many :order_sold_papers, class_name: 'Sellers::OrderSoldPaper', dependent: :destroy + has_many :item_sold_papers, class_name: 'Sellers::ItemSoldPaper', dependent: :destroy + has_many :items, through: :item_sold_papers + has_many :order_infos, -> { distinct }, class_name: 'OrderInfo', through: :items - has_many :order_infos, through: :order_sold_papers - - scope :permitted, -> { where(permission: PermitChangeList.where(permit_status: PermitStatus.permitted)) } - scope :applied, -> { where(permission: PermitChangeList.where(permit_status: PermitStatus.applied)) } + scope :permitted, -> { where(permission: Sellers::PermitChangeList.where(permit_status: Sellers::PermitStatus.permitted)) } + scope :applied, -> { where(permission: Sellers::PermitChangeList.where(permit_status: Sellers::PermitStatus.applied)) } delegate :name, to: :seller delegate :email, to: :seller @@ -48,43 +53,43 @@ class SellerInfo < ApplicationRecord def permitted? update_status_cache - permit_status == PermitStatus.permitted + permit_status == Sellers::PermitStatus.permitted end def play_permit!(reason = nil) - permit_change_lists << PermitChangeList.new( - permit_status: PermitStatus.permitted, + permit_change_lists << Sellers::PermitChangeList.new( + permit_status: Sellers::PermitStatus.permitted, reason: reason ) update_status_cache end def play_stop!(reason) - permit_change_lists << PermitChangeList.new( - permit_status: PermitStatus.stopped, + permit_change_lists << Sellers::PermitChangeList.new( + permit_status: Sellers::PermitStatus.stopped, reason: reason ) update_status_cache end def init_permit_status! - permit_change_lists << PermitChangeList.new( - permit_status: PermitStatus.applied + permit_change_lists << Sellers::PermitChangeList.new( + permit_status: Sellers::PermitStatus.applied ) update_status_cache end - def update_counter_cache(order_sold_paper = nil) - if order_sold_paper.nil? + def update_counter_cache(item_sold_paper = nil) + if item_sold_paper.nil? update_columns( - cumulative_amount: order_infos.map(&:payment).sum(&:total_price_sum), - cumulative_profit: order_sold_papers.sum(&:adjusted_profit) + cumulative_amount: items.sum(&:captured_retail_price), + cumulative_profit: item_sold_papers.sum(&:adjusted_profit) ) else - order_info = order_sold_paper.order_info + cart_item = item_sold_paper.cart_item update_columns( - cumulative_amount: cumulative_amount + order_info.payment.total_price_sum, - cumulative_profit: cumulative_profit + order_sold_paper.adjusted_profit + cumulative_amount: cumulative_amount + cart_item.captured_retail_price, + cumulative_profit: cumulative_profit + item_sold_paper.adjusted_profit ) end end diff --git a/app/models/user_interest_tag.rb b/app/models/user_interest_tag.rb index 00b94ee5..8914af8b 100644 --- a/app/models/user_interest_tag.rb +++ b/app/models/user_interest_tag.rb @@ -19,4 +19,5 @@ # fk_rails_... (user_id => users.id) # class UserInterestTag < ApplicationRecord + belongs_to :interest_tag end diff --git a/app/views/sellers/sessions/show.json.jbuilder b/app/views/sellers/sessions/show.json.jbuilder new file mode 100644 index 00000000..d02455b2 --- /dev/null +++ b/app/views/sellers/sessions/show.json.jbuilder @@ -0,0 +1 @@ +json.current_user current_user diff --git a/config/initializers/cors.rb b/config/initializers/cors.rb index ad0785ee..92132ec2 100644 --- a/config/initializers/cors.rb +++ b/config/initializers/cors.rb @@ -4,7 +4,8 @@ resource( '*', headers: :any, - methods: %i[get post put patch delete options head] + methods: %i[get post put patch delete options head], + expose: %w[Authorization] ) end end diff --git a/config/routes.rb b/config/routes.rb index e00701c6..21e6af63 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,18 +1,20 @@ Rails.application.routes.draw do - devise_for :users, controllers: { - sessions: 'users/sessions', - registrations: 'users/registrations', - passwords: 'users/passwords' - } - # global file crud (active_storage) resources :files, only: %i[show create destroy] - - draw :partner_center_routes - draw :gomisa_routes - draw :common_routes - draw :sellers_routes + constraints format: :json do + devise_for :users, controllers: { + sessions: 'users/sessions', + registrations: 'users/registrations', + passwords: 'users/passwords' + } + + draw :partner_center_routes + draw :gomisa_routes + + draw :common_routes + draw :sellers_routes + end namespace :haravan do namespace :settlement do diff --git a/config/routes/sellers_routes.rb b/config/routes/sellers_routes.rb index d93f84c9..61991a2e 100644 --- a/config/routes/sellers_routes.rb +++ b/config/routes/sellers_routes.rb @@ -1,4 +1,7 @@ namespace :sellers, except: %i[new edit] do + # get 'user', controller: 'users' + resource :user, only: :show, controller: :sessions + # === 유저 API # resources :users do diff --git a/db/data/20200730032746_set_item_sold_paper.rb b/db/data/20200730032746_set_item_sold_paper.rb new file mode 100644 index 00000000..900925e0 --- /dev/null +++ b/db/data/20200730032746_set_item_sold_paper.rb @@ -0,0 +1,19 @@ +class SetItemSoldPaper < ActiveRecord::Migration[6.0] + def up + Sellers::OrderSoldPaper.all.each do |paper| + order = paper.order_info + seller_info = paper.seller_info + order.items.each do |item| + Sellers::ItemSoldPaper.create( + item: item, + seller_info: seller_info, + adjusted_profit: (item.result_price * item.option_count * seller_info.commission_rate) + ) + end + end + end + + def down + raise ActiveRecord::IrreversibleMigration + end +end diff --git a/db/migrate/20200730032112_create_sellers_item_sold_papers.rb b/db/migrate/20200730032112_create_sellers_item_sold_papers.rb new file mode 100644 index 00000000..acaa0954 --- /dev/null +++ b/db/migrate/20200730032112_create_sellers_item_sold_papers.rb @@ -0,0 +1,13 @@ +class CreateSellersItemSoldPapers < ActiveRecord::Migration[6.0] + def change + create_table :sellers_item_sold_papers do |t| + t.references :item, null: false, foreign_key: { to_table: :cart_items } + t.references :seller_info, null: false, foreign_key: { to_table: :sellers_seller_infos } + t.boolean :paid, default: false + t.datetime :paid_at + t.integer :adjusted_profit, default: 0 + + t.timestamps + end + end +end diff --git a/db/migrate/20200730050325_remove_sellers_order_sold_paper.rb b/db/migrate/20200730050325_remove_sellers_order_sold_paper.rb new file mode 100644 index 00000000..09f91550 --- /dev/null +++ b/db/migrate/20200730050325_remove_sellers_order_sold_paper.rb @@ -0,0 +1,5 @@ +class RemoveSellersOrderSoldPaper < ActiveRecord::Migration[6.0] + def change + drop_table :sellers_order_sold_papers + end +end diff --git a/db/migrate/interest_tag/20200803013753_add_country_to_interest_tag.rb b/db/migrate/interest_tag/20200803013753_add_country_to_interest_tag.rb new file mode 100644 index 00000000..12d2df59 --- /dev/null +++ b/db/migrate/interest_tag/20200803013753_add_country_to_interest_tag.rb @@ -0,0 +1,5 @@ +class AddCountryToInterestTag < ActiveRecord::Migration[6.0] + def change + add_reference :interest_tags, :country, foreign_key: true + end +end diff --git a/db/migrate/interest_tag/20200803014024_add_classification_field_to_interest_tag.rb b/db/migrate/interest_tag/20200803014024_add_classification_field_to_interest_tag.rb new file mode 100644 index 00000000..13cf438d --- /dev/null +++ b/db/migrate/interest_tag/20200803014024_add_classification_field_to_interest_tag.rb @@ -0,0 +1,5 @@ +class AddClassificationFieldToInterestTag < ActiveRecord::Migration[6.0] + def change + add_column :interest_tags, :created_by, :string + end +end diff --git a/db/migrate/sellers/20200803014409_add_columns_to_seller_info.rb b/db/migrate/sellers/20200803014409_add_columns_to_seller_info.rb new file mode 100644 index 00000000..94ebee20 --- /dev/null +++ b/db/migrate/sellers/20200803014409_add_columns_to_seller_info.rb @@ -0,0 +1,7 @@ +class AddColumnsToSellerInfo < ActiveRecord::Migration[6.0] + def change + add_column :sellers_seller_infos, :sns_name, :string + add_column :sellers_seller_infos, :sns_id, :string + add_column :sellers_seller_infos, :purpose, :text + end +end diff --git a/test/controllers/sellers/sessions_controller_test.rb b/test/controllers/sellers/sessions_controller_test.rb new file mode 100644 index 00000000..9e21b3fc --- /dev/null +++ b/test/controllers/sellers/sessions_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class Sellers::SessionsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/test/fixtures/interest_tags.yml b/test/fixtures/interest_tags.yml index 198cc887..99df6b67 100644 --- a/test/fixtures/interest_tags.yml +++ b/test/fixtures/interest_tags.yml @@ -3,9 +3,19 @@ # Table name: interest_tags # # id :bigint not null, primary key +# created_by :string(255) # name :json # created_at :datetime not null # updated_at :datetime not null +# country_id :bigint +# +# Indexes +# +# index_interest_tags_on_country_id (country_id) +# +# Foreign Keys +# +# fk_rails_... (country_id => countries.id) # one: diff --git a/test/fixtures/sellers/item_sold_papers.yml b/test/fixtures/sellers/item_sold_papers.yml new file mode 100644 index 00000000..a0141669 --- /dev/null +++ b/test/fixtures/sellers/item_sold_papers.yml @@ -0,0 +1,15 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + item_info: one + seller_info: one + paid: false + paid_at: 2020-07-30 12:21:13 + adjusted_profit: 1 + +two: + item_info: two + seller_info: two + paid: false + paid_at: 2020-07-30 12:21:13 + adjusted_profit: 1 diff --git a/test/fixtures/sellers/seller_infos.yml b/test/fixtures/sellers/seller_infos.yml index e680bd05..7caa19b7 100644 --- a/test/fixtures/sellers/seller_infos.yml +++ b/test/fixtures/sellers/seller_infos.yml @@ -6,11 +6,14 @@ # cumulative_amount :integer default(0) # cumulative_profit :integer default(0) # present_profit :integer default(0) +# purpose :text(65535) +# sns_name :string(255) # withdrawable_profit :integer default(0) # created_at :datetime not null # updated_at :datetime not null # grade_id :bigint not null # seller_id :bigint not null +# sns_id :string(255) # # Indexes # diff --git a/test/models/interest_tag_test.rb b/test/models/interest_tag_test.rb index 561cec19..2eeffcd7 100644 --- a/test/models/interest_tag_test.rb +++ b/test/models/interest_tag_test.rb @@ -3,9 +3,19 @@ # Table name: interest_tags # # id :bigint not null, primary key +# created_by :string(255) # name :json # created_at :datetime not null # updated_at :datetime not null +# country_id :bigint +# +# Indexes +# +# index_interest_tags_on_country_id (country_id) +# +# Foreign Keys +# +# fk_rails_... (country_id => countries.id) # require 'test_helper' diff --git a/test/models/sellers/item_sold_paper_test.rb b/test/models/sellers/item_sold_paper_test.rb new file mode 100644 index 00000000..d07e0c92 --- /dev/null +++ b/test/models/sellers/item_sold_paper_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class Sellers::ItemSoldPaperTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/sellers/seller_info_test.rb b/test/models/sellers/seller_info_test.rb index b2d6abcf..c455f97a 100644 --- a/test/models/sellers/seller_info_test.rb +++ b/test/models/sellers/seller_info_test.rb @@ -6,11 +6,14 @@ # cumulative_amount :integer default(0) # cumulative_profit :integer default(0) # present_profit :integer default(0) +# purpose :text(65535) +# sns_name :string(255) # withdrawable_profit :integer default(0) # created_at :datetime not null # updated_at :datetime not null # grade_id :bigint not null # seller_id :bigint not null +# sns_id :string(255) # # Indexes #