Class: Dscf::Credit::UsersController

Inherits:
ApplicationController show all
Includes:
Dscf::Core::Common
Defined in:
app/controllers/dscf/credit/users_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/dscf/credit/users_controller.rb', line 5

def index
  super do
    if params[:business_type].present?
      business_type_name = params[:business_type].downcase

      business_type = Dscf::Core::BusinessType.find_by(name: business_type_name)
      unless business_type
        return render_error(
          "validation.invalid",
          errors: "Invalid business type: #{params[:business_type]}",
          status: :not_found
        )
      end

      users = @clazz
        .joins(businesses: :business_type)
        .where(dscf_core_business_types: { id: business_type.id })
        .where(
          <<-SQL.squish
            NOT EXISTS (
              SELECT 1
              FROM dscf_credit_facilitator_applications fa
              WHERE fa.user_id = dscf_core_users.id
              AND NOT EXISTS (
                SELECT 1
                FROM dscf_core_reviews r
                WHERE r.reviewable_type = 'Dscf::Credit::FacilitatorApplication'
                  AND r.reviewable_id = fa.id
                  AND r.status = 'rejected'
              )
            )
          SQL
        )
        .distinct

      users
    else
      @clazz.all
    end
  end
end

#showObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/dscf/credit/users_controller.rb', line 47

def show
  super do
    has_retailer_business = @obj.businesses.any? do |business|
      business.business_type&.name&.downcase == "retailer"
    end

    unless has_retailer_business
      return render_error(
        "errors.access_denied",
        errors: "User does not have retailer business type",
        status: :forbidden
      )
    end

    @obj
  end
end