diff --git a/app/controllers/api/cart_controller.rb b/app/controllers/api/cart_controller.rb index 5b59b2914..95f30fe1e 100644 --- a/app/controllers/api/cart_controller.rb +++ b/app/controllers/api/cart_controller.rb @@ -16,13 +16,13 @@ class API::CartController < API::ApiController state: 'cart').last end if current_user&.privileged? - @order = Order.where(operator_id: current_user.id, + @order = Order.where(operator_profile_id: current_user.invoicing_profile.id, state: 'cart').last end end if @order @order.update(statistic_profile_id: current_user.statistic_profile.id) if @order.statistic_profile_id.nil? && current_user&.member? - @order.update(operator_id: current_user.id) if @order.operator_id.nil? && current_user&.privileged? + @order.update(operator_profile_id: current_user.invoicing_profile.id) if @order.operator_profile_id.nil? && current_user&.privileged? end @order ||= Cart::CreateService.new.call(current_user) render 'api/orders/show' diff --git a/app/frontend/src/javascript/models/order.ts b/app/frontend/src/javascript/models/order.ts index c10b7d28d..d36036a9a 100644 --- a/app/frontend/src/javascript/models/order.ts +++ b/app/frontend/src/javascript/models/order.ts @@ -8,7 +8,7 @@ export interface Order { token: string, statistic_profile_id?: number, user?: User, - operator_id?: number, + operator_profile_id?: number, reference?: string, state?: string, total?: number, diff --git a/app/models/order.rb b/app/models/order.rb index dd2eb68d0..8ea21a646 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -3,6 +3,7 @@ # Order is a model for the user hold information of order class Order < ApplicationRecord belongs_to :statistic_profile + belongs_to :operator_profile, class_name: 'InvoicingProfile' has_many :order_items, dependent: :destroy ALL_STATES = %w[cart in_progress ready canceled return].freeze diff --git a/app/services/cart/create_service.rb b/app/services/cart/create_service.rb index 0a288b380..996baa73d 100644 --- a/app/services/cart/create_service.rb +++ b/app/services/cart/create_service.rb @@ -12,7 +12,7 @@ class Cart::CreateService if user order_param[:statistic_profile_id] = user.statistic_profile.id if user.member? - order_param[:operator_id] = user.id if user.privileged? + order_param[:operator_profile_id] = user.invoicing_profile.id if user.privileged? end Order.create!(order_param) end diff --git a/app/views/api/orders/_order.json.jbuilder b/app/views/api/orders/_order.json.jbuilder index 9614f9987..a002b1483 100644 --- a/app/views/api/orders/_order.json.jbuilder +++ b/app/views/api/orders/_order.json.jbuilder @@ -1,6 +1,6 @@ # frozen_string_literal: true -json.extract! order, :id, :token, :statistic_profile_id, :operator_id, :reference, :state, :created_at +json.extract! order, :id, :token, :statistic_profile_id, :operator_profile_id, :reference, :state, :created_at json.total order.total / 100.0 if order.total.present? if order&.statistic_profile&.user json.user do diff --git a/db/migrate/20220826093503_rename_operator_id_to_operator_profile_id_in_order.rb b/db/migrate/20220826093503_rename_operator_id_to_operator_profile_id_in_order.rb new file mode 100644 index 000000000..c199208f0 --- /dev/null +++ b/db/migrate/20220826093503_rename_operator_id_to_operator_profile_id_in_order.rb @@ -0,0 +1,7 @@ +class RenameOperatorIdToOperatorProfileIdInOrder < ActiveRecord::Migration[5.2] + def change + rename_column :orders, :operator_id, :operator_profile_id + add_index :orders, :operator_profile_id + add_foreign_key :orders, :invoicing_profiles, column: :operator_profile_id, primary_key: :id + end +end diff --git a/db/schema.rb b/db/schema.rb index 9f392a260..756cdbdb1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_08_26_091819) do +ActiveRecord::Schema.define(version: 2022_08_26_093503) do # These are extensions that must be enabled in order to support this database enable_extension "fuzzystrmatch" @@ -460,7 +460,7 @@ ActiveRecord::Schema.define(version: 2022_08_26_091819) do create_table "orders", force: :cascade do |t| t.bigint "statistic_profile_id" - t.integer "operator_id" + t.integer "operator_profile_id" t.string "token" t.string "reference" t.string "state" @@ -471,6 +471,7 @@ ActiveRecord::Schema.define(version: 2022_08_26_091819) do t.integer "wallet_amount" t.integer "wallet_transaction_id" t.string "payment_method" + t.index ["operator_profile_id"], name: "index_orders_on_operator_profile_id" t.index ["statistic_profile_id"], name: "index_orders_on_statistic_profile_id" end @@ -1164,6 +1165,7 @@ ActiveRecord::Schema.define(version: 2022_08_26_091819) do add_foreign_key "invoices", "wallet_transactions" add_foreign_key "invoicing_profiles", "users" add_foreign_key "order_items", "orders" + add_foreign_key "orders", "invoicing_profiles", column: "operator_profile_id" add_foreign_key "orders", "statistic_profiles" add_foreign_key "organizations", "invoicing_profiles" add_foreign_key "payment_gateway_objects", "payment_gateway_objects"