From 4ddf3d24cd8e6d78f0f648322c81d6c75edd4262 Mon Sep 17 00:00:00 2001 From: Anton Date: Mon, 7 Oct 2024 11:47:32 +0200 Subject: [PATCH] Fix moving action for shared locations [SCI-11149] --- app/controllers/storage_locations_controller.rb | 13 +++++++++++-- .../vue/storage_locations/modals/move.vue | 13 ++++++++++--- .../vue/storage_locations/modals/move_tree_mixin.js | 13 +++++++++++-- app/models/storage_location.rb | 5 +++++ .../lists/storage_location_serializer.rb | 2 +- config/locales/en.yml | 1 + 6 files changed, 39 insertions(+), 8 deletions(-) diff --git a/app/controllers/storage_locations_controller.rb b/app/controllers/storage_locations_controller.rb index 122f54225..0e39fb5da 100644 --- a/app/controllers/storage_locations_controller.rb +++ b/app/controllers/storage_locations_controller.rb @@ -123,8 +123,17 @@ class StorageLocationsController < ApplicationController end def tree - records = StorageLocation.viewable_by_user(current_user, current_team).where(parent: nil, container: [false, params[:container] == 'true']) - render json: storage_locations_recursive_builder(records) + records = StorageLocation.viewable_by_user(current_user, current_team) + .where( + parent: nil, + container: [false, params[:container] == 'true'] + ) + records = records.where(team_id: params[:team_id]) if params[:team_id] + + render json: { + locations: storage_locations_recursive_builder(records), + movable_to_root: params[:team_id] && current_team.id == params[:team_id].to_i + } end def available_positions diff --git a/app/javascript/vue/storage_locations/modals/move.vue b/app/javascript/vue/storage_locations/modals/move.vue index 1f9f9c3f3..fd8cb851d 100644 --- a/app/javascript/vue/storage_locations/modals/move.vue +++ b/app/javascript/vue/storage_locations/modals/move.vue @@ -25,9 +25,12 @@
-
+
{{ i18n.t('storage_locations.index.move_modal.search_header') }}
@@ -63,11 +66,15 @@ export default { selectedObject: Array, moveToUrl: String }, + created() { + this.teamId = this.selectedObject.team_id; + }, mixins: [modalMixin, MoveTreeMixin], data() { return { selectedStorageLocationId: null, storageLocationsTree: [], + teamId: null, query: '', moveMode: 'locations' }; diff --git a/app/javascript/vue/storage_locations/modals/move_tree_mixin.js b/app/javascript/vue/storage_locations/modals/move_tree_mixin.js index 744e8495b..49d4fb7f4 100644 --- a/app/javascript/vue/storage_locations/modals/move_tree_mixin.js +++ b/app/javascript/vue/storage_locations/modals/move_tree_mixin.js @@ -6,14 +6,19 @@ import { export default { mounted() { - axios.get(this.storageLocationsTreeUrl).then((response) => { - this.storageLocationsTree = response.data; + axios.get(this.storageLocationsTreeUrl, { params: { team_id: this.teamId } }).then((response) => { + this.storageLocationsTree = response.data.locations; + this.movableToRoot = response.data.movable_to_root; + if (!this.movableToRoot) { + this.selectedStorageLocationId = -1; + } this.dataLoaded = true; }); }, data() { return { selectedStorageLocationId: null, + movableToRoot: false, storageLocationsTree: [], query: '', dataLoaded: false @@ -46,6 +51,10 @@ export default { }).filter(Boolean); }, selectStorageLocation(storageLocationId) { + if (!this.movableToRoot && storageLocationId === null) { + return; + } + this.selectedStorageLocationId = storageLocationId; } } diff --git a/app/models/storage_location.rb b/app/models/storage_location.rb index d444e185c..c2880c6fc 100644 --- a/app/models/storage_location.rb +++ b/app/models/storage_location.rb @@ -21,6 +21,7 @@ class StorageLocation < ApplicationRecord has_many :repository_rows, through: :storage_location_repository_rows validates :name, length: { maximum: Constants::NAME_MAX_LENGTH } + validate :parent_same_team, if: -> { parent.present? } validate :parent_validation, if: -> { parent.present? } validate :no_grid_options, if: -> { !container } validate :no_dimensions, if: -> { !with_grid? } @@ -203,6 +204,10 @@ class StorageLocation < ApplicationRecord errors.add(:metadata, I18n.t('activerecord.errors.models.storage_location.attributes.metadata.invalid')) if metadata['display_type'] || metadata['dimensions'] end + def parent_same_team + errors.add(:parent, I18n.t('activerecord.errors.models.storage_location.attributes.parent_storage_location_team')) if parent.team != team + end + def no_dimensions errors.add(:metadata, I18n.t('activerecord.errors.models.storage_location.attributes.metadata.invalid')) if !with_grid? && metadata['dimensions'] end diff --git a/app/serializers/lists/storage_location_serializer.rb b/app/serializers/lists/storage_location_serializer.rb index f913e4e66..257eac53e 100644 --- a/app/serializers/lists/storage_location_serializer.rb +++ b/app/serializers/lists/storage_location_serializer.rb @@ -9,7 +9,7 @@ module Lists attributes :id, :code, :name, :container, :description, :owned_by, :created_by, :created_on, :urls, :metadata, :file_name, :sub_location_count, :is_empty, - :img_url, :sa_description, :name_hash + :img_url, :sa_description, :name_hash, :team_id def owned_by object['team_name'] diff --git a/config/locales/en.yml b/config/locales/en.yml index 2b3bdd0aa..938e41fc8 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -265,6 +265,7 @@ en: not_uniq_repository_row: 'Inventory item already exists' attributes: parent_storage_location: "Storage location cannot be parent to itself" + parent_storage_location_team: "Parent storage location and storage location should belongs to the same team" parent_storage_location_child: "Storage location cannot be moved to it's child" metadata: invalid: 'Invalid metadata'