2018-08-07 21:19:24 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-08-07 20:19:49 +08:00
|
|
|
module Api
|
|
|
|
module V1
|
|
|
|
class InventoryItemsController < BaseController
|
|
|
|
before_action :load_team
|
|
|
|
before_action :load_inventory
|
2018-11-07 22:43:44 +08:00
|
|
|
before_action only: %i(show update destroy) do
|
|
|
|
load_inventory_item(:id)
|
|
|
|
end
|
2018-08-24 17:26:49 +08:00
|
|
|
before_action :check_manage_permissions, only: %i(create update destroy)
|
2018-08-07 20:19:49 +08:00
|
|
|
|
|
|
|
def index
|
|
|
|
items =
|
|
|
|
@inventory.repository_rows
|
2020-02-03 22:20:01 +08:00
|
|
|
.preload(repository_cells: :repository_column)
|
|
|
|
.preload(repository_cells: @inventory.cell_preload_includes)
|
|
|
|
.page(params.dig(:page, :number))
|
2018-08-23 20:52:00 +08:00
|
|
|
.per(params.dig(:page, :size))
|
2018-10-07 00:37:40 +08:00
|
|
|
incl = params[:include] == 'inventory_cells' ? :inventory_cells : nil
|
2018-08-23 22:48:27 +08:00
|
|
|
render jsonapi: items,
|
2018-08-07 20:19:49 +08:00
|
|
|
each_serializer: InventoryItemSerializer,
|
2018-10-07 00:37:40 +08:00
|
|
|
include: incl
|
2018-08-07 20:19:49 +08:00
|
|
|
end
|
|
|
|
|
2018-08-23 20:52:00 +08:00
|
|
|
def create
|
|
|
|
attributes = inventory_item_params.merge(
|
|
|
|
created_by: current_user,
|
|
|
|
last_modified_by: current_user
|
|
|
|
)
|
|
|
|
if inventory_cells_params.present?
|
|
|
|
inventory_cells_params
|
|
|
|
.each { |p| p.require(:attributes).require(%i(column_id value)) }
|
|
|
|
item = @inventory.repository_rows.new(attributes)
|
|
|
|
item.transaction do
|
|
|
|
item.save!
|
|
|
|
inventory_cells_params.each do |cell_params|
|
|
|
|
cell_attributes = cell_params[:attributes]
|
|
|
|
column =
|
|
|
|
@inventory.repository_columns.find(cell_attributes[:column_id])
|
2018-08-24 17:26:49 +08:00
|
|
|
RepositoryCell.create_with_value!(
|
2018-08-23 20:52:00 +08:00
|
|
|
item, column, cell_attributes[:value], current_user
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
item = @inventory.repository_rows.create!(attributes)
|
|
|
|
end
|
2018-08-23 22:48:27 +08:00
|
|
|
render jsonapi: item,
|
2018-08-23 20:52:00 +08:00
|
|
|
serializer: InventoryItemSerializer,
|
|
|
|
include: :inventory_cells,
|
|
|
|
status: :created
|
|
|
|
end
|
|
|
|
|
2018-08-24 17:26:49 +08:00
|
|
|
def show
|
|
|
|
render jsonapi: @inventory_item,
|
|
|
|
serializer: InventoryItemSerializer,
|
|
|
|
include: :inventory_cells
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
item_changed = false
|
|
|
|
if inventory_cells_params.present?
|
|
|
|
inventory_cells_params.each do |p|
|
|
|
|
p.require(%i(id attributes))
|
|
|
|
p.require(:attributes).require(:value)
|
|
|
|
end
|
|
|
|
@inventory_item.transaction do
|
|
|
|
inventory_cells_params.each do |cell_params|
|
|
|
|
cell = @inventory_item.repository_cells.find(cell_params[:id])
|
|
|
|
cell_value = cell_params.dig(:attributes, :value)
|
|
|
|
next unless cell.value.data_changed?(cell_value)
|
|
|
|
cell.value.update_data!(cell_value, current_user)
|
|
|
|
item_changed = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@inventory_item.attributes = update_inventory_item_params
|
|
|
|
item_changed = true if @inventory_item.changed?
|
|
|
|
if item_changed
|
|
|
|
@inventory_item.last_modified_by = current_user
|
|
|
|
@inventory_item.save!
|
2018-08-24 22:41:26 +08:00
|
|
|
render jsonapi: @inventory_item,
|
2018-08-24 17:26:49 +08:00
|
|
|
serializer: InventoryItemSerializer,
|
|
|
|
include: :inventory_cells
|
|
|
|
else
|
2018-10-23 23:52:48 +08:00
|
|
|
render body: nil, status: :no_content
|
2018-08-24 17:26:49 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@inventory_item.destroy!
|
|
|
|
render body: nil
|
|
|
|
end
|
|
|
|
|
2018-08-07 20:19:49 +08:00
|
|
|
private
|
|
|
|
|
2018-08-23 20:52:00 +08:00
|
|
|
def check_manage_permissions
|
2019-07-12 22:43:54 +08:00
|
|
|
raise PermissionError.new(RepositoryItem, :manage) unless can_manage_repository_rows?(@inventory)
|
2018-08-23 20:52:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def inventory_item_params
|
|
|
|
unless params.require(:data).require(:type) == 'inventory_items'
|
2018-10-23 23:52:48 +08:00
|
|
|
raise TypeError
|
2018-08-23 20:52:00 +08:00
|
|
|
end
|
|
|
|
params.require(:data).require(:attributes)
|
2018-10-16 21:35:02 +08:00
|
|
|
params.permit(data: { attributes: :name })[:data]
|
2018-08-23 20:52:00 +08:00
|
|
|
end
|
|
|
|
|
2018-08-24 17:26:49 +08:00
|
|
|
def update_inventory_item_params
|
|
|
|
unless params.require(:data).require(:id).to_i == params[:id].to_i
|
2018-10-23 23:52:48 +08:00
|
|
|
raise IDMismatchError
|
2018-08-24 17:26:49 +08:00
|
|
|
end
|
|
|
|
inventory_item_params[:attributes]
|
|
|
|
end
|
|
|
|
|
2018-08-23 20:52:00 +08:00
|
|
|
# Partially implement sideposting draft
|
|
|
|
# https://github.com/json-api/json-api/pull/1197
|
|
|
|
def inventory_cells_params
|
|
|
|
params[:included]&.select { |el| el[:type] == 'inventory_cells' }
|
|
|
|
end
|
2018-08-07 20:19:49 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|