Class: Dscf::Marketplace::BusinessesController

Inherits:
ApplicationController show all
Includes:
Core::Common, Core::ReviewableController
Defined in:
app/controllers/dscf/marketplace/businesses_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



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
# File 'app/controllers/dscf/marketplace/businesses_controller.rb', line 14

def create
  business = @clazz.new(model_params.except(:business_license))
  if business.save
    # Add document upload after save
    if params[:business][:business_license].present?
      document = Dscf::Core::Document.new(
        documentable: business,
        document_type: :business_license
      )
      document.file.attach(params[:business][:business_license])
      document.save!
    end

    # Create pending review
    business.reviews.create!

    business = @clazz.includes(eager_loaded_associations).find(business.id) if eager_loaded_associations.present?
    includes = serializer_includes_for_action(:create)
    options = {include: includes} if includes.present?
    render_success(data: business, serializer_options: options, status: :created)
  else
    render_error(errors: business.errors.full_messages[0], status: :unprocessable_entity)
  end
rescue => e
  render_error(error: e.message)
end

#has_businessObject



9
10
11
12
# File 'app/controllers/dscf/marketplace/businesses_controller.rb', line 9

def has_business
  has_business = current_user&.businesses&.exists? || false
  render_success("business.success.has_business_check", data: {has_business: has_business})
end

#my_businessesObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/dscf/marketplace/businesses_controller.rb', line 41

def my_businesses
  service = MyResourceService.new(current_user)
  businesses = service.my_businesses(params)

  options = {
    include: [ :business_type, :user, :documents, {reviews: {reviewed_by: :user_profile}} ],
    meta: {resource_type: "my_businesses"}
  }

  # Manually serialize to ensure reviews are included
  serialized = businesses.map do |business|
    BusinessSerializer.new(business, options).as_json
  end

  render_success(data: serialized, meta: {resource_type: "my_businesses"})
end