2018-10-07 16:07:37 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Api
|
|
|
|
module V1
|
|
|
|
class TaskInventoryItemsController < BaseController
|
|
|
|
before_action :load_team
|
|
|
|
before_action :load_project
|
|
|
|
before_action :load_experiment
|
|
|
|
before_action :load_task
|
2022-03-30 20:33:26 +08:00
|
|
|
before_action :load_my_module_repository_row, only: :update
|
|
|
|
before_action :check_stock_consumption_update_permissions, only: :update
|
2018-10-07 16:07:37 +08:00
|
|
|
|
|
|
|
def index
|
|
|
|
items =
|
|
|
|
@task.repository_rows
|
2018-10-07 19:28:29 +08:00
|
|
|
.includes(repository_cells: :repository_column)
|
2021-07-07 23:43:51 +08:00
|
|
|
.preload(repository_cells: :value)
|
2018-10-07 19:28:29 +08:00
|
|
|
.page(params.dig(:page, :number))
|
|
|
|
.per(params.dig(:page, :size))
|
2018-10-07 16:07:37 +08:00
|
|
|
render jsonapi: items,
|
2022-03-30 20:33:26 +08:00
|
|
|
each_serializer: TaskInventoryItemSerializer,
|
2018-10-07 16:07:37 +08:00
|
|
|
show_repository: true,
|
2022-03-30 20:33:26 +08:00
|
|
|
my_module: @task,
|
2020-07-02 23:03:29 +08:00
|
|
|
include: include_params
|
2018-10-07 16:07:37 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2018-11-28 21:14:45 +08:00
|
|
|
render jsonapi: @task.repository_rows.find(params.require(:id)),
|
2022-03-30 20:33:26 +08:00
|
|
|
serializer: TaskInventoryItemSerializer,
|
2018-10-07 16:07:37 +08:00
|
|
|
show_repository: true,
|
2022-03-30 20:33:26 +08:00
|
|
|
my_module: @task,
|
|
|
|
include: %i(inventory_cells inventory)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@my_module_repository_row.consume_stock(
|
|
|
|
current_user,
|
|
|
|
repository_row_params[:attributes][:stock_consumption],
|
|
|
|
repository_row_params[:attributes][:stock_consumption_comment]
|
|
|
|
)
|
|
|
|
|
|
|
|
render jsonapi: @my_module_repository_row.repository_row,
|
|
|
|
serializer: TaskInventoryItemSerializer,
|
|
|
|
show_repository: true,
|
|
|
|
my_module: @task,
|
2018-10-07 16:07:37 +08:00
|
|
|
include: %i(inventory_cells inventory)
|
|
|
|
end
|
2020-07-02 23:03:29 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-03-30 20:33:26 +08:00
|
|
|
def load_my_module_repository_row
|
|
|
|
@my_module_repository_row = @task.repository_rows
|
|
|
|
.find(params.require(:id))
|
|
|
|
.my_module_repository_rows
|
|
|
|
.find_by(my_module: @task)
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_stock_consumption_update_permissions
|
|
|
|
unless can_update_my_module_stock_consumption?(@task) &&
|
|
|
|
can_manage_repository_rows?(@my_module_repository_row.repository_row.repository)
|
|
|
|
raise PermissionError.new(RepositoryRow, :update_stock_consumption)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def repository_row_params
|
|
|
|
raise TypeError unless params.require(:data).require(:type) == 'inventory_items'
|
|
|
|
|
|
|
|
params.require(:data).require(:attributes)
|
|
|
|
params.permit(data: { attributes: %i(stock_consumption stock_consumption_comment) })[:data]
|
|
|
|
end
|
|
|
|
|
2020-07-02 23:03:29 +08:00
|
|
|
def permitted_includes
|
|
|
|
%w(inventory_cells)
|
|
|
|
end
|
2018-10-07 16:07:37 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|