Sha256: 3d19f1e6112f3b8e228c347865d98c96d78541f3398329626839537b5a2c773f

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module Decidim
  module BulletinBoard
    module Authority
      # This class handles the creation of an election.
      class CreateElection
        def initialize(election_data, message_id)
          @client = BulletinBoard::Graphql::Client.client
          @election_data = election_data
          @message_id = message_id
          @private_key = private_key
        end

        def self.call(election_data, message_id)
          new(election_data, message_id).call
        end

        def call
          args = {
            message_id: message_id,
            signed_data: encode_data(election_data)
          }

          response = client.query do
            mutation do
              createElection(messageId: args[:message_id], signedData: args[:signed_data]) do
                election do
                  status
                end
                error
              end
            end
          end

          response.data.create_election
        end

        private

        attr_reader :client, :election_data, :message_id

        def private_key
          @private_key ||= JwkUtils.import_private_key(BulletinBoard.identification_private_key)
        end

        def encode_data(election_data)
          JWT.encode(election_data, private_key.keypair, "RS256")
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
decidim-bulletin_board-0.4.0 lib/decidim/bulletin_board/authority/create_election.rb