Improve objects loading and permissions checking for repository rows [SCI-9763] (#6875)

This commit is contained in:
ajugo 2024-01-09 09:37:50 +01:00 committed by GitHub
parent a201c96aef
commit a6c0c7294b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 54 additions and 55 deletions

View file

@ -1144,6 +1144,7 @@ var RepositoryDatatable = (function(global) {
clearRowSelection();
},
selectedRows: () => { return rowsSelected; },
repositoryId: () => $(TABLE_ID).data('repository-id'),
redrawTableOnSidebarToggle: redrawTableOnSidebarToggle,
checkAvailableColumns: checkAvailableColumns
});

View file

@ -33,8 +33,10 @@
PrintModalComponent.openModal();
if (selectedRows && selectedRows.length) {
$('#modal-info-repository-row').modal('hide');
PrintModalComponent.repository_id = $(this).data('repositoryId');
PrintModalComponent.row_ids = selectedRows;
} else {
PrintModalComponent.repository_id = RepositoryDatatable.repositoryId();
PrintModalComponent.row_ids = [...RepositoryDatatable.selectedRows()];
}
}

View file

@ -178,7 +178,8 @@ var zebraPrint = (function() {
printer_name: string,
number_of_copies: int,
label_template_id: int,
repository_row_ids: array[]
repository_row_ids: array[],
repository_id: int
}
*/
print: function(modalUrl, progressModal, printModal, printData) {

View file

@ -5,18 +5,15 @@ class RepositoryRowsController < ApplicationController
include MyModulesHelper
include RepositoryDatatableHelper
MAX_PRINTABLE_ITEM_NAME_LENGTH = 64
before_action :load_repository, except: %i(show print rows_to_print print_zpl
validate_label_template_columns actions_toolbar)
before_action :load_repository_row_print, only: %i(print rows_to_print print_zpl validate_label_template_columns)
before_action :load_show_vars, only: %i(show)
before_action :load_repository_or_snapshot, only: %i(print rows_to_print print_zpl validate_label_template_columns)
before_action :load_repository_row, only: %i(update update_cell assigned_task_list
before_action :load_repository, except: %i(show print rows_to_print print_zpl validate_label_template_columns)
before_action :load_repository_or_snapshot, only: %i(show print rows_to_print print_zpl
validate_label_template_columns)
before_action :load_repository_row, only: %i(show update update_cell assigned_task_list
active_reminder_repository_cells relationships)
before_action :check_read_permissions, except: %i(create update delete_records
copy_records reminder_repository_cells
delete_records archive_records restore_records
actions_toolbar)
before_action :load_repository_rows, only: %i(print rows_to_print print_zpl validate_label_template_columns)
before_action :check_read_permissions, except: %i(create update update_cell delete_records
copy_records archive_records restore_records)
before_action :check_snapshotting_status, only: %i(create update delete_records copy_records)
before_action :check_create_permissions, only: :create
before_action :check_delete_permissions, only: %i(delete_records archive_records restore_records)
@ -101,7 +98,7 @@ class RepositoryRowsController < ApplicationController
def validate_label_template_columns
label_template = LabelTemplate.where(team_id: current_team.id).find(params[:label_template_id])
label_code = LabelTemplates::RepositoryRowService.new(label_template, @repository_row.first).render
label_code = LabelTemplates::RepositoryRowService.new(label_template, @repository_rows.first).render
if label_code[:error].empty?
render json: { label_code: label_code[:label] }
else
@ -111,7 +108,7 @@ class RepositoryRowsController < ApplicationController
def print_zpl
label_template = LabelTemplate.find_by(id: params[:label_template_id])
labels = @repository_row.flat_map do |repository_row|
labels = @repository_rows.flat_map do |repository_row|
LabelTemplates::RepositoryRowService.new(label_template,
repository_row).render[:label]
end
@ -128,7 +125,7 @@ class RepositoryRowsController < ApplicationController
end
def rows_to_print
render json: @repository_row, each_serializer: RepositoryRowSerializer, user: current_user
render json: @repository_rows, each_serializer: RepositoryRowSerializer, user: current_user
end
def print
@ -141,7 +138,7 @@ class RepositoryRowsController < ApplicationController
label_printer = LabelPrinter.find(params[:label_printer_id])
label_template = LabelTemplate.find_by(id: params[:label_template_id])
job_ids = @repository_row.flat_map do |repository_row|
job_ids = @repository_rows.flat_map do |repository_row|
LabelPrinters::PrintJob.perform_later(
label_printer,
LabelTemplates::RepositoryRowService.new(label_template,
@ -369,26 +366,10 @@ class RepositoryRowsController < ApplicationController
render_404 unless @repository
end
def load_repository_row_print
@repository_row = RepositoryRow.where(id: params[:rows])
render_404 unless @repository_row
end
def load_repository_or_snapshot
@repository = Repository.accessible_by_teams(current_team).find_by(id: @repository_row&.first&.repository_id)
@repository ||= RepositorySnapshot.find_by(id: @repository_row&.first&.repository_id)
render_404 unless @repository
end
def load_show_vars
@repository = Repository.accessible_by_teams(current_team).find_by(id: params[:repository_id])
@repository ||= RepositorySnapshot.find_by(id: params[:repository_id])
@repository = Repository.accessible_by_teams(current_team).find_by(id: params[:repository_id]) ||
RepositorySnapshot.find_by(id: params[:repository_id])
return render_404 unless @repository
@repository_row = @repository.repository_rows.eager_load(:repository_columns).find_by(id: params[:id])
render_404 unless @repository_row
end
def load_repository_row
@ -396,6 +377,12 @@ class RepositoryRowsController < ApplicationController
render_404 unless @repository_row
end
def load_repository_rows
@repository_rows = @repository.repository_rows.eager_load(:repository_columns).where(id: params[:row_ids])
render_404 if @repository_rows.blank?
end
def check_read_permissions
render_403 unless can_read_repository?(@repository)
end

View file

@ -308,7 +308,8 @@
<div id="divider" class="w-500 bg-sn-light-grey flex px-8 items-center self-stretch h-px mb-6"></div>
<div id="bottom-button-wrapper" class="flex h-10 justify-end">
<button type="button" class="btn btn-primary print-label-button" data-e2e="e2e-BT-invInventoryItemSB-print"
:data-rows="JSON.stringify([repositoryRowId])">
:data-rows="JSON.stringify([repositoryRowId])"
:data-repository-id="repository?.id">
{{ i18n.t('repositories.item_card.print_label') }}
</button>
</div>

View file

@ -99,6 +99,7 @@ export default {
props: {
showModal: Boolean,
row_ids: Array,
repository_id: Number,
urls: Object
},
data() {
@ -166,7 +167,7 @@ export default {
}
},
row_ids() {
$.get(this.urls.rows, { rows: this.row_ids }, (result) => {
$.get(this.urls.rows, { repository_id: this.repository_id, row_ids: this.row_ids }, (result) => {
this.rows = result.data;
});
}
@ -194,7 +195,11 @@ export default {
validateTemplate() {
if (!this.selectedTemplate || this.row_ids.length == 0) return;
$.post(this.urls.printValidation, { label_template_id: this.selectedTemplate.id, rows: this.row_ids }, (result) => {
$.post(this.urls.printValidation, {
repository_id: this.repository_id,
label_template_id: this.selectedTemplate.id,
row_ids: this.row_ids
}, (result) => {
this.labelTemplateError = null;
this.labelTemplateCode = result.label_code;
}).fail((result) => {
@ -213,12 +218,14 @@ export default {
printer_name: this.selectedPrinter.attributes.name,
number_of_copies: this.copies,
label_template_id: this.selectedTemplate.id,
rows: this.row_ids
row_ids: this.row_ids,
repository_id: this.repository_id
}
);
} else {
$.post(this.urls.print, {
rows: this.row_ids,
row_ids: this.row_ids,
repository_id: this.repository_id,
label_printer_id: this.selectedPrinter.id,
label_template_id: this.selectedTemplate.id,
copies: this.copies

View file

@ -11,7 +11,6 @@ module LabelTemplates
def initialize(label_template, repository_row)
@label_template = label_template
@repository_row = repository_row
@repository_columns = RepositoryColumn.where(repository_id: @repository_row.repository_id).pluck(:name)
end
def render
@ -48,7 +47,7 @@ module LabelTemplates
case key
when /^c_(.*)/
name = Regexp.last_match(1)
unless @repository_columns.include?(name)
unless @repository_row.repository_columns.find_by(name: name)
raise LabelTemplates::ColumnNotFoundError, I18n.t('label_templates.repository_row.errors.column_not_found')
end

View file

@ -1,16 +1,17 @@
<div class="print-label-modal-container"
data-print-url="<%= print_repository_rows_path %>"
data-zebra-progress-url="<%= print_zpl_repository_rows_path%>"
data-print-url="<%= print_repositories_path %>"
data-zebra-progress-url="<%= print_zpl_repositories_path %>"
data-printers-url="<%= label_printers_path(format: :json) %>"
data-label-templates-url="<%= label_templates_path(format: :json) %>"
data-rows-url="<%= rows_to_print_repository_rows_path %>"
data-print-validation-url="<%= validate_label_template_columns_repository_rows_path %>"
data-rows-url="<%= rows_to_print_repositories_path %>"
data-print-validation-url="<%= validate_label_template_columns_repositories_path %>"
data-label-preview-url="<%= zpl_preview_label_templates_path %>"
data-fluics-info-url="<%= Constants::SCINOTE_FLUICS_URL %>"
>
<print-modal-container
:show-modal = "showModal"
:row_ids = "row_ids"
:repository_id="repository_id"
:urls = "urls"
@close="closeModal"
></print-modal-container>

View file

@ -60,7 +60,7 @@
%>
<div id="actionToolbar" data-behaviour="vue">
<action-toolbar actions-url="<%= actions_toolbar_repository_rows_url %>" />
<action-toolbar actions-url="<%= actions_toolbar_repository_repository_rows_path(@repository) %>" />
</div>
</div>

View file

@ -673,15 +673,6 @@ Rails.application.routes.draw do
resources :comments, only: %i(index create update destroy)
resources :repository_rows, only: %i() do
collection do
get :rows_to_print
post :print
get :print_zpl
post :validate_label_template_columns
get :actions_toolbar
end
end
resources :repositories do
post 'repository_index',
to: 'repository_rows#index',
@ -739,6 +730,11 @@ Rails.application.routes.draw do
get :active_reminder_repository_cells
put :update_cell
end
collection do
get :actions_toolbar
end
resources :repository_row_connections, only: %i(index create destroy)
member do
get 'repository_stock_value/new', to: 'repository_stock_values#new', as: 'new_repository_stock'
@ -755,6 +751,10 @@ Rails.application.routes.draw do
get :sidebar
post 'available_rows', to: 'repository_rows#available_rows', defaults: { format: 'json' }
get 'export_repository_stock_items_modal'
get :rows_to_print, to: 'repository_rows#rows_to_print'
get :print_zpl, to: 'repository_rows#print_zpl'
post :validate_label_template_columns, to: 'repository_rows#validate_label_template_columns'
post :print, to: 'repository_rows#print'
end
member do