2020-04-21 20:49:36 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class MyModuleRepositorySnapshotsController < ApplicationController
|
|
|
|
before_action :load_my_module
|
2020-05-13 18:58:31 +08:00
|
|
|
before_action :load_repository, only: :create
|
|
|
|
before_action :load_repository_snapshot, except: %i(create full_view_sidebar select)
|
2020-05-12 22:26:44 +08:00
|
|
|
before_action :check_view_permissions, except: %i(create destroy select)
|
2021-09-17 20:11:00 +08:00
|
|
|
before_action :check_create_permissions, only: %i(create)
|
2021-09-22 17:28:32 +08:00
|
|
|
before_action :check_manage_permissions, only: %i(destroy select)
|
2020-04-21 20:49:36 +08:00
|
|
|
|
|
|
|
def index_dt
|
|
|
|
@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-21 20:49:36 +08:00
|
|
|
page = (params[:start].to_i / per_page) + 1
|
|
|
|
datatable_service = RepositorySnapshotDatatableService.new(@repository_snapshot, params, current_user, @my_module)
|
|
|
|
|
|
|
|
@all_rows_count = datatable_service.all_count
|
|
|
|
@columns_mappings = datatable_service.mappings
|
2020-05-12 22:26:44 +08:00
|
|
|
if params[:simple_view]
|
|
|
|
repository_rows = datatable_service.repository_rows
|
2022-02-11 17:46:19 +08:00
|
|
|
@repository = @repository_snapshot
|
2023-07-18 17:43:55 +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_snapshot.cell_preload_includes })
|
2023-07-17 19:15:04 +08:00
|
|
|
rows_view = 'repository_rows/snapshot_index'
|
2020-05-12 22:26:44 +08:00
|
|
|
end
|
|
|
|
@repository_rows = repository_rows.page(page).per(per_page)
|
|
|
|
|
|
|
|
render rows_view
|
2020-04-21 20:49:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2024-02-06 21:23:01 +08:00
|
|
|
repository_snapshot = RepositorySnapshot.create_preliminary!(@repository, @my_module, current_user)
|
2020-09-11 22:08:03 +08:00
|
|
|
RepositorySnapshotProvisioningJob.perform_later(repository_snapshot)
|
2020-04-28 18:03:23 +08:00
|
|
|
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(partial: 'my_modules/repositories/full_view_version',
|
2020-05-22 22:19:21 +08:00
|
|
|
locals: { repository_snapshot: repository_snapshot,
|
2020-05-25 22:29:00 +08:00
|
|
|
can_delete_snapshot: can_manage_my_module_repository_snapshots?(@my_module) })
|
2020-04-28 18:03:23 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def status
|
|
|
|
render json: {
|
|
|
|
status: @repository_snapshot.status
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
render json: {
|
2022-04-12 19:29:11 +08:00
|
|
|
repository_id: @repository_snapshot.parent_id,
|
2020-04-28 18:03:23 +08:00
|
|
|
html: render_to_string(partial: 'my_modules/repositories/full_view_version',
|
2020-05-22 22:19:21 +08:00
|
|
|
locals: { repository_snapshot: @repository_snapshot,
|
2020-05-25 22:29:00 +08:00
|
|
|
can_delete_snapshot: can_manage_my_module_repository_snapshots?(@my_module) })
|
2020-04-28 18:03:23 +08:00
|
|
|
}
|
2020-04-21 20:49:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@repository_snapshot.destroy!
|
2020-04-28 18:03:23 +08:00
|
|
|
render json: {}
|
2020-04-21 20:49:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def full_view_table
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(partial: 'my_modules/repositories/full_view_snapshot_table')
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-05-13 18:58:31 +08:00
|
|
|
def full_view_sidebar
|
2022-12-13 23:12:08 +08:00
|
|
|
@repository = Repository.viewable_by_user(current_user, current_team).find_by(id: params[:repository_id])
|
2020-05-22 23:14:44 +08:00
|
|
|
@repository_snapshots = @my_module.repository_snapshots
|
|
|
|
.where(parent_id: params[:repository_id])
|
2020-05-26 21:59:49 +08:00
|
|
|
.order(created_at: :desc)
|
2020-05-22 23:14:44 +08:00
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'my_modules/repositories/full_view_sidebar',
|
|
|
|
locals: {
|
|
|
|
live_items_present: @repository ? @my_module.repository_rows_count(@repository).positive? : false
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2020-04-21 20:49:36 +08:00
|
|
|
end
|
|
|
|
|
2020-05-12 22:26:44 +08:00
|
|
|
def select
|
2020-05-18 17:06:42 +08:00
|
|
|
if params[:repository_id]
|
|
|
|
@my_module.repository_snapshots.where(parent_id: params[:repository_id]).update(selected: nil)
|
2020-05-12 22:26:44 +08:00
|
|
|
else
|
|
|
|
repository_snapshot = @my_module.repository_snapshots.find_by(id: params[:repository_snapshot_id])
|
|
|
|
return render_404 unless repository_snapshot
|
|
|
|
|
2020-05-18 17:06:42 +08:00
|
|
|
@my_module.repository_snapshots
|
|
|
|
.where(original_repository: repository_snapshot.original_repository)
|
|
|
|
.update(selected: nil)
|
2020-05-12 22:26:44 +08:00
|
|
|
repository_snapshot.update!(selected: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: {}
|
|
|
|
end
|
|
|
|
|
2020-08-05 15:59:58 +08:00
|
|
|
def export_repository_snapshot
|
|
|
|
if params[:header_ids]
|
2023-09-08 17:35:16 +08:00
|
|
|
RepositoryZipExportJob.perform_later(
|
|
|
|
user_id: current_user.id,
|
|
|
|
params: {
|
|
|
|
repository_id: @repository_snapshot.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_snapshot_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: @my_module.team,
|
2020-08-05 15:59:58 +08:00
|
|
|
message_items: {
|
|
|
|
my_module: @my_module.id,
|
|
|
|
repository_snapshot: @repository_snapshot.id,
|
|
|
|
created_at: @repository_snapshot.created_at
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-04-21 20:49:36 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def load_my_module
|
|
|
|
@my_module = MyModule.find_by(id: params[:my_module_id])
|
|
|
|
render_404 unless @my_module
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_repository
|
|
|
|
@repository = Repository.find_by(id: params[:repository_id])
|
|
|
|
render_404 unless @repository
|
|
|
|
render_403 unless can_read_repository?(@repository)
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_repository_snapshot
|
|
|
|
@repository_snapshot = @my_module.repository_snapshots.find_by(id: params[:id])
|
|
|
|
render_404 unless @repository_snapshot
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_view_permissions
|
2021-09-17 20:11:00 +08:00
|
|
|
render_403 unless can_read_my_module?(@my_module)
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_create_permissions
|
|
|
|
render_403 unless can_create_my_module_repository_snapshots?(@my_module)
|
2020-04-21 20:49:36 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_manage_permissions
|
2020-05-25 22:29:00 +08:00
|
|
|
render_403 unless can_manage_my_module_repository_snapshots?(@my_module)
|
2020-04-21 20:49:36 +08:00
|
|
|
end
|
|
|
|
end
|