module Tournaments class Invite def self.call(tournament:, email:, scope_kind:, match_ids: [], court: nil, on_date: nil, invited_by:, note: nil) new( tournament: tournament, email: email, scope_kind: scope_kind, match_ids: match_ids, court: court, on_date: on_date, invited_by: invited_by, note: note ).call end def initialize(tournament:, email:, scope_kind:, match_ids:, court:, on_date:, invited_by:, note: nil) @tournament = tournament @email = email.to_s.downcase.strip @scope_kind = scope_kind.to_s @match_ids = Array(match_ids).reject(&:blank?) @court = court @on_date = on_date @invited_by = invited_by @note = note.to_s.strip.presence end def call Tournaments::Entitlements.new(@tournament.club).assert_writable! raise ArgumentError, "Email non valida" if @email.blank? token = TournamentBroadcastInvitation.generate_token invitation = @tournament.broadcast_invitations.create!( email: @email, token_digest: Digest::SHA256.hexdigest(token), scope_kind: @scope_kind, match_ids: @match_ids, court: @court, on_date: @on_date, note: @note, expires_at: 7.days.from_now ) [invitation, token] end end end