Class: Dscf::Marketplace::RetailersController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#bypass_permissions_for_demo?, #pundit_user

Instance Method Details

#my_retailersObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/dscf/marketplace/retailers_controller.rb', line 47

def my_retailers
  authorize @clazz.new, :my_retailers?
  service = MyResourceService.new(current_user)
  retailers = service.my_retailers(params)

  options = {
    include: default_serializer_includes[:index] || [],
    meta: {resource_type: "my_retailers"}
  }

  render_success("retailers.success.index", data: retailers, serializer_options: options)
end

#registerObject



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/marketplace/retailers_controller.rb', line 9

def register
  ActiveRecord::Base.transaction do
    user = Dscf::Core::User.new(
      phone: registration_params[:phone],
      password: registration_params[:password],
      password_confirmation: registration_params[:password_confirmation]
    )

    # `authenticate_user` is skipped for this action so self-service
    # registration stays public, but `current_user` still resolves
    # whatever Bearer token was sent — an agent onboarding a retailer
    # on their behalf attaches their own token, so we derive the
    # onboarding agent from it rather than trusting a client-supplied
    # agent_id.
    onboarding_agent = Dscf::Marketplace::Agent.find_by(user_id: current_user&.id)

    retailer = Dscf::Marketplace::Retailer.new(
      name: registration_params[:name],
      phone: registration_params[:phone],
      tin_number: registration_params[:tin_number],
      location: registration_params[:location],
      status: :active,
      agent: onboarding_agent,
      onboarded_at: (Time.current if onboarding_agent)
    )

    user.save!
    assign_default_role(user, "RETAILER")
    retailer.user = user
    retailer.save!

    render_success("retailers.success.register", data: retailer, status: :created)
  end
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => e
  errors = e.respond_to?(:record) && e.record ? e.record.errors.full_messages : [ e.message ]
  render_error("retailers.errors.register", errors: errors, status: :unprocessable_entity)
end