2020-04-07 01:52:41 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class MyModuleRepositoriesController < ApplicationController
|
2020-04-22 23:55:22 +08:00
|
|
|
include ApplicationHelper
|
|
|
|
|
2024-03-27 22:26:25 +08:00
|
|
|
before_action :load_my_module, except: :assign_my_modules
|
2023-04-25 20:00:55 +08:00
|
|
|
before_action :load_repository, except: %i(repositories_dropdown_list repositories_list_html create)
|
2024-03-27 22:26:25 +08:00
|
|
|
before_action :check_my_module_view_permissions, except: %i(update consume_modal update_consumption assign_my_modules)
|
2023-04-25 20:00:55 +08:00
|
|
|
before_action :check_repository_view_permissions, except: %i(repositories_dropdown_list repositories_list_html create)
|
2022-01-28 18:10:56 +08:00
|
|
|
before_action :check_repository_row_consumption_permissions, only: %i(consume_modal update_consumption)
|
2023-04-25 20:00:55 +08:00
|
|
|
before_action :check_assign_repository_records_permissions, only: %i(update create)
|
2024-03-27 22:26:25 +08:00
|
|
|
before_action :load_my_modules, only: :assign_my_modules
|
2020-04-07 01:52:41 +08:00
|
|
|
|
2020-04-21 20:49:36 +08:00
|
|
|
def index_dt
|
2020-04-07 01:52:41 +08:00
|
|
|
@draw = params[:draw].to_i
|
2020-11-11 00:24:04 +08:00
|
|
|
per_page = params[:length].to_i < 1 ? Constants::REPOSITORY_DEFAULT_PAGE_SIZE : params[:length].to_i
|
2020-04-07 01:52:41 +08:00
|
|
|
page = (params[:start].to_i / per_page) + 1
|
|
|
|
datatable_service = RepositoryDatatableService.new(@repository, params, current_user, @my_module)
|
|
|
|
|
2020-04-07 18:36:01 +08:00
|
|
|
@datatable_params = {
|
|
|
|
view_mode: params[:view_mode],
|
2022-01-29 00:53:56 +08:00
|
|
|
my_module: @my_module,
|
2022-04-04 16:13:49 +08:00
|
|
|
include_stock_consumption: @repository.has_stock_management? && params[:assigned].present?,
|
|
|
|
disable_stock_management: true # stock management is always disabled in MyModule context
|
2020-04-07 18:36:01 +08:00
|
|
|
}
|
2022-04-04 16:13:49 +08:00
|
|
|
|
2020-04-07 01:52:41 +08:00
|
|
|
@all_rows_count = datatable_service.all_count
|
2020-04-08 03:02:16 +08:00
|
|
|
@columns_mappings = datatable_service.mappings
|
2022-01-29 00:53:56 +08:00
|
|
|
|
2020-05-12 22:26:44 +08:00
|
|
|
if params[:simple_view]
|
|
|
|
repository_rows = datatable_service.repository_rows
|
2023-07-11 15:21:22 +08:00
|
|
|
rows_view = 'repository_rows/simple_view_index'
|
2020-05-12 22:26:44 +08:00
|
|
|
else
|
2021-07-07 23:43:51 +08:00
|
|
|
repository_rows = datatable_service.repository_rows
|
|
|
|
.preload(:repository_columns,
|
|
|
|
:created_by,
|
|
|
|
repository_cells: { value: @repository.cell_preload_includes })
|
2023-07-11 15:21:22 +08:00
|
|
|
rows_view = 'repository_rows/index'
|
2020-05-12 22:26:44 +08:00
|
|
|
end
|
|
|
|
@repository_rows = repository_rows.page(page).per(per_page)
|
|
|
|
|
|
|
|
render rows_view
|
2020-04-07 01:52:41 +08:00
|
|
|
end
|
|
|
|
|
2024-03-27 22:26:25 +08:00
|
|
|
def assign_my_modules
|
|
|
|
assigned_count = 0
|
|
|
|
skipped_count = 0
|
|
|
|
status = :ok
|
|
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
@my_modules.find_each do |my_module|
|
|
|
|
service = RepositoryRows::MyModuleAssignUnassignService.call(
|
|
|
|
my_module:,
|
|
|
|
repository: @repository,
|
|
|
|
user: current_user,
|
|
|
|
params:
|
|
|
|
)
|
|
|
|
unless service.succeed?
|
|
|
|
status = :unprocessable_entity
|
|
|
|
raise ActiveRecord::Rollback
|
|
|
|
end
|
|
|
|
assigned_count += service.assigned_rows_count
|
|
|
|
skipped_count += (params[:rows_to_assign].length - service.assigned_rows_count)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: { assigned_count:, skipped_count: }, status:
|
|
|
|
end
|
|
|
|
|
2023-04-25 20:00:55 +08:00
|
|
|
def create
|
|
|
|
repository_row = RepositoryRow.find(params[:repository_row_id])
|
|
|
|
repository = repository_row.repository
|
|
|
|
return render_403 unless can_read_repository?(repository)
|
|
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
@my_module.my_module_repository_rows.create!(repository_row: repository_row, assigned_by: current_user)
|
|
|
|
|
|
|
|
Activities::CreateActivityService.call(activity_type: :assign_repository_record,
|
|
|
|
owner: current_user,
|
|
|
|
team: @my_module.experiment.project.team,
|
|
|
|
project: @my_module.experiment.project,
|
|
|
|
subject: @my_module,
|
|
|
|
message_items: { my_module: @my_module.id,
|
|
|
|
repository: repository.id,
|
|
|
|
record_names: repository_row.name })
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
flash: t('my_modules.assigned_items.direct_assign.success')
|
|
|
|
}
|
|
|
|
end
|
|
|
|
rescue StandardError => e
|
|
|
|
Rails.logger.error e.message
|
|
|
|
render json: {
|
|
|
|
flash: t('my_modules.repository.flash.update_error')
|
|
|
|
}, status: :bad_request
|
|
|
|
end
|
|
|
|
|
2020-04-22 23:55:22 +08:00
|
|
|
def update
|
2020-05-08 18:28:37 +08:00
|
|
|
service = RepositoryRows::MyModuleAssignUnassignService.call(my_module: @my_module,
|
|
|
|
repository: @repository,
|
|
|
|
user: current_user,
|
|
|
|
params: params)
|
2023-05-17 22:17:19 +08:00
|
|
|
if service.succeed?
|
2020-05-08 18:28:37 +08:00
|
|
|
flash = update_flash_message(service)
|
2020-04-22 23:55:22 +08:00
|
|
|
status = :ok
|
|
|
|
else
|
|
|
|
flash = t('my_modules.repository.flash.update_error')
|
|
|
|
status = :bad_request
|
|
|
|
end
|
|
|
|
|
2023-07-18 19:36:41 +08:00
|
|
|
render json: {
|
|
|
|
flash: flash,
|
|
|
|
rows_count: @my_module.repository_rows_count(@repository),
|
|
|
|
assigned_count: service.assigned_rows_count,
|
|
|
|
repository_id: @repository.repository_snapshots.find_by(selected: true)&.id || @repository.id
|
|
|
|
}, status: status
|
2020-04-22 23:55:22 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_repository_records_modal
|
|
|
|
modal = render_to_string(
|
2023-06-21 20:13:20 +08:00
|
|
|
partial: 'my_modules/modals/update_repository_records_modal_content',
|
2020-04-22 23:55:22 +08:00
|
|
|
locals: { my_module: @my_module,
|
|
|
|
repository: @repository,
|
2021-10-26 17:41:08 +08:00
|
|
|
selected_rows: params[:selected_rows],
|
|
|
|
downstream: params[:downstream] }
|
2020-04-22 23:55:22 +08:00
|
|
|
)
|
|
|
|
render json: {
|
|
|
|
html: modal,
|
2020-04-23 22:36:22 +08:00
|
|
|
update_url: my_module_repository_path(@my_module, @repository)
|
2020-04-22 23:55:22 +08:00
|
|
|
}, status: :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def assign_repository_records_modal
|
|
|
|
modal = render_to_string(
|
2023-06-21 20:13:20 +08:00
|
|
|
partial: 'my_modules/modals/assign_repository_records_modal_content',
|
2020-04-22 23:55:22 +08:00
|
|
|
locals: { my_module: @my_module,
|
|
|
|
repository: @repository,
|
2021-10-26 17:41:08 +08:00
|
|
|
selected_rows: params[:selected_rows],
|
|
|
|
downstream: params[:downstream] }
|
2020-04-22 23:55:22 +08:00
|
|
|
)
|
|
|
|
render json: {
|
|
|
|
html: modal,
|
2020-04-23 22:36:22 +08:00
|
|
|
update_url: my_module_repository_path(@my_module, @repository)
|
2020-04-22 23:55:22 +08:00
|
|
|
}, status: :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def repositories_list_html
|
2024-08-09 21:56:59 +08:00
|
|
|
@assigned_repositories = @my_module.readable_live_and_snapshot_repositories_list(current_user)
|
2020-05-08 23:01:55 +08:00
|
|
|
render json: {
|
|
|
|
html: render_to_string(partial: 'my_modules/repositories/repositories_list'),
|
2020-05-28 22:15:05 +08:00
|
|
|
assigned_rows_count: @assigned_repositories.map(&:assigned_rows_count).sum
|
2020-05-08 23:01:55 +08:00
|
|
|
}
|
2020-04-22 23:55:22 +08:00
|
|
|
end
|
|
|
|
|
2020-04-08 03:02:16 +08:00
|
|
|
def full_view_table
|
2020-04-21 20:49:36 +08:00
|
|
|
render json: {
|
2022-01-29 00:53:56 +08:00
|
|
|
html: render_to_string(
|
|
|
|
partial: 'my_modules/repositories/full_view_table',
|
2024-02-13 22:05:34 +08:00
|
|
|
locals: { include_stock_consumption: params[:include_stock_consumption] == 'true' }
|
2022-01-29 00:53:56 +08:00
|
|
|
)
|
2020-04-21 20:49:36 +08:00
|
|
|
}
|
2020-04-08 03:02:16 +08:00
|
|
|
end
|
|
|
|
|
2020-04-21 20:49:36 +08:00
|
|
|
def repositories_dropdown_list
|
2024-08-09 21:56:59 +08:00
|
|
|
@repositories = Repository.viewable_by_user(current_user).joins("
|
2020-06-24 17:23:17 +08:00
|
|
|
LEFT OUTER JOIN repository_rows ON
|
|
|
|
repository_rows.repository_id = repositories.id
|
|
|
|
LEFT OUTER JOIN my_module_repository_rows ON
|
|
|
|
my_module_repository_rows.repository_row_id = repository_rows.id
|
|
|
|
AND my_module_repository_rows.my_module_id = #{@my_module.id}
|
2020-07-09 20:50:14 +08:00
|
|
|
").select('COUNT(DISTINCT my_module_repository_rows.id) as rows_count, repositories.*')
|
2020-06-24 17:23:17 +08:00
|
|
|
.group(:id)
|
|
|
|
.having('COUNT(my_module_repository_rows.id) > 0 OR repositories.archived = FALSE')
|
|
|
|
.order(:name)
|
2020-04-09 21:14:55 +08:00
|
|
|
|
|
|
|
render json: { html: render_to_string(partial: 'my_modules/repositories/repositories_dropdown_list') }
|
|
|
|
end
|
|
|
|
|
2020-07-17 21:17:09 +08:00
|
|
|
def export_repository
|
|
|
|
if params[:header_ids]
|
2023-09-08 17:35:16 +08:00
|
|
|
RepositoryZipExportJob.perform_later(
|
|
|
|
user_id: current_user.id,
|
|
|
|
params: {
|
|
|
|
repository_id: @repository.id,
|
|
|
|
my_module_id: @my_module.id,
|
|
|
|
header_ids: params[:header_ids]
|
|
|
|
}
|
|
|
|
)
|
2020-08-05 15:59:58 +08:00
|
|
|
|
|
|
|
Activities::CreateActivityService.call(
|
|
|
|
activity_type: :export_inventory_items_assigned_to_task,
|
|
|
|
owner: current_user,
|
2020-09-15 17:58:35 +08:00
|
|
|
subject: @my_module,
|
2022-11-24 22:19:17 +08:00
|
|
|
team: @repository.team,
|
2020-08-05 15:59:58 +08:00
|
|
|
message_items: {
|
|
|
|
my_module: @my_module.id,
|
|
|
|
repository: @repository.id
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-07-17 21:17:09 +08:00
|
|
|
render json: { message: t('zip_export.export_request_success') }, status: :ok
|
|
|
|
else
|
|
|
|
render json: { message: t('zip_export.export_error') }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-28 18:10:56 +08:00
|
|
|
def consume_modal
|
|
|
|
@repository_row = @repository.repository_rows.find(params[:row_id])
|
|
|
|
@module_repository_row = @my_module.my_module_repository_rows.find_by(repository_row: @repository_row)
|
|
|
|
@stock_value = @module_repository_row.repository_row.repository_stock_value
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
2023-06-21 20:13:20 +08:00
|
|
|
partial: 'my_modules/repositories/consume_stock_modal_content'
|
2022-01-28 18:10:56 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_consumption
|
|
|
|
module_repository_row = @my_module.my_module_repository_rows.find_by(id: params[:module_row_id])
|
2022-03-30 20:33:26 +08:00
|
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
2022-03-10 01:43:38 +08:00
|
|
|
current_stock = module_repository_row.stock_consumption
|
2022-03-30 20:33:26 +08:00
|
|
|
module_repository_row.consume_stock(current_user, params[:stock_consumption], params[:comment])
|
2022-03-10 01:43:38 +08:00
|
|
|
|
2022-03-30 20:33:26 +08:00
|
|
|
log_activity(module_repository_row, current_stock, params[:comment])
|
2022-05-17 17:41:53 +08:00
|
|
|
protocol_consumption_notification(params[:comment], module_repository_row)
|
2022-01-28 18:10:56 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
render json: {}, status: :ok
|
|
|
|
end
|
|
|
|
|
2020-04-07 01:52:41 +08:00
|
|
|
private
|
|
|
|
|
2020-04-09 21:14:55 +08:00
|
|
|
def load_my_module
|
2020-04-15 00:17:54 +08:00
|
|
|
@my_module = MyModule.find_by(id: params[:my_module_id])
|
2020-04-09 21:14:55 +08:00
|
|
|
render_404 unless @my_module
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_repository
|
2020-06-22 20:57:35 +08:00
|
|
|
@repository = Repository.find_by(id: params[:id])
|
2020-04-15 00:17:54 +08:00
|
|
|
render_404 unless @repository
|
2020-04-09 21:14:55 +08:00
|
|
|
end
|
2020-04-07 01:52:41 +08:00
|
|
|
|
2024-03-27 22:26:25 +08:00
|
|
|
def load_my_modules
|
|
|
|
@my_modules = MyModule.where(id: params[:my_module_ids])
|
|
|
|
|
|
|
|
render_403 unless @my_modules.all? { |my_module| can_assign_my_module_repository_rows?(my_module) }
|
|
|
|
end
|
|
|
|
|
2020-04-09 21:14:55 +08:00
|
|
|
def check_my_module_view_permissions
|
2021-09-17 20:11:00 +08:00
|
|
|
render_403 unless can_read_my_module?(@my_module)
|
2020-04-07 01:52:41 +08:00
|
|
|
end
|
|
|
|
|
2020-04-09 21:14:55 +08:00
|
|
|
def check_repository_view_permissions
|
|
|
|
render_403 unless can_read_repository?(@repository)
|
2020-04-07 01:52:41 +08:00
|
|
|
end
|
2020-04-22 23:55:22 +08:00
|
|
|
|
|
|
|
def check_assign_repository_records_permissions
|
2021-09-14 17:08:35 +08:00
|
|
|
render_403 unless can_assign_my_module_repository_rows?(@my_module)
|
2020-04-22 23:55:22 +08:00
|
|
|
end
|
|
|
|
|
2022-01-28 18:10:56 +08:00
|
|
|
def check_repository_row_consumption_permissions
|
|
|
|
render_403 unless can_update_my_module_stock_consumption?(@my_module)
|
|
|
|
end
|
|
|
|
|
2020-05-08 18:28:37 +08:00
|
|
|
def update_flash_message(service)
|
|
|
|
assigned_count = service.assigned_rows_count
|
|
|
|
unassigned_count = service.unassigned_rows_count
|
2020-04-22 23:55:22 +08:00
|
|
|
|
|
|
|
if params[:downstream] == 'true'
|
|
|
|
if assigned_count && unassigned_count
|
|
|
|
t('my_modules.repository.flash.assign_and_unassign_from_task_and_downstream_html',
|
|
|
|
assigned_items: assigned_count,
|
2020-04-29 21:17:15 +08:00
|
|
|
unassigned_items: unassigned_count)
|
2020-04-22 23:55:22 +08:00
|
|
|
elsif assigned_count
|
|
|
|
t('my_modules.repository.flash.assign_to_task_and_downstream_html',
|
|
|
|
assigned_items: assigned_count)
|
|
|
|
elsif unassigned_count
|
|
|
|
t('my_modules.repository.flash.unassign_from_task_and_downstream_html',
|
|
|
|
unassigned_items: unassigned_count)
|
|
|
|
end
|
|
|
|
elsif assigned_count && unassigned_count
|
|
|
|
t('my_modules.repository.flash.assign_and_unassign_from_task_html',
|
|
|
|
assigned_items: assigned_count,
|
|
|
|
unassigned_items: unassigned_count)
|
|
|
|
elsif assigned_count
|
|
|
|
t('my_modules.repository.flash.assign_to_task_html',
|
|
|
|
assigned_items: assigned_count)
|
|
|
|
elsif unassigned_count
|
|
|
|
t('my_modules.repository.flash.unassign_from_task_html',
|
|
|
|
unassigned_items: unassigned_count)
|
|
|
|
end
|
|
|
|
end
|
2022-03-10 01:43:38 +08:00
|
|
|
|
2022-05-17 17:41:53 +08:00
|
|
|
def protocol_consumption_notification(comment, module_repository_row)
|
|
|
|
smart_annotation_notification(
|
|
|
|
old_text: nil,
|
|
|
|
new_text: comment,
|
2023-11-27 18:29:27 +08:00
|
|
|
subject: module_repository_row.repository_row,
|
2022-05-17 17:41:53 +08:00
|
|
|
title: t('notifications.my_module_consumption_comment_annotation_title',
|
|
|
|
repository_item: module_repository_row.repository_row.name,
|
|
|
|
repository: @repository.name,
|
|
|
|
user: current_user.full_name),
|
|
|
|
message: t('notifications.my_module_consumption_comment_annotation_message_html',
|
|
|
|
project: link_to(@my_module.experiment.project.name, project_url(@my_module.experiment.project)),
|
2023-01-12 18:31:04 +08:00
|
|
|
experiment: link_to(@my_module.experiment.name, my_modules_experiment_url(@my_module.experiment)),
|
2022-05-17 17:41:53 +08:00
|
|
|
my_module: link_to(@my_module.name, protocols_my_module_url(@my_module)))
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-03-10 01:43:38 +08:00
|
|
|
def log_activity(module_repository_row, stock_consumption_was, comment)
|
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: :task_inventory_item_stock_consumed,
|
|
|
|
owner: current_user,
|
|
|
|
subject: @my_module,
|
|
|
|
team: @repository.team,
|
2022-11-24 22:19:17 +08:00
|
|
|
project: @my_module.project,
|
2022-03-10 01:43:38 +08:00
|
|
|
message_items: {
|
|
|
|
repository: @repository.id,
|
|
|
|
repository_row: module_repository_row.repository_row_id,
|
|
|
|
stock_consumption_was: stock_consumption_was || 0,
|
|
|
|
unit: module_repository_row.repository_row.repository_stock_value.repository_stock_unit_item&.data || '',
|
|
|
|
stock_consumption: module_repository_row.stock_consumption || 0,
|
|
|
|
my_module: @my_module.id,
|
|
|
|
comment: comment
|
|
|
|
})
|
|
|
|
end
|
2020-04-07 01:52:41 +08:00
|
|
|
end
|