2019-08-19 19:30:56 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-22 23:54:30 +08:00
|
|
|
class RepositoriesController < ApplicationController
|
2019-09-02 16:30:29 +08:00
|
|
|
include InventoriesHelper
|
|
|
|
include ActionView::Helpers::TagHelper
|
|
|
|
include ActionView::Context
|
|
|
|
include IconsHelper
|
2020-04-23 22:02:36 +08:00
|
|
|
include TeamsHelper
|
2020-06-01 16:24:03 +08:00
|
|
|
include RepositoriesDatatableHelper
|
2021-12-07 22:28:20 +08:00
|
|
|
include MyModulesHelper
|
2019-09-02 16:30:29 +08:00
|
|
|
|
2023-06-16 16:48:39 +08:00
|
|
|
before_action :load_repository, except: %i(index create create_modal sidebar archive restore actions_toolbar
|
2024-07-29 21:41:40 +08:00
|
|
|
export_modal export_repositories list)
|
|
|
|
before_action :load_repositories, only: %i(index list)
|
2020-06-24 18:01:38 +08:00
|
|
|
before_action :load_repositories_for_archiving, only: :archive
|
|
|
|
before_action :load_repositories_for_restoring, only: :restore
|
2024-07-29 21:41:40 +08:00
|
|
|
before_action :check_view_all_permissions, only: %i(index sidebar list)
|
2023-06-16 16:48:39 +08:00
|
|
|
before_action :check_view_permissions, except: %i(index create_modal create update destroy parse_sheet
|
|
|
|
import_records sidebar archive restore actions_toolbar
|
2024-07-29 21:41:40 +08:00
|
|
|
export_modal export_repositories list)
|
2022-02-28 17:33:43 +08:00
|
|
|
before_action :check_manage_permissions, only: %i(rename_modal update)
|
|
|
|
before_action :check_delete_permissions, only: %i(destroy destroy_modal)
|
2020-06-19 00:07:23 +08:00
|
|
|
before_action :check_archive_permissions, only: %i(archive restore)
|
2020-05-15 23:46:59 +08:00
|
|
|
before_action :check_create_permissions, only: %i(create_modal create)
|
|
|
|
before_action :check_copy_permissions, only: %i(copy_modal copy)
|
2019-08-19 19:30:56 +08:00
|
|
|
before_action :set_inline_name_editing, only: %i(show)
|
2024-03-20 16:30:39 +08:00
|
|
|
before_action :load_repository_row, only: %i(show)
|
2023-04-19 16:53:39 +08:00
|
|
|
before_action :set_breadcrumbs_items, only: %i(index show)
|
2024-03-07 22:48:01 +08:00
|
|
|
before_action :validate_file_type, only: %i(export_repository export_repositories)
|
2017-05-22 23:54:30 +08:00
|
|
|
|
2018-03-28 20:12:11 +08:00
|
|
|
layout 'fluid'
|
2018-03-21 16:22:55 +08:00
|
|
|
|
2017-05-22 23:54:30 +08:00
|
|
|
def index
|
2020-06-01 16:24:03 +08:00
|
|
|
respond_to do |format|
|
2020-12-22 19:08:13 +08:00
|
|
|
format.html do
|
2023-05-30 17:19:10 +08:00
|
|
|
render 'index'
|
2020-12-22 19:08:13 +08:00
|
|
|
end
|
2020-06-01 16:24:03 +08:00
|
|
|
format.json do
|
2024-05-22 21:56:03 +08:00
|
|
|
repositories = Lists::RepositoriesService.new(@repositories, params, user: current_user).call
|
2024-01-06 01:58:12 +08:00
|
|
|
render json: repositories, each_serializer: Lists::RepositorySerializer, user: current_user,
|
|
|
|
meta: pagination_dict(repositories)
|
2020-06-01 16:24:03 +08:00
|
|
|
end
|
2018-03-20 21:45:57 +08:00
|
|
|
end
|
2017-05-22 23:54:30 +08:00
|
|
|
end
|
|
|
|
|
2024-07-29 21:41:40 +08:00
|
|
|
def list
|
|
|
|
results = @repositories
|
|
|
|
results = results.name_like(params[:query]) if params[:query].present?
|
|
|
|
render json: { data: results.map { |r| [r.id, r.name] } }
|
|
|
|
end
|
|
|
|
|
|
|
|
def rows_list
|
|
|
|
render json: { data: @repository.repository_rows.map { |r| [r.id, r.name] } }
|
|
|
|
end
|
|
|
|
|
2020-06-04 20:39:56 +08:00
|
|
|
def sidebar
|
|
|
|
render json: {
|
2023-06-21 20:13:20 +08:00
|
|
|
html: render_to_string(partial: 'repositories/sidebar', locals: {
|
2020-06-04 20:39:56 +08:00
|
|
|
repositories: @repositories,
|
2020-07-06 20:03:55 +08:00
|
|
|
archived: params[:archived] == 'true'
|
2020-06-04 20:39:56 +08:00
|
|
|
})
|
|
|
|
}
|
2017-05-22 23:54:30 +08:00
|
|
|
end
|
|
|
|
|
2018-03-20 21:45:57 +08:00
|
|
|
def show
|
2024-04-09 20:18:41 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
current_team_switch(@repository.team) unless @repository.shared_with?(current_team)
|
|
|
|
@display_edit_button = can_create_repository_rows?(@repository)
|
|
|
|
@display_delete_button = can_delete_repository_rows?(@repository)
|
|
|
|
@display_duplicate_button = can_create_repository_rows?(@repository)
|
|
|
|
@snapshot_provisioning = @repository.repository_snapshots.provisioning.any?
|
2021-08-02 17:11:11 +08:00
|
|
|
|
2024-04-09 20:18:41 +08:00
|
|
|
@busy_printer = LabelPrinter.where.not(current_print_job_ids: []).first
|
|
|
|
end
|
|
|
|
format.json do
|
|
|
|
# render serialized repository json
|
|
|
|
render json: @repository, serializer: RepositorySerializer
|
|
|
|
end
|
|
|
|
end
|
2020-05-15 23:46:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def table_toolbar
|
|
|
|
render json: {
|
2024-05-15 21:45:40 +08:00
|
|
|
html: render_to_string(partial: 'repositories/toolbar_buttons', locals: { view_mode: params[:view_mode]})
|
2020-05-15 23:46:59 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def status
|
|
|
|
render json: {
|
|
|
|
editable: can_manage_repository_rows?(@repository),
|
|
|
|
snapshot_provisioning: @repository.repository_snapshots.provisioning.any?
|
|
|
|
}
|
2019-11-21 00:12:47 +08:00
|
|
|
end
|
2017-06-08 17:44:24 +08:00
|
|
|
|
2020-01-14 18:13:19 +08:00
|
|
|
def load_table
|
|
|
|
render json: {
|
2023-06-21 20:13:20 +08:00
|
|
|
html: render_to_string(partial: 'repositories/repository_table',
|
2020-01-14 18:13:19 +08:00
|
|
|
locals: {
|
|
|
|
repository: @repository,
|
|
|
|
repository_index_link: repository_table_index_path(@repository)
|
|
|
|
})
|
|
|
|
}
|
2017-06-08 17:44:24 +08:00
|
|
|
end
|
|
|
|
|
2017-06-06 21:50:43 +08:00
|
|
|
def create_modal
|
|
|
|
@repository = Repository.new
|
2023-07-05 18:43:23 +08:00
|
|
|
render json: {
|
|
|
|
html: render_to_string(partial: 'create_repository_modal', formats: :html)
|
|
|
|
}
|
2017-05-24 15:29:44 +08:00
|
|
|
end
|
|
|
|
|
2022-03-30 16:54:55 +08:00
|
|
|
def hide_reminders
|
|
|
|
# synchronously hide currently visible reminders
|
|
|
|
if params[:visible_reminder_repository_row_ids].present?
|
|
|
|
repository_cell_ids =
|
|
|
|
RepositoryCell.joins(:repository_row)
|
|
|
|
.where(repository_rows: { id: params[:visible_reminder_repository_row_ids] })
|
|
|
|
.where.not(id: current_user.hidden_repository_cell_reminders.select(:repository_cell_id))
|
|
|
|
.with_active_reminder(current_user).pluck(:id)
|
|
|
|
|
|
|
|
HiddenRepositoryCellReminder.import(
|
|
|
|
repository_cell_ids.map { |id| { repository_cell_id: id, user_id: current_user.id } }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
# offload the rest to background job
|
2023-08-11 21:31:29 +08:00
|
|
|
HideRepositoryRemindersJob.perform_later(@repository, current_user.id)
|
2022-03-30 16:54:55 +08:00
|
|
|
|
|
|
|
render json: { status: :ok }, status: :accepted
|
|
|
|
end
|
|
|
|
|
2017-05-24 15:29:44 +08:00
|
|
|
def create
|
|
|
|
@repository = Repository.new(
|
2020-05-15 23:46:59 +08:00
|
|
|
team: current_team,
|
2017-05-24 15:29:44 +08:00
|
|
|
created_by: current_user
|
|
|
|
)
|
|
|
|
@repository.assign_attributes(repository_params)
|
|
|
|
|
2023-07-18 19:36:41 +08:00
|
|
|
if @repository.save
|
|
|
|
log_activity(:create_inventory)
|
2024-01-06 01:58:12 +08:00
|
|
|
render json: { message: t('repositories.index.modal_create.success_flash_html', name: @repository.name) }
|
2023-07-18 19:36:41 +08:00
|
|
|
else
|
2024-01-06 01:58:12 +08:00
|
|
|
render json: @repository.errors, status: :unprocessable_entity
|
2017-05-24 15:29:44 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-30 21:11:28 +08:00
|
|
|
def destroy_modal
|
2023-06-21 20:13:20 +08:00
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
2023-07-18 19:36:41 +08:00
|
|
|
partial: 'delete_repository_modal',
|
|
|
|
formats: :html
|
2023-06-21 20:13:20 +08:00
|
|
|
)
|
|
|
|
}
|
2017-05-30 21:11:28 +08:00
|
|
|
end
|
|
|
|
|
2020-06-19 00:07:23 +08:00
|
|
|
def archive
|
2020-06-24 17:32:20 +08:00
|
|
|
service = Repositories::ArchiveRepositoryService.call(repositories: @repositories,
|
2020-06-19 00:07:23 +08:00
|
|
|
user: current_user,
|
|
|
|
team: current_team)
|
|
|
|
if service.succeed?
|
2023-05-29 17:48:13 +08:00
|
|
|
render json: { message: t('repositories.archive_inventories.success_flash') }, status: :ok
|
2020-06-19 00:07:23 +08:00
|
|
|
else
|
2023-05-29 17:48:13 +08:00
|
|
|
render json: { message: service.error_message }, status: :unprocessable_entity
|
2020-06-19 00:07:23 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def restore
|
2020-06-24 17:32:20 +08:00
|
|
|
service = Repositories::RestoreRepositoryService.call(repositories: @repositories,
|
2020-06-19 00:07:23 +08:00
|
|
|
user: current_user,
|
|
|
|
team: current_team)
|
|
|
|
if service.succeed?
|
2023-05-29 17:48:13 +08:00
|
|
|
render json: { message: t('repositories.restore_inventories.success_flash') }, status: :ok
|
2020-06-19 00:07:23 +08:00
|
|
|
else
|
2023-05-29 17:48:13 +08:00
|
|
|
render json: { message: service.error_message }, status: :unprocessable_entity
|
2020-06-19 00:07:23 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-30 21:09:09 +08:00
|
|
|
def destroy
|
2019-03-07 02:27:40 +08:00
|
|
|
log_activity(:delete_inventory) # Log before delete id
|
|
|
|
|
2018-05-25 17:41:43 +08:00
|
|
|
@repository.discard
|
2018-05-29 21:33:02 +08:00
|
|
|
@repository.destroy_discarded(current_user.id)
|
2024-01-06 01:58:12 +08:00
|
|
|
|
|
|
|
render json: {
|
|
|
|
message: t('repositories.index.delete_flash', name: @repository.name)
|
|
|
|
}
|
2017-06-01 01:24:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def rename_modal
|
2023-06-21 20:13:20 +08:00
|
|
|
render json: {
|
2023-07-20 22:46:16 +08:00
|
|
|
html: render_to_string(partial: 'rename_repository_modal', formats: :html)
|
2023-06-21 20:13:20 +08:00
|
|
|
}
|
2017-06-01 01:24:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2019-09-12 23:21:48 +08:00
|
|
|
@repository.update(repository_params)
|
2017-05-30 21:09:09 +08:00
|
|
|
|
2023-06-21 20:13:20 +08:00
|
|
|
if @repository.save
|
|
|
|
log_activity(:rename_inventory) # Acton only for renaming
|
2019-03-07 02:27:40 +08:00
|
|
|
|
2023-06-21 20:13:20 +08:00
|
|
|
render json: { url: team_repositories_path }
|
|
|
|
else
|
|
|
|
render json: @repository.errors, status: :unprocessable_entity
|
2017-05-30 21:09:09 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-08 01:11:25 +08:00
|
|
|
def copy_modal
|
|
|
|
@tmp_repository = Repository.new(
|
2020-05-15 23:46:59 +08:00
|
|
|
team: current_team,
|
2017-06-08 01:11:25 +08:00
|
|
|
created_by: current_user,
|
|
|
|
name: @repository.name
|
|
|
|
)
|
2023-06-21 20:13:20 +08:00
|
|
|
render json: {
|
2023-07-20 18:59:45 +08:00
|
|
|
html: render_to_string(partial: 'copy_repository_modal', formats: :html)
|
2023-06-21 20:13:20 +08:00
|
|
|
}
|
2017-06-08 01:11:25 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def copy
|
|
|
|
@tmp_repository = Repository.new(
|
2020-05-15 23:46:59 +08:00
|
|
|
team: current_team,
|
2017-06-08 01:11:25 +08:00
|
|
|
created_by: current_user
|
|
|
|
)
|
|
|
|
@tmp_repository.assign_attributes(repository_params)
|
|
|
|
|
2023-06-21 20:13:20 +08:00
|
|
|
if !@tmp_repository.valid?
|
|
|
|
render json: @tmp_repository.errors, status: :unprocessable_entity
|
|
|
|
else
|
|
|
|
copied_repository = @repository.copy(current_user, @tmp_repository.name)
|
2024-02-28 20:42:11 +08:00
|
|
|
old_repo_stock_column = @repository.repository_columns.find_by(data_type: 'RepositoryStockValue')
|
|
|
|
copied_repo_stock_column = copied_repository.repository_columns.find_by(data_type: 'RepositoryStockValue')
|
|
|
|
|
|
|
|
if old_repo_stock_column && copied_repo_stock_column
|
2024-03-08 22:33:13 +08:00
|
|
|
old_repo_stock_column.repository_stock_unit_items.each do |item|
|
|
|
|
copied_item = item.dup
|
|
|
|
copied_repo_stock_column.repository_stock_unit_items << copied_item
|
|
|
|
end
|
2024-02-28 20:42:11 +08:00
|
|
|
copied_repository.save!
|
|
|
|
end
|
2017-06-08 01:11:25 +08:00
|
|
|
|
2023-06-21 20:13:20 +08:00
|
|
|
if !copied_repository
|
|
|
|
render json: { name: ['Server error'] }, status: :unprocessable_entity
|
|
|
|
else
|
|
|
|
render json: {
|
2024-01-06 01:58:12 +08:00
|
|
|
message: t('repositories.index.copy_flash', old: @repository.name, new: copied_repository.name)
|
2023-06-21 20:13:20 +08:00
|
|
|
}
|
2017-06-08 01:11:25 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-06 23:35:29 +08:00
|
|
|
# AJAX actions
|
|
|
|
def repository_table_index
|
2023-06-21 20:13:20 +08:00
|
|
|
render json: ::RepositoryDatatable.new(view_context, @repository, nil, current_user)
|
2017-06-06 23:35:29 +08:00
|
|
|
end
|
|
|
|
|
2017-06-13 14:10:10 +08:00
|
|
|
def parse_sheet
|
2020-05-15 23:46:59 +08:00
|
|
|
render_403 unless can_create_repository_rows?(@repository)
|
2017-06-14 15:11:20 +08:00
|
|
|
|
2018-01-16 01:01:35 +08:00
|
|
|
unless import_params[:file]
|
2024-06-07 15:55:11 +08:00
|
|
|
unprocessable_entity_repository_response(t('repositories.parse_sheet.errors.no_file_selected'))
|
2017-07-14 22:19:03 +08:00
|
|
|
return
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
parsed_file = ImportRepository::ParseRepository.new(
|
2018-01-16 01:01:35 +08:00
|
|
|
file: import_params[:file],
|
2020-05-15 23:46:59 +08:00
|
|
|
repository: @repository,
|
2024-07-09 19:34:40 +08:00
|
|
|
date_format: current_user.settings['date_format'],
|
2017-07-14 22:19:03 +08:00
|
|
|
session: session
|
|
|
|
)
|
|
|
|
if parsed_file.too_large?
|
2024-06-07 15:55:11 +08:00
|
|
|
render json: { error: t('general.file.size_exceeded', file_size: Rails.configuration.x.file_max_size_mb) },
|
|
|
|
status: :unprocessable_entity
|
2018-05-31 16:45:31 +08:00
|
|
|
elsif parsed_file.has_too_many_rows?
|
2024-06-07 15:55:11 +08:00
|
|
|
render json: { error: t('repositories.import_records.error_message.items_limit',
|
|
|
|
items_size: Constants::IMPORT_REPOSITORY_ITEMS_LIMIT) }, status: :unprocessable_entity
|
2024-06-28 15:30:20 +08:00
|
|
|
elsif parsed_file.has_too_little_rows?
|
|
|
|
render json: { error: t('repositories.parse_sheet.errors.items_min_limit') },
|
|
|
|
status: :unprocessable_entity
|
2017-07-14 22:19:03 +08:00
|
|
|
else
|
|
|
|
@import_data = parsed_file.data
|
2017-10-12 20:43:25 +08:00
|
|
|
|
2021-07-23 17:56:28 +08:00
|
|
|
if @import_data.header.blank? || @import_data.columns.blank?
|
2024-04-09 20:18:41 +08:00
|
|
|
return render json: { error: t('repositories.parse_sheet.errors.empty_file') }, status: :unprocessable_entity
|
2017-10-12 20:43:25 +08:00
|
|
|
end
|
|
|
|
|
2017-10-17 20:42:06 +08:00
|
|
|
if (@temp_file = parsed_file.generate_temp_file)
|
2024-06-07 15:55:11 +08:00
|
|
|
render json: { import_data: @import_data, temp_file: @temp_file }
|
2017-07-14 22:19:03 +08:00
|
|
|
else
|
2024-06-07 15:55:11 +08:00
|
|
|
render json: { error: t('repositories.parse_sheet.errors.temp_file_failure') }, status: :unprocessable_entity
|
2017-06-14 15:11:20 +08:00
|
|
|
end
|
|
|
|
end
|
2017-07-14 22:19:03 +08:00
|
|
|
rescue ArgumentError, CSV::MalformedCSVError
|
2024-06-07 15:55:11 +08:00
|
|
|
render json: { error: t('repositories.parse_sheet.errors.invalid_file', encoding: ''.encoding) },
|
|
|
|
status: :unprocessable_entity
|
2017-07-14 22:19:03 +08:00
|
|
|
rescue TypeError
|
2024-06-07 15:55:11 +08:00
|
|
|
render json: { error: t('repositories.parse_sheet.errors.invalid_extension') }, status: :unprocessable_entity
|
2017-06-14 15:11:20 +08:00
|
|
|
end
|
2017-06-13 14:10:10 +08:00
|
|
|
end
|
|
|
|
|
2017-06-19 20:05:37 +08:00
|
|
|
def import_records
|
2024-08-09 21:56:59 +08:00
|
|
|
render_403 unless can_create_repository_rows?(Repository.viewable_by_user(current_user)
|
2024-06-07 15:55:11 +08:00
|
|
|
.find_by(id: import_params[:id]))
|
2023-06-21 20:13:20 +08:00
|
|
|
# Check if there exist mapping for repository record (it's mandatory)
|
2024-04-09 20:18:41 +08:00
|
|
|
if import_params[:mappings].present? && import_params[:mappings].value?('-1')
|
2024-06-07 15:55:11 +08:00
|
|
|
status = ImportRepository::ImportRecords
|
|
|
|
.new(
|
|
|
|
temp_file: TempFile.find_by(id: import_params[:file_id]),
|
2024-08-09 21:56:59 +08:00
|
|
|
repository: Repository.viewable_by_user(current_user).find_by(id: import_params[:id]),
|
2024-06-07 15:55:11 +08:00
|
|
|
mappings: import_params[:mappings],
|
|
|
|
session: session,
|
|
|
|
user: current_user,
|
|
|
|
can_edit_existing_items: import_params[:can_edit_existing_items],
|
|
|
|
should_overwrite_with_empty_cells: import_params[:should_overwrite_with_empty_cells],
|
|
|
|
preview: import_params[:preview]
|
|
|
|
).import!
|
2024-06-17 19:33:04 +08:00
|
|
|
message = t('repositories.import_records.partial_success_flash',
|
2024-07-10 21:46:24 +08:00
|
|
|
successful_rows_count: (status[:created_rows_count] + status[:updated_rows_count]),
|
|
|
|
total_rows_count: status[:total_rows_count])
|
2024-06-17 19:33:04 +08:00
|
|
|
|
2023-06-21 20:13:20 +08:00
|
|
|
if status[:status] == :ok
|
2024-07-11 20:57:49 +08:00
|
|
|
unless import_params[:preview] || (status[:created_rows_count] + status[:updated_rows_count]).zero?
|
2024-07-10 21:46:24 +08:00
|
|
|
log_activity(
|
|
|
|
:inventory_items_added_or_updated_with_import,
|
|
|
|
created_rows_count: status[:created_rows_count],
|
|
|
|
updated_rows_count: status[:updated_rows_count]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2024-06-17 19:33:04 +08:00
|
|
|
render json: import_params[:preview] ? status : { message: message }, status: :ok
|
2023-06-21 20:13:20 +08:00
|
|
|
else
|
2024-06-17 19:33:04 +08:00
|
|
|
render json: { message: message }, status: :unprocessable_entity
|
2017-06-21 20:45:50 +08:00
|
|
|
end
|
2023-06-21 20:13:20 +08:00
|
|
|
else
|
2024-06-07 15:55:11 +08:00
|
|
|
render json: { error: t('repositories.import_records.error_message.mapping_error') },
|
|
|
|
status: :unprocessable_entity
|
2017-06-21 15:31:39 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-04 17:02:28 +08:00
|
|
|
def export_empty_repository
|
|
|
|
col_ids = [-3, -4, -5, -6, -7, -8, -9, -10]
|
|
|
|
col_ids << -11 if Repository.repository_row_connections_enabled?
|
|
|
|
col_ids += @repository.repository_columns.map(&:id)
|
|
|
|
|
|
|
|
xlsx = RepositoryXlsxExport.to_empty_xlsx(@repository, col_ids)
|
|
|
|
|
2024-07-08 22:49:17 +08:00
|
|
|
send_data(
|
|
|
|
xlsx,
|
|
|
|
filename: "#{@repository.name.gsub(/\s/, '_')}_template_#{Date.current}.xlsx",
|
|
|
|
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
|
|
|
)
|
2024-06-04 17:02:28 +08:00
|
|
|
end
|
|
|
|
|
2017-06-06 22:12:34 +08:00
|
|
|
def export_repository
|
2023-06-21 20:13:20 +08:00
|
|
|
if params[:row_ids] && params[:header_ids]
|
2023-09-08 17:35:16 +08:00
|
|
|
RepositoryZipExportJob.perform_later(
|
|
|
|
user_id: current_user.id,
|
|
|
|
params: {
|
|
|
|
repository_id: @repository.id,
|
|
|
|
row_ids: params[:row_ids],
|
|
|
|
header_ids: params[:header_ids]
|
2024-03-07 22:48:01 +08:00
|
|
|
},
|
2024-06-07 15:55:11 +08:00
|
|
|
file_type: params[:file_type]
|
2023-09-08 17:35:16 +08:00
|
|
|
)
|
2024-04-10 22:55:49 +08:00
|
|
|
update_user_export_file_type if current_user.settings[:repository_export_file_type] != params[:file_type]
|
2023-06-21 20:13:20 +08:00
|
|
|
log_activity(:export_inventory_items)
|
2023-09-07 19:12:27 +08:00
|
|
|
render json: { message: t('zip_export.export_request_success') }
|
2023-06-21 20:13:20 +08:00
|
|
|
else
|
|
|
|
render json: { message: t('zip_export.export_error') }, status: :unprocessable_entity
|
2017-06-06 22:12:34 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-12 16:29:17 +08:00
|
|
|
def export_repositories
|
|
|
|
repositories = Repository.viewable_by_user(current_user, current_team).where(id: params[:repository_ids])
|
2024-06-17 22:02:23 +08:00
|
|
|
if repositories.present?
|
2024-03-07 22:48:01 +08:00
|
|
|
RepositoriesExportJob
|
|
|
|
.perform_later(params[:file_type], repositories.pluck(:id), user_id: current_user.id, team_id: current_team.id)
|
2024-04-10 22:55:49 +08:00
|
|
|
update_user_export_file_type if current_user.settings[:repository_export_file_type] != params[:file_type]
|
2023-11-20 20:37:57 +08:00
|
|
|
log_activity(:export_inventories, inventories: repositories.pluck(:name).join(', '))
|
2023-06-12 16:29:17 +08:00
|
|
|
render json: { message: t('zip_export.export_request_success') }
|
|
|
|
else
|
|
|
|
render json: { message: t('zip_export.export_error') }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-07 19:12:27 +08:00
|
|
|
def export_repository_stock_items
|
2023-11-20 20:37:57 +08:00
|
|
|
repository_rows = @repository.repository_rows.where(id: params[:row_ids]).pluck(:id, :name)
|
|
|
|
if repository_rows.any?
|
2023-09-08 17:35:16 +08:00
|
|
|
RepositoryStockZipExportJob.perform_later(
|
|
|
|
user_id: current_user.id,
|
|
|
|
params: {
|
2023-11-20 20:37:57 +08:00
|
|
|
repository_row_ids: repository_rows.map { |row| row[0] }
|
2023-09-08 17:35:16 +08:00
|
|
|
}
|
|
|
|
)
|
2023-11-20 20:37:57 +08:00
|
|
|
log_activity(
|
|
|
|
:export_inventory_stock_consumption,
|
|
|
|
inventory_items: repository_rows.map { |row| row[1] }.join(', ')
|
|
|
|
)
|
2023-09-07 19:12:27 +08:00
|
|
|
render json: { message: t('zip_export.export_request_success') }
|
|
|
|
else
|
|
|
|
render json: { message: t('zip_export.export_error') }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-28 20:45:28 +08:00
|
|
|
def export_repository_stock_items_modal
|
|
|
|
render json: {
|
|
|
|
export_consumption_url: export_repository_stock_items_team_path(@repository),
|
|
|
|
name: @repository.name
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-12-07 22:28:20 +08:00
|
|
|
def assigned_my_modules
|
|
|
|
my_modules = MyModule.joins(:repository_rows).where(repository_rows: { repository: @repository })
|
|
|
|
.readable_by_user(current_user).distinct
|
2022-01-27 21:01:09 +08:00
|
|
|
render json: {data: grouped_by_prj_exp(my_modules).map { |g|
|
|
|
|
{
|
|
|
|
label: "#{g[:project_name]} / #{g[:experiment_name]}", options: g[:tasks].map do |t|
|
|
|
|
{ label: t.name, value: t.id }
|
|
|
|
end
|
|
|
|
}
|
|
|
|
} }
|
2021-12-07 22:28:20 +08:00
|
|
|
end
|
|
|
|
|
2021-12-15 22:16:12 +08:00
|
|
|
def repository_users
|
2022-01-28 05:50:04 +08:00
|
|
|
rows_type = params[:archived_by].present? ? :archived_repository_rows : :created_repository_rows
|
|
|
|
users = User.joins(rows_type)
|
|
|
|
.where(rows_type => { repository: @repository })
|
|
|
|
.group(:id)
|
2021-12-15 22:16:12 +08:00
|
|
|
|
|
|
|
render json: { users: users.map do |u|
|
|
|
|
{
|
|
|
|
label: u.full_name,
|
|
|
|
value: u.id,
|
|
|
|
params: {
|
|
|
|
email: u.email, avatar_url: u.avatar_url('icon_small')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end }
|
|
|
|
end
|
|
|
|
|
2023-05-24 15:57:06 +08:00
|
|
|
def actions_toolbar
|
|
|
|
render json: {
|
|
|
|
actions:
|
|
|
|
Toolbars::RepositoriesService.new(
|
|
|
|
current_user,
|
|
|
|
current_team,
|
2024-01-06 01:58:12 +08:00
|
|
|
repository_ids: JSON.parse(params[:items]).map { |i| i['id'] }
|
2023-05-24 15:57:06 +08:00
|
|
|
).actions
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-06-21 15:31:39 +08:00
|
|
|
private
|
|
|
|
|
2020-05-15 23:46:59 +08:00
|
|
|
def load_repository
|
2017-06-12 23:49:42 +08:00
|
|
|
repository_id = params[:id] || params[:repository_id]
|
2024-08-09 21:56:59 +08:00
|
|
|
@repository = Repository.viewable_by_user(current_user).find_by(id: repository_id)
|
2017-06-12 23:49:42 +08:00
|
|
|
render_404 unless @repository
|
|
|
|
end
|
|
|
|
|
2020-05-15 23:46:59 +08:00
|
|
|
def load_repositories
|
2024-08-09 21:56:59 +08:00
|
|
|
@repositories = Repository.viewable_by_user(current_user)
|
2017-05-22 23:54:30 +08:00
|
|
|
end
|
|
|
|
|
2020-06-24 17:32:20 +08:00
|
|
|
def load_repositories_for_archiving
|
2020-06-24 18:01:38 +08:00
|
|
|
@repositories = current_team.repositories.active.where(id: params[:repository_ids])
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_repositories_for_restoring
|
|
|
|
@repositories = current_team.repositories.archived.where(id: params[:repository_ids])
|
2020-06-24 17:32:20 +08:00
|
|
|
end
|
|
|
|
|
2024-03-20 16:30:39 +08:00
|
|
|
def load_repository_row
|
|
|
|
@repository_row = nil
|
2024-04-16 17:51:04 +08:00
|
|
|
@repository_row_landing_page = true if params[:landing_page].present?
|
2024-03-20 16:30:39 +08:00
|
|
|
return if params[:row_id].blank?
|
|
|
|
|
|
|
|
@repository_row = @repository.repository_rows.find_by(id: params[:row_id])
|
|
|
|
end
|
|
|
|
|
2019-08-19 19:30:56 +08:00
|
|
|
def set_inline_name_editing
|
2024-07-30 17:39:21 +08:00
|
|
|
return unless can_manage_repository?(@repository) && !@repository.is_a?(SoftLockedRepository)
|
2019-08-19 19:30:56 +08:00
|
|
|
|
|
|
|
@inline_editable_title_config = {
|
|
|
|
name: 'title',
|
|
|
|
params_group: 'repository',
|
2020-01-06 23:07:23 +08:00
|
|
|
item_id: @repository.id,
|
2019-08-19 19:30:56 +08:00
|
|
|
field_to_udpate: 'name',
|
2019-09-02 16:30:29 +08:00
|
|
|
path_to_update: team_repository_path(@repository),
|
2023-03-14 01:00:30 +08:00
|
|
|
label_after:
|
|
|
|
sanitize_input(
|
|
|
|
"<span class=\"repository-share-icon\">#{inventory_shared_status_icon(@repository, current_team)}</span>"
|
|
|
|
)
|
2019-08-19 19:30:56 +08:00
|
|
|
}
|
2017-06-19 20:05:37 +08:00
|
|
|
end
|
|
|
|
|
2017-05-22 23:54:30 +08:00
|
|
|
def check_view_all_permissions
|
2020-05-15 23:46:59 +08:00
|
|
|
render_403 unless can_read_team?(current_team)
|
2017-05-22 23:54:30 +08:00
|
|
|
end
|
2017-06-01 01:24:38 +08:00
|
|
|
|
2017-06-12 23:49:42 +08:00
|
|
|
def check_view_permissions
|
2019-07-17 22:00:49 +08:00
|
|
|
render_403 unless can_read_repository?(@repository)
|
2017-06-12 23:49:42 +08:00
|
|
|
end
|
|
|
|
|
2017-05-24 15:29:44 +08:00
|
|
|
def check_create_permissions
|
2020-05-15 23:46:59 +08:00
|
|
|
render_403 unless can_create_repositories?(current_team)
|
2017-05-24 15:29:44 +08:00
|
|
|
end
|
|
|
|
|
2019-08-29 19:39:22 +08:00
|
|
|
def check_copy_permissions
|
2020-05-15 23:46:59 +08:00
|
|
|
render_403 if !can_create_repositories?(current_team) || @repository.shared_with?(current_team)
|
2017-05-24 15:29:44 +08:00
|
|
|
end
|
|
|
|
|
2018-02-16 01:46:29 +08:00
|
|
|
def check_manage_permissions
|
|
|
|
render_403 unless can_manage_repository?(@repository)
|
2017-06-08 01:11:25 +08:00
|
|
|
end
|
|
|
|
|
2020-06-19 00:07:23 +08:00
|
|
|
def check_archive_permissions
|
2020-06-24 17:32:20 +08:00
|
|
|
@repositories.each do |repository|
|
|
|
|
return render_403 unless can_archive_repository?(repository)
|
|
|
|
end
|
2020-06-19 00:07:23 +08:00
|
|
|
end
|
|
|
|
|
2022-02-28 17:33:43 +08:00
|
|
|
def check_delete_permissions
|
|
|
|
render_403 unless can_delete_repository?(@repository)
|
|
|
|
end
|
|
|
|
|
2017-06-01 01:24:38 +08:00
|
|
|
def repository_params
|
|
|
|
params.require(:repository).permit(:name)
|
|
|
|
end
|
2017-06-14 15:11:20 +08:00
|
|
|
|
2018-01-16 01:01:35 +08:00
|
|
|
def import_params
|
2024-06-07 15:55:11 +08:00
|
|
|
params.permit(:id, :file, :file_id, :preview, :can_edit_existing_items,
|
|
|
|
:should_overwrite_with_empty_cells, :preview, mappings: {}).to_h
|
2018-01-16 01:01:35 +08:00
|
|
|
end
|
|
|
|
|
2017-06-14 15:11:20 +08:00
|
|
|
def repository_response(message)
|
2017-07-14 22:19:03 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
flash[:alert] = message
|
2017-12-08 00:59:23 +08:00
|
|
|
redirect_back(fallback_location: root_path)
|
2017-07-14 22:19:03 +08:00
|
|
|
end
|
|
|
|
format.json do
|
|
|
|
render json: { message: message },
|
|
|
|
status: :unprocessable_entity
|
|
|
|
end
|
2017-06-14 15:11:20 +08:00
|
|
|
end
|
|
|
|
end
|
2019-03-07 02:27:40 +08:00
|
|
|
|
2019-04-18 21:18:10 +08:00
|
|
|
def log_activity(type_of, message_items = {})
|
2023-11-20 20:37:57 +08:00
|
|
|
if @repository.present?
|
|
|
|
message_items = { repository: @repository.id }.merge(message_items)
|
|
|
|
|
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type_of,
|
|
|
|
owner: current_user,
|
|
|
|
subject: @repository,
|
|
|
|
team: @repository.team,
|
|
|
|
message_items: message_items)
|
|
|
|
else
|
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type_of,
|
|
|
|
owner: current_user,
|
|
|
|
subject: @current_team,
|
|
|
|
team: @current_team,
|
|
|
|
message_items: message_items)
|
|
|
|
end
|
2019-03-07 02:27:40 +08:00
|
|
|
end
|
2023-04-19 16:53:39 +08:00
|
|
|
|
|
|
|
def set_breadcrumbs_items
|
|
|
|
@breadcrumbs_items = []
|
2024-04-18 22:19:18 +08:00
|
|
|
archived_branch = @repository&.archived? || (!@repository && params[:view_mode] == 'archived')
|
2023-04-19 16:53:39 +08:00
|
|
|
|
|
|
|
@breadcrumbs_items.push({
|
|
|
|
label: t('breadcrumbs.inventories'),
|
2024-04-18 22:19:18 +08:00
|
|
|
url: archived_branch ? repositories_path(view_mode: 'archived') : repositories_path,
|
2023-06-01 20:28:47 +08:00
|
|
|
archived: archived_branch
|
2023-04-19 16:53:39 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
if @repository
|
|
|
|
@breadcrumbs_items.push({
|
|
|
|
label: @repository.name,
|
|
|
|
url: repository_path(@repository),
|
2023-06-01 20:28:47 +08:00
|
|
|
archived: archived_branch
|
2023-04-19 16:53:39 +08:00
|
|
|
})
|
|
|
|
end
|
2023-05-24 06:52:26 +08:00
|
|
|
|
2023-06-01 17:31:54 +08:00
|
|
|
@breadcrumbs_items.each do |item|
|
|
|
|
item[:label] = "(A) #{item[:label]}" if item[:archived]
|
2023-05-24 06:52:26 +08:00
|
|
|
end
|
2023-04-19 16:53:39 +08:00
|
|
|
end
|
2024-03-07 22:48:01 +08:00
|
|
|
|
|
|
|
def validate_file_type
|
|
|
|
render json: { message: 'Invalid file type' }, status: :bad_request unless %w(csv xlsx).include?(params[:file_type])
|
|
|
|
end
|
2024-04-10 22:55:49 +08:00
|
|
|
|
|
|
|
def update_user_export_file_type
|
|
|
|
current_user.update_simple_setting(key: 'repository_export_file_type', value: params[:file_type])
|
|
|
|
end
|
2017-05-22 23:54:30 +08:00
|
|
|
end
|