Class: Dashboard::V2::StaffsController

Inherits:
MainController show all
Defined in:
app/controllers/dashboard/v2/staffs_controller.rb

Overview

typed: ignore frozen_string_literal: true

Direct Known Subclasses

Group::StaffsController

Instance Method Summary collapse

Methods inherited from MainController

#default_fallback_location, #default_url_options, #kiosque_callback

Methods included from LogrageCustomLogger

#append_info_to_payload

Methods included from Stimulus::TagController

#stimulus

Methods included from OwnerLoginMode

#current_owner, #group_owner_type?, #request_path_contain_group?, #single_owner_type?

Methods included from OwnerDashboardsHelper

#events_ajax_previous_link, #show_source

Methods inherited from ApplicationController

#after_sign_in_path_for, #after_sign_out_path_for, #default_url_options, #identity_cache_memoization, #render_not_found, #routing_error, search_params_key=

Methods included from ControllerHelpers

#check_boolean_param, #get_banners, #inventory_params, #reservation_params

Instance Method Details

#createObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/dashboard/v2/staffs_controller.rb', line 25

def create
  @staff = Staff.new(staff_params)
  @staff.confirmed_at = Time.zone.now
  if group_owner_type?
    @staff.restaurant_group = current_owner
  else
    @staff.owner = current_owner
  end

  restaurants = group_owner_type? ? current_owner.restaurants : [current_owner.restaurant]
  restaurants.each do |restaurant|
    @staff.roles.build(restaurant: restaurant, role: :staff)
  end

  if @staff.save
    flash[:notice] = 'Staff successfully created'
    redirect_to current_owner.dashboard_v2_staffs_path
  else
    flash[:alert] = @staff.errors.full_messages.to_sentence
    render :new
  end
end

#create_first_accountObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/controllers/dashboard/v2/staffs_controller.rb', line 78

def 
  permitted_params = params.permit(:first_name, :last_name, :email, :password, :password_confirmation).tap do |p|
    name = "#{p[:first_name]} #{p[:last_name]}"
    p[:name] = name
    p.delete(:first_name)
    p.delete(:last_name)
  end

  @staff = Staff.new(permitted_params)
  @staff.confirmed_at = Time.zone.now
  if group_owner_type?
    @staff.restaurant_group = current_owner
  else
    @staff.owner = current_owner
  end
  restaurants = group_owner_type? ? current_owner.restaurants : [current_owner.restaurant]
  restaurants.each do |restaurant|
    @staff.roles.build(restaurant: restaurant, role: :staff)
  end

  if @staff.save
    current_owner.update(first_try_portal: true)

    render json: {
      success: true,
      message: 'Staff successfully created',
      redirect_url: current_owner.partner_path,
    }
  else
    render json: {
      success: false,
      message: @staff.errors.full_messages.to_sentence,
    }
  end
end

#destroyObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/dashboard/v2/staffs_controller.rb', line 58

def destroy
  if @staff.temporary_staff?
    flash[:alert] = I18n.t('partner.errors.staffs.delete_temporary_staff')
    return redirect_to current_owner.dashboard_v2_staffs_path
  end

  if @staff.critical_role?
    flash[:alert] = 'Cannot delete staff with critical roles'
    return redirect_to current_owner.dashboard_v2_staffs_path
  end

  if @staff.destroy && !@staff.persisted?
    flash[:notice] = 'Staff successfully deleted'
    redirect_to current_owner.dashboard_v2_staffs_path
  else
    flash[:alert] = @staff.errors.full_messages.to_sentence
    redirect_to current_owner.dashboard_v2_staffs_path
  end
end

#editObject



21
# File 'app/controllers/dashboard/v2/staffs_controller.rb', line 21

def edit; end

#indexObject



11
12
13
14
15
# File 'app/controllers/dashboard/v2/staffs_controller.rb', line 11

def index
  @grid = Dashboard::V2::StaffsGrid.new(default_params.merge(search_params)) do |scope|
    scope.by_restaurant_id(current_owner.restaurant.id).page(params[:page])
  end
end

#newObject



17
18
19
# File 'app/controllers/dashboard/v2/staffs_controller.rb', line 17

def new
  @staff = Staff.new
end

#showObject



23
# File 'app/controllers/dashboard/v2/staffs_controller.rb', line 23

def show; end

#skip_first_accountObject



114
115
116
117
118
# File 'app/controllers/dashboard/v2/staffs_controller.rb', line 114

def 
  current_owner.update(first_try_portal: true)

  redirect_to current_owner.partner_path
end

#updateObject



48
49
50
51
52
53
54
55
56
# File 'app/controllers/dashboard/v2/staffs_controller.rb', line 48

def update
  if @staff.update(staff_params)
    flash[:notice] = 'Staff updated successfully'
    redirect_to current_owner.dashboard_v2_staffs_path
  else
    flash[:alert] = @staff.errors.full_messages.to_sentence
    render :edit
  end
end