Class: Api::Partner::V1::StaffsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/api/partner/v1/staffs_controller.rb

Instance Method Summary collapse

Methods inherited from BaseController

#default_restaurant, #identity_cache_memoization, #render_unauthorize_action, #restaurants, #set_options

Methods included from LogrageCustomLogger

#append_info_to_payload

Methods included from ControllerHelpers

#check_boolean_param, #get_banners, #inventory_params, #reservation_params

Methods included from ResponseCacheConcern

#my_response_cache

Instance Method Details

#confirmObject



123
124
125
126
127
128
129
# File 'app/controllers/api/partner/v1/staffs_controller.rb', line 123

def confirm
  staff = Staff.find_by(confirmation_token: params[:confirmation_token])
  return error('Staff is not present.', :unprocessable_entity) if staff.blank?

  staff.update(confirmed_at: Time.current)
  redirect_to "#{ENV['PORTAL_FE_URL']}/register/verify"
end

#createObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/api/partner/v1/staffs_controller.rb', line 28

def create
  @staff = default_restaurant.staffs.new(staff_param)
  @staff.roles << assign_staff_role
  @staff.confirmed_at = Time.current

  if @staff.save
    @change_tracker.track_staff_created(@staff)
    @change_tracker.notify_managers default_restaurant
    success('Created successfully', :ok)
  else
    error(@staff.errors.full_messages.uniq.to_sentence, :bad_request)
  end
end

#create_temporary_staffObject



139
140
141
142
143
144
145
146
147
# File 'app/controllers/api/partner/v1/staffs_controller.rb', line 139

def create_temporary_staff
  service = StaffService::TemporaryAccess.new(default_restaurant.id)
  service.call
  if service.success?
    render json: { success: true, data: service.result }
  else
    render json: { success: false, message: service.errors.full_messages.uniq.to_sentence }
  end
end

#destroyObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/api/partner/v1/staffs_controller.rb', line 54

def destroy
  if @staff.temporary_staff?
    return error(I18n.t('partner.errors.staffs.delete_temporary_staff'), :bad_request)
  end

  if @staff.critical_role?
    return error('Cannot delete staff with critical roles', :bad_request)
  end

  @change_tracker.track_staff_deleted(@staff)
  @change_tracker.notify_managers default_restaurant

  if @staff.destroy && !@staff.persisted?
    success('Deleted successfully', :ok)
  else
    error(@staff.errors.full_messages.uniq.to_sentence, :bad_request)
  end
end

#editObject



24
25
26
# File 'app/controllers/api/partner/v1/staffs_controller.rb', line 24

def edit
  render json: ::Api::Partner::StaffSerializer.new(@staff).as_json
end

#forgot_passwordObject



73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/api/partner/v1/staffs_controller.rb', line 73

def forgot_password
  return error(I18n.t('partner.errors.staffs.email_missing'), :bad_request) if staff_param[:email].blank?

  staff = Staff.find_by(email: staff_param[:email])
  service = PartnerService::Staff::ForgotPassword.new(staff,
                                                      { reset_password_link: staff_param[:reset_password_link] })
  return success(I18n.t('partner.staffs.send_email_forgot_password'), :ok) if service.generate_password_token!

  error(I18n.t('partner.errors.staffs.email_not_found'), :not_found)
end

#get_mongodb_auth_tokenObject



131
132
133
134
135
136
137
# File 'app/controllers/api/partner/v1/staffs_controller.rb', line 131

def get_mongodb_auth_token
  secret_key = Knock.token_secret_signature_key.call
  payload = { sub: current_staff.id, exp: 55.minutes.since.to_i, iat: Time.current.to_i }
  token = JWT.encode payload, secret_key, Knock.token_signature_algorithm

  render json: { success: true, token: token }
end

#indexObject



12
13
14
15
16
17
# File 'app/controllers/api/partner/v1/staffs_controller.rb', line 12

def index
  staffs = Staff.by_restaurant_id(default_restaurant.id)
  pagy, staffs = pagy(staffs, items: params[:per_page] || 10, page: params[:page] || 1)

  render json: ::Api::Partner::StaffSerializer.new(staffs, set_options(pagy)).as_json
end

#newObject



19
20
21
22
# File 'app/controllers/api/partner/v1/staffs_controller.rb', line 19

def new
  roles = %w[Owner Contributor Viewer]
  render json: { roles: roles }
end

#redirect_to_reset_passwordObject



95
96
97
98
99
100
101
102
103
# File 'app/controllers/api/partner/v1/staffs_controller.rb', line 95

def redirect_to_reset_password
  if params[:reset_password_link].blank?
    return error(I18n.t('partner.errors.staffs.reset_password_link'),
                 :bad_request)
  end

  token = params[:reset_password_token]
  redirect_to "#{ENV['PORTAL_FE_URL']}/login?reset_password_token=#{token}"
end

#reset_passwordObject



84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/api/partner/v1/staffs_controller.rb', line 84

def reset_password
  staff = Staff.find_by(reset_password_token: staff_param[:reset_password_token])

  return error(I18n.t('partner.errors.staffs.reset_token_invalid'), :bad_request) if staff.blank?

  service = PartnerService::Staff::ForgotPassword.new(staff, staff_param.slice(:password, :password_confirmation))
  return success(I18n.t('partner.staffs.reset_password_success'), :ok) if service.reset_password!

  error(staff.errors.full_messages.uniq.to_sentence, :bad_request)
end

#show_profileObject



105
106
107
108
109
110
# File 'app/controllers/api/partner/v1/staffs_controller.rb', line 105

def show_profile
  cache_key = "staff_profile:#{restaurants.cache_key}"
  render json: ::Api::Partner::StaffSerializer.new(current_staff,
                                                   params: { restaurant_id: default_restaurant.id,
                                                             cache_key: cache_key }).as_json
end

#updateObject



42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/api/partner/v1/staffs_controller.rb', line 42

def update
  @change_tracker.track_staff_updated(@staff, staff_param)
  @staff.roles << assign_staff_role unless assign_staff_role.persisted?

  if @staff.update(staff_param)
    @change_tracker.notify_managers default_restaurant
    success('Updated successfully', :ok)
  else
    error(@staff.errors.full_messages.uniq.to_sentence, :bad_request)
  end
end

#update_profileObject



112
113
114
115
116
117
118
119
120
121
# File 'app/controllers/api/partner/v1/staffs_controller.rb', line 112

def update_profile
  @change_tracker.track_staff_updated(current_staff, staff_param)

  if current_staff.update(staff_param)
    @change_tracker.notify_managers default_restaurant
    render json: { success: true, message: I18n.t('partner.staffs.update_profile_success') }
  else
    render json: { success: true, message: current_staff.errors.full_messages.uniq.to_sentence }
  end
end