2020-06-10 00:50:36 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module RepositoryActions
|
|
|
|
class ArchiveRowsBaseService
|
|
|
|
extend Service
|
|
|
|
|
2020-06-10 04:16:55 +08:00
|
|
|
attr_reader :errors
|
2020-06-10 00:50:36 +08:00
|
|
|
|
2020-06-19 15:04:42 +08:00
|
|
|
def initialize(user:, team:, repository:, repository_rows:, log_activities: true)
|
2020-06-10 00:50:36 +08:00
|
|
|
@user = user
|
|
|
|
@team = team
|
|
|
|
@repository = repository
|
|
|
|
@repository_rows = scoped_repository_rows(repository_rows)
|
2020-06-19 15:04:42 +08:00
|
|
|
@log_activities = log_activities
|
2020-06-10 00:50:36 +08:00
|
|
|
@errors = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def call
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def succeed?
|
|
|
|
@errors.none?
|
|
|
|
end
|
|
|
|
|
2020-06-10 04:16:55 +08:00
|
|
|
def error_message
|
|
|
|
@errors.values.join(', ')
|
|
|
|
end
|
|
|
|
|
2020-06-10 00:50:36 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def scoped_repository_rows(_ids)
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid?
|
|
|
|
unless @user && @repository
|
|
|
|
@errors[:invalid_arguments] =
|
|
|
|
{ 'user': @user,
|
|
|
|
'repository': @repository }
|
|
|
|
.map do |key, value|
|
|
|
|
"Can't find #{key.capitalize}" if value.nil?
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
2020-06-10 04:16:55 +08:00
|
|
|
@errors[:repository_rows] = I18n.t('repositories.archive_records.invalid_rows_flash') if @repository_rows.blank?
|
2020-06-10 00:50:36 +08:00
|
|
|
|
|
|
|
succeed?
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_activity(type, row)
|
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type,
|
|
|
|
owner: @user,
|
2022-07-07 18:00:35 +08:00
|
|
|
subject: row,
|
2020-06-10 00:50:36 +08:00
|
|
|
team: @team,
|
|
|
|
message_items: {
|
|
|
|
repository_row: row.id
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|