2024-07-10 13:29:18 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class StorageLocationsController < ApplicationController
|
2024-07-23 17:27:36 +08:00
|
|
|
before_action :load_storage_location, only: %i(update destroy duplicate move)
|
2024-07-10 13:29:18 +08:00
|
|
|
before_action :check_read_permissions, only: :index
|
|
|
|
before_action :check_manage_permissions, except: :index
|
2024-07-16 21:20:02 +08:00
|
|
|
before_action :set_breadcrumbs_items, only: :index
|
2024-07-10 13:29:18 +08:00
|
|
|
|
|
|
|
def index
|
2024-07-16 21:20:02 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.json do
|
|
|
|
storage_locations = Lists::StorageLocationsService.new(current_team, params).call
|
|
|
|
render json: storage_locations, each_serializer: Lists::StorageLocationSerializer,
|
|
|
|
user: current_user, meta: pagination_dict(storage_locations)
|
|
|
|
end
|
|
|
|
end
|
2024-07-10 13:29:18 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2024-07-19 19:24:23 +08:00
|
|
|
@storage_location.image.purge if params[:file_name].blank?
|
|
|
|
@storage_location.image.attach(params[:signed_blob_id]) if params[:signed_blob_id]
|
2024-07-10 13:29:18 +08:00
|
|
|
@storage_location.update(storage_location_params)
|
|
|
|
|
|
|
|
if @storage_location.save
|
2024-07-11 13:54:44 +08:00
|
|
|
render json: @storage_location, serializer: Lists::StorageLocationSerializer
|
2024-07-10 13:29:18 +08:00
|
|
|
else
|
2024-07-19 19:24:23 +08:00
|
|
|
render json: { error: @storage_location.errors.full_messages }, status: :unprocessable_entity
|
2024-07-10 13:29:18 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@storage_location = StorageLocation.new(
|
|
|
|
storage_location_params.merge({ team: current_team, created_by: current_user })
|
|
|
|
)
|
|
|
|
|
2024-07-19 19:24:23 +08:00
|
|
|
@storage_location.image.attach(params[:signed_blob_id]) if params[:signed_blob_id]
|
2024-07-10 13:29:18 +08:00
|
|
|
|
|
|
|
if @storage_location.save
|
2024-07-11 13:54:44 +08:00
|
|
|
render json: @storage_location, serializer: Lists::StorageLocationSerializer
|
2024-07-10 13:29:18 +08:00
|
|
|
else
|
2024-07-19 19:24:23 +08:00
|
|
|
render json: { error: @storage_location.errors.full_messages }, status: :unprocessable_entity
|
2024-07-10 13:29:18 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
if @storage_location.discard
|
|
|
|
render json: {}
|
|
|
|
else
|
|
|
|
render json: { errors: @storage_location.errors.full_messages }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-07-22 18:12:47 +08:00
|
|
|
def duplicate
|
|
|
|
new_storage_location = @storage_location.duplicate!
|
|
|
|
if new_storage_location
|
|
|
|
render json: new_storage_location, serializer: Lists::StorageLocationSerializer
|
|
|
|
else
|
|
|
|
render json: { errors: :failed }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
2024-07-23 17:27:36 +08:00
|
|
|
|
2024-07-23 14:50:17 +08:00
|
|
|
def move
|
|
|
|
storage_location_destination =
|
|
|
|
if move_params[:destination_storage_location_id] == 'root_storage_location'
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
current_team.storage_locations.find(move_params[:destination_storage_location_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
@storage_location.update!(parent: storage_location_destination)
|
|
|
|
|
|
|
|
render json: { message: I18n.t('storage_locations.index.move_modal.success_flash') }
|
|
|
|
rescue StandardError => e
|
|
|
|
Rails.logger.error e.message
|
|
|
|
Rails.logger.error e.backtrace.join("\n")
|
|
|
|
render json: { error: I18n.t('storage_locations.index.move_modal.error_flash') }, status: :bad_request
|
|
|
|
end
|
|
|
|
|
|
|
|
def tree
|
|
|
|
records = StorageLocation.inner_storage_locations(current_team)
|
|
|
|
.order(:name)
|
|
|
|
.select(:id, :name, :parent_id, :container)
|
|
|
|
render json: storage_locations_recursive_builder(nil, records)
|
|
|
|
end
|
2024-07-22 18:12:47 +08:00
|
|
|
|
2024-07-16 21:20:02 +08:00
|
|
|
def actions_toolbar
|
|
|
|
render json: {
|
2024-07-18 17:46:30 +08:00
|
|
|
actions:
|
|
|
|
Toolbars::StorageLocationsService.new(
|
|
|
|
current_user,
|
|
|
|
storage_location_ids: JSON.parse(params[:items]).map { |i| i['id'] }
|
|
|
|
).actions
|
2024-07-16 21:20:02 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2024-07-10 13:29:18 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def storage_location_params
|
2024-07-19 19:24:23 +08:00
|
|
|
params.permit(:id, :parent_id, :name, :container, :description,
|
|
|
|
metadata: [:display_type, dimensions: [], parent_coordinations: []])
|
2024-07-10 13:29:18 +08:00
|
|
|
end
|
|
|
|
|
2024-07-23 14:50:17 +08:00
|
|
|
def move_params
|
|
|
|
params.permit(:id, :destination_storage_location_id)
|
|
|
|
end
|
|
|
|
|
2024-07-10 13:29:18 +08:00
|
|
|
def load_storage_location
|
2024-07-23 14:50:17 +08:00
|
|
|
@storage_location = current_team.storage_locations.find_by(id: storage_location_params[:id])
|
2024-07-10 13:29:18 +08:00
|
|
|
render_404 unless @storage_location
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_read_permissions
|
|
|
|
render_403 unless true
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_manage_permissions
|
|
|
|
render_403 unless true
|
|
|
|
end
|
2024-07-16 21:20:02 +08:00
|
|
|
|
|
|
|
def set_breadcrumbs_items
|
|
|
|
@breadcrumbs_items = []
|
|
|
|
|
|
|
|
@breadcrumbs_items.push({
|
|
|
|
label: t('breadcrumbs.inventories')
|
|
|
|
})
|
|
|
|
|
|
|
|
@breadcrumbs_items.push({
|
|
|
|
label: t('breadcrumbs.locations'),
|
|
|
|
url: storage_locations_path
|
|
|
|
})
|
|
|
|
|
|
|
|
storage_locations = []
|
|
|
|
if params[:parent_id]
|
2024-07-23 14:50:17 +08:00
|
|
|
location = current_team.storage_locations.find_by(id: params[:parent_id])
|
2024-07-16 21:20:02 +08:00
|
|
|
if location
|
|
|
|
storage_locations.unshift(breadcrumbs_item(location))
|
|
|
|
while location.parent
|
|
|
|
location = location.parent
|
|
|
|
storage_locations.unshift(breadcrumbs_item(location))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@breadcrumbs_items += storage_locations
|
|
|
|
end
|
|
|
|
|
|
|
|
def breadcrumbs_item(location)
|
|
|
|
{
|
|
|
|
label: location.name,
|
|
|
|
url: storage_locations_path(parent_id: location.id)
|
|
|
|
}
|
|
|
|
end
|
2024-07-23 14:50:17 +08:00
|
|
|
|
2024-07-23 15:44:57 +08:00
|
|
|
def storage_locations_recursive_builder(parent_storage_location, records)
|
|
|
|
records.where(parent: parent_storage_location, container: false).map do |storage_location|
|
2024-07-23 14:50:17 +08:00
|
|
|
{
|
2024-07-23 15:44:57 +08:00
|
|
|
storage_location: storage_location,
|
|
|
|
children: storage_locations_recursive_builder(storage_location, records)
|
2024-07-23 14:50:17 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2024-07-10 13:29:18 +08:00
|
|
|
end
|