Sha256: c2206ccb3f440080e79f053948669f3209994773159f8951e29aa7700be31448

Contents?: true

Size: 1.14 KB

Versions: 7

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module Decidim
  module Votings
    # A command to send the access code
    class SendAccessCode < Decidim::Command
      def initialize(datum, medium)
        @datum = datum
        @medium = medium
      end

      # Executes the command. Broadcast this events:
      # - :invalid when params are missing
      #
      # Returns nothing.
      def call
        return broadcast(:invalid) unless datum

        send_access_code

        broadcast(:ok)
      end

      private

      attr_reader :datum, :medium

      def send_access_code
        case medium
        when "email"
          AccessCodeMailer.send_access_code(datum).deliver_later
        when "sms"
          sms_gateway.new(datum.mobile_phone_number, access_code, sms_gateway_context).deliver_code
        else
          raise ArgumentError, "Medium parameter is invalid"
        end
      end

      def sms_gateway
        Decidim.sms_gateway_service.to_s.safe_constantize
      end

      def sms_gateway_context
        { organization: try(:current_organization) }
      end

      def access_code
        @access_code ||= datum.access_code
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
decidim-elections-0.28.4 app/commands/decidim/votings/send_access_code.rb
decidim-elections-0.28.3 app/commands/decidim/votings/send_access_code.rb
decidim-elections-0.28.2 app/commands/decidim/votings/send_access_code.rb
decidim-elections-0.28.1 app/commands/decidim/votings/send_access_code.rb
decidim-elections-0.28.0 app/commands/decidim/votings/send_access_code.rb
decidim-elections-0.28.0.rc5 app/commands/decidim/votings/send_access_code.rb
decidim-elections-0.28.0.rc4 app/commands/decidim/votings/send_access_code.rb