Fix import and validation for containers without box [SCI-11065]

This commit is contained in:
Anton 2024-09-20 15:16:16 +02:00
parent 656e386cdd
commit 3fc6f20bd4
5 changed files with 35 additions and 10 deletions

View file

@ -31,7 +31,7 @@ class StorageLocationRepositoryRowsController < ApplicationController
render json: @storage_location_repository_row,
serializer: Lists::StorageLocationRepositoryRowSerializer
else
render json: @storage_location_repository_row.errors, status: :unprocessable_entity
render json: { errors: @storage_location_repository_row.errors.full_messages }, status: :unprocessable_entity
end
end
end
@ -45,7 +45,7 @@ class StorageLocationRepositoryRowsController < ApplicationController
render json: @storage_location_repository_row,
serializer: Lists::StorageLocationRepositoryRowSerializer
else
render json: @storage_location_repository_row.errors, status: :unprocessable_entity
render json: { errors: @storage_location_repository_row.errors.full_messages }, status: :unprocessable_entity
end
end
end

View file

@ -91,6 +91,8 @@ export default {
metadata: { position: this.position?.map((pos) => parseInt(pos, 10)) }
}).then(() => {
this.$emit('close');
}).catch((error) => {
HelperModule.flashAlertMsg(error.response.data.errors.join(', '), 'danger');
});
}
}

View file

@ -14,6 +14,10 @@ class StorageLocationRepositoryRow < ApplicationRecord
validate :ensure_uniq_position
end
with_options if: -> { storage_location.container && storage_location.metadata['display_type'] != 'grid' } do
validate :unique_repository_row
end
def human_readable_position
return unless metadata['position']
@ -34,4 +38,12 @@ class StorageLocationRepositoryRow < ApplicationRecord
errors.add(:base, I18n.t('activerecord.errors.models.storage_location.not_uniq_position'))
end
end
def unique_repository_row
if storage_location.storage_location_repository_rows
.where(repository_row_id: repository_row_id)
.where.not(id: id).exists?
errors.add(:base, I18n.t('activerecord.errors.models.storage_location.not_uniq_repository_row'))
end
end
end

View file

@ -26,8 +26,13 @@ module StorageLocations
return { status: :error, message: I18n.t('storage_locations.show.import_modal.errors.invalid_position') }
end
# Check if duplicate repository rows are present in the file
if !@storage_location.with_grid? && @rows.pluck(:repository_row_id).uniq.length != @rows.length
return { status: :error, message: I18n.t('storage_locations.show.import_modal.errors.duplicate_items') }
end
ActiveRecord::Base.transaction do
unassign_repository_rows!
unassign_repository_rows! if @storage_location.with_grid?
@rows.each do |row|
if @storage_location.with_grid? && !position_valid?(row[:position])
@ -59,18 +64,22 @@ module StorageLocations
row = SpreadsheetParser.parse_row(r, @sheet)
{
position: convert_position_letter_to_number(row[0]),
repository_row_id: row[1].gsub('IT', '').to_i
repository_row_id: row[1].to_s.gsub('IT', '').to_i
}
end
end
def import_row!(row)
storage_location_repository_row =
storage_location_repository_row = if @storage_location.with_grid?
@storage_location.storage_location_repository_rows
.find_or_initialize_by(
repository_row_id: row[:repository_row_id],
metadata: { position: row[:position] }
)
else
@storage_location.storage_location_repository_rows
.find_or_initialize_by(repository_row_id: row[:repository_row_id])
end
if storage_location_repository_row.new_record?
@assigned_count += 1

View file

@ -264,6 +264,7 @@ en:
storage_location:
missing_position: 'Missing position metadata'
not_uniq_position: 'Position already taken'
not_uniq_repository_row: 'Inventory item already exists'
attributes:
parent_storage_location: "Storage location cannot be parent to itself"
parent_storage_location_child: "Storage location cannot be moved to it's child"
@ -2709,6 +2710,7 @@ en:
errors:
invalid_structure: "The imported file content doesn't meet criteria."
invalid_position: "Positions in the file must match with the box."
duplicate_items: "Item ID has duplicates in the imported file"
invalid_item: "Item ID %{row_id} doesn't exist."
index:
head_title: "Locations"