Ops può correggere dati clienti errati (es. email titolare) dalla scheda società senza interventi manuali sul DB. Co-authored-by: Cursor <cursoragent@cursor.com>
157 lines
4.6 KiB
Ruby
157 lines
4.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Admin
|
|
class UpdateClubData
|
|
class Error < StandardError; end
|
|
|
|
def self.call(**kwargs)
|
|
new(**kwargs).call
|
|
end
|
|
|
|
def initialize(club:, club_attrs:, owner_attrs: {}, staff_attrs: {}, invitation_attrs: {}, team_attrs: {})
|
|
@club = club
|
|
@club_attrs = club_attrs.to_h
|
|
@owner_attrs = owner_attrs.to_h
|
|
@staff_attrs = staff_attrs.to_h
|
|
@invitation_attrs = invitation_attrs.to_h
|
|
@team_attrs = team_attrs.to_h
|
|
end
|
|
|
|
def call
|
|
ActiveRecord::Base.transaction do
|
|
update_club!
|
|
update_owner!
|
|
update_staff!
|
|
update_invitations!
|
|
update_teams!
|
|
end
|
|
@club.reload
|
|
end
|
|
|
|
private
|
|
|
|
def update_club!
|
|
attrs = @club_attrs.dup
|
|
if attrs[:sport].present?
|
|
attrs[:sport] = Sports::Catalog.normalize_key(attrs[:sport])
|
|
end
|
|
%i[primary_color secondary_color].each do |key|
|
|
next unless attrs.key?(key)
|
|
|
|
attrs[key] = normalize_hex(attrs[key], key == :primary_color ? "#e53935" : "#ffffff")
|
|
end
|
|
%i[
|
|
billing_legal_name billing_vat_number billing_fiscal_code billing_email billing_phone
|
|
billing_address_line billing_city billing_province billing_postal_code billing_country
|
|
billing_recipient_code billing_pec logo_url
|
|
].each do |key|
|
|
next unless attrs.key?(key)
|
|
|
|
attrs[key] = attrs[key].to_s.strip.presence
|
|
end
|
|
if attrs[:billing_province].present?
|
|
attrs[:billing_province] = attrs[:billing_province].to_s.upcase
|
|
end
|
|
if attrs[:billing_country].present?
|
|
attrs[:billing_country] = attrs[:billing_country].to_s.upcase
|
|
end
|
|
if attrs[:billing_recipient_code].present?
|
|
attrs[:billing_recipient_code] = attrs[:billing_recipient_code].to_s.upcase
|
|
end
|
|
|
|
@club.assign_attributes(attrs)
|
|
@club.save!
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
raise Error, e.record.errors.full_messages.join(", ")
|
|
end
|
|
|
|
def update_owner!
|
|
return if @owner_attrs.blank?
|
|
|
|
owner = @club.owner
|
|
raise Error, I18n.t("admin.clubs.edit.errors.owner_missing") if owner.blank?
|
|
|
|
update_user!(owner, @owner_attrs)
|
|
end
|
|
|
|
def update_staff!
|
|
return if @staff_attrs.blank?
|
|
|
|
allowed_ids = staff_users.index_by(&:id)
|
|
@staff_attrs.each do |user_id, attrs|
|
|
user = allowed_ids[user_id.to_s] || allowed_ids[user_id]
|
|
next unless user
|
|
|
|
update_user!(user, attrs)
|
|
end
|
|
end
|
|
|
|
def update_invitations!
|
|
return if @invitation_attrs.blank?
|
|
|
|
pending = TeamInvitation.pending.where(team_id: @club.teams.select(:id)).index_by { |inv| inv.id.to_s }
|
|
@invitation_attrs.each do |invitation_id, attrs|
|
|
invitation = pending[invitation_id.to_s]
|
|
next unless invitation
|
|
|
|
email = attrs[:email].to_s.strip.presence
|
|
next if email.blank?
|
|
|
|
invitation.update!(email: email)
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
raise Error, e.record.errors.full_messages.join(", ")
|
|
end
|
|
end
|
|
|
|
def update_teams!
|
|
return if @team_attrs.blank?
|
|
|
|
teams = @club.teams.index_by { |t| t.id.to_s }
|
|
@team_attrs.each do |team_id, attrs|
|
|
team = teams[team_id.to_s]
|
|
next unless team
|
|
|
|
updates = {}
|
|
updates[:name] = attrs[:name].to_s.strip if attrs.key?(:name) && attrs[:name].present?
|
|
if attrs[:sport].present?
|
|
updates[:sport] = Sports::Catalog.normalize_key(attrs[:sport])
|
|
end
|
|
next if updates.empty?
|
|
|
|
team.update!(updates)
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
raise Error, e.record.errors.full_messages.join(", ")
|
|
end
|
|
end
|
|
|
|
def update_user!(user, attrs)
|
|
updates = {}
|
|
updates[:name] = attrs[:name].to_s.strip if attrs.key?(:name) && attrs[:name].present?
|
|
if attrs.key?(:email)
|
|
email = attrs[:email].to_s.strip.presence
|
|
updates[:email] = email if email.present?
|
|
end
|
|
return if updates.empty?
|
|
|
|
user.update!(updates)
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
raise Error, e.record.errors.full_messages.join(", ")
|
|
rescue ActiveRecord::RecordNotUnique
|
|
raise Error, I18n.t("admin.clubs.edit.errors.email_taken", email: updates[:email])
|
|
end
|
|
|
|
def staff_users
|
|
User.joins(:user_teams).where(user_teams: { team_id: @club.teams.select(:id) }).distinct.to_a
|
|
end
|
|
|
|
def normalize_hex(value, fallback)
|
|
raw = value.to_s.strip
|
|
return fallback if raw.blank?
|
|
return raw.downcase if raw.match?(/\A#[0-9a-fA-F]{6}\z/)
|
|
return "##{raw.downcase}" if raw.match?(/\A[0-9a-fA-F]{6}\z/)
|
|
|
|
fallback
|
|
end
|
|
end
|
|
end
|