Sha256: 25cbb1bdcf29acf7b94362a864d96a01c3e8a043da6a618dd6c979c6104cb0f1

Contents?: true

Size: 2 KB

Versions: 1

Compression:

Stored size: 2 KB

Contents

# frozen_string_literal: true

module Decidim
  module DirectVerifications
    class UserStats
      def initialize(organization)
        @organization = organization
        @authorization_handler = ""
        @emails = []
      end

      attr_reader :organization, :authorization_handler
      attr_accessor :emails

      def authorization_handler=(name)
        @workflow_manifest = nil
        @authorization_handler = name
      end

      def registered
        registered_users.count
      end

      def unconfirmed
        registered_users.where("decidim_users.confirmed_at IS NULL").count
      end

      def authorized
        authorized_users.count
      end

      def authorized_unconfirmed
        authorized_users.where("decidim_users.confirmed_at IS NULL").count
      end

      private

      def registered_users
        if authorization_handler.empty?
          filter = { decidim_organization_id: organization.id }
          filter[:email] = emails unless emails.empty?
          return User.where(filter)
        end
        authorized_users(false)
      end

      def authorized_users(strict = true)
        q = Decidim::Authorization.joins(:user)
        unless authorization_handler.empty?
          q = q.where(name: authorization_handler)
          if strict
            q = q.where.not(granted_at: nil)
            q = q.where("decidim_authorizations.granted_at >= :date", date: Time.current - expires_in) if expires_in
          end
        end
        q = q.where("decidim_users.decidim_organization_id=:org", org: organization.id)
        return q if emails.empty?
        q.where("decidim_users.email IN (:emails)", emails: emails)
      end

      def expires_in
        return unless workflow_manifest
        return if workflow_manifest.expires_in.zero?
        workflow_manifest.expires_in
      end

      def workflow_manifest
        return if authorization_handler.empty?
        @workflow_manifest ||= Decidim::Verifications.find_workflow_manifest(authorization_handler)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
decidim-direct_verifications-0.17.7 lib/decidim/direct_verifications/user_stats.rb