Skip to content

기능: 자신의 정보를 확인하는 API 구현 #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
10 changes: 10 additions & 0 deletions app/controllers/sellers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Sellers
class SessionsController < BaseController
before_action :authenticate_user!

# = GET /sellers/user
def show
# = show.json.jbuilder
end
end
end
5 changes: 0 additions & 5 deletions app/models/adjustment_product_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions app/models/barcode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 0 additions & 5 deletions app/models/barcode_option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions app/models/cart_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down
12 changes: 11 additions & 1 deletion app/models/interest_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions app/models/order_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand 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
Expand All @@ -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
[
Expand All @@ -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
14 changes: 14 additions & 0 deletions app/models/sellers/item_sold_paper.rb
Original file line number Diff line number Diff line change
@@ -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
43 changes: 24 additions & 19 deletions app/models/sellers/seller_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand All @@ -27,19 +30,21 @@ 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
has_one :permission, -> { order('created_at DESC') }, class_name: 'Sellers::PermitChangeList'
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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/models/user_interest_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
# fk_rails_... (user_id => users.id)
#
class UserInterestTag < ApplicationRecord
belongs_to :interest_tag
end
1 change: 1 addition & 0 deletions app/views/sellers/sessions/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.current_user current_user
3 changes: 2 additions & 1 deletion config/initializers/cors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 13 additions & 11 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions config/routes/sellers_routes.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
19 changes: 19 additions & 0 deletions db/data/20200730032746_set_item_sold_paper.rb
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions db/migrate/20200730032112_create_sellers_item_sold_papers.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions db/migrate/20200730050325_remove_sellers_order_sold_paper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveSellersOrderSoldPaper < ActiveRecord::Migration[6.0]
def change
drop_table :sellers_order_sold_papers
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCountryToInterestTag < ActiveRecord::Migration[6.0]
def change
add_reference :interest_tags, :country, foreign_key: true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddClassificationFieldToInterestTag < ActiveRecord::Migration[6.0]
def change
add_column :interest_tags, :created_by, :string
end
end
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions test/controllers/sellers/sessions_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class Sellers::SessionsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end
10 changes: 10 additions & 0 deletions test/fixtures/interest_tags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/sellers/item_sold_papers.yml
Original file line number Diff line number Diff line change
@@ -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
Loading