Permette agli admin di prolungare la scadenza dei replay.
Aggiunge data personalizzata e pulsanti rapidi nell'archivio admin così il job di purge non elimina i video fino alla nuova scadenza. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -13,7 +13,12 @@ module Recordings
|
||||
end
|
||||
|
||||
def update
|
||||
permitted = params.require(:recording).permit(:privacy_status, :title)
|
||||
if admin_expiry_update_requested?
|
||||
apply_admin_expiry_update!
|
||||
return
|
||||
end
|
||||
|
||||
permitted = recording_update_params
|
||||
attrs = {}
|
||||
privacy = permitted[:privacy_status]
|
||||
attrs[:privacy_status] = privacy if privacy.in?(Recording::PRIVACY_STATUSES)
|
||||
@@ -25,6 +30,8 @@ module Recordings
|
||||
redirect_to archive_index_path, notice: "Replay aggiornato"
|
||||
rescue ActiveRecord::RecordInvalid => e
|
||||
redirect_to archive_index_path, alert: e.record.errors.full_messages.join(", ")
|
||||
rescue Recordings::ExtendExpiry::Error => e
|
||||
redirect_to archive_index_path, alert: e.message
|
||||
end
|
||||
|
||||
def destroy
|
||||
@@ -87,5 +94,30 @@ module Recordings
|
||||
def archive_namespace
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def recording_update_params
|
||||
params.require(:recording).permit(:privacy_status, :title, :expires_at, :extend_days)
|
||||
end
|
||||
|
||||
def admin_expiry_update_requested?
|
||||
return false unless archive_namespace == :admin
|
||||
|
||||
permitted = recording_update_params
|
||||
permitted[:extend_days].present? || permitted[:expires_at].present?
|
||||
end
|
||||
|
||||
def apply_admin_expiry_update!
|
||||
permitted = recording_update_params
|
||||
extender = Recordings::ExtendExpiry.new(@recording)
|
||||
|
||||
if permitted[:extend_days].present?
|
||||
extender.by_days(permitted[:extend_days])
|
||||
redirect_to archive_index_path, notice: "Scadenza replay prolungata di #{permitted[:extend_days].to_i} giorni"
|
||||
else
|
||||
expires_at = Time.zone.parse(permitted[:expires_at].to_s)&.end_of_day
|
||||
extender.to_time(expires_at)
|
||||
redirect_to archive_index_path, notice: "Scadenza replay aggiornata"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
39
backend/app/services/recordings/extend_expiry.rb
Normal file
39
backend/app/services/recordings/extend_expiry.rb
Normal file
@@ -0,0 +1,39 @@
|
||||
module Recordings
|
||||
class ExtendExpiry
|
||||
class Error < StandardError; end
|
||||
|
||||
def initialize(recording)
|
||||
@recording = recording
|
||||
end
|
||||
|
||||
def to_time(expires_at)
|
||||
raise Error, "Data scadenza obbligatoria" if expires_at.blank?
|
||||
|
||||
parsed = expires_at.is_a?(Time) || expires_at.is_a?(ActiveSupport::TimeWithZone) ? expires_at : Time.zone.parse(expires_at.to_s)
|
||||
raise Error, "Data non valida" unless parsed
|
||||
|
||||
apply(parsed)
|
||||
end
|
||||
|
||||
def by_days(days)
|
||||
count = days.to_i
|
||||
raise Error, "Giorni non validi" if count <= 0
|
||||
|
||||
base = [@recording.expires_at, Time.current].compact.max
|
||||
apply(base + count.days)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def apply(new_expires_at)
|
||||
raise Error, "Replay già eliminato" if @recording.deleted?
|
||||
raise Error, "La scadenza deve essere nel futuro" unless new_expires_at > Time.current
|
||||
|
||||
@recording.update!(
|
||||
expires_at: new_expires_at,
|
||||
expiry_warning_sent_at: nil
|
||||
)
|
||||
@recording
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -112,6 +112,26 @@
|
||||
Nessuna scadenza
|
||||
<% end %>
|
||||
</span>
|
||||
<% if local_assigns[:admin_mode] && !rec.deleted? && rec.expires_at.present? %>
|
||||
<%= form_with url: paths.update.call(rec), method: :patch, local: true, class: "replay-archive__expiry-form" do %>
|
||||
<% filter_params.each { |key, value| concat hidden_field_tag(key, value) } %>
|
||||
<%= date_field_tag "recording[expires_at]",
|
||||
rec.expires_at.to_date,
|
||||
min: Date.tomorrow,
|
||||
class: "replay-archive__expiry-input",
|
||||
title: "Nuova data di scadenza" %>
|
||||
<%= submit_tag "Salva", class: "replay-archive__expiry-save" %>
|
||||
<% end %>
|
||||
<div class="replay-archive__expiry-quick">
|
||||
<% [[30, "+30 gg"], [90, "+90 gg"], [180, "+180 gg"]].each do |days, label| %>
|
||||
<%= button_to label, paths.update.call(rec),
|
||||
method: :patch,
|
||||
params: filter_params.merge(recording: { extend_days: days }),
|
||||
class: "replay-archive__expiry-extend",
|
||||
title: "Prolunga la scadenza di #{days} giorni" %>
|
||||
<% end %>
|
||||
</div>
|
||||
<% end %>
|
||||
</td>
|
||||
<td class="replay-archive-table__col-status">
|
||||
<span class="replay-archive__status replay-archive__status--<%= rec.status %>"><%= rec.status_label %></span>
|
||||
|
||||
Reference in New Issue
Block a user